-
Notifications
You must be signed in to change notification settings - Fork 5
/
global_test.go
229 lines (189 loc) · 5.53 KB
/
global_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
package sno
import (
"reflect"
"testing"
)
func TestGlobal_Init(t *testing.T) {
t.Run("sane", func(t *testing.T) {
defer func() {
if err := recover(); err != nil {
t.Fatal("expected init to not panic")
}
}()
// Must never panic.
doInit()
})
t.Run("panics", func(t *testing.T) {
defer func() {
err := recover()
if err == nil {
t.Fatal("expected init to panic")
}
if _, ok := err.(*PartitionPoolExhaustedError); !ok {
t.Errorf("expected panic with type [%T], got [%T]", &PartitionPoolExhaustedError{}, err)
return
}
}()
// Theoretically impossible to happen but ensure that we cover all "potential" cases
// where the global generator could fail to get constructed and we need to panic.
//
// At present only one branch even has an error return, so we simulate that... impossibility
// by trying to create more Generators without snapshots than we have a Partition pool for.
// Note that we are invoking doInit() instead of NewGenerator() directly.
for i := 0; i < 2*MaxPartition; i++ {
doInit()
}
})
}
func TestGlobal_FromEncodedString_Valid(t *testing.T) {
src := "brpk4q72xwf2m63l"
expected := ID{78, 111, 33, 96, 160, 255, 154, 10, 16, 51}
actual, err := FromEncodedString(src)
if err != nil {
t.Fatal(err)
}
if actual != expected {
t.Errorf("expected [%v], got [%v]", expected, actual)
}
}
func TestGlobal_FromEncodedString_Invalid(t *testing.T) {
_, err := FromEncodedString("012brpk4q72xwf2m63l1245453gfdgxz")
if _, ok := err.(*InvalidDataSizeError); !ok {
t.Errorf("expected error with type [%T], got [%T]", &InvalidDataSizeError{}, err)
}
if err != nil && err.Error() != errInvalidDataSizeMsg {
t.Errorf("expected error [%s], got [%s]", errInvalidDataSizeMsg, err.Error())
}
}
func TestGlobal_FromEncodedBytes_Valid(t *testing.T) {
src := []byte("brpk4q72xwf2m63l")
expected := ID{78, 111, 33, 96, 160, 255, 154, 10, 16, 51}
actual, err := FromEncodedBytes(src)
if err != nil {
t.Fatal(err)
}
if actual != expected {
t.Errorf("expected [%v], got [%v]", expected, actual)
}
}
func TestGlobal_FromEncodedBytes_Invalid(t *testing.T) {
_, err := FromEncodedBytes([]byte("012brpk4q72xwf2m63l1245453gfdgxz"))
if _, ok := err.(*InvalidDataSizeError); !ok {
t.Errorf("expected error with type [%T], got [%T]", &InvalidDataSizeError{}, err)
}
if err != nil && err.Error() != errInvalidDataSizeMsg {
t.Errorf("expected error [%s], got [%s]", errInvalidDataSizeMsg, err.Error())
}
}
func TestGlobal_FromBinaryBytes_Valid(t *testing.T) {
src := []byte{78, 111, 33, 96, 160, 255, 154, 10, 16, 51}
expected := ID{78, 111, 33, 96, 160, 255, 154, 10, 16, 51}
actual, err := FromBinaryBytes(src)
if err != nil {
t.Fatal(err)
}
if actual != expected {
t.Errorf("expected [%v], got [%v]", expected, actual)
}
}
func TestGlobal_FromBinaryBytes_Invariant(t *testing.T) {
expected := New(255)
actual, err := FromBinaryBytes(expected[:])
if err != nil {
t.Fatal(err)
}
if actual != expected {
t.Errorf("expected [%v], got [%v]", expected, actual)
}
}
func TestGlobal_FromBinaryBytes_Invalid(t *testing.T) {
for _, c := range []struct {
n int
invalid bool
}{
{4, true},
{8, true},
{10, false},
{12, true},
{16, true},
} {
b := make([]byte, c.n)
_, err := FromBinaryBytes(b)
if actual, expected := err != nil, c.invalid; actual != expected {
t.Errorf("expected error [%v], got [%v]", expected, actual)
}
}
}
func TestGlobal_Collection(t *testing.T) {
var ids = []ID{{1}, {2}, {3}, {4}, {5}, {6}}
t.Run("len", makeCollectionLenTest(ids))
t.Run("less", makeCollectionLessTest(ids))
t.Run("swap", makeCollectionSwapTest(ids))
t.Run("sort", makeCollectionSortTest(ids))
}
func makeCollectionLenTest(ids []ID) func(t *testing.T) {
n := len(ids)
return func(t *testing.T) {
if actual, expected := collection([]ID{}).Len(), 0; actual != expected {
t.Errorf("Len() %v, want %v", expected, actual)
}
if actual, expected := collection(ids).Len(), n; actual != expected {
t.Errorf("expected [%v], got [%v]", expected, actual)
}
}
}
func makeCollectionLessTest(ids []ID) func(t *testing.T) {
return func(t *testing.T) {
c := collection(ids)
if c.Less(0, 0) {
t.Errorf("expected [false], got [true]")
}
if !c.Less(0, 1) {
t.Errorf("expected [true], got [false]")
}
if !c.Less(1, 2) {
t.Errorf("expected [true], got [false]")
}
}
}
func makeCollectionSwapTest(ids []ID) func(t *testing.T) {
return func(t *testing.T) {
b := make([]ID, len(ids))
copy(b, ids)
c := collection(b)
c.Swap(1, 2)
if actual, expected := c[1], ids[2]; actual != expected {
t.Errorf("expected [%v], got [%v]", expected, actual)
}
if actual, expected := c[2], ids[1]; actual != expected {
t.Errorf("expected [%v], got [%v]", expected, actual)
}
c.Swap(3, 3)
if actual, expected := c[3], ids[3]; actual != expected {
t.Errorf("expected [%v], got [%v]", expected, actual)
}
}
}
func makeCollectionSortTest(ids []ID) func(t *testing.T) {
return func(t *testing.T) {
src := make([]ID, len(ids))
copy(src, ids)
// Input IDs are sorted, so a comparison will do the trick.
src[2], src[1] = src[1], src[2]
src[4], src[3] = src[3], src[4]
Sort(src)
if actual, expected := src, ids; !reflect.DeepEqual(actual, expected) {
t.Errorf("expected [%v], got [%v]", expected, actual)
}
}
}
func TestGlobal_Zero(t *testing.T) {
if actual := Zero(); actual != (ID{}) {
t.Error("Zero() not equal to ID{}")
}
}
func TestGlobal_Zero_IsZero(t *testing.T) {
if !Zero().IsZero() {
t.Error("Zero().IsZero() is not true")
}
}