forked from bakanis/uuid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rfc4122_test.go
209 lines (187 loc) · 5.28 KB
/
rfc4122_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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package uuid
/****************
* Date: 16/02/14
* Time: 11:29 AM
***************/
import (
"fmt"
"net/url"
"testing"
)
var (
goLang Name = "https://google.com/golang.org?q=golang"
)
const (
generate = 1000000
)
func TestUUID_NewV1(t *testing.T) {
u := NewV1()
if u.Version() != 1 {
t.Errorf("Expected correct version %d, but got %d", 1, u.Version())
}
if u.Variant() != ReservedRFC4122 {
t.Errorf("Expected RFC4122 variant %x, but got %x", ReservedRFC4122, u.Variant())
}
if !parseUUIDRegex.MatchString(u.String()) {
t.Errorf("Expected string representation to be valid, given: %s", u.String())
}
}
func TestUUID_NewV1Bulk(t *testing.T) {
for i := 0; i < generate; i++ {
NewV1()
}
}
// Tests NewV3
func TestUUID_NewV3(t *testing.T) {
u := NewV3(NamespaceURL, goLang)
if u.Version() != 3 {
t.Errorf("Expected correct version %d, but got %d", 3, u.Version())
}
if u.Variant() != ReservedRFC4122 {
t.Errorf("Expected RFC4122 variant %x, but got %x", ReservedRFC4122, u.Variant())
}
if !parseUUIDRegex.MatchString(u.String()) {
t.Errorf("Expected string representation to be valid, given: %s", u.String())
}
ur, _ := url.Parse(string(goLang))
// Same NS same name MUST be equal
u2 := NewV3(NamespaceURL, ur)
if !Equal(u2, u) {
t.Errorf("Expected UUIDs generated with same namespace and name to equal but got: %s and %s", u2, u)
}
// Different NS same name MUST NOT be equal
u3 := NewV3(NamespaceDNS, ur)
if Equal(u3, u) {
t.Errorf("Expected UUIDs generated with different namespace and same name to be different but got: %s and %s", u3, u)
}
// Same NS different name MUST NOT be equal
u4 := NewV3(NamespaceURL, u)
if Equal(u4, u) {
t.Errorf("Expected UUIDs generated with the same namespace and different names to be different but got: %s and %s", u4, u)
}
ids := []UUID{
u, u2, u3, u4,
}
for j, id := range ids {
i := NewV3(NamespaceURL, NewName(string(j), id))
if Equal(id, i) {
t.Errorf("Expected UUIDs generated with the same namespace and different names to be different but got: %s and %s", id, i)
}
}
}
func TestUUID_NewV3Bulk(t *testing.T) {
for i := 0; i < generate; i++ {
NewV3(NamespaceDNS, goLang)
}
}
func TestUUID_NewV4(t *testing.T) {
u := NewV4()
if u.Version() != 4 {
t.Errorf("Expected correct version %d, but got %d", 4, u.Version())
}
if u.Variant() != ReservedRFC4122 {
t.Errorf("Expected RFC4122 variant %x, but got %x", ReservedRFC4122, u.Variant())
}
if !parseUUIDRegex.MatchString(u.String()) {
t.Errorf("Expected string representation to be valid, given: %s", u.String())
}
}
func TestUUID_NewV4Bulk(t *testing.T) {
for i := 0; i < generate; i++ {
NewV4()
}
}
// Tests NewV5
func TestUUID_NewV5(t *testing.T) {
u := NewV5(NamespaceURL, goLang)
if u.Version() != 5 {
t.Errorf("Expected correct version %d, but got %d", 5, u.Version())
}
if u.Variant() != ReservedRFC4122 {
t.Errorf("Expected RFC4122 variant %x, but got %x", ReservedRFC4122, u.Variant())
}
if !parseUUIDRegex.MatchString(u.String()) {
t.Errorf("Expected string representation to be valid, given: %s", u.String())
}
ur, _ := url.Parse(string(goLang))
// Same NS same name MUST be equal
u2 := NewV5(NamespaceURL, ur)
if !Equal(u2, u) {
t.Errorf("Expected UUIDs generated with same namespace and name to equal but got: %s and %s", u2, u)
}
// Different NS same name MUST NOT be equal
u3 := NewV5(NamespaceDNS, ur)
if Equal(u3, u) {
t.Errorf("Expected UUIDs generated with different namespace and same name to be different but got: %s and %s", u3, u)
}
// Same NS different name MUST NOT be equal
u4 := NewV5(NamespaceURL, u)
if Equal(u4, u) {
t.Errorf("Expected UUIDs generated with the same namespace and different names to be different but got: %s and %s", u4, u)
}
ids := []UUID{
u, u2, u3, u4,
}
for j, id := range ids {
i := NewV5(NamespaceURL, NewName(string(j), id))
if Equal(id, i) {
t.Errorf("Expected UUIDs generated with the same namespace and different names to be different but got: %s and %s", id, i)
}
}
}
func TestUUID_NewV5Bulk(t *testing.T) {
for i := 0; i < generate; i++ {
NewV5(NamespaceDNS, goLang)
}
}
// A small test to test uniqueness across all UUIDs created
func TestUUID_EachIsUnique(t *testing.T) {
s := 1000
ids := make([]UUID, s)
for i := 0; i < s; i++ {
u := NewV1()
ids[i] = u
for j := 0; j < i; j++ {
if Equal(ids[j], u) {
t.Error("Should not create the same V1 UUID", u, ids[j])
}
}
}
ids = make([]UUID, s)
for i := 0; i < s; i++ {
u := NewV3(NamespaceDNS, NewName(string(i), Name(goLang)))
ids[i] = u
for j := 0; j < i; j++ {
if Equal(ids[j], u) {
t.Error("Should not create the same V3 UUID", u, ids[j])
}
}
}
ids = make([]UUID, s)
for i := 0; i < s; i++ {
u := NewV4()
ids[i] = u
for j := 0; j < i; j++ {
if Equal(ids[j], u) {
t.Error("Should not create the same V4 UUID", u, ids[j])
}
}
}
ids = make([]UUID, s)
for i := 0; i < s; i++ {
u := NewV5(NamespaceDNS, NewName(string(i), Name(goLang)))
ids[i] = u
for j := 0; j < i; j++ {
if Equal(ids[j], u) {
t.Error("Should not create the same V5 UUID", u, ids[j])
}
}
}
}
// Not really a test but used for visual verification of the defaults
func UUID_NamespaceDefaults() {
fmt.Println(NamespaceDNS)
fmt.Println(NamespaceURL)
fmt.Println(NamespaceOID)
fmt.Println(NamespaceX500)
}