This repository has been archived by the owner on Jul 29, 2021. It is now read-only.
forked from RoaringBitmap/roaring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_test.go
295 lines (244 loc) · 5.51 KB
/
container_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
package roaring
import (
"fmt"
"github.com/stretchr/testify/assert"
"log"
"testing"
)
func makeContainer(ss []uint16) container {
c := newArrayContainer()
for _, s := range ss {
c.iadd(s)
}
return c
}
func checkContent(c container, s []uint16) bool {
si := c.getShortIterator()
ctr := 0
fail := false
for si.hasNext() {
if ctr == len(s) {
log.Println("HERE")
fail = true
break
}
i := si.next()
if i != s[ctr] {
log.Println("THERE", i, s[ctr])
fail = true
break
}
ctr++
}
if ctr != len(s) {
log.Println("LAST")
fail = true
}
if fail {
log.Println("fail, found ")
si = c.getShortIterator()
z := 0
for si.hasNext() {
si.next()
z++
}
log.Println(z, len(s))
}
return !fail
}
func testContainerIteratorPeekNext(t *testing.T, c container) {
testSize := 5000
for i := 0; i < testSize; i++ {
c.iadd(uint16(i))
}
i := c.getShortIterator()
assert.True(t, i.hasNext())
for i.hasNext() {
assert.Equal(t, i.peekNext(), i.next())
testSize--
}
assert.Equal(t, 0, testSize)
}
func testContainerIteratorAdvance(t *testing.T, con container) {
values := []uint16{1, 2, 15, 16, 31, 32, 33, 9999}
for _, v := range values {
con.iadd(v)
}
cases := []struct {
minval uint16
expected uint16
}{
{0, 1},
{1, 1},
{2, 2},
{3, 15},
{15, 15},
{30, 31},
{31, 31},
{33, 33},
{34, 9999},
{9998, 9999},
{9999, 9999},
}
t.Run("advance by using a new short iterator", func(t *testing.T) {
for _, c := range cases {
i := con.getShortIterator()
i.advanceIfNeeded(c.minval)
assert.True(t, i.hasNext())
assert.Equal(t, c.expected, i.peekNext())
}
})
t.Run("advance by using the same short iterator", func(t *testing.T) {
i := con.getShortIterator()
for _, c := range cases {
i.advanceIfNeeded(c.minval)
assert.True(t, i.hasNext())
assert.Equal(t, c.expected, i.peekNext())
}
})
t.Run("advance out of a container value", func(t *testing.T) {
i := con.getShortIterator()
i.advanceIfNeeded(33)
assert.True(t, i.hasNext())
assert.EqualValues(t, 33, i.peekNext())
i.advanceIfNeeded(MaxUint16 - 1)
assert.False(t, i.hasNext())
i.advanceIfNeeded(MaxUint16)
assert.False(t, i.hasNext())
})
t.Run("advance on a value that is less than the pointed value", func(t *testing.T) {
i := con.getShortIterator()
i.advanceIfNeeded(29)
assert.True(t, i.hasNext())
assert.EqualValues(t, 31, i.peekNext())
i.advanceIfNeeded(13)
assert.True(t, i.hasNext())
assert.EqualValues(t, 31, i.peekNext())
})
}
func benchmarkContainerIteratorAdvance(b *testing.B, con container) {
for _, initsize := range []int{1, 650, 6500, MaxUint16} {
for i := 0; i < initsize; i++ {
con.iadd(uint16(i))
}
b.Run(fmt.Sprintf("init size %d shortIterator advance", initsize), func(b *testing.B) {
b.StartTimer()
diff := uint16(0)
for n := 0; n < b.N; n++ {
val := uint16(n % initsize)
i := con.getShortIterator()
i.advanceIfNeeded(val)
diff += i.peekNext() - val
}
b.StopTimer()
if diff != 0 {
b.Fatalf("Expected diff 0, got %d", diff)
}
})
}
}
func benchmarkContainerIteratorNext(b *testing.B, con container) {
for _, initsize := range []int{1, 650, 6500, MaxUint16} {
for i := 0; i < initsize; i++ {
con.iadd(uint16(i))
}
b.Run(fmt.Sprintf("init size %d shortIterator next", initsize), func(b *testing.B) {
b.StartTimer()
diff := 0
for n := 0; n < b.N; n++ {
i := con.getShortIterator()
j := 0
for i.hasNext() {
i.next()
j++
}
diff += j - initsize
}
b.StopTimer()
if diff != 0 {
b.Fatalf("Expected diff 0, got %d", diff)
}
})
}
}
func TestContainerReverseIterator(t *testing.T) {
content := []uint16{1, 3, 5, 7, 9}
c := makeContainer(content)
si := c.getReverseIterator()
i := 4
for si.hasNext() {
assert.Equal(t, content[i], si.next())
i--
}
assert.Equal(t, -1, i)
}
func TestRoaringContainer(t *testing.T) {
t.Run("countTrailingZeros", func(t *testing.T) {
x := uint64(0)
o := countTrailingZeros(x)
assert.Equal(t, 64, o)
x = 1 << 3
o = countTrailingZeros(x)
assert.Equal(t, 3, o)
})
t.Run("ArrayShortIterator", func(t *testing.T) {
content := []uint16{1, 3, 5, 7, 9}
c := makeContainer(content)
si := c.getShortIterator()
i := 0
for si.hasNext() {
si.next()
i++
}
assert.Equal(t, 5, i)
})
t.Run("BinarySearch", func(t *testing.T) {
content := []uint16{1, 3, 5, 7, 9}
res := binarySearch(content, 5)
assert.Equal(t, 2, res)
res = binarySearch(content, 4)
assert.Less(t, res, 0)
})
t.Run("bitmapcontainer", func(t *testing.T) {
content := []uint16{1, 3, 5, 7, 9}
a := newArrayContainer()
b := newBitmapContainer()
for _, v := range content {
a.iadd(v)
b.iadd(v)
}
c := a.toBitmapContainer()
assert.Equal(t, b.getCardinality(), a.getCardinality())
assert.Equal(t, b.getCardinality(), c.getCardinality())
})
t.Run("inottest0", func(t *testing.T) {
content := []uint16{9}
c := makeContainer(content)
c = c.inot(0, 11)
si := c.getShortIterator()
i := 0
for si.hasNext() {
si.next()
i++
}
assert.Equal(t, 10, i)
})
t.Run("inotTest1", func(t *testing.T) {
// Array container, range is complete
content := []uint16{1, 3, 5, 7, 9}
//content := []uint16{1}
edge := 1 << 13
c := makeContainer(content)
c = c.inot(0, edge+1)
size := edge - len(content)
s := make([]uint16, size+1)
pos := 0
for i := uint16(0); i < uint16(edge+1); i++ {
if binarySearch(content, i) < 0 {
s[pos] = i
pos++
}
}
assert.True(t, checkContent(c, s))
})
}