@@ -21,7 +21,9 @@ import (
2121 "encoding/base64"
2222 "encoding/json"
2323 "fmt"
24+ "io"
2425 "reflect"
26+ "regexp"
2527 "strings"
2628 "testing"
2729
@@ -175,9 +177,26 @@ func TestFormatMAC(t *testing.T) {
175177func TestFormatUUID3 (t * testing.T ) {
176178 first3 := uuid .NewMD5 (uuid .NameSpaceURL , []byte ("somewhere.com" ))
177179 other3 := uuid .NewMD5 (uuid .NameSpaceURL , []byte ("somewhereelse.com" ))
180+ other4 := uuid .Must (uuid .NewRandom ())
181+ other5 := uuid .NewSHA1 (uuid .NameSpaceURL , []byte ("somewhereelse.com" ))
178182 uuid3 := UUID3 (first3 .String ())
179183 str := other3 .String ()
180- testStringFormat (t , & uuid3 , "uuid3" , str , []string {}, []string {"not-a-uuid" })
184+ testStringFormat (t , & uuid3 , "uuid3" , str ,
185+ []string {
186+ other3 .String (),
187+ strings .ReplaceAll (other3 .String (), "-" , "" ),
188+ },
189+ []string {
190+ "not-a-uuid" ,
191+ other4 .String (),
192+ other5 .String (),
193+ strings .ReplaceAll (other4 .String (), "-" , "" ),
194+ strings .ReplaceAll (other5 .String (), "-" , "" ),
195+ strings .Replace (other3 .String (), "-" , "" , 2 ),
196+ strings .Replace (other4 .String (), "-" , "" , 2 ),
197+ strings .Replace (other5 .String (), "-" , "" , 2 ),
198+ },
199+ )
181200
182201 // special case for zero UUID
183202 var uuidZero UUID3
@@ -188,10 +207,27 @@ func TestFormatUUID3(t *testing.T) {
188207
189208func TestFormatUUID4 (t * testing.T ) {
190209 first4 := uuid .Must (uuid .NewRandom ())
210+ other3 := uuid .NewMD5 (uuid .NameSpaceURL , []byte ("somewhere.com" ))
191211 other4 := uuid .Must (uuid .NewRandom ())
212+ other5 := uuid .NewSHA1 (uuid .NameSpaceURL , []byte ("somewhereelse.com" ))
192213 uuid4 := UUID4 (first4 .String ())
193214 str := other4 .String ()
194- testStringFormat (t , & uuid4 , "uuid4" , str , []string {}, []string {"not-a-uuid" })
215+ testStringFormat (t , & uuid4 , "uuid4" , str ,
216+ []string {
217+ other4 .String (),
218+ strings .ReplaceAll (other4 .String (), "-" , "" ),
219+ },
220+ []string {
221+ "not-a-uuid" ,
222+ other3 .String (),
223+ other5 .String (),
224+ strings .ReplaceAll (other3 .String (), "-" , "" ),
225+ strings .ReplaceAll (other5 .String (), "-" , "" ),
226+ strings .Replace (other3 .String (), "-" , "" , 2 ),
227+ strings .Replace (other4 .String (), "-" , "" , 2 ),
228+ strings .Replace (other5 .String (), "-" , "" , 2 ),
229+ },
230+ )
195231
196232 // special case for zero UUID
197233 var uuidZero UUID4
@@ -202,10 +238,27 @@ func TestFormatUUID4(t *testing.T) {
202238
203239func TestFormatUUID5 (t * testing.T ) {
204240 first5 := uuid .NewSHA1 (uuid .NameSpaceURL , []byte ("somewhere.com" ))
241+ other3 := uuid .NewMD5 (uuid .NameSpaceURL , []byte ("somewhere.com" ))
242+ other4 := uuid .Must (uuid .NewRandom ())
205243 other5 := uuid .NewSHA1 (uuid .NameSpaceURL , []byte ("somewhereelse.com" ))
206244 uuid5 := UUID5 (first5 .String ())
207245 str := other5 .String ()
208- testStringFormat (t , & uuid5 , "uuid5" , str , []string {}, []string {"not-a-uuid" })
246+ testStringFormat (t , & uuid5 , "uuid5" , str ,
247+ []string {
248+ other5 .String (),
249+ strings .ReplaceAll (other5 .String (), "-" , "" ),
250+ },
251+ []string {
252+ "not-a-uuid" ,
253+ other3 .String (),
254+ other4 .String (),
255+ strings .ReplaceAll (other3 .String (), "-" , "" ),
256+ strings .ReplaceAll (other4 .String (), "-" , "" ),
257+ strings .Replace (other3 .String (), "-" , "" , 2 ),
258+ strings .Replace (other4 .String (), "-" , "" , 2 ),
259+ strings .Replace (other5 .String (), "-" , "" , 2 ),
260+ },
261+ )
209262
210263 // special case for zero UUID
211264 var uuidZero UUID5
@@ -216,10 +269,34 @@ func TestFormatUUID5(t *testing.T) {
216269
217270func TestFormatUUID (t * testing.T ) {
218271 first5 := uuid .NewSHA1 (uuid .NameSpaceURL , []byte ("somewhere.com" ))
272+ other3 := uuid .NewSHA1 (uuid .NameSpaceURL , []byte ("somewhereelse.com" ))
273+ other4 := uuid .Must (uuid .NewRandom ())
219274 other5 := uuid .NewSHA1 (uuid .NameSpaceURL , []byte ("somewhereelse.com" ))
275+ other6 := uuid .Must (uuid .NewV6 ())
276+ other7 := uuid .Must (uuid .NewV7 ())
277+ microsoft := "0" + other4 .String () + "f"
278+
220279 uuid := UUID (first5 .String ())
221280 str := other5 .String ()
222- testStringFormat (t , & uuid , "uuid" , str , []string {}, []string {"not-a-uuid" })
281+ testStringFormat (t , & uuid , "uuid" , str ,
282+ []string {
283+ other3 .String (),
284+ other4 .String (),
285+ other5 .String (),
286+ strings .ReplaceAll (other3 .String (), "-" , "" ),
287+ strings .ReplaceAll (other4 .String (), "-" , "" ),
288+ strings .ReplaceAll (other5 .String (), "-" , "" ),
289+ other6 .String (),
290+ other7 .String (),
291+ microsoft ,
292+ },
293+ []string {
294+ "not-a-uuid" ,
295+ strings .Replace (other3 .String (), "-" , "" , 2 ),
296+ strings .Replace (other4 .String (), "-" , "" , 2 ),
297+ strings .Replace (other5 .String (), "-" , "" , 2 ),
298+ },
299+ )
223300
224301 // special case for zero UUID
225302 var uuidZero UUID
@@ -775,3 +852,48 @@ func TestDeepCopyPassword(t *testing.T) {
775852 out3 := inNil .DeepCopy ()
776853 assert .Nil (t , out3 )
777854}
855+
856+ func BenchmarkIsUUID (b * testing.B ) {
857+ const sampleSize = 100
858+ rxUUID := regexp .MustCompile (UUIDPattern )
859+ rxUUID3 := regexp .MustCompile (UUID3Pattern )
860+ rxUUID4 := regexp .MustCompile (UUID4Pattern )
861+ rxUUID5 := regexp .MustCompile (UUID5Pattern )
862+
863+ uuids := make ([]string , 0 , sampleSize )
864+ uuid3s := make ([]string , 0 , sampleSize )
865+ uuid4s := make ([]string , 0 , sampleSize )
866+ uuid5s := make ([]string , 0 , sampleSize )
867+
868+ for i := 0 ; i < sampleSize ; i ++ {
869+ seed := []byte (uuid .Must (uuid .NewRandom ()).String ())
870+ uuids = append (uuids , uuid .Must (uuid .NewRandom ()).String ())
871+ uuid3s = append (uuid3s , uuid .NewMD5 (uuid .NameSpaceURL , seed ).String ())
872+ uuid4s = append (uuid4s , uuid .Must (uuid .NewRandom ()).String ())
873+ uuid5s = append (uuid5s , uuid .NewSHA1 (uuid .NameSpaceURL , seed ).String ())
874+ }
875+
876+ b .Run ("IsUUID - google.uuid" , benchmarkIs (uuids , IsUUID ))
877+ b .Run ("IsUUID - regexp" , benchmarkIs (uuids , func (id string ) bool { return rxUUID .MatchString (id ) }))
878+
879+ b .Run ("IsUUIDv3 - google.uuid" , benchmarkIs (uuid3s , IsUUID3 ))
880+ b .Run ("IsUUIDv3 - regexp" , benchmarkIs (uuid3s , func (id string ) bool { return rxUUID3 .MatchString (id ) }))
881+
882+ b .Run ("IsUUIDv4 - google.uuid" , benchmarkIs (uuid4s , IsUUID4 ))
883+ b .Run ("IsUUIDv4 - regexp" , benchmarkIs (uuid4s , func (id string ) bool { return rxUUID4 .MatchString (id ) }))
884+
885+ b .Run ("IsUUIDv5 - google.uuid" , benchmarkIs (uuid5s , IsUUID5 ))
886+ b .Run ("IsUUIDv5 - regexp" , benchmarkIs (uuid5s , func (id string ) bool { return rxUUID5 .MatchString (id ) }))
887+ }
888+
889+ func benchmarkIs (input []string , fn func (string ) bool ) func (* testing.B ) {
890+ return func (b * testing.B ) {
891+ var isTrue bool
892+ b .ReportAllocs ()
893+ b .ResetTimer ()
894+ for i := 0 ; i < b .N ; i ++ {
895+ isTrue = fn (input [i % len (input )])
896+ }
897+ fmt .Fprintln (io .Discard , isTrue )
898+ }
899+ }
0 commit comments