-
Notifications
You must be signed in to change notification settings - Fork 4
/
buffer_bitwise_test.go
370 lines (354 loc) · 10.8 KB
/
buffer_bitwise_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
// Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package bitarray_test
import (
"testing"
"github.com/tunabay/go-bitarray"
)
func TestBuffer_ToggleBitAt(t *testing.T) {
buf := bitarray.NewBuffer(18)
chk := func(wantS string) {
t.Helper()
buf.V()
got := buf.BitArray()
want := bitarray.MustParse(wantS)
if !got.Equal(want) {
t.Error("unexpected:")
t.Logf(" got: %#b", got)
t.Logf("want: %#b", want)
t.Logf(" buf: %s", buf.D())
}
}
chk("0000-0000 0000-0000 00")
buf.ToggleBitAt(0)
chk("1000-0000 0000-0000 00")
buf.ToggleBitAt(1)
chk("1100-0000 0000-0000 00")
buf.ToggleBitAt(2)
chk("1110-0000 0000-0000 00")
buf.ToggleBitAt(6)
chk("1110-0010 0000-0000 00")
buf.ToggleBitAt(7)
chk("1110-0011 0000-0000 00")
buf.ToggleBitAt(8)
chk("1110-0011 1000-0000 00")
buf.ToggleBitAt(0)
chk("0110-0011 1000-0000 00")
buf.ToggleBitAt(1)
chk("0010-0011 1000-0000 00")
buf.ToggleBitAt(7)
chk("0010-0010 1000-0000 00")
buf.ToggleBitAt(8)
chk("0010-0010 0000-0000 00")
buf.ToggleBitAt(17)
chk("0010-0010 0000-0000 01")
buf.ToggleBitAt(16)
chk("0010-0010 0000-0000 11")
buf.ToggleBitAt(15)
chk("0010-0010 0000-0001 11")
buf.ToggleBitAt(17)
chk("0010-0010 0000-0001 10")
buf.ToggleBitAt(10)
chk("0010-0010 0010-0001 10")
chkpanic := func(off int) {
defer func() {
if recover() == nil {
t.Errorf("panic expected: off=%d, buf=%s.", off, buf.D())
}
}()
buf.ToggleBitAt(off)
}
chkpanic(-1)
chkpanic(18)
}
func TestBuffer_ToggleBitsAt(t *testing.T) {
buf := bitarray.NewBuffer(28)
chk := func(wantS string) {
t.Helper()
buf.V()
got := buf.BitArray()
want := bitarray.MustParse(wantS)
if !got.Equal(want) {
t.Error("unexpected:")
t.Logf(" got: %#b", got)
t.Logf("want: %#b", want)
t.Logf(" buf: %s", buf.D())
}
}
chk("0000-0000 0000-0000 0000-0000 0000")
buf.ToggleBitsAt(0, 0)
chk("0000-0000 0000-0000 0000-0000 0000")
buf.ToggleBitsAt(28, 0)
chk("0000-0000 0000-0000 0000-0000 0000")
buf.ToggleBitsAt(0, 16)
chk("1111-1111 1111-1111 0000-0000 0000")
buf.ToggleBitsAt(0, 8)
chk("0000-0000 1111-1111 0000-0000 0000")
buf.ToggleBitsAt(16, 8)
chk("0000-0000 1111-1111 1111-1111 0000")
buf.ToggleBitsAt(0, 1)
chk("1000-0000 1111-1111 1111-1111 0000")
buf.ToggleBitsAt(0, 2)
chk("0100-0000 1111-1111 1111-1111 0000")
buf.ToggleBitsAt(0, 4)
chk("1011-0000 1111-1111 1111-1111 0000")
buf.ToggleBitsAt(0, 7)
chk("0100-1110 1111-1111 1111-1111 0000")
buf.ToggleBitsAt(1, 7)
chk("0011-0001 1111-1111 1111-1111 0000")
buf.ToggleBitsAt(0, 28)
chk("1100-1110 0000-0000 0000-0000 1111")
buf.ToggleBitsAt(6, 20)
chk("1100-1101 1111-1111 1111-1111 0011")
buf.ToggleBitsAt(2, 5)
chk("1111-0011 1111-1111 1111-1111 0011")
buf.ToggleBitsAt(9, 6)
chk("1111-0011 1000-0001 1111-1111 0011")
buf.ToggleBitsAt(16, 2)
chk("1111-0011 1000-0001 0011-1111 0011")
buf.ToggleBitsAt(21, 3)
chk("1111-0011 1000-0001 0011-1000 0011")
buf.ToggleBitsAt(7, 2)
chk("1111-0010 0000-0001 0011-1000 0011")
chkpanic := func(off, nBits int) {
defer func() {
if recover() == nil {
t.Errorf("panic expected: off=%d, nBits=%d, buf=%s.", off, nBits, buf.D())
}
}()
buf.ToggleBitsAt(off, nBits)
}
chkpanic(-1, 0)
chkpanic(-1, 1)
chkpanic(0, -1)
chkpanic(0, 29)
chkpanic(29, 0)
chkpanic(28, 1)
chkpanic(27, 2)
chkpanic(24, 5)
}
func TestBuffer_AndAt(t *testing.T) {
buf := bitarray.NewBuffer(30)
chk := func(wantS string) {
t.Helper()
buf.V()
got := buf.BitArray()
want := bitarray.MustParse(wantS)
if !got.Equal(want) {
t.Error("unexpected:")
t.Logf(" got: %#b", got)
t.Logf("want: %#b", want)
t.Logf(" buf: %s", buf.D())
}
}
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.AndAt(0, nil)
chk("0000-0000 0000-0000 0000-0000 0000-00")
var nilba *bitarray.BitArray
buf.AndAt(0, nilba)
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.AndAt(0, bitarray.NewZeroFilled(0))
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.AndAt(30, bitarray.NewZeroFilled(0))
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.AndAt(0, bitarray.NewZeroFilled(30))
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.FillBitsAt(0, 30, 1)
chk("1111-1111 1111-1111 1111-1111 1111-11")
buf.AndAt(0, bitarray.NewZeroFilled(30))
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.FillBitsAt(0, 30, 1)
buf.AndAt(0, bitarray.MustParse("00000000"))
chk("0000-0000 1111-1111 1111-1111 1111-11")
buf.FillBitsAt(0, 30, 1)
buf.AndAt(0, bitarray.MustParse("101010"))
chk("1010-1011 1111-1111 1111-1111 1111-11")
buf.AndAt(4, bitarray.MustParse("101010"))
chk("1010-1010 1011-1111 1111-1111 1111-11")
buf.AndAt(6, bitarray.MustParse("1111-0000 1111-0000"))
chk("1010-1010 1000-0011 1100-0011 1111-11")
buf.AndAt(7, bitarray.MustParse("10"))
chk("1010-1010 0000-0011 1100-0011 1111-11")
buf.AndAt(14, bitarray.MustParse("1100-1100 1100-1100"))
chk("1010-1010 0000-0011 0000-0011 0011-00")
buf.AndAt(8, bitarray.MustParse("1010-1010"))
chk("1010-1010 0000-0010 0000-0011 0011-00")
chkpanic := func(off int, ba bitarray.BitArrayer) {
defer func() {
if recover() == nil {
t.Errorf("panic expected: off=%d, ba=%v.", off, ba)
t.Logf("buf: %s", buf.D())
}
}()
buf.AndAt(off, ba)
}
chkpanic(-1, bitarray.NewZeroFilled(1))
chkpanic(30, bitarray.NewZeroFilled(1))
chkpanic(8, bitarray.NewOneFilled(23))
chkpanic(0, bitarray.NewZeroFilled(31))
}
func TestBuffer_OrAt(t *testing.T) {
buf := bitarray.NewBuffer(30)
chk := func(wantS string) {
t.Helper()
buf.V()
got := buf.BitArray()
want := bitarray.MustParse(wantS)
if !got.Equal(want) {
t.Error("unexpected:")
t.Logf(" got: %#b", got)
t.Logf("want: %#b", want)
t.Logf(" buf: %s", buf.D())
}
}
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.OrAt(0, nil)
chk("0000-0000 0000-0000 0000-0000 0000-00")
var nilba *bitarray.BitArray
buf.OrAt(0, nilba)
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.OrAt(0, bitarray.NewOneFilled(0))
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.OrAt(30, bitarray.NewOneFilled(0))
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.OrAt(0, bitarray.NewZeroFilled(30))
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.OrAt(0, bitarray.NewOneFilled(30))
chk("1111-1111 1111-1111 1111-1111 1111-11")
buf.OrAt(0, bitarray.NewZeroFilled(30))
chk("1111-1111 1111-1111 1111-1111 1111-11")
buf.FillBitsAt(0, 30, 0)
buf.OrAt(0, bitarray.NewOneFilled(4))
chk("1111-0000 0000-0000 0000-0000 0000-00")
buf.OrAt(6, bitarray.NewOneFilled(2))
chk("1111-0011 0000-0000 0000-0000 0000-00")
buf.OrAt(8, bitarray.MustParse("1010-1010"))
chk("1111-0011 1010-1010 0000-0000 0000-00")
buf.OrAt(22, bitarray.MustParse("10110011"))
chk("1111-0011 1010-1010 0000-0010 1100-11")
buf.OrAt(29, bitarray.MustParse("1"))
chk("1111-0011 1010-1010 0000-0010 1100-11")
buf.FillBitsAt(8, 20, 0)
chk("1111-0011 0000-0000 0000-0000 0000-11")
buf.OrAt(4, bitarray.MustParse("1010-1010 1010-1010 1010"))
chk("1111-1011 1010-1010 1010-1010 0000-11")
buf.OrAt(10, bitarray.MustParse("0101-0101 0101"))
chk("1111-1011 1011-1111 1111-1110 0000-11")
chkpanic := func(off int, ba bitarray.BitArrayer) {
defer func() {
if recover() == nil {
t.Errorf("panic expected: off=%d, ba=%v.", off, ba)
t.Logf("buf: %s", buf.D())
}
}()
buf.OrAt(off, ba)
}
chkpanic(-1, bitarray.NewZeroFilled(1))
chkpanic(30, bitarray.NewZeroFilled(1))
chkpanic(8, bitarray.NewOneFilled(23))
}
func TestBuffer_XorAt(t *testing.T) {
buf := bitarray.NewBuffer(30)
chk := func(wantS string) {
t.Helper()
buf.V()
got := buf.BitArray()
want := bitarray.MustParse(wantS)
if !got.Equal(want) {
t.Error("unexpected:")
t.Logf(" got: %#b", got)
t.Logf("want: %#b", want)
t.Logf(" buf: %s", buf.D())
}
}
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.XorAt(0, nil)
chk("0000-0000 0000-0000 0000-0000 0000-00")
var nilba *bitarray.BitArray
buf.XorAt(0, nilba)
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.XorAt(0, bitarray.NewOneFilled(0))
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.XorAt(30, bitarray.NewOneFilled(0))
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.XorAt(0, bitarray.NewZeroFilled(30))
chk("0000-0000 0000-0000 0000-0000 0000-00")
buf.XorAt(0, bitarray.NewOneFilled(30))
chk("1111-1111 1111-1111 1111-1111 1111-11")
buf.XorAt(0, bitarray.NewZeroFilled(30))
chk("1111-1111 1111-1111 1111-1111 1111-11")
buf.XorAt(0, bitarray.NewOneFilled(4))
chk("0000-1111 1111-1111 1111-1111 1111-11")
buf.XorAt(6, bitarray.NewOneFilled(2))
chk("0000-1100 1111-1111 1111-1111 1111-11")
buf.XorAt(8, bitarray.MustParse("1010-1010"))
chk("0000-1100 0101-0101 1111-1111 1111-11")
buf.XorAt(22, bitarray.MustParse("10110011"))
chk("0000-1100 0101-0101 1111-1101 0011-00")
buf.XorAt(29, bitarray.MustParse("1"))
chk("0000-1100 0101-0101 1111-1101 0011-01")
buf.XorAt(10, bitarray.NewOneFilled(20))
chk("0000-1100 0110-1010 0000-0010 1100-10")
buf.XorAt(4, bitarray.MustParse("1010-1010 1010-1010 1010"))
chk("0000-0110 1100-0000 1010-1000 1100-10")
buf.XorAt(10, bitarray.MustParse("0101-0101 0101"))
chk("0000-0110 1101-0101 1111-1100 1100-10")
chkpanic := func(off int, ba bitarray.BitArrayer) {
defer func() {
if recover() == nil {
t.Errorf("panic expected: off=%d, ba=%v.", off, ba)
t.Logf("buf: %s", buf.D())
}
}()
buf.XorAt(off, ba)
}
chkpanic(-1, bitarray.NewZeroFilled(1))
chkpanic(30, bitarray.NewZeroFilled(1))
chkpanic(8, bitarray.NewOneFilled(23))
}
func TestBuffer_LeadingZeros(t *testing.T) {
buf := bitarray.NewBuffer(30)
buf.PutBitArrayAt(6, bitarray.MustParse("0000-0111 0000-0111 0011"))
if n := buf.LeadingZeros(); n != 11 {
t.Errorf("unexpected: got %d, want 11", n)
}
buf.PutBitArrayAt(6, bitarray.MustParse("101"))
if n := buf.LeadingZeros(); n != 6 {
t.Errorf("unexpected: got %d, want 6", n)
}
buf.Resize(0, bitarray.AlignLeft)
if n := buf.LeadingZeros(); n != 0 {
t.Errorf("unexpected: got %d, want 0", n)
}
}
func TestBuffer_TrailingZeros(t *testing.T) {
buf := bitarray.NewBuffer(30)
buf.PutBitArrayAt(6, bitarray.MustParse("1111-0111 0000-0111"))
if n := buf.TrailingZeros(); n != 8 {
t.Errorf("unexpected: got %d, want 8", n)
}
buf.PutBitArrayAt(22, bitarray.MustParse("1010"))
if n := buf.TrailingZeros(); n != 5 {
t.Errorf("unexpected: got %d, want 5", n)
}
buf.Resize(0, bitarray.AlignLeft)
if n := buf.TrailingZeros(); n != 0 {
t.Errorf("unexpected: got %d, want 0", n)
}
}
func TestBuffer_OnesCount(t *testing.T) {
buf := bitarray.NewBuffer(30)
buf.PutBitArrayAt(6, bitarray.MustParse("1111-0111 0000-0111"))
if n := buf.OnesCount(); n != 10 {
t.Errorf("unexpected: got %d, want 10", n)
}
buf.PutBitArrayAt(2, bitarray.MustParse("1010"))
if n := buf.OnesCount(); n != 12 {
t.Errorf("unexpected: got %d, want 12", n)
}
buf.Resize(0, bitarray.AlignLeft)
if n := buf.OnesCount(); n != 0 {
t.Errorf("unexpected: got %d, want 0", n)
}
}