-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathproperty_test.go
More file actions
287 lines (266 loc) · 6.12 KB
/
property_test.go
File metadata and controls
287 lines (266 loc) · 6.12 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
package roaring
import (
"fmt"
"math/rand"
"testing"
)
// TestBitmapProperties runs all invariants against all bitmaps in the corpus
func TestBitmapProperties(t *testing.T) {
corpus := getBitmapCorpus()
invariants := getInvariants()
for _, gen := range corpus {
for _, inv := range invariants {
t.Run(fmt.Sprintf("%s/%s", gen.name, inv.name), func(t *testing.T) {
b := gen.gen()
inv.test(t, b)
})
}
}
}
// TestBitmapPropertiesWithRunOptimize tests all invariants on RunOptimize'd bitmaps
func TestBitmapPropertiesWithRunOptimize(t *testing.T) {
corpus := getBitmapCorpus()
invariants := getInvariants()
for _, gen := range corpus {
for _, inv := range invariants {
t.Run(fmt.Sprintf("%s/%s_optimized", gen.name, inv.name), func(t *testing.T) {
b := gen.gen()
b.RunOptimize()
inv.test(t, b)
})
}
}
}
// bitmapGenerator is a function that creates a test bitmap
type bitmapGenerator struct {
name string
gen func() *Bitmap
}
// invariant is a property that should hold for all bitmaps
type invariant struct {
name string
test func(t *testing.T, b *Bitmap)
}
// getInvariants returns all property invariants to test
func getInvariants() []invariant {
return []invariant{
{name: "doubleflip", test: doubleFlipInvariant},
{name: "iteratorbits", test: iteratorBitsInvariant},
{name: "unsetiteratorbits", test: unsetIteratorBitsInvariant},
}
}
// doubleFlipInvariant checks that flip(flip(b)) == b
func doubleFlipInvariant(t *testing.T, b *Bitmap) {
original := b.Clone()
// Find the range to flip (slightly larger than the bitmap extent)
var maxVal uint64
if b.IsEmpty() {
maxVal = 1000
} else {
maxVal = uint64(b.Maximum()) + 1000
}
// Flip twice
b.Flip(0, maxVal)
b.Flip(0, maxVal)
// Should be equal to original
if !original.Equals(b) {
t.Errorf("double flip should restore original bitmap, original card=%d, result card=%d",
original.GetCardinality(), b.GetCardinality())
}
}
// iteratorBitsInvariant checks that creating a bitmap from iterator bits gives the same bitmap
func iteratorBitsInvariant(t *testing.T, b *Bitmap) {
original := b.Clone()
// Create new bitmap from iterator
result := NewBitmap()
iter := original.Iterator()
for iter.HasNext() {
result.Add(iter.Next())
}
// Should be equal to original
if !original.Equals(result) {
t.Errorf("bitmap reconstructed from iterator should equal original, original card=%d, result card=%d",
original.GetCardinality(), result.GetCardinality())
}
}
// unsetIteratorBitsInvariant checks that creating a bitmap from unset iterator, then flipping, gives the same bitmap
func unsetIteratorBitsInvariant(t *testing.T, b *Bitmap) {
original := b.Clone()
numUnset := 0x100000000 - b.GetCardinality()
if numUnset > 1000000 {
t.Skip("too many iterations")
}
// Create bitmap from unset bits
result := NewBitmap()
iter := original.UnsetIterator(0, 0x100000000)
i := 0
for iter.HasNext() {
i++
result.Add(iter.Next())
}
// Flip the result in the same range
result.Flip(0, 0x100000000)
// Should be equal to original
if !original.Equals(result) {
t.Errorf("bitmap reconstructed from unset iterator + flip should equal original, original card=%d, result card=%d",
original.GetCardinality(), result.GetCardinality())
}
}
// getBitmapCorpus returns a diverse set of bitmaps for property testing
func getBitmapCorpus() []bitmapGenerator {
return []bitmapGenerator{
{
name: "empty",
gen: func() *Bitmap {
return NewBitmap()
},
},
{
name: "single_bit",
gen: func() *Bitmap {
b := NewBitmap()
b.Add(42)
return b
},
},
{
name: "sparse_small",
gen: func() *Bitmap {
b := NewBitmap()
for i := 0; i < 100; i++ {
b.Add(uint32(i * 1000))
}
return b
},
},
{
name: "sparse_random",
gen: func() *Bitmap {
b := NewBitmap()
r := rand.New(rand.NewSource(12345))
domain := 100000000
count := 10000
for j := 0; j < count; j++ {
v := uint32(r.Intn(domain))
b.Add(v)
}
return b
},
},
{
name: "dense_small",
gen: func() *Bitmap {
b := NewBitmap()
for i := 0; i < 10000; i++ {
b.Add(uint32(i))
}
return b
},
},
{
name: "dense_range",
gen: func() *Bitmap {
b := NewBitmap()
b.AddRange(0, 100000)
return b
},
},
{
name: "sequential_ranges",
gen: func() *Bitmap {
b := NewBitmap()
b.AddRange(0, 1000)
b.AddRange(10000, 11000)
b.AddRange(100000, 101000)
return b
},
},
{
name: "mixed_containers",
gen: func() *Bitmap {
b := NewBitmap()
// Sparse in first container
for i := 0; i < 100; i++ {
b.Add(uint32(i * 100))
}
// Dense in second container
for i := 0; i < 60000; i++ {
b.Add(uint32(65536 + i))
}
// Sparse in third container
for i := 0; i < 50; i++ {
b.Add(uint32(131072 + i*1000))
}
return b
},
},
{
name: "alternating_bits",
gen: func() *Bitmap {
b := NewBitmap()
for i := 0; i < 100000; i += 2 {
b.Add(uint32(i))
}
return b
},
},
{
name: "high_values",
gen: func() *Bitmap {
b := NewBitmap()
r := rand.New(rand.NewSource(54321))
for i := 0; i < 1000; i++ {
v := uint32(r.Intn(0x70000000) + 0x7fffffff)
b.Add(v)
}
return b
},
},
{
name: "iterator_benchmark_sparse",
gen: func() *Bitmap {
// Based on BenchmarkIteratorAlloc
b := NewBitmap()
r := rand.New(rand.NewSource(0))
sz := 1000000
initsize := 50000
for i := 0; i < initsize; i++ {
b.Add(uint32(r.Intn(sz)))
}
return b
},
},
{
name: "iterator_benchmark_dense",
gen: func() *Bitmap {
// Based on BenchmarkNexts
b := NewBitmap()
for i := 0; i < 200000; i++ {
b.Add(uint32(i))
}
return b
},
},
{
name: "iterator_benchmark_rle",
gen: func() *Bitmap {
// Based on BenchmarkNextsRLE
b := NewBitmap()
b.AddRange(0, 1000000)
b.RunOptimize()
return b
},
},
{
name: "gaps_and_runs",
gen: func() *Bitmap {
b := NewBitmap()
for i := 0; i < 10; i++ {
start := uint64(i * 100000)
b.AddRange(start, start+10000)
}
return b
},
},
}
}