-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid_test.go
118 lines (107 loc) · 3 KB
/
uuid_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package uuid
import (
"regexp"
"testing"
)
var (
stringUUIDRegex = regexp.MustCompile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
urnUUIDRegex = regexp.MustCompile("urn:uuid:[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")
)
func TestParse(t *testing.T) {
tests := map[string]struct {
uuid string
want string
wantErr bool
}{
"with dashes": {uuid: "53bfe550-4165-4f81-a8e7-c2609579ccc0", want: "53bfe550-4165-4f81-a8e7-c2609579ccc0"},
"no dashes": {uuid: "53bfe55041654f81a8e7c2609579ccc0", want: "53bfe550-4165-4f81-a8e7-c2609579ccc0"},
"urn:uuid prefix": {uuid: "urn:uuid:53bfe550-4165-4f81-a8e7-c2609579ccc0", want: "53bfe550-4165-4f81-a8e7-c2609579ccc0"},
"uppercase": {uuid: "53BFE550-4165-4F81-A8E7-C2609579CCC0", want: "53bfe550-4165-4f81-a8e7-c2609579ccc0"},
"mixed case": {uuid: "53bfe550-4165-4f81-A8E7-C2609579CCC0", want: "53bfe550-4165-4f81-a8e7-c2609579ccc0"},
"invalid urn prefix": {uuid: "abc:1234:53bfe550-4165-4f81-a8e7-c2609579ccc0", want: "00000000-0000-0000-0000-000000000000", wantErr: true},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
uuid, err := Parse(tt.uuid)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() wantErr = %t, gotErr = %v", tt.wantErr, err)
return
}
if uuid.String() != tt.want {
t.Errorf("want = %s, got = %s", tt.want, uuid.String())
}
})
}
}
func TestUUID_String(t *testing.T) {
tests := map[string]struct {
new func() (UUID, error)
}{
"nil": {new: func() (UUID, error) {
return Nil, nil
}},
"version 4": {new: NewV4},
"version 7": {new: NewV7},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
u, _ := tt.new()
if !stringUUIDRegex.MatchString(u.String()) {
t.Errorf("UUID.String(): did not match string regex")
}
})
}
}
func TestUUID_URN(t *testing.T) {
tests := map[string]struct {
new func() (UUID, error)
}{
"nil": {new: func() (UUID, error) {
return Nil, nil
}},
"version 4": {new: NewV4},
"version 7": {new: NewV7},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
u, _ := tt.new()
if !urnUUIDRegex.MatchString(u.URN()) {
t.Errorf("UUID.URN(): did not match string regex")
}
})
}
}
func TestUUID_Duplicates(t *testing.T) {
var iterations int = 1e6 // 1 million
set := make(map[UUID]struct{}, iterations)
tests := map[string]struct {
new func() (UUID, error)
}{
"version 4": {new: NewV4},
"version 7": {new: NewV7},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
for i := 0; i < iterations; i++ {
u, _ := tt.new()
if _, ok := set[u]; ok {
t.Errorf("iter %d: duplicate UUID detected!", i)
} else {
set[u] = struct{}{}
}
}
})
}
}
func TestPrint(t *testing.T) {
u, _ := NewV4()
t.Logf("v4: %s %v", u, u[:])
u, _ = NewV7()
t.Logf("v7: %s %v", u, u[:])
}
func BenchmarkNewV4(b *testing.B) {
for i := 0; i < b.N; i++ {
a, _ := NewV4()
_ = a // prevent compiler optimization
}
}