forked from gofrs/uuid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uuid_test.go
183 lines (171 loc) · 5.58 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
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
// Copyright (C) 2013-2018 by Maxim Bublis <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package uuid
import (
"bytes"
"fmt"
"testing"
"time"
)
func TestUUID(t *testing.T) {
t.Run("Bytes", testUUIDBytes)
t.Run("String", testUUIDString)
t.Run("Version", testUUIDVersion)
t.Run("Variant", testUUIDVariant)
t.Run("SetVersion", testUUIDSetVersion)
t.Run("SetVariant", testUUIDSetVariant)
}
func testUUIDBytes(t *testing.T) {
got := codecTestUUID.Bytes()
want := codecTestData
if !bytes.Equal(got, want) {
t.Errorf("%v.Bytes() = %x, want %x", codecTestUUID, got, want)
}
}
func testUUIDString(t *testing.T) {
got := NamespaceDNS.String()
want := "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
if got != want {
t.Errorf("%v.String() = %q, want %q", NamespaceDNS, got, want)
}
}
func testUUIDVersion(t *testing.T) {
u := UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}
if got, want := u.Version(), V1; got != want {
t.Errorf("%v.Version() == %d, want %d", u, got, want)
}
}
func testUUIDVariant(t *testing.T) {
tests := []struct {
u UUID
want byte
}{
{
u: UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
want: VariantNCS,
},
{
u: UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
want: VariantRFC4122,
},
{
u: UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
want: VariantMicrosoft,
},
{
u: UUID{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
want: VariantFuture,
},
}
for _, tt := range tests {
if got := tt.u.Variant(); got != tt.want {
t.Errorf("%v.Variant() == %d, want %d", tt.u, got, tt.want)
}
}
}
func testUUIDSetVersion(t *testing.T) {
u := UUID{}
want := V4
u.SetVersion(want)
if got := u.Version(); got != want {
t.Errorf("%v.Version() == %d after SetVersion(%d)", u, got, want)
}
}
func testUUIDSetVariant(t *testing.T) {
variants := []byte{
VariantNCS,
VariantRFC4122,
VariantMicrosoft,
VariantFuture,
}
for _, want := range variants {
u := UUID{}
u.SetVariant(want)
if got := u.Variant(); got != want {
t.Errorf("%v.Variant() == %d after SetVariant(%d)", u, got, want)
}
}
}
func TestMust(t *testing.T) {
sentinel := fmt.Errorf("uuid: sentinel error")
defer func() {
r := recover()
if r == nil {
t.Fatalf("did not panic, want %v", sentinel)
}
err, ok := r.(error)
if !ok {
t.Fatalf("panicked with %T, want error (%v)", r, sentinel)
}
if err != sentinel {
t.Fatalf("panicked with %v, want %v", err, sentinel)
}
}()
fn := func() (UUID, error) {
return Nil, sentinel
}
Must(fn())
}
func TestTimeFromTimestamp(t *testing.T) {
tests := []struct {
t Timestamp
want time.Time
}{
// a zero timestamp represents October 15, 1582 at midnight UTC
{t: Timestamp(0), want: time.Date(1582, 10, 15, 0, 0, 0, 0, time.UTC)},
// a one value is 100ns later
{t: Timestamp(1), want: time.Date(1582, 10, 15, 0, 0, 0, 100, time.UTC)},
// 10 million 100ns intervals later is one second
{t: Timestamp(10000000), want: time.Date(1582, 10, 15, 0, 0, 1, 0, time.UTC)},
{t: Timestamp(60 * 10000000), want: time.Date(1582, 10, 15, 0, 1, 0, 0, time.UTC)},
{t: Timestamp(60 * 60 * 10000000), want: time.Date(1582, 10, 15, 1, 0, 0, 0, time.UTC)},
{t: Timestamp(24 * 60 * 60 * 10000000), want: time.Date(1582, 10, 16, 0, 0, 0, 0, time.UTC)},
{t: Timestamp(365 * 24 * 60 * 60 * 10000000), want: time.Date(1583, 10, 15, 0, 0, 0, 0, time.UTC)},
// maximum timestamp value in a UUID is the year 5236
{t: Timestamp(uint64(1<<60 - 1)), want: time.Date(5236, 03, 31, 21, 21, 0, 684697500, time.UTC)},
}
for _, tt := range tests {
got, _ := tt.t.Time()
if !got.Equal(tt.want) {
t.Errorf("%v.Time() == %v, want %v", tt.t, got, tt.want)
}
}
}
func TestTimestampFromV1(t *testing.T) {
tests := []struct {
u UUID
want Timestamp
wanterr bool
}{
{u: Must(NewV4()), wanterr: true},
{u: Must(FromString("00000000-0000-1000-0000-000000000000")), want: 0},
{u: Must(FromString("424f137e-a2aa-11e8-98d0-529269fb1459")), want: 137538640775418750},
{u: Must(FromString("ffffffff-ffff-1fff-ffff-ffffffffffff")), want: Timestamp(1<<60 - 1)},
}
for _, tt := range tests {
got, goterr := TimestampFromV1(tt.u)
if tt.wanterr && goterr == nil {
t.Errorf("TimestampFromV1(%v) want error, got %v", tt.u, got)
} else if tt.want != got {
t.Errorf("TimestampFromV1(%v) got %v, want %v", tt.u, got, tt.want)
}
}
}