forked from twinj/uuid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types_test.go
255 lines (203 loc) · 6.19 KB
/
types_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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package uuid
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
)
func TestUuid_Bytes(t *testing.T) {
id := make(Uuid, length)
copy(id, NameSpaceDNS.Bytes())
assert.Equal(t, id.Bytes(), NameSpaceDNS.Bytes(), "Bytes should be the same")
}
func TestUuid_Size(t *testing.T) {
id := make(Uuid, length)
assert.Equal(t, 16, id.Size(), "The size of the array should be sixteen")
}
func TestUuid_String(t *testing.T) {
id := Uuid(uuidBytes)
assert.Equal(t, idString, id.String(), "The Format given should match the output")
}
func TestUuid_Variant(t *testing.T) {
bytes := make(Uuid, length)
copy(bytes, uuidBytes[:])
for _, v := range uuidVariants {
for i := 0; i <= 255; i++ {
bytes[variantIndex] = byte(i)
id := createMarshaler(bytes, 4, v)
b := id[variantIndex] >> 4
tVariantConstraint(v, b, id, t)
assert.Equal(t, v, id.Variant(), "%x does not resolve to %x", id.Variant(), v)
}
}
assert.True(t, didMarshalerSetVariantPanic(bytes[:]), "Array creation should panic if invalid variant")
}
func didMarshalerSetVariantPanic(bytes []byte) bool {
return func() (didPanic bool) {
defer func() {
if recover() != nil {
didPanic = true
}
}()
createMarshaler(bytes[:], 4, 0xbb)
return
}()
}
func TestUuid_Version(t *testing.T) {
id := make(Uuid, length)
bytes := make(Uuid, length)
copy(bytes, uuidBytes[:])
assert.Equal(t, Unknown, id.Version(), "The version should be 0")
for v := 0; v < 16; v++ {
for i := 0; i <= 255; i++ {
bytes[versionIndex] = byte(i)
copy(id, bytes)
setVersion(&id[versionIndex], v)
if v > 0 && v < 6 {
assert.Equal(t, Version(v), id.Version(), "%x does not resolve to %x", id.Version(), v)
} else {
assert.Equal(t, Version(v), getVersion(id), "%x does not resolve to %x", getVersion(id), v)
}
}
}
}
func TestImmutable_Bytes(t *testing.T) {
b := make([]byte, length)
copy(b[:], NameSpaceDNS.Bytes())
id := Immutable(b)
assert.Equal(t, NameSpaceDNS.Bytes(), id.Bytes())
}
func TestImmutable_Size(t *testing.T) {
assert.Equal(t, 16, Nil.Size(), "The size of the array should be sixteen")
}
func TestImmutable_String(t *testing.T) {
id := Immutable(uuidBytes)
assert.Equal(t, idString, id.String(), "The Format given should match the output")
}
func TestImmutable_Variant(t *testing.T) {
bytes := make(Uuid, length)
copy(bytes, uuidBytes[:])
for _, v := range uuidVariants {
for i := 0; i <= 255; i++ {
bytes[variantIndex] = byte(i)
id := createMarshaler(bytes, 4, v)
b := id[variantIndex] >> 4
tVariantConstraint(v, b, id, t)
id2 := Immutable(id)
assert.Equal(t, v, id2.Variant(), "%x does not resolve to %x", id2.Variant(), v)
}
}
}
func TestImmutable_Version(t *testing.T) {
id := make(Uuid, length)
bytes := make(Uuid, length)
copy(bytes, uuidBytes[:])
for v := 0; v < 16; v++ {
for i := 0; i <= 255; i++ {
bytes[versionIndex] = byte(i)
copy(id, bytes)
setVersion(&id[versionIndex], v)
id2 := Immutable(id)
if v > 0 && v < 6 {
assert.Equal(t, Version(v), id2.Version(), "%x does not resolve to %x", id2.Version(), v)
} else {
assert.Equal(t, Version(v), getVersion(Uuid(id)), "%x does not resolve to %x", getVersion(Uuid(id)), v)
}
}
}
}
func TestUuid_MarshalBinary(t *testing.T) {
id := Uuid(uuidBytes)
bytes, err := id.MarshalBinary()
assert.Nil(t, err, "There should be no error")
assert.Equal(t, uuidBytes[:], bytes, "Byte should be the same")
}
func TestUuid_UnmarshalBinary(t *testing.T) {
assert.True(t, didUnmarshalPanic(), "Should panic")
u := Uuid{}
err := u.UnmarshalBinary([]byte{1, 2, 3, 4, 5})
assert.Error(t, err, "Expect length error")
err = u.UnmarshalBinary(uuidBytes[:])
u = Uuid{}
err = u.UnmarshalBinary(uuidBytes[:])
assert.Nil(t, err, "There should be no error but got %s", err)
for k, v := range namespaces {
id, _ := Parse(v)
u = Uuid{}
u.UnmarshalBinary(id.Bytes())
assert.Equal(t, id.Bytes(), u.Bytes(), "The array id should equal the uuid id")
assert.Equal(t, k.Bytes(), u.Bytes(), "The array id should equal the uuid id")
}
}
func didUnmarshalPanic() bool {
return func() (didPanic bool) {
defer func() {
if recover() != nil {
didPanic = true
}
}()
u := make(Uuid, length)
u.UnmarshalBinary(uuidBytes[:])
return
}()
}
func TestUuid_Scan(t *testing.T) {
var v Uuid
assert.Nil(t, v)
err := v.Scan(nil)
assert.NoError(t, err, "When nil there should be no error")
assert.Empty(t, v, "Should have no data")
err = v.Scan("")
assert.NoError(t, err, "When nil there should be no error")
assert.Empty(t, v, "Should have no data")
var v2 Uuid
err = v2.Scan(NameSpaceDNS.Bytes())
assert.NoError(t, err, "When nil there should be no error")
assert.Equal(t, NameSpaceDNS.Bytes(), v2.Bytes(), "Values should be the same")
err = v.Scan(NameSpaceDNS.String())
assert.NoError(t, err, "When nil there should be no error")
assert.Equal(t, NameSpaceDNS.String(), v.String(), "Values should be the same")
var v3 Uuid
err = v3.Scan([]byte(NameSpaceDNS.String()))
assert.NoError(t, err, "When []byte represents string should be no error")
assert.Equal(t, NameSpaceDNS.String(), v3.String(), "Values should be the same")
err = v.Scan(22)
assert.Error(t, err, "When wrong type should error")
}
func TestUuid_Value(t *testing.T) {
var v Uuid
assert.Nil(t, v)
id, err := v.Value()
assert.Nil(t, id, "There hsould be no driver valuie")
assert.NoError(t, err, "There should be no error")
ns := Uuid(NameSpaceDNS)
id, err = ns.Value()
assert.NotNil(t, id, "Ther hsould be a vliad driver value")
assert.NoError(t, err, "There should be no error")
}
func getVersion(pId Uuid) Version {
return Version(pId[versionIndex] >> 4)
}
func createMarshaler(pData []byte, pVersion int, pVariant uint8) Uuid {
o := make(Uuid, length)
copy(o, pData)
setVersion(&o[versionIndex], pVersion)
setVariant(&o[variantIndex], pVariant)
return o
}
func setVersion(pByte *byte, pVersion int) {
*pByte &= 0x0f
*pByte |= uint8(pVersion << 4)
}
func setVariant(pByte *byte, pVariant uint8) {
switch pVariant {
case VariantRFC4122:
*pByte &= variantSet
case VariantFuture, VariantMicrosoft:
*pByte &= 0x1F
case VariantNCS:
*pByte &= 0x7F
default:
panic(errors.New("uuid.setVariant: invalid variant mask"))
}
*pByte |= pVariant
}