MCPcopy
hub / github.com/RoaringBitmap/roaring / TestExample_roaring060

Function TestExample_roaring060

example_roaring_test.go:10–74  ·  view source on GitHub ↗

Example_roaring demonstrates how to use the roaring library.

(t *testing.T)

Source from the content-addressed store, hash-verified

8
9// Example_roaring demonstrates how to use the roaring library.
10func TestExample_roaring060(t *testing.T) {
11 // example inspired by https://github.com/fzandona/goroar
12 fmt.Println("==roaring==")
13 rb1 := BitmapOf(1, 2, 3, 4, 5, 100, 1000)
14 fmt.Println(rb1.String())
15
16 rb2 := BitmapOf(3, 4, 1000)
17 fmt.Println(rb2.String())
18
19 rb3 := New()
20 fmt.Println(rb3.String())
21
22 fmt.Println("Cardinality: ", rb1.GetCardinality())
23 if rb1.GetCardinality() != 7 {
24 t.Errorf("Bad cardinality: %v", rb1.GetCardinality())
25 }
26
27 fmt.Println("Contains 3? ", rb1.Contains(3))
28 if !rb1.Contains(3) {
29 t.Errorf("Should contain 3.")
30 }
31
32 rb1.And(rb2)
33
34 rb3.Add(1)
35 rb3.Add(5)
36
37 rb3.Or(rb1)
38
39 // prints 1, 3, 4, 5, 1000
40 i := rb3.Iterator()
41 for i.HasNext() {
42 fmt.Println(i.Next())
43 }
44 fmt.Println()
45
46 // next we include an example of serialization
47 buf := new(bytes.Buffer)
48 size, err := rb1.WriteTo(buf)
49 if err != nil {
50 fmt.Println("Failed writing")
51 t.Errorf("Failed writing")
52
53 } else {
54 fmt.Println("Wrote ", size, " bytes")
55 }
56 newrb := New()
57 _, err = newrb.ReadFrom(buf)
58 if err != nil {
59 fmt.Println("Failed reading")
60 t.Errorf("Failed reading")
61 }
62 // if buf is an untrusted source, you should validate the result
63 // (this adds a bit of complexity but it is necessary for security)
64 if newrb.Validate() != nil {
65 fmt.Println("Failed validation")
66 }
67 if !rb1.Equals(newrb) {

Callers

nothing calls this directly

Calls 15

BitmapOfFunction · 0.70
NewFunction · 0.70
StringMethod · 0.65
HasNextMethod · 0.65
NextMethod · 0.65
GetCardinalityMethod · 0.45
ContainsMethod · 0.45
AndMethod · 0.45
AddMethod · 0.45
OrMethod · 0.45
IteratorMethod · 0.45
WriteToMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…