-
Notifications
You must be signed in to change notification settings - Fork 4
/
uuid_test.go
107 lines (81 loc) · 3.57 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
package uuid
import (
"testing"
)
// TestHelloName calls greetings.Hello with a name, checking
// for a valid return value.
func TestUUIDv7FromBytes(t *testing.T) {
_, err := UUIDv7FromBytes([]byte{0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16})
if err != nil {
t.Fatal("Did not expet an error during a conversion", err)
}
_, err = UUIDv7FromBytes([]byte{0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16})
if err == nil {
t.Fatal("Expeted a conversion to error", err)
}
temp, _ := UUIDv7FromBytes([]byte{0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16})
var byteArray [16]byte = [16]byte(temp)
if byteArray != [16]byte{0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16, 0x16} {
t.Fatal("Somehow byte array is changed after a marshaling")
}
}
func TestUUIDv7Ver(t *testing.T) {
test, _ := UUIDv7FromBytes([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
if test.Ver() != 15 {
t.Fatal("Expected Ver to be 16, got ", test.Ver(), " instead")
}
test, _ = UUIDv7FromBytes([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
if test.Ver() != 0 {
t.Fatal("Expected Ver to be 0, got ", test.Ver(), " instead")
}
test, _ = UUIDv7FromBytes([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
if test.Ver() != 7 {
t.Fatal("Expected Ver to be 7, got ", test.Ver(), " instead")
}
}
func TestUUIDv7Var(t *testing.T) {
test, _ := UUIDv7FromBytes([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
if test.Var() != 3 {
t.Fatal("Expected Var to be 3, got ", test.Var(), " instead")
}
test, _ = UUIDv7FromBytes([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
if test.Var() != 0 {
t.Fatal("Expected Var to be 0, got ", test.Var(), " instead")
}
test, _ = UUIDv7FromBytes([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x7F, 0xFF, 0xBF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
if test.Var() != 2 {
t.Fatal("Expected Var to be 2, got ", test.Var(), " instead")
}
}
func TestUUIDv7Timestamp(t *testing.T) {
test, _ := UUIDv7FromBytes([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
if test.Timestamp() != 68719476735 {
t.Fatal("Expected UnixTS to be 68719476735, got ", test.Timestamp(), " instead")
}
test, _ = UUIDv7FromBytes([]byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
if test.Timestamp() != 0 {
t.Fatal("Expected UnixTS to be 0, got ", test.Timestamp(), " instead")
}
test, _ = UUIDv7FromBytes([]byte{0x01, 0xE5, 0x30, 0xE0, 0xF0, 0xFF, 0xFF, 0xFF, 0xBF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF})
if test.Timestamp() != 508759567 {
t.Fatal("Expected UnixTS to be 508759567, got ", test.Timestamp(), " instead")
}
}
func TestUUIDv7Generation(t *testing.T) {
var test UUIDv7Generator
test.SubsecondPrecisionLength = 16
test.NodePrecisionLength = 4
test.Node = 15
test.CounterPrecisionLength = 8
guid := test.Next()
test.Parse(&guid)
//ps, c, n := test.Parse(&guid)
// fmt.Println("binary", guid.ToBinaryString())
// fmt.Println("guid", guid.ToString())
// fmt.Print(ps, c, n)
}
func TestConverters(t *testing.T) {
// out, _ := encodeDecimal(209302800, 64)
// put, _ := decodeDecimal(out, 64)
// fmt.Print(out, put)
}