-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitmap_8_test.go
272 lines (232 loc) · 4.82 KB
/
bitmap_8_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
package bitmap
import (
"testing"
"github.com/stretchr/testify/assert"
)
func Benchmark_Bitmap8_Set(b *testing.B) {
var bm Bitmap8
for i := 0; i < b.N; i++ {
bm.Set(1)
}
}
func Test_Bitmap8_Set(t *testing.T) {
t.Run("must set the specified bit", func(t *testing.T) {
t.Run("0", func(t *testing.T) {
var b Bitmap8
b.Set(0)
assert.Equal(t, Bitmap8{1}, b)
})
t.Run("2", func(t *testing.T) {
var b Bitmap8
b.Set(2)
assert.Equal(t, Bitmap8{4}, b)
})
t.Run("0,2", func(t *testing.T) {
var b Bitmap8
b.Set(0)
b.Set(2)
assert.Equal(t, Bitmap8{5}, b)
})
t.Run("7", func(t *testing.T) {
var b Bitmap8
b.Set(7)
assert.Equal(t, Bitmap8{0x80}, b)
})
t.Run("8", func(t *testing.T) {
var b Bitmap8
b.Set(8)
assert.Equal(t, Bitmap8{0, 1}, b)
})
})
t.Run("must do nothing if the specified bit is already == 1", func(t *testing.T) {
var b Bitmap8
b.Set(0)
b.Set(0)
assert.Equal(t, Bitmap8{1}, b)
})
}
func Test_Bitmap8_Remove(t *testing.T) {
var b Bitmap8
b.Remove(0)
assert.Nil(t, b)
b.Set(0)
b.Set(1)
b.Set(10)
b.Remove(10)
assert.Equal(t, Bitmap8{3, 0}, b)
b.Remove(1)
assert.Equal(t, Bitmap8{1, 0}, b)
b.Remove(0)
assert.Equal(t, Bitmap8{0, 0}, b)
}
func Benchmark_Bitmap8_Xor(b *testing.B) {
var bm Bitmap8
for i := 0; i < b.N; i++ {
bm.Xor(1)
}
}
func Test_Bitmap8_Xor(t *testing.T) {
t.Run("must invert the specified bit", func(t *testing.T) {
var b Bitmap8
b.Xor(0)
assert.Equal(t, Bitmap8{1}, b)
b.Xor(1)
assert.Equal(t, Bitmap8{3}, b)
b.Xor(0)
assert.Equal(t, Bitmap8{2}, b)
b.Xor(1)
assert.Equal(t, Bitmap8{0}, b)
})
}
func Test_Bitmap8_IsEmpty(t *testing.T) {
t.Run("must return true if it's empty", func(t *testing.T) {
var b Bitmap8
assert.True(t, b.IsEmpty())
b.Xor(1)
b.Xor(1)
assert.True(t, b.IsEmpty())
})
t.Run("must return false if it is not empty", func(t *testing.T) {
var b Bitmap8
b.Set(1)
assert.False(t, b.IsEmpty())
})
}
func Test_Bitmap8_Has(t *testing.T) {
var b Bitmap8
assert.False(t, b.Has(0))
b.Xor(0)
assert.True(t, b.Has(0))
b.Xor(0)
assert.False(t, b.Has(0))
}
func Benchmark_Bitmap8_CountDiff(b *testing.B) {
var b1, b2 Bitmap8
b1.Set(1)
b1.Set(2)
b2.Set(31)
for i := 0; i < b.N; i++ {
b1.CountDiff(b2)
}
}
func Test_Bitmap8_CountDiff(t *testing.T) {
t.Run("must return 0 if bitmaps are equal", func(t *testing.T) {
var b1, b2 Bitmap8
b1.Set(1)
b2.Set(1)
assert.Equal(t, 0, b1.CountDiff(b2))
})
t.Run("must return correct count of different bits", func(t *testing.T) {
var b1, b2 Bitmap8
b1.Set(1)
b1.Set(2)
b1.Set(64)
b2.Set(31)
b2.Set(64)
b2.Set(650)
assert.Equal(t, 4, b1.CountDiff(b2))
})
}
func Test_Bitmap8_Or(t *testing.T) {
var b1, b2 Bitmap8
b1.Set(0)
b1.Set(1)
b1.Set(10)
b2.Set(0)
b2.Set(1)
b2.Set(2)
b2.Set(11)
b2.Set(16)
b1.Or(b2)
assert.Equal(t, Bitmap8{0x7, 0xc, 0x1}, b1)
assert.True(t, b1.Has(0))
assert.True(t, b1.Has(1))
assert.True(t, b1.Has(2))
assert.True(t, b1.Has(10))
assert.True(t, b1.Has(11))
assert.True(t, b1.Has(16))
assert.False(t, b2.Has(10))
}
func Test_Bitmap8_And(t *testing.T) {
var b1, b2 Bitmap8
b1.Set(0)
b1.Set(1)
b1.Set(10)
b2.Set(0)
b2.Set(1)
b2.Set(2)
b2.Set(11)
b2.Set(16)
b1.And(b2)
assert.Equal(t, Bitmap8{3, 0}, b1)
assert.True(t, b1.Has(0))
assert.True(t, b1.Has(1))
assert.False(t, b1.Has(2))
assert.False(t, b1.Has(10))
assert.False(t, b1.Has(11))
assert.False(t, b1.Has(16))
assert.True(t, b2.Has(2))
}
func Test_Bitmap8_Shrink(t *testing.T) {
var b Bitmap8
b.Set(1)
b.Set(100)
b.Remove(100)
b.Shrink()
assert.Equal(t, Bitmap8{2}, b)
}
func Test_Bitmap8_Clone(t *testing.T) {
var b1 Bitmap8
b1.Set(0)
b2 := b1.Clone()
assert.Equal(t, b1, b2)
b2.Set(2)
assert.Equal(t, Bitmap8{5}, b2)
assert.Equal(t, Bitmap8{1}, b1)
}
func Test_Bitmap8_Range(t *testing.T) {
var b1 Bitmap8
b1.Set(0)
b1.Set(1)
b1.Set(2)
b1.Set(1000)
b1.Set(10000)
var items []uint32
b1.Range(func(n uint32) bool {
items = append(items, n)
if n == 1000 {
return false
}
return true
})
assert.Equal(t, []uint32{0, 1, 2, 1000}, items)
}
func Benchmark_Bitmap8_String(b *testing.B) {
bm := Bitmap8{0, 5, 100}
for i := 0; i < b.N; i++ {
_ = bm.String()
}
}
func Test_Bitmap8_String(t *testing.T) {
b := Bitmap8{}
assert.Equal(t, "", b.String())
b = Bitmap8{0, 5, 100}
assert.Equal(t, "0|5|100", b.String())
}
func Test_FromString8(t *testing.T) {
t.Run("must return error if unable to parse the string", func(t *testing.T) {
_, err := FromString8("qwe")
assert.Error(t, err)
})
t.Run("must parse the string correctly", func(t *testing.T) {
v, err := FromString8("")
assert.Nil(t, err)
assert.Equal(t, Bitmap8{}, v)
v, err = FromString8("0")
assert.Nil(t, err)
assert.Equal(t, Bitmap8{0}, v)
v, err = FromString8("0|5")
assert.Nil(t, err)
assert.Equal(t, Bitmap8{0, 5}, v)
})
}