-
Notifications
You must be signed in to change notification settings - Fork 16
/
flag_test.go
360 lines (302 loc) · 7.39 KB
/
flag_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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
* Atree - Scalable Arrays and Ordered Maps
*
* Copyright Flow Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package atree
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestFlagIsRoot(t *testing.T) {
testCases := []struct {
name string
h head
}{
{"v0", head([2]byte{})},
{"v1", head([2]byte{1 << 4, 0x0})},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i := 0; i <= 255; i++ {
tc.h[1] = byte(i)
if i >= 1<<7 {
require.True(t, tc.h.isRoot())
} else {
require.False(t, tc.h.isRoot())
}
}
})
}
}
func TestFlagSetRootV1(t *testing.T) {
var h head
h[0] = 1 << 4 // version 1
for i := 0; i <= 255; i++ {
h[1] = byte(i)
h.setRoot()
require.True(t, h.isRoot())
}
}
func TestFlagHasPointers(t *testing.T) {
testCases := []struct {
name string
h head
}{
{"v0", head([2]byte{})},
{"v1", head([2]byte{1 << 4, 0x0})},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i := 0; i <= 255; i++ {
tc.h[1] = byte(i)
if byte(i)&maskSlabHasPointers != 0 {
require.True(t, tc.h.hasPointers())
} else {
require.False(t, tc.h.hasPointers())
}
}
})
}
}
func TestFlagSetHasPointersV1(t *testing.T) {
var h head
h[0] = 1 << 4 // version 1
for i := 0; i <= 255; i++ {
h[1] = byte(i)
h.setHasPointers()
require.True(t, h.hasPointers())
}
}
func TestFlagHasSizeLimit(t *testing.T) {
testCases := []struct {
name string
h head
}{
{"v0", head([2]byte{})},
{"v1", head([2]byte{1 << 4, 0x0})},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i := 0; i <= 255; i++ {
tc.h[1] = byte(i)
if byte(i)&maskSlabAnySize == 0 {
require.True(t, tc.h.hasSizeLimit())
} else {
require.False(t, tc.h.hasSizeLimit())
}
}
})
}
}
func TestFlagSetNoSizeLimitV1(t *testing.T) {
var h head
h[0] = 1 << 4 // version 1
for i := 0; i <= 255; i++ {
h[1] = byte(i)
h.setNoSizeLimit()
require.False(t, h.hasSizeLimit())
}
}
func TestFlagHasNextSlabID(t *testing.T) {
var h head
h[0] = 1 << 4 // v1
t.Run("has", func(t *testing.T) {
// Flags in the first byte
for i := 0; i < 32; i++ {
h[0] |= byte(i)
h[0] |= maskHasNextSlabID
// Flags in the second byte
for j := 0; j <= 255; j++ {
h[1] = byte(j)
require.True(t, h.hasNextSlabID())
}
}
})
t.Run("doesn't have", func(t *testing.T) {
// Flags in the first byte
for i := 0; i < 32; i++ {
h[0] |= byte(i)
h[0] &= ^maskHasNextSlabID
// Flags in the second byte
for j := 0; j <= 255; j++ {
h[1] = byte(j)
require.False(t, h.hasNextSlabID())
}
}
})
}
func TestFlagSetHasNextSlabIDV1(t *testing.T) {
var h head
h[0] = 1 << 4 // version 1
// Flags in the first byte
for i := 0; i < 32; i++ {
h[0] |= byte(i)
// Flags in the second byte
for i := 0; i <= 255; i++ {
h[1] = byte(i)
h.setHasNextSlabID()
require.True(t, h.hasNextSlabID())
}
}
}
func TestFlagHasInlinedSlabs(t *testing.T) {
var h head
h[0] = 1 << 4 // v1
t.Run("has", func(t *testing.T) {
// Flags in the first byte
for i := 0; i < 32; i++ {
h[0] |= byte(i)
h[0] |= maskHasInlinedSlabs
// Flags in the second byte
for j := 0; j <= 255; j++ {
h[1] = byte(j)
require.True(t, h.hasInlinedSlabs())
}
}
})
t.Run("doesn't have", func(t *testing.T) {
// Flags in the first byte
for i := 0; i < 32; i++ {
h[0] |= byte(i)
h[0] &= ^maskHasInlinedSlabs
// Flags in the second byte
for j := 0; j <= 255; j++ {
h[1] = byte(j)
require.False(t, h.hasInlinedSlabs())
}
}
})
}
func TestFlagSetHasInlinedSlabsV1(t *testing.T) {
var h head
h[0] = 1 << 4 // version 1
// Flags in the first byte
for i := 0; i < 32; i++ {
h[0] |= byte(i)
// Flags in the second byte
for i := 0; i <= 255; i++ {
h[1] = byte(i)
h.setHasInlinedSlabs()
require.True(t, h.hasInlinedSlabs())
}
}
}
func TestFlagGetSlabType(t *testing.T) {
testCases := []struct {
name string
h head
}{
{"v0", head([2]byte{})},
{"v1", head([2]byte{1 << 4, 0x0})},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i := 0; i <= 255; i++ {
arrayFlag := byte(i) & 0b111_00111
tc.h[1] = arrayFlag
require.Equal(t, slabArray, tc.h.getSlabType())
mapFlag := arrayFlag | 0b000_01000
tc.h[1] = mapFlag
require.Equal(t, slabMap, tc.h.getSlabType())
storableFlag := arrayFlag | 0b000_11111
tc.h[1] = storableFlag
require.Equal(t, slabStorable, tc.h.getSlabType())
}
})
}
}
func TestFlagGetSlabArrayType(t *testing.T) {
testCases := []struct {
name string
h head
}{
{"v0", head([2]byte{})},
{"v1", head([2]byte{1 << 4, 0x0})},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i := 0; i <= 255; i++ {
arrayDataFlag := byte(i) & 0b111_00000
tc.h[1] = arrayDataFlag
require.Equal(t, slabArrayData, tc.h.getSlabArrayType())
arrayMetaFlag := arrayDataFlag | 0b000_00001
tc.h[1] = arrayMetaFlag
require.Equal(t, slabArrayMeta, tc.h.getSlabArrayType())
arrayLargeImmutableArrayFlag := arrayDataFlag | 0b000_00010
tc.h[1] = arrayLargeImmutableArrayFlag
require.Equal(t, slabLargeImmutableArray, tc.h.getSlabArrayType())
basicArrayFlag := arrayDataFlag | 0b000_00011
tc.h[1] = basicArrayFlag
require.Equal(t, slabBasicArray, tc.h.getSlabArrayType())
}
})
}
}
func TestFlagGetSlabMapType(t *testing.T) {
testCases := []struct {
name string
h head
}{
{"v0", head([2]byte{})},
{"v1", head([2]byte{1 << 4, 0x0})},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
for i := 0; i <= 255; i++ {
b := byte(i)
b |= 0b000_01000 // turn on map flag
b &= 0b111_01111 // turn off storable flag
mapDataFlag := b & 0b111_11000
tc.h[1] = mapDataFlag
require.Equal(t, slabMapData, tc.h.getSlabMapType())
mapMetaFlag := mapDataFlag | 0b000_00001
tc.h[1] = mapMetaFlag
require.Equal(t, slabMapMeta, tc.h.getSlabMapType())
mapLargeImmutableArrayFlag := mapDataFlag | 0b000_00010
tc.h[1] = mapLargeImmutableArrayFlag
require.Equal(t, slabMapLargeEntry, tc.h.getSlabMapType())
collisionGroupFlag := mapDataFlag | 0b000_00011
tc.h[1] = collisionGroupFlag
require.Equal(t, slabMapCollisionGroup, tc.h.getSlabMapType())
}
})
}
}
func TestVersion(t *testing.T) {
t.Run("v0", func(t *testing.T) {
const expectedVersion = byte(0)
var h head
// Flags in the second byte
for i := 0; i <= 255; i++ {
h[1] = byte(i)
require.Equal(t, expectedVersion, h.version())
}
})
t.Run("v1", func(t *testing.T) {
const expectedVersion = byte(1)
var h head
h[0] = 0x10
// Flags in the first byte
for i := 0; i < 32; i++ {
h[0] |= byte(i)
// Flags in the second byte
for j := 0; j <= 255; j++ {
h[1] = byte(j)
require.Equal(t, expectedVersion, h.version())
}
}
})
}