Debian Codegraph @

uuid_test.go

raw symbols SCIP
1 // Copyright (C) 2013-2018 by Maxim Bublis <[email protected]>
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 "fmt"
27 "io"
28 "testing"
29 "time"
30 )
31
32 func TestUUID(t *testing.T) {
33 t.Run("IsNil", testUUIDIsNil)
34 t.Run("IsZero", testUUIDIsZero)
35 t.Run("Bytes", testUUIDBytes)
36 t.Run("String", testUUIDString)
37 t.Run("Version", testUUIDVersion)
38 t.Run("Variant", testUUIDVariant)
39 t.Run("SetVersion", testUUIDSetVersion)
40 t.Run("SetVariant", testUUIDSetVariant)
41 t.Run("Format", testUUIDFormat)
42 }
43
44 func testUUIDIsNil(t *testing.T) {
45 u := UUID{}
46 got := u.IsNil()
47 want := true
48 if got != want {
49 t.Errorf("%v.IsNil() = %t, want %t", u, got, want)
50 }
51 }
52
53 func testUUIDIsZero(t *testing.T) {
54 // Test zero UUID
55 u := UUID{}
56 got := u.IsZero()
57 want := true
58 if got != want {
59 t.Errorf("%v.IsZero() = %t, want %t", u, got, want)
60 }
61
62 // Test non-zero UUID
63 nonZeroUUID := NamespaceDNS // Use a known non-zero UUID
64 got = nonZeroUUID.IsZero()
65 want = false
66 if got != want {
67 t.Errorf("%v.IsZero() = %t, want %t", nonZeroUUID, got, want)
68 }
69 }
70
71 func testUUIDBytes(t *testing.T) {
72 got := codecTestUUID.Bytes()
73 want := codecTestData
74 if !bytes.Equal(got, want) {
75 t.Errorf("%v.Bytes() = %x, want %x", codecTestUUID, got, want)
76 }
77 }
78
79 func testUUIDString(t *testing.T) {
80 got := NamespaceDNS.String()
81 want := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
82 if got != want {
83 t.Errorf("%v.String() = %q, want %q", NamespaceDNS, got, want)
84 }
85 }
86
87 func testUUIDVersion(t *testing.T) {
88 u := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
89 if got, want := u.Version(), V1; got != want {
90 t.Errorf("%v.Version() == %d, want %d", u, got, want)
91 }
92 }
93
94 func testUUIDVariant(t *testing.T) {
95 tests := []struct {
96 u UUID
97 want byte
98 }{
99 {
100 u: UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
101 want: VariantNCS,
102 },
103 {
104 u: UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
105 want: VariantRFC9562,
106 },
107 {
108 u: UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
109 want: VariantMicrosoft,
110 },
111 {
112 u: UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
113 want: VariantFuture,
114 },
115 }
116 for _, tt := range tests {
117 if got := tt.u.Variant(); got != tt.want {
118 t.Errorf("%v.Variant() == %d, want %d", tt.u, got, tt.want)
119 }
120 }
121 }
122
123 func testUUIDSetVersion(t *testing.T) {
124 u := UUID{}
125 want := V4
126 u.SetVersion(want)
127 if got := u.Version(); got != want {
128 t.Errorf("%v.Version() == %d after SetVersion(%d)", u, got, want)
129 }
130 }
131
132 func testUUIDSetVariant(t *testing.T) {
133 variants := []byte{
134 VariantNCS,
135 VariantRFC9562,
136 VariantMicrosoft,
137 VariantFuture,
138 }
139 for _, want := range variants {
140 u := UUID{}
141 u.SetVariant(want)
142 if got := u.Variant(); got != want {
143 t.Errorf("%v.Variant() == %d after SetVariant(%d)", u, got, want)
144 }
145 }
146 }
147
148 func testUUIDFormat(t *testing.T) {
149 val := Must(FromString("12345678-90ab-cdef-1234-567890abcdef"))
150 tests := []struct {
151 u UUID
152 f string
153 want string
154 }{
155 {u: val, f: "%s", want: "12345678-90ab-cdef-1234-567890abcdef"},
156 {u: val, f: "%S", want: "12345678-90AB-CDEF-1234-567890ABCDEF"},
157 {u: val, f: "%q", want: `"12345678-90ab-cdef-1234-567890abcdef"`},
158 {u: val, f: "%x", want: "1234567890abcdef1234567890abcdef"},
159 {u: val, f: "%X", want: "1234567890ABCDEF1234567890ABCDEF"},
160 {u: val, f: "%v", want: "12345678-90ab-cdef-1234-567890abcdef"},
161 {u: val, f: "%+v", want: "12345678-90ab-cdef-1234-567890abcdef"},
162 {u: val, f: "%#v", want: "[16]uint8{0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef, 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef}"},
163 {u: val, f: "%T", want: "uuid.UUID"},
164 {u: val, f: "%t", want: "%!t(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
165 {u: val, f: "%b", want: "%!b(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
166 {u: val, f: "%c", want: "%!c(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
167 {u: val, f: "%d", want: "%!d(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
168 {u: val, f: "%e", want: "%!e(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
169 {u: val, f: "%E", want: "%!E(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
170 {u: val, f: "%f", want: "%!f(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
171 {u: val, f: "%F", want: "%!F(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
172 {u: val, f: "%g", want: "%!g(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
173 {u: val, f: "%G", want: "%!G(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
174 {u: val, f: "%o", want: "%!o(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
175 {u: val, f: "%U", want: "%!U(uuid.UUID=12345678-90ab-cdef-1234-567890abcdef)"},
176 }
177 for _, tt := range tests {
178 got := fmt.Sprintf(tt.f, tt.u)
179 if tt.want != got {
180 t.Errorf(`Format("%s") got %s, want %s`, tt.f, got, tt.want)
181 }
182 }
183 }
184
185 func TestMust(t *testing.T) {
186 sentinel := fmt.Errorf("uuid: sentinel error")
187 defer func() {
188 r := recover()
189 if r == nil {
190 t.Fatalf("did not panic, want %v", sentinel)
191 }
192 err, ok := r.(error)
193 if !ok {
194 t.Fatalf("panicked with %T, want error (%v)", r, sentinel)
195 }
196 if err != sentinel {
197 t.Fatalf("panicked with %v, want %v", err, sentinel)
198 }
199 }()
200 fn := func() (UUID, error) {
201 return Nil, sentinel
202 }
203 Must(fn())
204 }
205
206 func TestTimeFromTimestamp(t *testing.T) {
207 tests := []struct {
208 t Timestamp
209 want time.Time
210 }{
211 // a zero timestamp represents October 15, 1582 at midnight UTC
212 {t: Timestamp(0), want: time.Date(1582, 10, 15, 0, 0, 0, 0, time.UTC)},
213 // a one value is 100ns later
214 {t: Timestamp(1), want: time.Date(1582, 10, 15, 0, 0, 0, 100, time.UTC)},
215 // 10 million 100ns intervals later is one second
216 {t: Timestamp(10000000), want: time.Date(1582, 10, 15, 0, 0, 1, 0, time.UTC)},
217 {t: Timestamp(60 * 10000000), want: time.Date(1582, 10, 15, 0, 1, 0, 0, time.UTC)},
218 {t: Timestamp(60 * 60 * 10000000), want: time.Date(1582, 10, 15, 1, 0, 0, 0, time.UTC)},
219 {t: Timestamp(24 * 60 * 60 * 10000000), want: time.Date(1582, 10, 16, 0, 0, 0, 0, time.UTC)},
220 {t: Timestamp(365 * 24 * 60 * 60 * 10000000), want: time.Date(1583, 10, 15, 0, 0, 0, 0, time.UTC)},
221 // maximum timestamp value in a UUID is the year 5236
222 {t: Timestamp(uint64(1<<60 - 1)), want: time.Date(5236, 03, 31, 21, 21, 0, 684697500, time.UTC)},
223 }
224 for _, tt := range tests {
225 got, _ := tt.t.Time()
226 if !got.Equal(tt.want) {
227 t.Errorf("%v.Time() == %v, want %v", tt.t, got, tt.want)
228 }
229 }
230 }
231
232 func TestTimestampFromV1(t *testing.T) {
233 tests := []struct {
234 u UUID
235 want Timestamp
236 wanterr bool
237 }{
238 {u: Must(NewV4()), wanterr: true},
239 {u: Must(FromString("00000000-0000-1000-0000-000000000000")), want: 0},
240 {u: Must(FromString("424f137e-a2aa-11e8-98d0-529269fb1459")), want: 137538640775418750},
241 {u: Must(FromString("ffffffff-ffff-1fff-ffff-ffffffffffff")), want: Timestamp(1<<60 - 1)},
242 }
243 for _, tt := range tests {
244 got, goterr := TimestampFromV1(tt.u)
245 if tt.wanterr && goterr == nil {
246 t.Errorf("TimestampFromV1(%v) want error, got %v", tt.u, got)
247 } else if tt.want != got {
248 t.Errorf("TimestampFromV1(%v) got %v, want %v", tt.u, got, tt.want)
249 }
250 }
251 }
252
253 func TestTimestampFromV6(t *testing.T) {
254 tests := []struct {
255 u UUID
256 want Timestamp
257 wanterr bool
258 }{
259 {u: Must(NewV1()), wanterr: true},
260 {u: Must(FromString("00000000-0000-6000-0000-000000000000")), want: 0},
261 {u: Must(FromString("1ec06cff-e9b1-621c-8627-ba3fd7e551c9")), want: 138493178941215260},
262 {u: Must(FromString("ffffffff-ffff-6fff-ffff-ffffffffffff")), want: Timestamp(1<<60 - 1)},
263 }
264
265 for _, tt := range tests {
266 got, err := TimestampFromV6(tt.u)
267
268 switch {
269 case tt.wanterr && err == nil:
270 t.Errorf("TimestampFromV6(%v) want error, got %v", tt.u, got)
271
272 case tt.want != got:
273 t.Errorf("TimestampFromV6(%v) got %v, want %v", tt.u, got, tt.want)
274 }
275 }
276 }
277
278 func TestTimestampFromV7(t *testing.T) {
279 tests := []struct {
280 u UUID
281 want Timestamp
282 wanterr bool
283 }{
284 // These non-V7 versions should not be able to be provided to TimestampFromV7
285 {u: Must(NewV1()), wanterr: true},
286 {u: NewV3(NamespaceDNS, "a.example.com"), wanterr: true},
287 // v7 is unix_ts_ms, so zero value time is unix epoch
288 {u: Must(FromString("00000000-0000-7000-0000-000000000000")), want: 122192928000000000},
289 {u: Must(FromString("018a8fec-3ced-7164-995f-93c80cbdc575")), want: 139139245386050000},
290 // Calculated as `(1<<48)-1` milliseconds, times 10,000 (100-ns units per ms), plus epoch offset from 1970 to 1582.
291 {u: Must(FromString("ffffffff-ffff-7fff-bfff-ffffffffffff")), want: 2936942695106550000},
292 }
293 for _, tt := range tests {
294 got, err := TimestampFromV7(tt.u)
295
296 switch {
297 case tt.wanterr && err == nil:
298 t.Errorf("TimestampFromV7(%v) want error, got %v", tt.u, got)
299
300 case tt.want != got:
301 t.Errorf("TimestampFromV7(%v) got %v, want %v", tt.u, got, tt.want)
302 }
303 }
304 }
305
306 func TestMinMaxTimestamps(t *testing.T) {
307 tests := []struct {
308 u UUID
309 want time.Time
310 }{
311
312 // v1 min and max
313 {u: Must(FromString("00000000-0000-1000-8000-000000000000")), want: time.Date(1582, 10, 15, 0, 0, 0, 0, time.UTC)}, //1582-10-15 0:00:00 (UTC)
314 {u: Must(FromString("ffffffff-ffff-1fff-bfff-ffffffffffff")), want: time.Date(5236, 3, 31, 21, 21, 00, 684697500, time.UTC)}, //5236-03-31 21:21:00 (UTC)
315
316 // v6 min and max
317 {u: Must(FromString("00000000-0000-6000-8000-000000000000")), want: time.Date(1582, 10, 15, 0, 0, 0, 0, time.UTC)}, //1582-10-15 0:00:00 (UTC)
318 {u: Must(FromString("ffffffff-ffff-6fff-bfff-ffffffffffff")), want: time.Date(5236, 3, 31, 21, 21, 00, 684697500, time.UTC)}, //5236-03-31 21:21:00 (UTC)
319
320 // v7 min and max
321 {u: Must(FromString("00000000-0000-7000-8000-000000000000")), want: time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC)}, //1970-01-01 0:00:00 (UTC)
322 {u: Must(FromString("ffffffff-ffff-7fff-bfff-ffffffffffff")), want: time.Date(10889, 8, 2, 5, 31, 50, 655000000, time.UTC)}, //10889-08-02 5:31:50.655 (UTC)
323 }
324 for _, tt := range tests {
325 var got Timestamp
326 var err error
327 var functionName string
328
329 switch tt.u.Version() {
330 case V1:
331 functionName = "TimestampFromV1"
332 got, err = TimestampFromV1(tt.u)
333 case V6:
334 functionName = "TimestampFromV6"
335 got, err = TimestampFromV6(tt.u)
336 case V7:
337 functionName = "TimestampFromV7"
338 got, err = TimestampFromV7(tt.u)
339 }
340
341 if err != nil {
342 t.Errorf(functionName+"(%v) got error %v, want %v", tt.u, err, tt.want)
343 }
344
345 tm, err := got.Time()
346 if err != nil {
347 t.Errorf(functionName+"(%v) got error %v, want %v", tt.u, err, tt.want)
348 }
349
350 if !tt.want.Equal(tm) {
351 t.Errorf(functionName+"(%v) got %v, want %v", tt.u, tm.UTC(), tt.want)
352 }
353 }
354 }
355
356 func BenchmarkFormat(b *testing.B) {
357 var tests = []string{
358 "%s",
359 "%S",
360 "%q",
361 "%x",
362 "%X",
363 "%v",
364 "%+v",
365 "%#v",
366 }
367 for _, x := range tests {
368 b.Run(x[1:], func(b *testing.B) {
369 for i := 0; i < b.N; i++ {
370 fmt.Fprintf(io.Discard, x, &codecTestUUID)
371 }
372 })
373 }
374 }
375
376 var timestampBenchmarkSink Timestamp
377 var timeBenchmarkSink time.Time
378
379 func BenchmarkTimestampFrom(b *testing.B) {
380 var err error
381 numbUUIDs := 1000
382 if testing.Short() {
383 numbUUIDs = 10
384 }
385
386 funcs := []struct {
387 name string
388 create func() (UUID, error)
389 timestamp func(UUID) (Timestamp, error)
390 }{
391 {"v1", NewV1, TimestampFromV1},
392 {"v6", NewV6, TimestampFromV6},
393 {"v7", NewV7, TimestampFromV7},
394 }
395
396 for _, fns := range funcs {
397 b.Run(fns.name, func(b *testing.B) {
398 // Make sure we don't just encode the same string over and over again as that will hit memory caches unrealistically
399 uuids := make([]UUID, numbUUIDs)
400 for i := 0; i < numbUUIDs; i++ {
401 uuids[i] = Must(fns.create())
402 if !testing.Short() {
403 time.Sleep(1 * time.Millisecond)
404 }
405 }
406 b.ResetTimer()
407 for i := 0; i < b.N; i++ {
408 timestampBenchmarkSink, err = fns.timestamp(uuids[i%numbUUIDs])
409
410 if err != nil {
411 b.Fatal(err)
412 }
413 }
414 })
415 }
416 }
417
418 func BenchmarkTimestampTime(b *testing.B) {
419 var err error
420 numbUUIDs := 1000
421 if testing.Short() {
422 numbUUIDs = 10
423 }
424
425 funcs := []struct {
426 name string
427 create func() (UUID, error)
428 timestamp func(UUID) (Timestamp, error)
429 }{
430 {"v1", NewV1, TimestampFromV1},
431 {"v6", NewV6, TimestampFromV6},
432 {"v7", NewV7, TimestampFromV7},
433 }
434
435 for _, fns := range funcs {
436 b.Run(fns.name, func(b *testing.B) {
437 // Make sure we don't just encode the same string over and over again as that will hit memory caches unrealistically
438 uuids := make([]UUID, numbUUIDs)
439 timestamps := make([]Timestamp, numbUUIDs)
440 for i := 0; i < numbUUIDs; i++ {
441 uuids[i] = Must(fns.create())
442 timestamps[i], err = fns.timestamp(uuids[i])
443 if err != nil {
444 b.Fatal(err)
445 }
446 if !testing.Short() {
447 time.Sleep(1 * time.Millisecond)
448 }
449 }
450 b.ResetTimer()
451 for i := 0; i < b.N; i++ {
452 timeBenchmarkSink, err = timestamps[i%numbUUIDs].Time()
453 if err != nil {
454 b.Fatal(err)
455 }
456 }
457 })
458 }
459
460 }