-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
bitmap_test.go
101 lines (87 loc) · 1.62 KB
/
bitmap_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
package tensor
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestBitMap(t *testing.T) {
assert := assert.New(t)
bm := NewBitMap(64)
assert.Equal(1, len(bm.n))
track := uint64(0)
for i := 0; i < 64; i++ {
bm.Set(i)
track |= uint64(1) << uint64(i)
assert.Equal(track, bm.n[0])
assert.Equal(true, bm.IsSet(i))
if i < 63 {
assert.Equal(false, bm.IsSet(i+1))
} else {
fails := func() {
bm.IsSet(i + 1)
}
assert.Panics(fails)
}
}
for i := 0; i < 64; i++ {
bm.Clear(i)
track &= ^(uint64(1) << uint64(i))
assert.Equal(track, bm.n[0])
assert.Equal(false, bm.IsSet(i))
}
bm = NewBitMap(124)
assert.Equal(2, len(bm.n))
track0 := uint64(0)
track1 := uint64(0)
for i := 0; i < 128; i++ {
if i < 124 {
bm.Set(i)
} else {
fails := func() {
bm.Set(i)
}
assert.Panics(fails)
}
if i < 64 {
track0 |= uint64(1) << uint64(i)
assert.Equal(track0, bm.n[0])
assert.Equal(true, bm.IsSet(i))
} else if i > 123 {
fails := func() {
bm.IsSet(i)
}
assert.Panics(fails)
} else {
track1 |= uint64(1) << uint64(i-64)
assert.Equal(track1, bm.n[1])
assert.Equal(true, bm.IsSet(i))
}
if i < 123 {
assert.Equal(false, bm.IsSet(i+1))
} else {
fails := func() {
bm.IsSet(i + 1)
}
assert.Panics(fails)
}
}
for i := 48; i < 70; i++ {
bm.Clear(i)
}
for i := 48; i < 70; i++ {
assert.Equal(false, bm.IsSet(i))
}
fails := func() {
bm.Clear(125)
}
assert.Panics(fails)
// idiots section!
bm = NewBitMap(3)
fails = func() {
bm.Set(-1)
}
assert.Panics(fails)
fails = func() {
bm.Set(3)
}
assert.Panics(fails)
}