Repository navigation
Expand file tree
/
Copy pathserialization_frozen_test.go
More file actions
289 lines (267 loc) · 8.1 KB
/
Copy pathserialization_frozen_test.go
File metadata and controls
289 lines (267 loc) · 8.1 KB
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
//go:build (386 && !appengine) || (amd64 && !appengine) || (arm && !appengine) || (arm64 && !appengine) || (ppc64le && !appengine) || (mipsle && !appengine) || (mips64le && !appengine) || (mips64p32le && !appengine) || (wasm && !appengine)
// +build 386,!appengine amd64,!appengine arm,!appengine arm64,!appengine ppc64le,!appengine mipsle,!appengine mips64le,!appengine mips64p32le,!appengine wasm,!appengine
package roaring
import (
"bytes"
"fmt"
"os"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFrozenFormat(t *testing.T) {
tests := [...]struct {
name, frozenPath, portablePath string
}{
{
name: "bitmaps only",
frozenPath: "testfrozendata/bitmaps_only.frozen",
portablePath: "testfrozendata/bitmaps_only.portable",
},
{
name: "arrays only",
frozenPath: "testfrozendata/arrays_only.frozen",
portablePath: "testfrozendata/arrays_only.portable",
},
{
name: "runs only",
frozenPath: "testfrozendata/runs_only.frozen",
portablePath: "testfrozendata/runs_only.portable",
},
{
name: "mixed",
frozenPath: "testfrozendata/mixed.frozen",
portablePath: "testfrozendata/mixed.portable",
},
}
for _, test := range tests {
// NOTE: opted for loading files twice rather than optimizing it because:
// 1. It's still cheap enough, it's small files; and
// 2. In a buggy scenario one of the tests may write into the buffer and cause
// a race condition, making it harder to figure out why the tests fail.
name, fpath, ppath := test.name, test.frozenPath, test.portablePath
t.Run("view "+name, func(t *testing.T) {
t.Parallel()
frozenBuf, err := os.ReadFile(fpath)
if err != nil {
fmt.Fprintf(os.Stderr, "\n\nIMPORTANT: For testing file IO, the roaring library requires disk access.\nWe omit some tests for now.\n\n")
return
}
portableBuf, err := os.ReadFile(ppath)
if err != nil {
fmt.Fprintf(os.Stderr, "\n\nIMPORTANT: For testing file IO, the roaring library requires disk access.\nWe omit some tests for now.\n\n")
return
}
frozen, portable := New(), New()
if err := frozen.FrozenView(frozenBuf); err != nil {
t.Fatalf("failed to load bitmap from %s: %s", fpath, err)
}
if _, err := portable.FromBuffer(portableBuf); err != nil {
t.Fatalf("failed to load bitmap from %s: %s", ppath, err)
}
if !frozen.Equals(portable) {
t.Fatalf("bitmaps for %s and %s differ", fpath, ppath)
}
})
t.Run("freeze "+name, func(t *testing.T) {
t.Parallel()
frozenBuf, err := os.ReadFile(fpath)
if err != nil {
fmt.Fprintf(os.Stderr, "\n\nIMPORTANT: For testing file IO, the roaring library requires disk access.\nWe omit some tests for now.\n\n")
return
}
portableBuf, err := os.ReadFile(ppath)
if err != nil {
fmt.Fprintf(os.Stderr, "\n\nIMPORTANT: For testing file IO, the roaring library requires disk access.\nWe omit some tests for now.\n\n")
return
}
portable := New()
if _, err := portable.FromBuffer(portableBuf); err != nil {
t.Fatalf("failed to load bitmap from %s: %s", ppath, err)
}
frozenSize := portable.GetFrozenSizeInBytes()
if int(frozenSize) != len(frozenBuf) {
t.Errorf("size for serializing %s differs from %s's size", ppath, fpath)
}
frozen, err := portable.Freeze()
if err != nil {
t.Fatalf("can't freeze %s: %s", ppath, err)
}
if !reflect.DeepEqual(frozen, frozenBuf) {
t.Fatalf("frozen file for %s and %s differ", fpath, ppath)
}
})
t.Run("freeze with writer"+name, func(t *testing.T) {
t.Parallel()
frozenBuf, err := os.ReadFile(fpath)
if err != nil {
fmt.Fprintf(os.Stderr, "\n\nIMPORTANT: For testing file IO, the roaring library requires disk access.\nWe omit some tests for now.\n\n")
return
}
portableBuf, err := os.ReadFile(ppath)
if err != nil {
fmt.Fprintf(os.Stderr, "\n\nIMPORTANT: For testing file IO, the roaring library requires disk access.\nWe omit some tests for now.\n\n")
return
}
portable := New()
if _, err := portable.FromBuffer(portableBuf); err != nil {
t.Fatalf("failed to load bitmap from %s: %s", ppath, err)
}
wr := &bytes.Buffer{}
frozenSize, err := portable.WriteFrozenTo(wr)
if err != nil {
t.Fatalf("can't freeze %s: %s", ppath, err)
}
if frozenSize != len(frozenBuf) {
t.Errorf("size for serializing %s differs from %s's size", ppath, fpath)
}
if !reflect.DeepEqual(wr.Bytes(), frozenBuf) {
t.Fatalf("frozen file for %s and %s differ", fpath, ppath)
}
})
}
}
func TestBitMapValidationFromFrozen(t *testing.T) {
// To understand what is going on here, read https://github.com/RoaringBitmap/RoaringFormatSpec
// Maintainers: The loader and corruptor are dependent on one another
// The tests expect a certain size, with values at certain location.
// The tests are geared toward single byte corruption.
// There is no way to test Bitmap container corruption once the bitmap is deserialized
deserializationTests := []struct {
name string
loader func(bm *Bitmap)
corruptor func(s []byte)
err error
}{
{
name: "Last Run Extends Past Container End",
loader: func(bm *Bitmap) {
bm.AddRange(0, 2)
bm.AddRange(4, 6)
bm.AddRange(8, 100)
bm.RunOptimize() // one run container
},
corruptor: func(s []byte) {
// 10-11 is the length of the last run, which starts at 8:
// 65535 makes it end past 65535
s[10] = 0xFF
s[11] = 0xFF
},
err: ErrRunIntervalOutOfRange,
},
{
name: "Run Extends Past Container End",
loader: func(bm *Bitmap) {
bm.AddRange(100, 110)
bm.RunOptimize() // one run container
},
corruptor: func(s []byte) {
// 2-3 is the length of the run, which starts at 100:
// 65535 makes it end past 65535
s[2] = 0xFF
s[3] = 0xFF
},
err: ErrRunIntervalOutOfRange,
},
{
name: "Creates Interval Overlap",
loader: func(bm *Bitmap) {
bm.AddRange(100, 110)
bm.AddRange(115, 125)
},
corruptor: func(s []byte) {
// sets the start of the second run
// Creates overlapping intervals
s[4] = 108
},
err: ErrRunIntervalOverlap,
},
{
name: "Break Array Sort Order",
loader: func(bm *Bitmap) {
arrayEntries := make([]uint32, 0, 10)
for i := 0; i < 10; i++ {
arrayEntries = append(arrayEntries, uint32(i))
}
bm.AddMany(arrayEntries)
},
corruptor: func(s []byte) {
// breaks the sort order
s[4] = 0
},
err: ErrArrayIncorrectSort,
},
}
for _, tt := range deserializationTests {
t.Run(tt.name, func(t *testing.T) {
bm := NewBitmap()
tt.loader(bm)
assert.NoError(t, bm.Validate())
serialized, err := bm.Freeze()
assert.NoError(t, err)
tt.corruptor(serialized)
corruptedDeserializedBitMap := NewBitmap()
// Check that MustFrozenView returns nil if and only if tt.err is nil
frozenViewErr := corruptedDeserializedBitMap.MustFrozenView(serialized)
if tt.err == nil {
assert.NoError(t, frozenViewErr, "expected MustFrozenView to succeed when tt.err is nil")
} else {
assert.ErrorIs(t, frozenViewErr, tt.err)
}
})
}
}
func TestFrozenViewKindsMissing(t *testing.T) {
tests := []struct {
name string
build func() *Bitmap
}{
{
// no container at all: the frozen buffer is only the 4 byte header
name: "empty bitmap",
build: func() *Bitmap {
return New()
},
},
{
// no array and no run container
name: "one bitmap container",
build: func() *Bitmap {
bm := New()
for i := uint32(0); i < 10000; i += 2 { // 5000 values
bm.Add(i)
}
return bm
},
},
{
// no bitmap and no run container
name: "one array container",
build: func() *Bitmap {
return BitmapOf(1, 5, 9)
},
},
{
// no run container
name: "bitmap and array containers",
build: func() *Bitmap {
bm := New()
for i := uint32(0); i < 10000; i += 2 {
bm.Add(i)
}
bm.Add(1 << 16)
return bm
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bm := tt.build()
buf, err := bm.Freeze()
assert.NoError(t, err)
view := New()
assert.NoError(t, view.FrozenView(buf))
assert.True(t, view.Equals(bm))
})
}
}