forked from RoaringBitmap/roaring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
container_test.go
144 lines (130 loc) · 2.65 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
package roaring
import (
"log"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
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 TestContainerReverseIterator(t *testing.T) {
Convey("ArrayReverseIterator", t, func() {
content := []uint16{1, 3, 5, 7, 9}
c := makeContainer(content)
si := c.getReverseIterator()
i := 4
for si.hasNext() {
So(si.next(), ShouldEqual, content[i])
i--
}
So(i, ShouldEqual, -1)
})
}
func TestRoaringContainer(t *testing.T) {
Convey("countTrailingZeros", t, func() {
x := uint64(0)
o := countTrailingZeros(x)
So(o, ShouldEqual, 64)
x = 1 << 3
o = countTrailingZeros(x)
So(o, ShouldEqual, 3)
})
Convey("ArrayShortIterator", t, func() {
content := []uint16{1, 3, 5, 7, 9}
c := makeContainer(content)
si := c.getShortIterator()
i := 0
for si.hasNext() {
si.next()
i++
}
So(i, ShouldEqual, 5)
})
Convey("BinarySearch", t, func() {
content := []uint16{1, 3, 5, 7, 9}
res := binarySearch(content, 5)
So(res, ShouldEqual, 2)
res = binarySearch(content, 4)
So(res, ShouldBeLessThan, 0)
})
Convey("bitmapcontainer", t, func() {
content := []uint16{1, 3, 5, 7, 9}
a := newArrayContainer()
b := newBitmapContainer()
for _, v := range content {
a.iadd(v)
b.iadd(v)
}
c := a.toBitmapContainer()
So(a.getCardinality(), ShouldEqual, b.getCardinality())
So(c.getCardinality(), ShouldEqual, b.getCardinality())
})
Convey("inottest0", t, func() {
content := []uint16{9}
c := makeContainer(content)
c = c.inot(0, 11)
si := c.getShortIterator()
i := 0
for si.hasNext() {
si.next()
i++
}
So(i, ShouldEqual, 10)
})
Convey("inotTest1", t, func() {
// 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++
}
}
So(checkContent(c, s), ShouldEqual, true)
})
}