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
|
package bitarray
import (
"fmt"
"testing"
)
func TestAddBit(t *testing.T) {
tests := []struct {
ba *BitArray
in uint
expected string
}{
{NewFromBytes([]byte{0xF0}, 4), 1, "[11111---]"},
{NewFromBytes([]byte{0xFF}, 8), 1, "[11111111 1-------]"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d", tt.in), func(t *testing.T) {
lBefore := tt.ba.Len()
tt.ba.AddBit(tt.in)
actual := tt.ba.String()
if actual != tt.expected {
t.Errorf("got %s, want %s", actual, tt.expected)
}
expected := lBefore + 1
if tt.ba.Len() != expected {
t.Errorf("got %d, want %d", tt.ba.Len(), expected)
}
})
}
}
func TestAdd(t *testing.T) {
tests := []struct {
ba *BitArray
in []uint
expected string
expectedN int64
}{
{NewFromBytes([]byte{0xF0}, 4), []uint{1}, "[11111---]", 5},
{NewFromBytes([]byte{0xF0}, 8), []uint{1}, "[11110000 1-------]", 9},
{NewFromBytes([]byte{0xF0}, 4), []uint{0}, "[11110---]", 5},
{NewFromBytes([]byte{0xF0}, 4), []uint{0xf0ff, 0x0f}, "[11111111 00001111 11111111]", 24},
{NewFromBytes([]byte{0xF0}, 8), []uint{0xf0ff, 0x0f}, "[11110000 11110000 11111111 1111----]", 28},
{NewFromBytes([]byte{0xF0}, 8), []uint{0x0f0fff}, "[11110000 11110000 11111111 1111----]", 28},
{NewFromBytes([]byte{0xF0}, 8), []uint{0x81bf0fff}, "[11110000 10000001 10111111 00001111 11111111]", 40},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d", tt.in), func(t *testing.T) {
for _, i := range tt.in {
tt.ba.Add(i)
}
actual := tt.ba.String()
if actual != tt.expected {
t.Errorf("got %s, want %s", actual, tt.expected)
}
if tt.ba.Len() != tt.expectedN {
t.Errorf("got len=%d, want %d", tt.ba.Len(), tt.expectedN)
}
})
}
}
func TestAddN(t *testing.T) {
tests := []struct {
ba *BitArray
in uint
l int
expected string
}{
{NewFromBytes([]byte{0xF0}, 4), 1, 1, "[11111---]"},
{NewFromBytes([]byte{0xF0}, 4), 1, 2, "[111101--]"},
{NewFromBytes([]byte{0xF0}, 4), 1, 3, "[1111001-]"},
{NewFromBytes([]byte{0xF0}, 4), 1, 4, "[11110001]"},
{NewFromBytes([]byte{0xF0}, 4), 1, 5, "[11110000 1-------]"},
{NewFromBytes([]byte{0xF0}, 4), 1, 6, "[11110000 01------]"},
{NewFromBytes([]byte{0xF0}, 4), 1, 7, "[11110000 001-----]"},
{NewFromBytes([]byte{0xF0}, 4), 1, 8, "[11110000 0001----]"},
{NewFromBytes([]byte{0xF0}, 4), 1, 9, "[11110000 00001---]"},
{NewFromBytes([]byte{0xF0}, 4), 1, 10, "[11110000 000001--]"},
{NewFromBytes([]byte{0xF0}, 4), 1, 11, "[11110000 0000001-]"},
{NewFromBytes([]byte{0xF0}, 4), 1, 12, "[11110000 00000001]"},
{NewFromBytes([]byte{0xF0}, 4), 1, 13, "[11110000 00000000 1-------]"},
// Add zero
{NewFromBytes([]byte{0xF0}, 4), 0, 6, "[11110000 00------]"},
// Truncate
{NewFromBytes([]byte{0xF0}, 4), 255, 1, "[11111---]"},
{NewFromBytes([]byte{0x00}, 0), 255, 2, "[11------]"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d", tt.in), func(t *testing.T) {
lBefore := tt.ba.Len()
n := tt.ba.AddN(tt.in, tt.l)
actual := tt.ba.String()
if actual != tt.expected {
t.Errorf("got %s, want %s", actual, tt.expected)
}
if n != tt.l {
t.Errorf("got n=%d, want %d", n, tt.l)
}
expected := lBefore + int64(tt.l)
if tt.ba.Len() != expected {
t.Errorf("got len=%d, want %d", tt.ba.Len(), expected)
}
})
}
}
func TestSlice(t *testing.T) {
tests := []struct {
ba *BitArray
s, l int64 // start and length
expected string
avail uint
}{
{NewFromBytes([]byte{0xff}, 8), 0, 8, "[11111111]", 0},
{NewFromBytes([]byte{0xff}, 8), 0, 1, "[1-------]", 7},
{NewFromBytes([]byte{0xfe}, 8), 0, 8, "[11111110]", 0},
{NewFromBytes([]byte{0x03}, 8), 7, 1, "[1-------]", 7},
{NewFromBytes([]byte{0xd0}, 4), 0, 4, "[1101----]", 4},
// Multiple bytes
{NewFromBytes([]byte{0xd0, 0xff}, 16), 0, 9, "[11010000 1-------]", 7},
{NewFromBytes([]byte{0x0f, 0xf0}, 16), 4, 8, "[11111111]", 0},
// Cases
// 10010110 00101100 01001001 => 1000101
{NewFromBytes([]byte{0x96, 0x2c, 0x49}, 24), 6, 7, "[1000101-]", 1},
// 10010110 00101100 01001001 01110010 00101011 10000000
// ^^^^^ ^^
{NewFromBytes([]byte{0x96, 0x2c, 0x49, 0x72, 0x2b, 0x80}, 48), 27, 7, "[1001000-]", 1},
{NewFromBytes([]byte{0, 0xFF, 0xFF}, 24), 8, 0, "[]", 0},
}
for _, tt := range tests {
name := fmt.Sprintf("%x[%d:%d]", tt.ba.raw, tt.s, tt.l)
t.Run(name, func(t *testing.T) {
a, err := tt.ba.Slice(tt.s, tt.l)
if err != nil {
t.Errorf("failed with %q", err)
}
actual := a.String()
if actual != tt.expected {
t.Errorf("got %q, want %q", actual, tt.expected)
}
if a.avail() != tt.avail {
t.Errorf("got avail %d, want %d", a.avail(), tt.avail)
}
})
}
}
func TestReadUint(t *testing.T) {
tests := []struct {
ba *BitArray
s, l int64 // start and length
expected uint
}{
{NewFromBytes([]byte{0x02}, 8), 0, 8, 0x02},
{NewFromBytes([]byte{0xff}, 8), 0, 8, 0xff},
{NewFromBytes([]byte{0xff}, 8), 0, 1, 0x01},
{NewFromBytes([]byte{0xfe}, 8), 0, 8, 0xfe},
{NewFromBytes([]byte{0x03}, 8), 7, 1, 0x01},
{NewFromBytes([]byte{0xd0}, 8), 0, 4, 0x0d},
// Multiple bytes
{NewFromBytes([]byte{0xd0, 0xff}, 16), 0, 9, 0x1a1},
{NewFromBytes([]byte{0x0f, 0xf0}, 16), 4, 8, 0xff},
}
for _, tt := range tests {
name := fmt.Sprintf("%x[%d:%d]", tt.ba.raw, tt.s, tt.l)
t.Run(name, func(t *testing.T) {
actual, err := tt.ba.ReadUint(tt.s, tt.l)
if err != nil {
t.Errorf("failed with %q", err)
}
if actual != tt.expected {
t.Errorf("got %d, want %d", actual, tt.expected)
}
})
}
}
func TestTest(t *testing.T) {
tests := []struct {
ba *BitArray
in int64
expected bool
}{
{NewFromBytes([]byte{0x01}, 8), 0, false},
{NewFromBytes([]byte{0x01}, 8), 1, false},
{NewFromBytes([]byte{0x01}, 8), 7, true},
{NewFromBytes([]byte{0x01}, 8), 8, false},
{NewFromBytes([]byte{0x00, 0x80}, 16), 8, true},
{NewFromBytes([]byte{0x00, 0x02}, 16), 14, true},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d", tt.in), func(t *testing.T) {
actual := tt.ba.Test(tt.in)
if actual != tt.expected {
t.Errorf("got %t, want %t", actual, tt.expected)
}
})
}
}
func TestSet(t *testing.T) {
tests := []struct {
ba *BitArray
in int64
expected string
}{
{NewFromBytes([]byte{0x00}, 1), 0, "[10000000]"},
{NewFromBytes([]byte{0x80}, 8), 1, "[11000000]"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d", tt.in), func(t *testing.T) {
tt.ba.Set(tt.in)
actual := fmt.Sprintf("%08b", tt.ba.Bytes())
if actual != tt.expected {
t.Errorf("got %s, want %s", actual, tt.expected)
}
})
}
}
func TestShiftL(t *testing.T) {
tests := map[string]struct {
ba *BitArray
shift int64
expected string
}{
"short": {NewFromBytes([]byte{0x01}, 8), 1, "[0000001-]"},
"longer": {NewFromBytes([]byte{0x01}, 8), 7, "[1-------]"},
"zero": {NewFromBytes([]byte{0x01}, 8), 0, "[00000001]"},
"empty": {NewFromBytes([]byte(nil), 0), 0, "[]"},
"fullByte": {NewFromBytes([]byte{0x01}, 8), 8, "[]"},
"acrossByte": {NewFromBytes([]byte{0x01, 0x01}, 16), 5, "[00100000 001-----]"},
"acrossBytesTrimmed": {NewFromBytes([]byte{0x01, 0x01}, 16), 8, "[00000001]"},
"moreThan8": {NewFromBytes([]byte{0x00, 0x01}, 16), 11, "[00001---]"},
"moreThan16": {NewFromBytes([]byte{0x00, 0x00, 0x01}, 24), 22, "[01------]"},
"shiftMoreThanSize": {NewFromBytes([]byte{0x00, 0x01}, 16), 22, "[]"},
"shiftZero": {NewFromBytes([]byte{0x00, 0x01}, 16), 0, "[00000000 00000001]"},
"trimmed": {NewFromBytes([]byte{0x00, 0x01}, 16), 8, "[00000001]"},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
tt.ba.ShiftL(tt.shift)
actual := tt.ba.String()
if actual != tt.expected {
t.Errorf("got %s, want %s", actual, tt.expected)
}
})
}
}
func TestPack(t *testing.T) {
tests := []struct {
in interface{}
expected string
}{
// Types
{uint(108), "[1101100-]"},
{uint8(108), "[1101100-]"},
{uint16(108), "[1101100-]"},
{uint32(108), "[1101100-]"},
{uint64(108), "[1101100-]"},
{int(108), "[1101100-]"},
{int8(0xf), "[1111----]"},
{int16(108), "[1101100-]"},
{int32(108), "[1101100-]"},
{int64(108), "[1101100-]"},
{[]byte{0x6c}, "[1101100-]"},
{[]uint{108}, "[1101100-]"},
{[]int{108}, "[1101100-]"},
{[]int8{108}, "[1101100-]"},
{[]int16{108}, "[1101100-]"},
{[]int32{108}, "[1101100-]"},
{[]int64{108}, "[1101100-]"},
// Zero
{uint(0), "[0-------]"},
{uint8(0), "[0-------]"},
{uint16(0), "[0-------]"},
{uint32(0), "[0-------]"},
{uint64(0), "[0-------]"},
{int(0), "[0-------]"},
{int8(0), "[0-------]"},
{int16(0), "[0-------]"},
{int32(0), "[0-------]"},
{int64(0), "[0-------]"},
{[]byte{0}, "[0-------]"},
{[]uint{0}, "[0-------]"},
{[]int{0}, "[0-------]"},
{[]int8{0}, "[0-------]"},
{[]int16{0}, "[0-------]"},
{[]int32{0}, "[0-------]"},
{[]int64{0}, "[0-------]"},
// Adding a zero
{[]int{1, 0, 23}, "[1010111-]"},
{[]uint16{1, 0, 23}, "[1010111-]"},
{[]uint32{1, 0, 23}, "[1010111-]"},
{[]uint64{1, 0, 23}, "[1010111-]"},
// Cases
{[]uint8{0xff, 0xff}, "[11111111 11111111]"},
{[]uint8{0xff, 0xf0}, "[11111111 11110000]"},
{[]uint8{0xf0, 0xf0}, "[11110000 11110000]"},
{[]uint8{0xf0, 0xf0, 1}, "[11110000 11110000 1-------]"},
{[]int{1, 128, 23}, "[11000000 010111--]"},
{[]int{1, 129, 23}, "[11000000 110111--]"},
{[]interface{}{uint8(0xff), 1, 2, 1, 4, 1, 1}, "[11111111 11011001 1-------]"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%v", tt.in), func(t *testing.T) {
ba, err := Pack(tt.in)
if err != nil {
t.Fatalf("failed %q", err)
}
actual := ba.String()
if actual != tt.expected {
t.Errorf("got %s, want %s", actual, tt.expected)
}
if actual != ba.String() {
t.Errorf("got %q, want %q", ba.String(), actual)
}
})
}
}
func TestAppend(t *testing.T) {
tests := []struct {
ba1 *BitArray
ba2 *BitArray
expected string
}{
{NewFromBytes([]byte{0x80}, 1), NewFromBytes([]byte{0x80}, 1), "[11000000]"},
{NewFromBytes([]byte{0x80}, 8), NewFromBytes([]byte{0x80}, 1), "[10000000 10000000]"},
{NewFromBytes([]byte{0xF0}, 4), NewFromBytes([]byte{0xF0}, 4), "[11111111]"},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%s+%s", tt.ba1, tt.ba2), func(t *testing.T) {
tt.ba1.Append(*tt.ba2)
actual := fmt.Sprintf("%08b", tt.ba1.Bytes())
if actual != tt.expected {
t.Errorf("got %s, want %s", actual, tt.expected)
}
})
}
}
|