1package roaring
2
3import (
4	"log"
5	"testing"
6
7	. "github.com/smartystreets/goconvey/convey"
8)
9
10func makeContainer(ss []uint16) container {
11	c := newArrayContainer()
12	for _, s := range ss {
13		c.iadd(s)
14	}
15	return c
16}
17
18func checkContent(c container, s []uint16) bool {
19	si := c.getShortIterator()
20	ctr := 0
21	fail := false
22	for si.hasNext() {
23		if ctr == len(s) {
24			log.Println("HERE")
25			fail = true
26			break
27		}
28		i := si.next()
29		if i != s[ctr] {
30
31			log.Println("THERE", i, s[ctr])
32			fail = true
33			break
34		}
35		ctr++
36	}
37	if ctr != len(s) {
38		log.Println("LAST")
39		fail = true
40	}
41	if fail {
42		log.Println("fail, found ")
43		si = c.getShortIterator()
44		z := 0
45		for si.hasNext() {
46			si.next()
47			z++
48		}
49		log.Println(z, len(s))
50	}
51
52	return !fail
53}
54
55func TestContainerReverseIterator(t *testing.T) {
56	Convey("ArrayReverseIterator", t, func() {
57		content := []uint16{1, 3, 5, 7, 9}
58		c := makeContainer(content)
59		si := c.getReverseIterator()
60		i := 4
61		for si.hasNext() {
62			So(si.next(), ShouldEqual, content[i])
63			i--
64		}
65		So(i, ShouldEqual, -1)
66	})
67}
68
69func TestRoaringContainer(t *testing.T) {
70	Convey("countTrailingZeros", t, func() {
71		x := uint64(0)
72		o := countTrailingZeros(x)
73		So(o, ShouldEqual, 64)
74		x = 1 << 3
75		o = countTrailingZeros(x)
76		So(o, ShouldEqual, 3)
77	})
78	Convey("ArrayShortIterator", t, func() {
79		content := []uint16{1, 3, 5, 7, 9}
80		c := makeContainer(content)
81		si := c.getShortIterator()
82		i := 0
83		for si.hasNext() {
84			si.next()
85			i++
86		}
87
88		So(i, ShouldEqual, 5)
89	})
90
91	Convey("BinarySearch", t, func() {
92		content := []uint16{1, 3, 5, 7, 9}
93		res := binarySearch(content, 5)
94		So(res, ShouldEqual, 2)
95		res = binarySearch(content, 4)
96		So(res, ShouldBeLessThan, 0)
97	})
98	Convey("bitmapcontainer", t, func() {
99		content := []uint16{1, 3, 5, 7, 9}
100		a := newArrayContainer()
101		b := newBitmapContainer()
102		for _, v := range content {
103			a.iadd(v)
104			b.iadd(v)
105		}
106		c := a.toBitmapContainer()
107
108		So(a.getCardinality(), ShouldEqual, b.getCardinality())
109		So(c.getCardinality(), ShouldEqual, b.getCardinality())
110
111	})
112	Convey("inottest0", t, func() {
113		content := []uint16{9}
114		c := makeContainer(content)
115		c = c.inot(0, 11)
116		si := c.getShortIterator()
117		i := 0
118		for si.hasNext() {
119			si.next()
120			i++
121		}
122		So(i, ShouldEqual, 10)
123	})
124
125	Convey("inotTest1", t, func() {
126		// Array container, range is complete
127		content := []uint16{1, 3, 5, 7, 9}
128		//content := []uint16{1}
129		edge := 1 << 13
130		c := makeContainer(content)
131		c = c.inot(0, edge+1)
132		size := edge - len(content)
133		s := make([]uint16, size+1)
134		pos := 0
135		for i := uint16(0); i < uint16(edge+1); i++ {
136			if binarySearch(content, i) < 0 {
137				s[pos] = i
138				pos++
139			}
140		}
141		So(checkContent(c, s), ShouldEqual, true)
142	})
143
144}
145