forked from RoaringBitmap/roaring
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setutil_test.go
138 lines (107 loc) · 3.38 KB
/
setutil_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
package roaring
// to run just these tests: go test -run TestSetUtil*
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestSetUtilDifference(t *testing.T) {
data1 := []uint16{0, 1, 2, 3, 4, 9}
data2 := []uint16{2, 3, 4, 5, 8, 9, 11}
result := make([]uint16, 0, len(data1)+len(data2))
expectedresult := []uint16{0, 1}
nl := difference(data1, data2, result)
result = result[:nl]
assert.Equal(t, expectedresult, result)
expectedresult = []uint16{5, 8, 11}
nl = difference(data2, data1, result)
result = result[:nl]
assert.Equal(t, expectedresult, result)
// empty set2
data2 = []uint16{}
expectedresult = []uint16{0, 1, 2, 3, 4, 9}
nl = difference(data1, data2, result)
result = result[:nl]
assert.Equal(t, expectedresult, result)
// empty set 1
data1 = []uint16{}
data2 = []uint16{2, 3, 4, 5, 8, 9, 11}
expectedresult = []uint16{}
nl = difference(data1, data2, result)
result = result[:nl]
assert.Equal(t, expectedresult, result)
}
func TestSetUtilUnion(t *testing.T) {
data1 := []uint16{0, 1, 2, 3, 4, 9}
data2 := []uint16{2, 3, 4, 5, 8, 9, 11}
result := make([]uint16, 0, len(data1)+len(data2))
expectedresult := []uint16{0, 1, 2, 3, 4, 5, 8, 9, 11}
nl := union2by2(data1, data2, result)
result = result[:nl]
assert.Equal(t, expectedresult, result)
nl = union2by2(data2, data1, result)
result = result[:nl]
assert.Equal(t, expectedresult, result)
}
func TestSetUtilExclusiveUnion(t *testing.T) {
data1 := []uint16{0, 1, 2, 3, 4, 9}
data2 := []uint16{2, 3, 4, 5, 8, 9, 11}
result := make([]uint16, 0, len(data1)+len(data2))
expectedresult := []uint16{0, 1, 5, 8, 11}
nl := exclusiveUnion2by2(data1, data2, result)
result = result[:nl]
assert.Equal(t, expectedresult, result)
nl = exclusiveUnion2by2(data2, data1, result)
result = result[:nl]
assert.Equal(t, expectedresult, result)
}
func TestSetUtilIntersection(t *testing.T) {
data1 := []uint16{0, 1, 2, 3, 4, 9}
data2 := []uint16{2, 3, 4, 5, 8, 9, 11}
result := make([]uint16, 0, len(data1)+len(data2))
expectedresult := []uint16{2, 3, 4, 9}
nl := intersection2by2(data1, data2, result)
result = result[:nl]
result = result[:len(expectedresult)]
assert.Equal(t, expectedresult, result)
nl = intersection2by2(data2, data1, result)
result = result[:nl]
assert.Equal(t, expectedresult, result)
data1 = []uint16{4}
data2 = make([]uint16, 10000)
for i := range data2 {
data2[i] = uint16(i)
}
result = make([]uint16, 0, len(data1)+len(data2))
expectedresult = data1
nl = intersection2by2(data1, data2, result)
result = result[:nl]
assert.Equal(t, expectedresult, result)
nl = intersection2by2(data2, data1, result)
result = result[:nl]
assert.Equal(t, expectedresult, result)
}
func TestSetUtilIntersection2(t *testing.T) {
data1 := []uint16{0, 2, 4, 6, 8, 10, 12, 14, 16, 18}
data2 := []uint16{0, 3, 6, 9, 12, 15, 18}
result := make([]uint16, 0, len(data1)+len(data2))
expectedresult := []uint16{0, 6, 12, 18}
nl := intersection2by2(data1, data2, result)
result = result[:nl]
result = result[:len(expectedresult)]
assert.Equal(t, expectedresult, result)
}
func TestSetUtilBinarySearch(t *testing.T) {
data := make([]uint16, 256)
for i := range data {
data[i] = uint16(2 * i)
}
for i := 0; i < 2*len(data); i++ {
key := uint16(i)
loc := binarySearch(data, key)
if (key & 1) == 0 {
assert.Equal(t, int(key)/2, loc)
} else {
assert.Equal(t, -int(key)/2-2, loc)
}
}
}