forked from RoaringBitmap/roaring
-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_roaring_test.go
122 lines (105 loc) · 2.74 KB
/
example_roaring_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
package roaring
import (
"bytes"
"fmt"
"testing"
)
// Example_roaring demonstrates how to use the roaring library.
func TestExample_roaring060(t *testing.T) {
// example inspired by https://github.com/fzandona/goroar
fmt.Println("==roaring==")
rb1 := BitmapOf(1, 2, 3, 4, 5, 100, 1000)
fmt.Println(rb1.String())
rb2 := BitmapOf(3, 4, 1000)
fmt.Println(rb2.String())
rb3 := New()
fmt.Println(rb3.String())
fmt.Println("Cardinality: ", rb1.GetCardinality())
if rb1.GetCardinality() != 7 {
t.Errorf("Bad cardinality: %v", rb1.GetCardinality())
}
fmt.Println("Contains 3? ", rb1.Contains(3))
if !rb1.Contains(3) {
t.Errorf("Should contain 3.")
}
rb1.And(rb2)
rb3.Add(1)
rb3.Add(5)
rb3.Or(rb1)
// prints 1, 3, 4, 5, 1000
i := rb3.Iterator()
for i.HasNext() {
fmt.Println(i.Next())
}
fmt.Println()
// next we include an example of serialization
buf := new(bytes.Buffer)
size, err := rb1.WriteTo(buf)
if err != nil {
fmt.Println("Failed writing")
t.Errorf("Failed writing")
} else {
fmt.Println("Wrote ", size, " bytes")
}
newrb := New()
_, err = newrb.ReadFrom(buf)
if err != nil {
fmt.Println("Failed reading")
t.Errorf("Failed reading")
}
if !rb1.Equals(newrb) {
fmt.Println("I did not get back to original bitmap?")
t.Errorf("Bad serialization")
} else {
fmt.Println("I wrote the content to a byte stream and read it back.")
}
}
// Example_roaring demonstrates how to use the roaring library with run containers.
func TestExample2_roaring061(t *testing.T) {
r1 := New()
for i := uint32(100); i < 1000; i++ {
r1.Add(i)
}
if !r1.Contains(500) {
t.Errorf("should contain 500")
}
rb2 := r1.Clone()
// compute how many bits there are:
cardinality := r1.GetCardinality()
// if your bitmaps have long runs, you can compress them by calling
// run_optimize
size := r1.GetSizeInBytes()
r1.RunOptimize()
if cardinality != r1.GetCardinality() {
t.Errorf("RunOptimize should not change cardinality.")
}
compactSize := r1.GetSizeInBytes()
if compactSize >= size {
t.Errorf("Run optimized size should be smaller.")
}
if !r1.Equals(rb2) {
t.Errorf("RunOptimize should not affect equality.")
}
fmt.Print("size before run optimize: ", size, " bytes, and after: ", compactSize, " bytes.\n")
rb3 := New()
rb3.AddRange(1, 10000000)
r1.Or(rb3)
if !r1.Equals(rb3) {
t.Errorf("union with large run should give back contained set")
}
rb1 := r1.Clone()
rb1.AndNot(rb3)
if !rb1.IsEmpty() {
t.Errorf("And not with large should clear...")
}
for i := uint32(0); i < 10000; i += 3 {
rb1.Add(i)
}
rb1.AndNot(rb3)
rb1card := rb1.GetCardinality()
if rb1card != 1 {
//rb1.RunOptimize()
//fmt.Printf("\n rb1 = %s\n", rb1)
t.Errorf("Only the value 0 should survive the andNot; rb1card = %v", rb1card)
}
}