-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitbox_test.go
391 lines (327 loc) · 7.23 KB
/
bitbox_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
package bitbox
import (
"testing"
)
func TestNewBitBox(t *testing.T) {
b := NewBitBox(129)
if b.Size() != 136 {
t.Errorf("Expected bitbox to have size of 136, but was %d", b.Size())
}
}
func TestBitPosition(t *testing.T) {
b := &BitBox{}
by, bi := b.Position(0)
if by != 0 || bi != 0x80 {
t.Errorf("Expected Position(0) to be 0, 0x80, but was %d, %0b", by, bi)
}
by, bi = b.Position(7)
if by != 0 || bi != 0x01 {
t.Errorf("Expected Position(7) to be 0, 0x01, but was %d, %0b", by, bi)
}
by, bi = b.Position(8)
if by != 1 || bi != 0x80 {
t.Errorf("Expected Position(8) to be 1, 0x80, but was %d, %0b", by, bi)
}
by, bi = b.Position(36)
if by != 4 || bi != 0x08 {
t.Errorf("Expected Position(37) to be 4, 0x08, but was %d, %0b", by, bi)
}
}
func TestBasicBitManipulation(t *testing.T) {
b := &BitBox{}
b.Set(5)
if !b.Get(5) {
t.Errorf("Expected bit 5 to be true, but got false")
}
if b.Bytes[0] != uint8(4) {
t.Errorf("Expected byte uint8(4), but got uint8(%d)", b.Bytes[0])
}
b.Set(15)
if !b.Get(15) {
t.Errorf("Expected bit 15 to be set, but got %v", b.Get(15))
}
b.Unset(15)
if b.Get(15) {
t.Errorf("Expected bit 15 to be unset, but to %v", b.Get(15))
}
if b.max != 16 {
t.Errorf("Expected Unset to not change max, but changed to %d", b.max)
}
if len(b.Bytes) != 2 {
t.Errorf("Expected Unset to not change bytes, but is %d", len(b.Bytes))
}
}
func TestByteExpansion(t *testing.T) {
b := &BitBox{}
b.Set(0)
if b.max != 8 {
t.Errorf("Expected 8 bit max, but got %d", b.max)
}
if len(b.Bytes) != 1 {
t.Errorf("Expected 1 byte, but was %d", len(b.Bytes))
}
b.Set(15)
if b.max != 16 {
t.Errorf("Expected 16 bit max, but got %d", b.max)
}
if len(b.Bytes) != 2 {
t.Errorf("Expected 2 bytes, but was %d", len(b.Bytes))
}
b.Set(641)
if b.max != 648 {
t.Errorf("Expected 648 bit max, but got %d", b.max)
}
if len(b.Bytes) != 81 {
t.Errorf("Expected 81 bytes, but was %d", len(b.Bytes))
}
b.Get(4500)
if b.max != 648 {
t.Errorf("Get should not expand max, but expanded to %d", b.max)
}
if len(b.Bytes) != 81 {
t.Errorf("Expected 81 bytes, but was %d", len(b.Bytes))
}
}
func TestGetByte(t *testing.T) {
b := &BitBox{}
b.Set(3)
if b.GetByte(0) != 0x10 {
t.Errorf("Expected byte 0 to by 0x10, but was %x", b.GetByte(1))
}
if b.GetByte(32) != 0x0 {
t.Errorf("Expected byte 32 to by 0x00, but was %x", b.GetByte(1))
}
}
func TestResizing(t *testing.T) {
b := &BitBox{}
b.Set(90)
b.Clear()
if len(b.Bytes) != 12 {
t.Errorf("Clear expected byte length of 0, but was %d", len(b.Bytes))
}
for i := 0; i < 12; i++ {
if b.Bytes[i] != 0x00 {
t.Errorf("Expected all bits to be cleared, but byte %x wasnt", i)
}
}
b.Resize(15)
if b.max != 16 {
t.Errorf("Expected Resize to set max to 16, but was %d", b.max)
}
if len(b.Bytes) != 2 {
t.Errorf("Expected Resize byte length of 2, but was %d", len(b.Bytes))
}
b.Set(15)
b.Resize(15)
if !b.Get(15) {
t.Errorf("Expected growing with resize to not clear bit Position 15")
}
b.Resize(4)
if b.Get(15) {
t.Errorf("Expected resize to clear bit Position 15")
}
if len(b.Bytes) != 1 {
t.Errorf("Expected Resize byte length of 1, but was %d", len(b.Bytes))
}
if b.Resize(16) != 16 {
t.Errorf("Expected returned size of 16, but was %d", b.Size())
}
b = NewBitBox(63)
if b.Resize(64) != 64 {
t.Errorf("Expected returned size of 64, but was %d", b.Size())
}
}
func TestToggleBits(t *testing.T) {
b := &BitBox{}
b.Toggle(23)
if !b.Get(23) {
t.Errorf("Expected Toggle to set bit 23, but was %v", b.Get(23))
}
b.Toggle(23)
if b.Get(23) {
t.Errorf("Expected Toggle to unset bit 23, but was %v", b.Get(23))
}
}
func TestAnd(t *testing.T) {
b := &BitBox{}
b.Set(0)
b.Set(19)
b.Set(43)
and := b.And([]int{0, 19, 43})
if !and {
t.Errorf("Expected And to be true but was %v", and)
}
and = b.And([]int{0, 19, 99})
if and {
t.Errorf("Expected And to be false but was %v", and)
}
and = b.And([]int{0})
if !and {
t.Errorf("Expected And to be true but was %v", and)
}
and = b.And([]int{42})
if and {
t.Errorf("Expected And to be true but was %v", and)
}
}
func TestOr(t *testing.T) {
b := &BitBox{}
b.Set(7)
b.Set(12)
or := b.Or([]int{0, 7})
if !or {
t.Errorf("Expected Or to be true but was %v", or)
}
or = b.Or([]int{0, 33})
if or {
t.Errorf("Expected Or to be false but was %v", or)
}
or = b.Or([]int{1, 2, 3, 4, 7})
if !or {
t.Errorf("Expected Or to be true but was %v", or)
}
or = b.Or([]int{7, 12})
if !or {
t.Errorf("Expected Or to be true but was %v", or)
}
}
func TestXOr(t *testing.T) {
b := &BitBox{}
b.Set(7)
b.Set(12)
xor := b.Xor([]int{0, 7})
if !xor {
t.Errorf("Expected Xor to be true but was %v", xor)
}
xor = b.Xor([]int{0, 33})
if xor {
t.Errorf("Expected Xor to be false but was %v", xor)
}
xor = b.Xor([]int{1, 2, 3, 4, 7})
if !xor {
t.Errorf("Expected Xor to be true but was %v", xor)
}
xor = b.Xor([]int{7, 12})
if xor {
t.Errorf("Expected Xor to be false but was %v", xor)
}
xor = b.Xor([]int{7, 0, 12})
if xor {
t.Errorf("Expected Xor to be false but was %v", xor)
}
}
func BenchmarkToggle(b *testing.B) {
bb := NewBitBox(10000)
for n := 0; n < b.N; n++ {
bb.Toggle(500)
}
}
func BenchmarkGet(b *testing.B) {
bb := NewBitBox(10000)
for n := 0; n < b.N; n++ {
bb.Get(500)
}
}
func BenchmarkSet(b *testing.B) {
bb := NewBitBox(10000)
for n := 0; n < b.N; n++ {
bb.Set(500)
}
}
func BenchmarkUnset(b *testing.B) {
bb := NewBitBox(10000)
for n := 0; n < b.N; n++ {
bb.Unset(500)
}
}
func BenchmarkClear(b *testing.B) {
bb := NewBitBox(10000)
for n := 0; n < b.N; n++ {
bb.Clear()
}
}
func BenchmarkTwoAnd(b *testing.B) {
bb := NewBitBox(10000)
bb.Set(300)
bb.Set(500)
for n := 0; n < b.N; n++ {
bb.And([]int{300, 500})
}
}
func BenchmarkThreeAnd(b *testing.B) {
bb := NewBitBox(10000)
bb.Set(100)
bb.Set(300)
bb.Set(500)
for n := 0; n < b.N; n++ {
bb.And([]int{100, 300, 500})
}
}
func BenchmarkFourAndWorstCase(b *testing.B) {
bb := NewBitBox(10000)
bb.Set(100)
bb.Set(300)
bb.Set(500)
bb.Set(800)
for n := 0; n < b.N; n++ {
bb.And([]int{100, 300, 500, 800})
}
}
func BenchmarkFourAndBestCase(b *testing.B) {
bb := NewBitBox(10000)
bb.Set(100)
bb.Set(300)
bb.Set(500)
bb.Set(800)
for n := 0; n < b.N; n++ {
bb.And([]int{0, 300, 500, 800})
}
}
func BenchmarkTwoOrFirst(b *testing.B) {
bb := NewBitBox(10000)
bb.Set(1700)
for n := 0; n < b.N; n++ {
bb.Or([]int{1700, 800})
}
}
func BenchmarkTwoOrSecond(b *testing.B) {
bb := NewBitBox(10000)
bb.Set(1700)
for n := 0; n < b.N; n++ {
bb.Or([]int{800, 1700})
}
}
func BenchmarkThreeOrNone(b *testing.B) {
bb := NewBitBox(10000)
for n := 0; n < b.N; n++ {
bb.Or([]int{800, 1700, 2500})
}
}
func BenchmarkXorTwo(b *testing.B) {
bb := NewBitBox(10000)
bb.Set(1776)
for n := 0; n < b.N; n++ {
bb.Or([]int{800, 1776})
}
}
func BenchmarkXorTwoNone(b *testing.B) {
bb := NewBitBox(10000)
bb.Set(1776)
for n := 0; n < b.N; n++ {
bb.Or([]int{800, 43})
}
}
func BenchmarkXorThreeWorstCase(b *testing.B) {
bb := NewBitBox(10000)
bb.Set(1776)
for n := 0; n < b.N; n++ {
bb.Or([]int{1906, 9999, 1776})
}
}
func BenchmarkXorThreeBestCase(b *testing.B) {
bb := NewBitBox(10000)
bb.Set(1776)
for n := 0; n < b.N; n++ {
bb.Or([]int{1776, 1906, 9999})
}
}