-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitvec_test.go
211 lines (200 loc) · 5.46 KB
/
bitvec_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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package bitvec
import (
"fmt"
"math/rand"
"testing"
)
var GoodSets = []uint64{0, 1, 2, 3, 4, 5, 10, 11, 20, 50, 89, 98, 99}
var UnSets = []uint64{6, 7, 9, 96, 97}
var BadSets = []uint64{100, 101, 200, 1000}
var setSizes = []int{13, 256, 65536, 16777216}
func TestBV(t *testing.T) {
bv := New(100)
bv2 := New(100)
abv := NewAtomic(100)
abv2 := NewAtomic(100)
t.Run("valid indices", func(t *testing.T) {
for _, k := range GoodSets {
if !bv.TrySet(k) {
t.Errorf("bv could not set good index %d", k)
}
if !abv.TrySet(k) {
t.Errorf("abv could not set good index %d", k)
}
bv2.Set(k)
abv2.Set(k)
if x, _ := bv.Get(k); !x {
t.Errorf("bv bit %d was supposed to be set but wasn't", k)
}
if x, _ := abv.Get(k); !x {
t.Errorf("abv bit %d was supposed to be set but wasn't", k)
}
if x, _ := bv2.Get(k); !x {
t.Errorf("bv2 bit %d was supposed to be set but wasn't", k)
}
if x, _ := abv2.Get(k); !x {
t.Errorf("abv2 bit %d was supposed to be set but wasn't", k)
}
bv.Clear(k)
if x, _ := bv.Get(k); x {
t.Errorf("bv bit %d was supposed to be cleared but wasn't", k)
}
abv.Clear(k)
if x, _ := abv.Get(k); x {
t.Errorf("abv bit %d was supposed to be cleared but wasn't", k)
}
}
})
t.Run("invalid indices", func(t *testing.T) {
for _, k := range BadSets {
if bv.TrySet(k) {
t.Errorf("bv successfully set bad index %d ", k)
}
if abv.TrySet(k) {
t.Errorf("abv successfully set bad index %d ", k)
}
if bv2.Set(k) == nil {
t.Errorf("bv2 successfully set bad index %d ", k)
}
if abv2.Set(k) == nil {
t.Errorf("abv2 successfully set bad index %d ", k)
}
if _, err := bv.Get(k); err == nil {
t.Errorf("bv error should be thrown for out of bounds access")
}
if _, err := abv.Get(k); err == nil {
t.Errorf("abv error should be thrown for out of bounds access")
}
if bv.Clear(k) == nil {
t.Errorf("bv error should be thrown for out of bounds access")
}
}
})
t.Run("unset indices", func(t *testing.T) {
for _, k := range UnSets {
if x, _ := bv.Get(k); x {
t.Errorf("bv bit %d was not supposed to be set but was", k)
}
if x, _ := abv.Get(k); x {
t.Errorf("abv bit %d was not supposed to be set but was", k)
}
}
})
}
var r = rand.New(rand.NewSource(99))
var sets = r.Perm(setSizes[len(setSizes)-1])
func BenchmarkSet(b *testing.B) {
for _, setSize := range setSizes {
b.Run(fmt.Sprintf("bitvec/Set size %d", setSize), func(b *testing.B) {
bv := New(uint64(setSize))
b.ResetTimer()
for i := 0; i < b.N; i++ {
bv.Set(uint64(sets[i%len(sets)] % setSize))
}
})
b.Run(fmt.Sprintf("abitvec/Set size %d", setSize), func(b *testing.B) {
abv := NewAtomic(uint64(setSize))
b.ResetTimer()
for i := 0; i < b.N; i++ {
abv.Set(uint64(sets[i%len(sets)] % setSize))
}
})
b.Run(fmt.Sprintf("slice/Set size %d", setSize), func(b *testing.B) {
slice := make([]bool, setSize)
b.ResetTimer()
for i := 0; i < b.N; i++ {
slice[sets[i%len(sets)]%setSize] = true
}
})
}
fmt.Println()
}
func BenchmarkTrySet(b *testing.B) {
for _, setSize := range setSizes {
b.Run(fmt.Sprintf("bitvec/TrySet size %d", setSize), func(b *testing.B) {
bv := New(uint64(setSize))
b.ResetTimer()
for i := 0; i < b.N; i++ {
bv.TrySet(uint64(sets[i%len(sets)] % setSize))
}
})
b.Run(fmt.Sprintf("abitvec/TrySet size %d", setSize), func(b *testing.B) {
abv := NewAtomic(uint64(setSize))
b.ResetTimer()
for i := 0; i < b.N; i++ {
abv.TrySet(uint64(sets[i%len(sets)] % setSize))
}
})
}
fmt.Println()
}
func BenchmarkGet(b *testing.B) {
for _, setSize := range setSizes {
b.Run(fmt.Sprintf("bitvec/Get size %d", setSize), func(b *testing.B) {
bv := New(uint64(setSize))
for n := 0; n < setSize && sets[n]%2 == 0; n++ {
bv.TrySet(uint64(n))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
bv.Get(uint64(sets[i%len(sets)] % setSize))
}
})
b.Run(fmt.Sprintf("abitvec/Get size %d", setSize), func(b *testing.B) {
abv := NewAtomic(uint64(setSize))
for n := 0; n < setSize && sets[n]%2 == 0; n++ {
abv.TrySet(uint64(n))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
abv.Get(uint64(sets[i%len(sets)] % setSize))
}
})
b.Run(fmt.Sprintf("slice/Get size %d", setSize), func(b *testing.B) {
slice := make([]bool, setSize)
for n := 0; n < setSize && sets[n]%2 == 0; n++ {
slice[n] = true
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = slice[sets[i%len(sets)]%setSize]
}
})
}
fmt.Println()
}
func BenchmarkClear(b *testing.B) {
for _, setSize := range setSizes {
b.Run(fmt.Sprintf("bitvec/Clear size %d", setSize), func(b *testing.B) {
bv := New(uint64(setSize))
for i := 0; i < b.N; i++ {
bv.Set(uint64(sets[i%len(sets)] % setSize))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
bv.Clear(uint64(sets[i%len(sets)] % setSize))
}
})
b.Run(fmt.Sprintf("abitvec/Clear size %d", setSize), func(b *testing.B) {
abv := NewAtomic(uint64(setSize))
for i := 0; i < b.N; i++ {
abv.Set(uint64(sets[i%len(sets)] % setSize))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
abv.Clear(uint64(sets[i%len(sets)] % setSize))
}
})
b.Run(fmt.Sprintf("slice/Clear size %d", setSize), func(b *testing.B) {
slice := make([]bool, setSize)
for i := 0; i < b.N; i++ {
slice[sets[i%len(sets)]%setSize] = true
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
slice[sets[i%len(sets)]%setSize] = false
}
})
}
fmt.Println()
}