-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitfield_test.go
158 lines (137 loc) · 3.11 KB
/
bitfield_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
package bitfield_test
import (
"github.com/josestg/bitfield"
"math"
"testing"
)
func TestInvert(t *testing.T) {
f := bitfield.BitField(0)
g := bitfield.Invert(f)
if g != math.MaxUint64 {
t.Error("Expect all bits set")
}
// Invert of inverted bitfield should be back to original.
if bitfield.Invert(g) != f {
t.Error("Expect all bits unset")
}
}
func TestUnion(t *testing.T) {
f := bitfield.BitField(0b0011_0011)
g := bitfield.BitField(0b0101_0101)
h := bitfield.Union(f, g)
if h != 0b0111_0111 {
t.Error("Expect 0b0111_0111")
}
}
func TestIntersection(t *testing.T) {
f := bitfield.BitField(0b0011_0011)
g := bitfield.BitField(0b0101_0101)
h := bitfield.Intersection(f, g)
if h != 0b0001_0001 {
t.Error("Expect 0b0001_0001")
}
}
func TestDifference(t *testing.T) {
f := bitfield.BitField(0b0011_0011)
g := bitfield.BitField(0b0101_0101)
h := bitfield.Difference(f, g)
if h != 0b0010_0010 {
t.Error("Expect 0b0010_0010")
}
}
func TestBitField_AllSet(t *testing.T) {
f := bitfield.BitField(0)
if f.AllSet() {
t.Error("Expect false")
}
f = bitfield.BitField(math.MaxUint64)
if !f.AllSet() {
t.Error("Expect true")
}
}
func TestBitField_NotEmpty(t *testing.T) {
f := bitfield.BitField(0)
if f.NotEmpty() {
t.Error("Expect false")
}
f = bitfield.BitField(0b0000_1000)
if !f.NotEmpty() {
t.Error("Expect true")
}
}
func TestBitField_Empty(t *testing.T) {
f := bitfield.BitField(0)
if !f.Empty() {
t.Error("Expect true")
}
f = bitfield.BitField(0b0000_1000)
if f.Empty() {
t.Error("Expect false")
}
}
func TestBitField_SetBit(t *testing.T) {
var f bitfield.BitField
f = f.SetBit(0).SetBit(1).SetBit(2)
if f != 0b0000_0111 {
t.Error("Expect 0b0000_0111")
}
// position out of range should be ignored.
f = f.SetBit(64)
if f != 0b0000_0111 {
t.Error("Expect no effect")
}
}
func TestBitField_IsSet(t *testing.T) {
// empty bitfield should return false for all positions.
var f bitfield.BitField
for i := 0; i < 64; i++ {
if f.IsSet(uint8(i)) {
t.Errorf("Expect false for %d", i)
}
}
// expect true for positions 0, 1, 2.
f = 0b0000_0111
for i := 0; i < 3; i++ {
if !f.IsSet(uint8(i)) {
t.Errorf("Expect true for %d", i)
}
}
// position out of range should return false.
if f.IsSet(64) {
t.Error("Expect false for 64")
}
}
func TestBitField_DelBit(t *testing.T) {
f := bitfield.BitField(0b1010_1010)
f = f.DelBit(1)
if f != 0b1010_1000 {
t.Error("Expect 0b1010_1000")
}
f = f.DelBit(7)
if f != 0b0010_1000 {
t.Error("Expect 0b0010_1000")
}
f = f.DelBit(3)
if f != 0b0010_0000 {
t.Error("Expect 0b0010_0000")
}
// position out of range should be ignored.
f = f.DelBit(64)
if f != 0b0010_0000 {
t.Error("Expect no effect")
}
}
func TestBitField_Cardinal(t *testing.T) {
var f bitfield.BitField
if f.Cardinal() != 0 {
t.Error("Expect cardinality of enpty bitfield to be 0")
}
f = 0b0000_0111
if f.Cardinal() != 3 {
t.Error("Expect cardinality of 0b0000_0111 to be 3")
}
g := bitfield.Invert(f)
if g.Cardinal() != (64 - f.Cardinal()) {
t.Error("Expect cardinality of inverted bitfield to be 64 - cardinality of original bitfield")
}
}