forked from bits-and-blooms/bitset
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitset_test.go
488 lines (438 loc) · 10.4 KB
/
bitset_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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Copyright 2011 Will Fitzgerald. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file tests bit sets
package bitset
import (
"testing"
"rand"
"math"
)
func TestEmptyBitSet(t *testing.T) {
defer func() {
if r := recover(); r != nil {
t.Error("A zero-length bitset should be fine")
}
}()
b := New(0)
if b.Len() != 0 {
t.Errorf("Empty set should have capacity 0, not %d", b.Cap())
}
}
func TestBitSetNew(t *testing.T) {
v := New(16)
if v.Test(0) != false {
t.Errorf("Unable to make a bit set and read its 0th value.")
}
}
func TestBitSetHuge(t *testing.T) {
v := New(uint(math.MaxUint32))
if v.Test(0) != false {
t.Errorf("Unable to make a huge bit set and read its 0th value.")
}
}
func TestCap(t *testing.T) {
v := New(1000)
if v.Cap() != uint(math.MaxUint32) {
t.Errorf("Cap should be MaxUint32, but is %d.", v.Cap())
}
}
func TestLen(t *testing.T) {
v := New(1000)
if v.Len() != 1000 {
t.Errorf("Len should be 1000, but is %d.", v.Cap())
}
}
func TestBitSetIsClear(t *testing.T) {
v := New(1000)
for i := uint(0); i < 1000; i++ {
if v.Test(i) != false {
t.Errorf("Bit %d is set, and it shouldn't be.", i)
}
}
}
func TestExendOnBoundary(t *testing.T) {
v := New(32)
defer func() {
if r := recover(); r != nil {
t.Error("Border out of index error should not have caused a panic")
}
}()
v.Set(32)
}
func TestExpand (t *testing.T) {
v := New(0)
defer func() {
if r := recover(); r != nil {
t.Error("Expansion should not have caused a panic")
}
}()
for i := uint(0); i < 1000; i++ {
v.Set(i)
}
}
func TestBitSetAndGet(t *testing.T) {
v := New(1000)
v.Set(100)
if v.Test(100) != true {
t.Errorf("Bit %d is clear, and it shouldn't be.", 100)
}
}
func TestChain(t *testing.T) {
if New(1000).Set(100).Set(99).Clear(99).Test(100) != true {
t.Errorf("Bit %d is clear, and it shouldn't be.", 100)
}
}
func TestOutOfBoundsLong(t *testing.T) {
v := New(64)
defer func() {
if r := recover(); r != nil {
t.Error("Long distance out of index error should not have caused a panic")
}
}()
v.Set(1000)
}
func TestOutOfBoundsClose(t *testing.T) {
v := New(65)
defer func() {
if r := recover(); r != nil {
t.Error("Local out of index error should not have caused a panic")
}
}()
v.Set(66)
}
func TestCount(t *testing.T) {
tot := uint(64*4+11) // just some multi unit64 number
v := New(tot)
checkLast := true
for i := uint(0); i < tot; i++ {
sz := v.Count()
if sz != i {
t.Errorf("Count reported as %d, but it should be %d", sz, i)
checkLast = false
break
}
v.Set(i)
}
if checkLast {
sz := v.Count()
if sz != tot {
t.Errorf("After all bits set, size reported as %d, but it should be %d", sz, tot)
}
}
}
// test setting every 3rd bit, just in case something odd is happening
func TestCount2(t *testing.T) {
tot := uint(64*4+11) // just some multi unit64 number
v := New(tot)
for i := uint(0); i < tot; i+=3 {
sz := v.Count()
if sz != i/3 {
t.Errorf("Count reported as %d, but it should be %d", sz, i)
break
}
v.Set(i)
}
}
// nil tests
func TestNullTest(t *testing.T) {
var v *BitSet = nil
defer func() {
if r := recover(); r == nil {
t.Error("Checking bit of null reference should have caused a panic")
}
}()
v.Test(66)
}
func TestNullSet(t *testing.T) {
var v *BitSet = nil
defer func() {
if r := recover(); r == nil {
t.Error("Setting bit of null reference should have caused a panic")
}
}()
v.Set(66)
}
func TestNullClear(t *testing.T) {
var v *BitSet = nil
defer func() {
if r := recover(); r == nil {
t.Error("Clearning bit of null reference should have caused a panic")
}
}()
v.Clear(66)
}
func TestNullCount(t *testing.T) {
var v *BitSet = nil
defer func() {
if r := recover(); r != nil {
t.Error("Counting null reference should not have caused a panic")
}
}()
cnt := v.Count()
if cnt != 0 {
t.Errorf("Count reported as %d, but it should be 0", cnt)
}
}
func TestPanicDifferenceBNil(t *testing.T) {
var b *BitSet = nil
var compare = New(10)
defer func() {
if r := recover(); r == nil {
t.Error("Nil First should should have caused a panic")
}
}()
b.Difference(compare)
}
func TestPanicDifferenceCompareNil(t *testing.T) {
var compare *BitSet = nil
var b = New(10)
defer func() {
if r := recover(); r == nil {
t.Error("Nil Second should should have caused a panic")
}
}()
b.Difference(compare)
}
func TestPanicUnionBNil(t *testing.T) {
var b *BitSet = nil
var compare = New(10)
defer func() {
if r := recover(); r == nil {
t.Error("Nil First should should have caused a panic")
}
}()
b.Union(compare)
}
func TestPanicUnionCompareNil(t *testing.T) {
var compare *BitSet = nil
var b = New(10)
defer func() {
if r := recover(); r == nil {
t.Error("Nil Second should should have caused a panic")
}
}()
b.Union(compare)
}
func TestPanicIntersectionBNil(t *testing.T) {
var b *BitSet = nil
var compare = New(10)
defer func() {
if r := recover(); r == nil {
t.Error("Nil First should should have caused a panic")
}
}()
b.Intersection(compare)
}
func TestPanicIntersectionCompareNil(t *testing.T) {
var compare *BitSet = nil
var b = New(10)
defer func() {
if r := recover(); r == nil {
t.Error("Nil Second should should have caused a panic")
}
}()
b.Intersection(compare)
}
func TestPanicSymmetricDifferenceBNil(t *testing.T) {
var b *BitSet = nil
var compare = New(10)
defer func() {
if r := recover(); r == nil {
t.Error("Nil First should should have caused a panic")
}
}()
b.SymmetricDifference(compare)
}
func TestPanicSymmetricDifferenceCompareNil(t *testing.T) {
var compare *BitSet = nil
var b = New(10)
defer func() {
if r := recover(); r == nil {
t.Error("Nil Second should should have caused a panic")
}
}()
b.SymmetricDifference(compare)
}
func TestPanicComplementBNil(t *testing.T) {
var b *BitSet = nil
defer func() {
if r := recover(); r == nil {
t.Error("Nil should should have caused a panic")
}
}()
b.Complement()
}
func TestPanicAnytBNil(t *testing.T) {
var b *BitSet = nil
defer func() {
if r := recover(); r == nil {
t.Error("Nil should should have caused a panic")
}
}()
b.Any()
}
func TestPanicNonetBNil(t *testing.T) {
var b *BitSet = nil
defer func() {
if r := recover(); r == nil {
t.Error("Nil should should have caused a panic")
}
}()
b.None()
}
func TestPanicAlltBNil(t *testing.T) {
var b *BitSet = nil
defer func() {
if r := recover(); r == nil {
t.Error("Nil should should have caused a panic")
}
}()
b.All()
}
func TestEqual(t *testing.T) {
a := New(100)
b := New(99)
c := New(100)
if a.Equal(b) {
t.Error("Sets of different sizes should be not be equal")
}
if !a.Equal(c){
t.Error("Two empty sets of the same size should be equal")
}
a.Set(99)
c.Set(0)
if a.Equal(c){
t.Error("Two sets with differences should not be equal")
}
c.Set(99)
a.Set(0)
if !a.Equal(c){
t.Error("Two sets with the same bits set should be equal")
}
}
func TestUnion(t *testing.T) {
a := New(100)
b := New(200)
for i := uint(1); i < 100; i += 2 {
a.Set(i)
b.Set(i-1)
}
for i := uint(100); i < 200; i++ {
b.Set(i)
}
c := a.Union(b)
d := b.Union(a)
if c.Count()!=200 {
t.Errorf("Union should have 200 bits set, but had %d", c.Count())
}
if !c.Equal(d) {
t.Errorf("Union should be symmetric")
}
}
func TestIntersection(t *testing.T) {
a := New(100)
b := New(200)
for i := uint(1); i < 100; i += 2 {
a.Set(i)
b.Set(i-1).Set(i)
}
for i := uint(100); i < 200; i++ {
b.Set(i)
}
c := a.Intersection(b)
d := b.Intersection(a)
if c.Count()!=50 {
t.Errorf("Intersection should have 50 bits set, but had %d", c.Count())
}
if !c.Equal(d) {
t.Errorf("Intersection should be symmetric")
}
}
func TestDifference(t *testing.T) {
a := New(100)
b := New(200)
for i := uint(1); i < 100; i += 2 {
a.Set(i)
b.Set(i-1)
}
for i := uint(100); i < 200; i++ {
b.Set(i)
}
c := a.Difference(b)
d := b.Difference(a)
if c.Count()!=50 {
t.Errorf("a-b Difference should have 50 bits set, but had %d", c.Count())
}
if d.Count()!=150 {
t.Errorf("b-a Difference should have 150 bits set, but had %d", c.Count())
}
if c.Equal(d) {
t.Errorf("Difference, here, should not be symmetric")
}
}
func TestSymmetricDifference(t *testing.T) {
a := New(100)
b := New(200)
for i := uint(1); i < 100; i += 2 {
a.Set(i) // 01010101010 ... 0000000
b.Set(i-1).Set(i) // 11111111111111111000000
}
for i := uint(100); i < 200; i++ {
b.Set(i)
}
c := a.SymmetricDifference(b)
d := b.SymmetricDifference(a)
if c.Count()!=150 {
t.Errorf("a^b Difference should have 150 bits set, but had %d", c.Count())
}
if d.Count()!=150 {
t.Errorf("b^a Difference should have 150 bits set, but had %d", c.Count())
}
if !c.Equal(d) {
t.Errorf("SymmetricDifference should be symmetric")
}
}
func TestComplement(t *testing.T) {
a := New(50)
b := a.Complement()
if b.Count()!= 50 {
t.Errorf("Complement failed, size should be 50, but was %d", b.Count())
}
a = New(50)
a.Set(10).Set(20).Set(42)
b = a.Complement()
if b.Count()!= 47 {
t.Errorf("Complement failed, size should be 47, but was %d", b.Count())
}
}
// BENCHMARKS
func BenchmarkSet(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(0))
sz := 100000
s := New(uint(sz))
b.StartTimer()
for i := 0; i < b.N; i++ {
s.Set(uint(r.Int31n(int32(sz))))
}
}
func BenchmarkGetTest(b *testing.B) {
b.StopTimer()
r := rand.New(rand.NewSource(0))
sz := 100000
s := New(uint(sz))
b.StartTimer()
for i := 0; i < b.N; i++ {
s.Test(uint(r.Int31n(int32(sz))))
}
}
func BenchmarkSetExpand(b *testing.B) {
b.StopTimer()
sz := uint(100000)
b.StartTimer()
for i := 0; i < b.N; i++ {
s := New()
s.Set(sz)
}
}