-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils_test.go
343 lines (273 loc) · 7.75 KB
/
utils_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
/*
* Atree - Scalable Arrays and Ordered Maps
*
* Copyright 2021 Dapper Labs, Inc.
*
* 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 (
"flag"
"math/rand"
"testing"
"time"
"github.com/fxamacker/cbor/v2"
"github.com/stretchr/testify/require"
)
var (
runes = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_")
)
var seed = flag.Int64("seed", 0, "seed for pseudo-random source")
func newRand(tb testing.TB) *rand.Rand {
if *seed == 0 {
*seed = time.Now().UnixNano()
}
// Benchmarks always log, so only log for tests which
// will only log with -v flag or on error.
if t, ok := tb.(*testing.T); ok {
t.Logf("seed: %d\n", *seed)
}
return rand.New(rand.NewSource(*seed))
}
// randStr returns random UTF-8 string of given length.
func randStr(r *rand.Rand, length int) string {
b := make([]rune, length)
for i := 0; i < length; i++ {
b[i] = runes[r.Intn(len(runes))]
}
return string(b)
}
func randomValue(r *rand.Rand, maxInlineSize int) Value {
switch r.Intn(6) {
case 0:
return Uint8Value(r.Intn(255))
case 1:
return Uint16Value(r.Intn(6535))
case 2:
return Uint32Value(r.Intn(4294967295))
case 3:
return Uint64Value(r.Intn(1844674407370955161))
case 4: // small string (inlinable)
slen := r.Intn(maxInlineSize)
return NewStringValue(randStr(r, slen))
case 5: // large string (external)
slen := r.Intn(1024) + maxInlineSize
return NewStringValue(randStr(r, slen))
default:
panic(NewUnreachableError())
}
}
type testTypeInfo struct {
value uint64
}
var _ TypeInfo = testTypeInfo{}
func (i testTypeInfo) Encode(enc *cbor.StreamEncoder) error {
return enc.EncodeUint64(i.value)
}
func (i testTypeInfo) Equal(other TypeInfo) bool {
otherTestTypeInfo, ok := other.(testTypeInfo)
return ok && i.value == otherTestTypeInfo.value
}
func typeInfoComparator(a, b TypeInfo) bool {
x, ok := a.(testTypeInfo)
if !ok {
return false
}
y, ok := b.(testTypeInfo)
return ok && x.value == y.value
}
func newTestPersistentStorage(t testing.TB) *PersistentSlabStorage {
baseStorage := NewInMemBaseStorage()
encMode, err := cbor.EncOptions{}.EncMode()
require.NoError(t, err)
decMode, err := cbor.DecOptions{}.DecMode()
require.NoError(t, err)
return NewPersistentSlabStorage(
baseStorage,
encMode,
decMode,
decodeStorable,
decodeTypeInfo,
)
}
func newTestPersistentStorageWithData(t testing.TB, data map[StorageID][]byte) *PersistentSlabStorage {
baseStorage := NewInMemBaseStorage()
baseStorage.segments = data
return newTestPersistentStorageWithBaseStorage(t, baseStorage)
}
func newTestPersistentStorageWithBaseStorage(t testing.TB, baseStorage BaseStorage) *PersistentSlabStorage {
encMode, err := cbor.EncOptions{}.EncMode()
require.NoError(t, err)
decMode, err := cbor.DecOptions{}.DecMode()
require.NoError(t, err)
return NewPersistentSlabStorage(
baseStorage,
encMode,
decMode,
decodeStorable,
decodeTypeInfo,
)
}
func newTestBasicStorage(t testing.TB) *BasicSlabStorage {
encMode, err := cbor.EncOptions{}.EncMode()
require.NoError(t, err)
decMode, err := cbor.DecOptions{}.DecMode()
require.NoError(t, err)
return NewBasicSlabStorage(
encMode,
decMode,
decodeStorable,
decodeTypeInfo,
)
}
type InMemBaseStorage struct {
segments map[StorageID][]byte
storageIndex map[Address]StorageIndex
bytesRetrieved int
bytesStored int
segmentsReturned map[StorageID]struct{}
segmentsUpdated map[StorageID]struct{}
segmentsTouched map[StorageID]struct{}
}
var _ BaseStorage = &InMemBaseStorage{}
func NewInMemBaseStorage() *InMemBaseStorage {
return NewInMemBaseStorageFromMap(
make(map[StorageID][]byte),
)
}
func NewInMemBaseStorageFromMap(segments map[StorageID][]byte) *InMemBaseStorage {
return &InMemBaseStorage{
segments: segments,
storageIndex: make(map[Address]StorageIndex),
segmentsReturned: make(map[StorageID]struct{}),
segmentsUpdated: make(map[StorageID]struct{}),
segmentsTouched: make(map[StorageID]struct{}),
}
}
func (s *InMemBaseStorage) Retrieve(id StorageID) ([]byte, bool, error) {
seg, ok := s.segments[id]
s.bytesRetrieved += len(seg)
s.segmentsReturned[id] = struct{}{}
s.segmentsTouched[id] = struct{}{}
return seg, ok, nil
}
func (s *InMemBaseStorage) Store(id StorageID, data []byte) error {
s.segments[id] = data
s.bytesStored += len(data)
s.segmentsUpdated[id] = struct{}{}
s.segmentsTouched[id] = struct{}{}
return nil
}
func (s *InMemBaseStorage) Remove(id StorageID) error {
s.segmentsUpdated[id] = struct{}{}
s.segmentsTouched[id] = struct{}{}
delete(s.segments, id)
return nil
}
func (s *InMemBaseStorage) GenerateStorageID(address Address) (StorageID, error) {
index := s.storageIndex[address]
nextIndex := index.Next()
s.storageIndex[address] = nextIndex
return NewStorageID(address, nextIndex), nil
}
func (s *InMemBaseStorage) SegmentCounts() int {
return len(s.segments)
}
func (s *InMemBaseStorage) Size() int {
total := 0
for _, seg := range s.segments {
total += len(seg)
}
return total
}
func (s *InMemBaseStorage) BytesRetrieved() int {
return s.bytesRetrieved
}
func (s *InMemBaseStorage) BytesStored() int {
return s.bytesStored
}
func (s *InMemBaseStorage) SegmentsReturned() int {
return len(s.segmentsReturned)
}
func (s *InMemBaseStorage) SegmentsUpdated() int {
return len(s.segmentsUpdated)
}
func (s *InMemBaseStorage) SegmentsTouched() int {
return len(s.segmentsTouched)
}
func (s *InMemBaseStorage) ResetReporter() {
s.bytesStored = 0
s.bytesRetrieved = 0
s.segmentsReturned = make(map[StorageID]struct{})
s.segmentsUpdated = make(map[StorageID]struct{})
s.segmentsTouched = make(map[StorageID]struct{})
}
func valueEqual(t *testing.T, tic TypeInfoComparator, a Value, b Value) {
switch a.(type) {
case *Array:
arrayEqual(t, tic, a, b)
case *OrderedMap:
mapEqual(t, tic, a, b)
default:
require.Equal(t, a, b)
}
}
func arrayEqual(t *testing.T, tic TypeInfoComparator, a Value, b Value) {
array1, ok := a.(*Array)
require.True(t, ok)
array2, ok := b.(*Array)
require.True(t, ok)
require.True(t, tic(array1.Type(), array2.Type()))
require.Equal(t, array1.Address(), array2.Address())
require.Equal(t, array1.Count(), array2.Count())
require.Equal(t, array1.StorageID(), array2.StorageID())
iterator1, err := array1.Iterator()
require.NoError(t, err)
iterator2, err := array2.Iterator()
require.NoError(t, err)
for {
value1, err := iterator1.Next()
require.NoError(t, err)
value2, err := iterator2.Next()
require.NoError(t, err)
valueEqual(t, tic, value1, value2)
if value1 == nil || value2 == nil {
break
}
}
}
func mapEqual(t *testing.T, tic TypeInfoComparator, a Value, b Value) {
m1, ok := a.(*OrderedMap)
require.True(t, ok)
m2, ok := b.(*OrderedMap)
require.True(t, ok)
require.True(t, tic(m1.Type(), m2.Type()))
require.Equal(t, m1.Address(), m2.Address())
require.Equal(t, m1.Count(), m2.Count())
require.Equal(t, m1.StorageID(), m2.StorageID())
iterator1, err := m1.Iterator()
require.NoError(t, err)
iterator2, err := m2.Iterator()
require.NoError(t, err)
for {
key1, value1, err := iterator1.Next()
require.NoError(t, err)
key2, value2, err := iterator2.Next()
require.NoError(t, err)
valueEqual(t, tic, key1, key2)
valueEqual(t, tic, value1, value2)
if key1 == nil || key2 == nil {
break
}
}
}