-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuuid_test.go
66 lines (55 loc) · 2.34 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
package uuid
import (
"testing"
"time"
"github.com/rsms/go-testutil"
)
func TestUUID(t *testing.T) {
assert := testutil.NewAssert(t)
assert.Eq("Min encoding", Min.String(), "0")
assert.Eq("Max encoding", Max.String(), "7n42DGM5Tflk9n8mt7Fhc7")
smallId := UUID{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2}
id1, err := Gen()
assert.NoErr("Gen", err)
id2 := MustGen() // calls Gen and panics if it returns an error
// t.Logf("id1: % x %q", id1[:], id1)
// t.Logf("id2: %x %q", id2[:], id2)
assert.Eq("Min decode(encode())", Min, FromString(Min.String()))
assert.Eq("Max decode(encode())", Max, FromString(Max.String()))
assert.Eq("smallId decode(encode())", smallId, FromString(smallId.String()))
assert.Eq("id1 decode(encode())", id1, FromString(id1.String()))
assert.Eq("id1 decode(encode())", id1, FromString(id1.String()))
assert.Eq("id2 decode(encode())", id2, FromString(id2.String()))
// DecodeString: Check that DecodeString rewrites all bytes, not just some
var id3 UUID
assert.Eq("id3", Min, id3)
id3.DecodeString([]byte("7n42DGM5Tflk9n8mt7Fhc7"))
assert.Eq("id3 DecodeString should work", id3, Max)
id3.DecodeString([]byte("A"))
assert.Eq("id3 should be zeroed", id3, UUID{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xA})
// Check that Time() and Timestamp() returns the correct time as encoded
tm := time.Unix(1600000000+3212345, 123*1000000) // 2020-10-20 16:45:45.123 UTC
idt := New(tm.Unix(), tm.Nanosecond(), []byte{})
// t.Logf("tm %s", tm.UTC())
// t.Logf("idt %x", idt[:])
tssec, tsms := idt.Timestamp()
assert.Eq("Timestamp() tssec", tssec, uint32(3212345))
assert.Eq("Timestamp() tsms", tsms, uint16(123))
// must check with rounded time since UUID timestamp has only millisecond precision
assert.Eq("Time()", idt.Time().Unix(), tm.Unix())
assert.Eq("Time()",
idt.Time().Nanosecond()/int(time.Millisecond),
tm.Nanosecond()/int(time.Millisecond))
// raw byte representation
bytes := id1.Bytes()
id1b := FromBytes(bytes)
assert.Eq("FromBytes(Bytes()) yields same result", id1, id1b)
// // --------------------------------------------------------
// // Generate UUIDs for documentation or demo
// for i := 0; i < 5; i++ {
// id := Gen()
// // 2020-10-20 16:45:45.xxx UTC
// id = New(1603212345+int64(i), time.Now().Nanosecond()+i*1000000, id[6:])
// t.Logf("% x %q %s", id[:], id, id.Time().UTC())
// }
}