ires 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,633 @@
1
+ // Copyright (C) 2013, 2015 by Maxim Bublis <b@codemonkey.ru>
2
+ //
3
+ // Permission is hereby granted, free of charge, to any person obtaining
4
+ // a copy of this software and associated documentation files (the
5
+ // "Software"), to deal in the Software without restriction, including
6
+ // without limitation the rights to use, copy, modify, merge, publish,
7
+ // distribute, sublicense, and/or sell copies of the Software, and to
8
+ // permit persons to whom the Software is furnished to do so, subject to
9
+ // the following conditions:
10
+ //
11
+ // The above copyright notice and this permission notice shall be
12
+ // included in all copies or substantial portions of the Software.
13
+ //
14
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
+
22
+ package uuid
23
+
24
+ import (
25
+ "bytes"
26
+ "testing"
27
+ )
28
+
29
+ func TestBytes(t *testing.T) {
30
+ u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
31
+
32
+ bytes1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
33
+
34
+ if !bytes.Equal(u.Bytes(), bytes1) {
35
+ t.Errorf("Incorrect bytes representation for UUID: %s", u)
36
+ }
37
+ }
38
+
39
+ func TestString(t *testing.T) {
40
+ if NamespaceDNS.String() != "6ba7b810-9dad-11d1-80b4-00c04fd430c8" {
41
+ t.Errorf("Incorrect string representation for UUID: %s", NamespaceDNS.String())
42
+ }
43
+ }
44
+
45
+ func TestEqual(t *testing.T) {
46
+ if !Equal(NamespaceDNS, NamespaceDNS) {
47
+ t.Errorf("Incorrect comparison of %s and %s", NamespaceDNS, NamespaceDNS)
48
+ }
49
+
50
+ if Equal(NamespaceDNS, NamespaceURL) {
51
+ t.Errorf("Incorrect comparison of %s and %s", NamespaceDNS, NamespaceURL)
52
+ }
53
+ }
54
+
55
+ func TestOr(t *testing.T) {
56
+ u1 := UUID{0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff}
57
+ u2 := UUID{0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00}
58
+
59
+ u := UUID{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
60
+
61
+ if !Equal(u, Or(u1, u2)) {
62
+ t.Errorf("Incorrect bitwise OR result %s", Or(u1, u2))
63
+ }
64
+ }
65
+
66
+ func TestAnd(t *testing.T) {
67
+ u1 := UUID{0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff}
68
+ u2 := UUID{0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00}
69
+
70
+ u := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
71
+
72
+ if !Equal(u, And(u1, u2)) {
73
+ t.Errorf("Incorrect bitwise AND result %s", And(u1, u2))
74
+ }
75
+ }
76
+
77
+ func TestVersion(t *testing.T) {
78
+ u := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
79
+
80
+ if u.Version() != 1 {
81
+ t.Errorf("Incorrect version for UUID: %d", u.Version())
82
+ }
83
+ }
84
+
85
+ func TestSetVersion(t *testing.T) {
86
+ u := UUID{}
87
+ u.SetVersion(4)
88
+
89
+ if u.Version() != 4 {
90
+ t.Errorf("Incorrect version for UUID after u.setVersion(4): %d", u.Version())
91
+ }
92
+ }
93
+
94
+ func TestVariant(t *testing.T) {
95
+ u1 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
96
+
97
+ if u1.Variant() != VariantNCS {
98
+ t.Errorf("Incorrect variant for UUID variant %d: %d", VariantNCS, u1.Variant())
99
+ }
100
+
101
+ u2 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
102
+
103
+ if u2.Variant() != VariantRFC4122 {
104
+ t.Errorf("Incorrect variant for UUID variant %d: %d", VariantRFC4122, u2.Variant())
105
+ }
106
+
107
+ u3 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
108
+
109
+ if u3.Variant() != VariantMicrosoft {
110
+ t.Errorf("Incorrect variant for UUID variant %d: %d", VariantMicrosoft, u3.Variant())
111
+ }
112
+
113
+ u4 := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
114
+
115
+ if u4.Variant() != VariantFuture {
116
+ t.Errorf("Incorrect variant for UUID variant %d: %d", VariantFuture, u4.Variant())
117
+ }
118
+ }
119
+
120
+ func TestSetVariant(t *testing.T) {
121
+ u := new(UUID)
122
+ u.SetVariant()
123
+
124
+ if u.Variant() != VariantRFC4122 {
125
+ t.Errorf("Incorrect variant for UUID after u.setVariant(): %d", u.Variant())
126
+ }
127
+ }
128
+
129
+ func TestFromBytes(t *testing.T) {
130
+ u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
131
+ b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
132
+
133
+ u1, err := FromBytes(b1)
134
+ if err != nil {
135
+ t.Errorf("Error parsing UUID from bytes: %s", err)
136
+ }
137
+
138
+ if !Equal(u, u1) {
139
+ t.Errorf("UUIDs should be equal: %s and %s", u, u1)
140
+ }
141
+
142
+ b2 := []byte{}
143
+
144
+ _, err = FromBytes(b2)
145
+ if err == nil {
146
+ t.Errorf("Should return error parsing from empty byte slice, got %s", err)
147
+ }
148
+ }
149
+
150
+ func TestMarshalBinary(t *testing.T) {
151
+ u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
152
+ b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
153
+
154
+ b2, err := u.MarshalBinary()
155
+ if err != nil {
156
+ t.Errorf("Error marshaling UUID: %s", err)
157
+ }
158
+
159
+ if !bytes.Equal(b1, b2) {
160
+ t.Errorf("Marshaled UUID should be %s, got %s", b1, b2)
161
+ }
162
+ }
163
+
164
+ func TestUnmarshalBinary(t *testing.T) {
165
+ u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
166
+ b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
167
+
168
+ u1 := UUID{}
169
+ err := u1.UnmarshalBinary(b1)
170
+ if err != nil {
171
+ t.Errorf("Error unmarshaling UUID: %s", err)
172
+ }
173
+
174
+ if !Equal(u, u1) {
175
+ t.Errorf("UUIDs should be equal: %s and %s", u, u1)
176
+ }
177
+
178
+ b2 := []byte{}
179
+ u2 := UUID{}
180
+
181
+ err = u2.UnmarshalBinary(b2)
182
+ if err == nil {
183
+ t.Errorf("Should return error unmarshalling from empty byte slice, got %s", err)
184
+ }
185
+ }
186
+
187
+ func TestFromString(t *testing.T) {
188
+ u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
189
+
190
+ s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
191
+ s2 := "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}"
192
+ s3 := "urn:uuid:6ba7b810-9dad-11d1-80b4-00c04fd430c8"
193
+
194
+ _, err := FromString("")
195
+ if err == nil {
196
+ t.Errorf("Should return error trying to parse empty string, got %s", err)
197
+ }
198
+
199
+ u1, err := FromString(s1)
200
+ if err != nil {
201
+ t.Errorf("Error parsing UUID from string: %s", err)
202
+ }
203
+
204
+ if !Equal(u, u1) {
205
+ t.Errorf("UUIDs should be equal: %s and %s", u, u1)
206
+ }
207
+
208
+ u2, err := FromString(s2)
209
+ if err != nil {
210
+ t.Errorf("Error parsing UUID from string: %s", err)
211
+ }
212
+
213
+ if !Equal(u, u2) {
214
+ t.Errorf("UUIDs should be equal: %s and %s", u, u2)
215
+ }
216
+
217
+ u3, err := FromString(s3)
218
+ if err != nil {
219
+ t.Errorf("Error parsing UUID from string: %s", err)
220
+ }
221
+
222
+ if !Equal(u, u3) {
223
+ t.Errorf("UUIDs should be equal: %s and %s", u, u3)
224
+ }
225
+ }
226
+
227
+ func TestFromStringShort(t *testing.T) {
228
+ // Invalid 35-character UUID string
229
+ s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c"
230
+
231
+ for i := len(s1); i >= 0; i-- {
232
+ _, err := FromString(s1[:i])
233
+ if err == nil {
234
+ t.Errorf("Should return error trying to parse too short string, got %s", err)
235
+ }
236
+ }
237
+ }
238
+
239
+ func TestFromStringLong(t *testing.T) {
240
+ // Invalid 37+ character UUID string
241
+ s := []string{
242
+ "6ba7b810-9dad-11d1-80b4-00c04fd430c8=",
243
+ "6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
244
+ "{6ba7b810-9dad-11d1-80b4-00c04fd430c8}f",
245
+ "6ba7b810-9dad-11d1-80b4-00c04fd430c800c04fd430c8",
246
+ }
247
+
248
+ for _, str := range s {
249
+ _, err := FromString(str)
250
+ if err == nil {
251
+ t.Errorf("Should return error trying to parse too long string, passed %s", str)
252
+ }
253
+ }
254
+ }
255
+
256
+ func TestFromStringInvalid(t *testing.T) {
257
+ // Invalid UUID string formats
258
+ s := []string{
259
+ "6ba7b8109dad11d180b400c04fd430c8",
260
+ "6ba7b8109dad11d180b400c04fd430c86ba7b8109dad11d180b400c04fd430c8",
261
+ "urn:uuid:{6ba7b810-9dad-11d1-80b4-00c04fd430c8}",
262
+ "6ba7b8109-dad-11d1-80b4-00c04fd430c8",
263
+ "6ba7b810-9dad1-1d1-80b4-00c04fd430c8",
264
+ "6ba7b810-9dad-11d18-0b4-00c04fd430c8",
265
+ "6ba7b810-9dad-11d1-80b40-0c04fd430c8",
266
+ "6ba7b810+9dad+11d1+80b4+00c04fd430c8",
267
+ "6ba7b810-9dad11d180b400c04fd430c8",
268
+ "6ba7b8109dad-11d180b400c04fd430c8",
269
+ "6ba7b8109dad11d1-80b400c04fd430c8",
270
+ "6ba7b8109dad11d180b4-00c04fd430c8",
271
+ }
272
+
273
+ for _, str := range s {
274
+ _, err := FromString(str)
275
+ if err == nil {
276
+ t.Errorf("Should return error trying to parse invalid string, passed %s", str)
277
+ }
278
+ }
279
+ }
280
+
281
+ func TestFromStringOrNil(t *testing.T) {
282
+ u := FromStringOrNil("")
283
+ if u != Nil {
284
+ t.Errorf("Should return Nil UUID on parse failure, got %s", u)
285
+ }
286
+ }
287
+
288
+ func TestFromBytesOrNil(t *testing.T) {
289
+ b := []byte{}
290
+ u := FromBytesOrNil(b)
291
+ if u != Nil {
292
+ t.Errorf("Should return Nil UUID on parse failure, got %s", u)
293
+ }
294
+ }
295
+
296
+ func TestMarshalText(t *testing.T) {
297
+ u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
298
+ b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
299
+
300
+ b2, err := u.MarshalText()
301
+ if err != nil {
302
+ t.Errorf("Error marshaling UUID: %s", err)
303
+ }
304
+
305
+ if !bytes.Equal(b1, b2) {
306
+ t.Errorf("Marshaled UUID should be %s, got %s", b1, b2)
307
+ }
308
+ }
309
+
310
+ func TestUnmarshalText(t *testing.T) {
311
+ u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
312
+ b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
313
+
314
+ u1 := UUID{}
315
+ err := u1.UnmarshalText(b1)
316
+ if err != nil {
317
+ t.Errorf("Error unmarshaling UUID: %s", err)
318
+ }
319
+
320
+ if !Equal(u, u1) {
321
+ t.Errorf("UUIDs should be equal: %s and %s", u, u1)
322
+ }
323
+
324
+ b2 := []byte("")
325
+ u2 := UUID{}
326
+
327
+ err = u2.UnmarshalText(b2)
328
+ if err == nil {
329
+ t.Errorf("Should return error trying to unmarshal from empty string")
330
+ }
331
+ }
332
+
333
+ func TestValue(t *testing.T) {
334
+ u, err := FromString("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
335
+ if err != nil {
336
+ t.Errorf("Error parsing UUID from string: %s", err)
337
+ }
338
+
339
+ val, err := u.Value()
340
+ if err != nil {
341
+ t.Errorf("Error getting UUID value: %s", err)
342
+ }
343
+
344
+ if val != u.String() {
345
+ t.Errorf("Wrong value returned, should be equal: %s and %s", val, u)
346
+ }
347
+ }
348
+
349
+ func TestValueNil(t *testing.T) {
350
+ u := UUID{}
351
+
352
+ val, err := u.Value()
353
+ if err != nil {
354
+ t.Errorf("Error getting UUID value: %s", err)
355
+ }
356
+
357
+ if val != Nil.String() {
358
+ t.Errorf("Wrong value returned, should be equal to UUID.Nil: %s", val)
359
+ }
360
+ }
361
+
362
+ func TestNullUUIDValueNil(t *testing.T) {
363
+ u := NullUUID{}
364
+
365
+ val, err := u.Value()
366
+ if err != nil {
367
+ t.Errorf("Error getting UUID value: %s", err)
368
+ }
369
+
370
+ if val != nil {
371
+ t.Errorf("Wrong value returned, should be nil: %s", val)
372
+ }
373
+ }
374
+
375
+ func TestScanBinary(t *testing.T) {
376
+ u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
377
+ b1 := []byte{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
378
+
379
+ u1 := UUID{}
380
+ err := u1.Scan(b1)
381
+ if err != nil {
382
+ t.Errorf("Error unmarshaling UUID: %s", err)
383
+ }
384
+
385
+ if !Equal(u, u1) {
386
+ t.Errorf("UUIDs should be equal: %s and %s", u, u1)
387
+ }
388
+
389
+ b2 := []byte{}
390
+ u2 := UUID{}
391
+
392
+ err = u2.Scan(b2)
393
+ if err == nil {
394
+ t.Errorf("Should return error unmarshalling from empty byte slice, got %s", err)
395
+ }
396
+ }
397
+
398
+ func TestScanString(t *testing.T) {
399
+ u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
400
+ s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
401
+
402
+ u1 := UUID{}
403
+ err := u1.Scan(s1)
404
+ if err != nil {
405
+ t.Errorf("Error unmarshaling UUID: %s", err)
406
+ }
407
+
408
+ if !Equal(u, u1) {
409
+ t.Errorf("UUIDs should be equal: %s and %s", u, u1)
410
+ }
411
+
412
+ s2 := ""
413
+ u2 := UUID{}
414
+
415
+ err = u2.Scan(s2)
416
+ if err == nil {
417
+ t.Errorf("Should return error trying to unmarshal from empty string")
418
+ }
419
+ }
420
+
421
+ func TestScanText(t *testing.T) {
422
+ u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
423
+ b1 := []byte("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
424
+
425
+ u1 := UUID{}
426
+ err := u1.Scan(b1)
427
+ if err != nil {
428
+ t.Errorf("Error unmarshaling UUID: %s", err)
429
+ }
430
+
431
+ if !Equal(u, u1) {
432
+ t.Errorf("UUIDs should be equal: %s and %s", u, u1)
433
+ }
434
+
435
+ b2 := []byte("")
436
+ u2 := UUID{}
437
+
438
+ err = u2.Scan(b2)
439
+ if err == nil {
440
+ t.Errorf("Should return error trying to unmarshal from empty string")
441
+ }
442
+ }
443
+
444
+ func TestScanUnsupported(t *testing.T) {
445
+ u := UUID{}
446
+
447
+ err := u.Scan(true)
448
+ if err == nil {
449
+ t.Errorf("Should return error trying to unmarshal from bool")
450
+ }
451
+ }
452
+
453
+ func TestScanNil(t *testing.T) {
454
+ u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
455
+
456
+ err := u.Scan(nil)
457
+ if err == nil {
458
+ t.Errorf("Error UUID shouldn't allow unmarshalling from nil")
459
+ }
460
+ }
461
+
462
+ func TestNullUUIDScanValid(t *testing.T) {
463
+ u := UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}
464
+ s1 := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
465
+
466
+ u1 := NullUUID{}
467
+ err := u1.Scan(s1)
468
+ if err != nil {
469
+ t.Errorf("Error unmarshaling NullUUID: %s", err)
470
+ }
471
+
472
+ if !u1.Valid {
473
+ t.Errorf("NullUUID should be valid")
474
+ }
475
+
476
+ if !Equal(u, u1.UUID) {
477
+ t.Errorf("UUIDs should be equal: %s and %s", u, u1.UUID)
478
+ }
479
+ }
480
+
481
+ func TestNullUUIDScanNil(t *testing.T) {
482
+ u := NullUUID{UUID{0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30, 0xc8}, true}
483
+
484
+ err := u.Scan(nil)
485
+ if err != nil {
486
+ t.Errorf("Error unmarshaling NullUUID: %s", err)
487
+ }
488
+
489
+ if u.Valid {
490
+ t.Errorf("NullUUID should not be valid")
491
+ }
492
+
493
+ if !Equal(u.UUID, Nil) {
494
+ t.Errorf("NullUUID value should be equal to Nil: %s", u)
495
+ }
496
+ }
497
+
498
+ func TestNewV1(t *testing.T) {
499
+ u := NewV1()
500
+
501
+ if u.Version() != 1 {
502
+ t.Errorf("UUIDv1 generated with incorrect version: %d", u.Version())
503
+ }
504
+
505
+ if u.Variant() != VariantRFC4122 {
506
+ t.Errorf("UUIDv1 generated with incorrect variant: %d", u.Variant())
507
+ }
508
+
509
+ u1 := NewV1()
510
+ u2 := NewV1()
511
+
512
+ if Equal(u1, u2) {
513
+ t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u1, u2)
514
+ }
515
+
516
+ oldFunc := epochFunc
517
+ epochFunc = func() uint64 { return 0 }
518
+
519
+ u3 := NewV1()
520
+ u4 := NewV1()
521
+
522
+ if Equal(u3, u4) {
523
+ t.Errorf("UUIDv1 generated two equal UUIDs: %s and %s", u3, u4)
524
+ }
525
+
526
+ epochFunc = oldFunc
527
+ }
528
+
529
+ func TestNewV2(t *testing.T) {
530
+ u1 := NewV2(DomainPerson)
531
+
532
+ if u1.Version() != 2 {
533
+ t.Errorf("UUIDv2 generated with incorrect version: %d", u1.Version())
534
+ }
535
+
536
+ if u1.Variant() != VariantRFC4122 {
537
+ t.Errorf("UUIDv2 generated with incorrect variant: %d", u1.Variant())
538
+ }
539
+
540
+ u2 := NewV2(DomainGroup)
541
+
542
+ if u2.Version() != 2 {
543
+ t.Errorf("UUIDv2 generated with incorrect version: %d", u2.Version())
544
+ }
545
+
546
+ if u2.Variant() != VariantRFC4122 {
547
+ t.Errorf("UUIDv2 generated with incorrect variant: %d", u2.Variant())
548
+ }
549
+ }
550
+
551
+ func TestNewV3(t *testing.T) {
552
+ u := NewV3(NamespaceDNS, "www.example.com")
553
+
554
+ if u.Version() != 3 {
555
+ t.Errorf("UUIDv3 generated with incorrect version: %d", u.Version())
556
+ }
557
+
558
+ if u.Variant() != VariantRFC4122 {
559
+ t.Errorf("UUIDv3 generated with incorrect variant: %d", u.Variant())
560
+ }
561
+
562
+ if u.String() != "5df41881-3aed-3515-88a7-2f4a814cf09e" {
563
+ t.Errorf("UUIDv3 generated incorrectly: %s", u.String())
564
+ }
565
+
566
+ u = NewV3(NamespaceDNS, "python.org")
567
+
568
+ if u.String() != "6fa459ea-ee8a-3ca4-894e-db77e160355e" {
569
+ t.Errorf("UUIDv3 generated incorrectly: %s", u.String())
570
+ }
571
+
572
+ u1 := NewV3(NamespaceDNS, "golang.org")
573
+ u2 := NewV3(NamespaceDNS, "golang.org")
574
+ if !Equal(u1, u2) {
575
+ t.Errorf("UUIDv3 generated different UUIDs for same namespace and name: %s and %s", u1, u2)
576
+ }
577
+
578
+ u3 := NewV3(NamespaceDNS, "example.com")
579
+ if Equal(u1, u3) {
580
+ t.Errorf("UUIDv3 generated same UUIDs for different names in same namespace: %s and %s", u1, u2)
581
+ }
582
+
583
+ u4 := NewV3(NamespaceURL, "golang.org")
584
+ if Equal(u1, u4) {
585
+ t.Errorf("UUIDv3 generated same UUIDs for sane names in different namespaces: %s and %s", u1, u4)
586
+ }
587
+ }
588
+
589
+ func TestNewV4(t *testing.T) {
590
+ u := NewV4()
591
+
592
+ if u.Version() != 4 {
593
+ t.Errorf("UUIDv4 generated with incorrect version: %d", u.Version())
594
+ }
595
+
596
+ if u.Variant() != VariantRFC4122 {
597
+ t.Errorf("UUIDv4 generated with incorrect variant: %d", u.Variant())
598
+ }
599
+ }
600
+
601
+ func TestNewV5(t *testing.T) {
602
+ u := NewV5(NamespaceDNS, "www.example.com")
603
+
604
+ if u.Version() != 5 {
605
+ t.Errorf("UUIDv5 generated with incorrect version: %d", u.Version())
606
+ }
607
+
608
+ if u.Variant() != VariantRFC4122 {
609
+ t.Errorf("UUIDv5 generated with incorrect variant: %d", u.Variant())
610
+ }
611
+
612
+ u = NewV5(NamespaceDNS, "python.org")
613
+
614
+ if u.String() != "886313e1-3b8a-5372-9b90-0c9aee199e5d" {
615
+ t.Errorf("UUIDv5 generated incorrectly: %s", u.String())
616
+ }
617
+
618
+ u1 := NewV5(NamespaceDNS, "golang.org")
619
+ u2 := NewV5(NamespaceDNS, "golang.org")
620
+ if !Equal(u1, u2) {
621
+ t.Errorf("UUIDv5 generated different UUIDs for same namespace and name: %s and %s", u1, u2)
622
+ }
623
+
624
+ u3 := NewV5(NamespaceDNS, "example.com")
625
+ if Equal(u1, u3) {
626
+ t.Errorf("UUIDv5 generated same UUIDs for different names in same namespace: %s and %s", u1, u2)
627
+ }
628
+
629
+ u4 := NewV5(NamespaceURL, "golang.org")
630
+ if Equal(u1, u4) {
631
+ t.Errorf("UUIDv3 generated same UUIDs for sane names in different namespaces: %s and %s", u1, u4)
632
+ }
633
+ }