forked from RoaringBitmap/gocroaring
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgocroaring_test.go
181 lines (166 loc) · 4.01 KB
/
gocroaring_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
package gocroaring
import (
"fmt"
"runtime"
"testing"
"math/rand"
)
func TestDisplayVersion(t *testing.T) {
fmt.Printf("CRoaring %v.%v.%v\n", CRoaringMajor, CRoaringMinor, CRoaringRevision)
}
func bToMb(b uint64) uint64 {
return b / 1024 / 1024
}
func PrintMemUsage() {
var m runtime.MemStats
runtime.ReadMemStats(&m)
// For info on each, see: https://golang.org/pkg/runtime/#MemStats
fmt.Printf("Alloc = %v MiB", bToMb(m.Alloc))
fmt.Printf("\tTotalAlloc = %v MiB", bToMb(m.TotalAlloc))
fmt.Printf("\tSys = %v MiB", bToMb(m.Sys))
fmt.Printf("\tNumGC = %v\n", m.NumGC)
}
// go test -run StressMemory
func TestStressMemory(t *testing.T) {
for i := 0; i < 10; i++ {
r0 := New()
var j uint32
for k := 0; k < 10000000; k++ {
j = uint32(rand.Intn(10000000))
r0.Add(j)
}
r0.RunOptimize() // improves compression
buf0 := make([]byte, r0.SerializedSizeInBytes())
r0.Write(buf0) // we omit error handling
PrintMemUsage()
}
}
// go test -run MemoryUsage
func TestMemoryUsage(t *testing.T) {
bitmap := New()
for i := 0; i < 1000000; i++ {
bitmap.Add(uint32(i) * 10)
}
sb := bitmap.SerializedSizeInBytes()
memoryAlloc := 8 * 1024 * 1024
howmany := (memoryAlloc + sb - 1) / sb
fmt.Println("size in kB of one bitmap ", sb/(1024), "; number of copies = ", howmany, "; total alloc: ", howmany*sb/(1024), "kB")
for i := 0; i < howmany; i++ {
y := bitmap.Clone()
_ = y
}
}
func TestSimpleCard(t *testing.T) {
bitmap := New()
for i := 100; i < 1000; i++ {
bitmap.Add(uint32(i))
}
c := bitmap.Cardinality()
fmt.Println("cardinality: ", c)
if c != 900 {
t.Error("Expected ", 900, ", got ", c)
}
bitmap.RunOptimize()
if c != 900 {
t.Error("Expected ", 900, ", got ", c)
}
}
func TestNewWithVals(t *testing.T) {
vals := []uint32{1, 2, 3, 6, 7, 8, 20, 44444}
rb := New(vals...)
for _, v := range vals {
if !rb.Contains(v) {
t.Errorf("expected %d from initialized values\n", v)
}
}
}
func TestAddMany(t *testing.T) {
rb1 := New()
sl := []uint32{1, 2, 3, 6, 7, 8, 20, 44444}
rb1.Add(sl...)
if int(rb1.Cardinality()) != len(sl) {
t.Errorf("cardinality: expected %d, got %d", rb1.Cardinality(), len(sl))
}
if rb1.Contains(5) {
t.Error("didn't expect to contain 5")
}
for _, v := range sl {
if !rb1.Contains(v) {
t.Errorf("expected to contain %d", v)
}
}
}
func TestFancier(t *testing.T) {
rb1 := New()
rb1.Add(1)
rb1.Add(2)
rb1.Add(3)
rb1.Add(4)
rb1.Add(5)
rb1.Add(100)
rb1.Add(1000)
rb1.RunOptimize()
rb2 := New()
rb2.Add(3)
rb2.Add(4)
rb2.Add(1000)
rb2.RunOptimize()
rb3 := New()
fmt.Println("Cardinality: ", rb1.Cardinality())
if rb1.Cardinality() != 7 {
t.Error("Bad card")
}
if !rb1.Contains(3) {
t.Error("should contain it")
}
rb1.And(rb2)
fmt.Println(rb1)
rb3.Add(5)
rb3.Or(rb1)
// prints 3, 4, 5, 1000
i := rb3.Iterator()
for i.HasNext() {
fmt.Println(i.Next())
}
fmt.Println()
fmt.Println(rb3.ToArray())
fmt.Println(rb3)
rb4 := FastOr(rb1, rb2, rb3)
fmt.Println(rb4)
// next we include an example of serialization
buf := make([]byte, rb1.SerializedSizeInBytes())
rb1.Write(buf) // we omit error handling
newrb, _ := Read(buf)
if rb1.Equals(newrb) {
fmt.Println("I wrote the content to a byte stream and read it back.")
} else {
t.Error("Bad read")
}
}
func TestString(t *testing.T) {
ans := New(1, 2, 3).String()
fmt.Println(ans)
if ans != "{1,2,3}" {
t.Errorf("bad string ")
}
}
func TestStats(t *testing.T) {
rb := New()
rb.Add(1, 2, 3, 4, 6, 7)
rb.Add(999991, 999992, 999993, 999994, 999996, 999997)
stats := rb.Stats()
if stats["cardinality"] != rb.Cardinality() {
t.Errorf("cardinality: expected %d got %d\n", rb.Cardinality(), stats["cardinality"])
}
if stats["n_containers"] != 2 {
t.Errorf("n_containers: expected %d got %d\n", 2, stats["n_containers"])
}
if stats["n_array_containers"] != 2 {
t.Errorf("n_array_containers: expected %d got %d\n", 2, stats["n_array_containers"])
}
for _, c := range []string{"n_run_containers", "n_bitmap_containers"} {
if stats[c] != 0 {
t.Errorf("%s: expected 0 got %d\n", c, stats[c])
}
}
}