-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitstring_test.go
425 lines (377 loc) · 12.6 KB
/
bitstring_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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package bitstring
import (
"fmt"
"math/rand"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNew(t *testing.T) {
bs := New(100)
assert.Equal(t, 100, bs.Len())
for i := 0; i < bs.Len(); i++ {
assert.False(t, bs.Bit(i))
}
}
func TestRandom(t *testing.T) {
rng := rand.New(rand.NewSource(99))
assert.Equal(t, Random(100, rng).Len(), 100)
}
func TestSetBit(t *testing.T) {
bs := New(69)
bs.SetBit(1)
bs.SetBit(4)
assert.False(t, bs.Bit(0))
assert.True(t, bs.Bit(1))
assert.False(t, bs.Bit(2))
assert.False(t, bs.Bit(3))
assert.True(t, bs.Bit(4))
bs.ClearBit(4)
assert.False(t, bs.Bit(4))
}
func TestFlipBit(t *testing.T) {
bs := New(69)
bs.FlipBit(2)
assert.True(t, bs.Bit(2))
bs.FlipBit(2)
assert.False(t, bs.Bit(2))
bs.FlipBit(67)
assert.True(t, bs.Bit(67))
bs.FlipBit(67)
assert.False(t, bs.Bit(67))
}
func TestString(t *testing.T) {
tests := []struct {
name string
str string
valid bool
}{
// valid cases
{"only 0s", "0000", true},
{"only 1s", "1111111", true},
{"mixed 0s and 1s", "1000111011", true},
{"leading 0", "00001000111011", true},
{"empty string", "", true},
{"valid long", "111010101110101100010100101000101000011010100010111100011101001010101110101011010101101011100000000110110101011110101010101", true},
// error cases
{"with spaces", "11 ", false},
{"invalid ascii chars", "0101012", false},
{"non ascii chars", "10日本", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := NewFromString(tt.str)
if !tt.valid {
assert.Nil(t, got)
assert.Error(t, err)
return
}
assert.NotNil(t, got)
assert.NoError(t, err)
// Convert back to string
assert.Equal(t, tt.str, got.String())
})
}
}
func TestReverse(t *testing.T) {
tests := []string{
"0000000000000000000000000000000000000000000000000000000000000001",
"00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001",
"1100010010111100010010111100010010110010101000101101001011111110011010010000100101101011100110100100001001011010100001101111010011000100101111000100101111000100101100101010001011010010111111100110100100001001011010111001101001000010010110101000011011110100",
"00000001",
"11110000000000000000000000000000000000000000000000000000000000000111",
"10000000000000000000000000000000000000000000000000000000000000011111",
fmt.Sprintf("%s000000000000000000", strings.Repeat("1", 1029)),
"000000000000000000111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
}
for _, str := range tests {
t.Run("", func(t *testing.T) {
bs, _ := NewFromString(str)
ones := bs.OnesCount()
bs.Reverse()
assert.Equal(t, string(reverseBytes([]byte(str))), bs.String())
assert.Equal(t, ones, bs.OnesCount())
})
}
}
func TestOnesCount(t *testing.T) {
bs := New(65)
assert.Zero(t, bs.OnesCount())
bs.SetBit(0)
bs.SetBit(31)
bs.SetBit(32)
bs.SetBit(33)
bs.SetBit(63)
bs.SetBit(64)
setBits := bs.OnesCount()
assert.EqualValues(t, 6, setBits)
}
func TestZeroesCount(t *testing.T) {
bs := New(12)
assert.EqualValues(t, 12, bs.ZeroesCount())
bs.SetBit(0)
bs.SetBit(5)
bs.SetBit(6)
bs.SetBit(9)
bs.SetBit(10)
setBits := bs.ZeroesCount()
assert.EqualValues(t, 7, setBits)
}
func TestClone(t *testing.T) {
rng := rand.New(rand.NewSource(99))
bs := Random(2000, rng)
bs.SetBit(3)
bs.SetBit(7)
bs.SetBit(8)
cpy := bs.Clone()
equalbits(t, cpy, bs)
}
func TestCopy(t *testing.T) {
rng := rand.New(rand.NewSource(99))
bs := Random(2000, rng)
bs.SetBit(3)
bs.SetBit(7)
bs.SetBit(1898)
// zero-value destination
var cpy Bitstring
Copy(&cpy, bs)
equalbits(t, bs, &cpy)
// smaller destination
cpy2 := New(1)
Copy(cpy2, bs)
equalbits(t, bs, cpy2)
// same size destination
cpy3 := New(2000)
Copy(cpy3, bs)
equalbits(t, bs, cpy3)
// larger destination
cpy4 := New(3000)
Copy(cpy4, bs)
equalbits(t, bs, cpy4)
}
func TestEquals(t *testing.T) {
org := New(10)
org.SetBit(2)
org.SetBit(5)
org.SetBit(8)
assert.True(t, org.Equals(org))
assert.False(t, org.Equals(nil))
assert.False(t, org.Equals(&Bitstring{}))
clone := org.Clone()
assert.Truef(t, clone.Equals(org), "different bitstrings, clone=%s, org=%s", clone, org)
clone.FlipBit(0)
assert.Falsef(t, clone.Equals(org), "same bitstrings, clone=%s, org=%s", clone, org)
// Bitstrings of different lengths but with the same bits set should not be equal.
bs2 := New(9)
bs2.SetBit(2)
bs2.SetBit(5)
bs2.SetBit(8)
assert.False(t, bs2.Equals(org))
}
func trimLeadingZeroes(s string) string {
last := len(s) - 1
return strings.TrimLeft(s[:last], "0") + string(s[last])
}
func TestBig(t *testing.T) {
nums := []string{
"00101",
"00101110",
"001011100010111000101110001011100010111000101110001011100010111",
"0010111000101110001011100010111000101110001011100010111000101110",
"00101110001011100010111000101110001011100010111000101110001011101",
"001011100010111000101110001011100010111000101110001011100010111001010101000111101010101000111101",
"0010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111",
"00101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110",
"001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011101",
"00101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111",
"001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110",
"0010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011100010111000101110001011101",
}
for _, num := range nums {
t.Run(fmt.Sprintf("%d-bit", len(num)), func(t *testing.T) {
// Create a bitstring, converts it to big.Int and format it as a
// binary number, compare the numbers with the original.
bs, _ := NewFromString(num)
bi := bs.BigInt()
sbig := fmt.Sprintf("%b", bi)
assert.Equal(t, trimLeadingZeroes(num), sbig)
// Create a bitstring from the binary string representation, create
// a Bitstring from it and compare the significant bits.
bs2 := NewFromBig(bi)
// Remove leading zeroes since a big int doesn't have leading zeroes.
got := bs.String()[bs.LeadingZeroes():]
assert.Equal(t, got, bs2.String())
})
}
}
func TestLeadingTrailingZeroes(t *testing.T) {
tests := []struct {
name string
s string
leading, trailing int
}{
{
name: "less than 64 bits",
s: "0",
leading: 1,
trailing: 1,
},
{
name: "less than 64 bits",
s: "1",
leading: 0,
trailing: 0,
},
{
name: "less than 64 bits",
s: "10",
leading: 0,
trailing: 1,
},
{
name: "less than 64 bits",
s: "01",
leading: 1,
trailing: 0,
},
{
name: "less than 64 bits",
s: "00",
leading: 2,
trailing: 2,
},
{
name: "less than 64 bits",
s: "11",
leading: 0,
trailing: 0,
},
{
name: "exactly 64 bits",
s: "1111111111111111111111111111111111111111111111111111111111111111",
leading: 0,
trailing: 0,
},
{
name: "exactly 64 bits",
s: "0111111111111111111111111111111111111111111111111111111111111111",
leading: 1,
trailing: 0,
},
{
name: "exactly 64 bits",
s: "1111111111111111111111111111111111111111111111111111111111111110",
leading: 0,
trailing: 1,
},
{
name: "exactly 64 bits",
s: "1111111111111111111111111111111111111111111111111111111111111111",
leading: 0,
trailing: 0,
},
{
name: "exactly 64 bits",
s: "0000000000000000000000000000000000000000000000000000000000000000",
leading: 64,
trailing: 64,
},
{
name: "128 bits",
s: "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
leading: 0,
trailing: 0,
},
{
name: "128 bits",
s: "01111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
leading: 1,
trailing: 0,
},
{
name: "128 bits",
s: "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110",
leading: 0,
trailing: 1,
},
{
name: "128 bits",
s: "00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111",
leading: 64,
trailing: 0,
},
{
name: "128 bits",
s: "11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
leading: 0,
trailing: 0,
},
{
name: "128 bits",
s: "00000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111",
leading: 64,
trailing: 0,
},
{
name: "128 bits",
s: "11111111111111111111111111111111111111111111111111111111111111110000000000000000000000000000000000000000000000000000000000000000",
leading: 0,
trailing: 64,
},
{
name: "129 bits",
s: "111111111111111111111111111111111111111111111111111111111111111100000000000000000000000000000000000000000000000000000000000000000",
leading: 0,
trailing: 65,
},
{
name: "129 bits",
s: "000000000000000000000000000000000000000000000000000000000000000001111111111111111111111111111111111111111111111111111111111111111",
leading: 65,
trailing: 0,
},
{
name: "129 bits",
s: "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000111000",
leading: 123,
trailing: 3,
},
{
name: "129 bits",
s: "000111000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
leading: 3,
trailing: 123,
},
{
name: "129 bits",
s: "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111",
leading: 0,
trailing: 0,
},
{
name: "259 bits",
s: "0010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
leading: 2,
trailing: 136,
},
}
for _, tt := range tests {
bs, _ := NewFromString(tt.s)
t.Run(tt.name, func(t *testing.T) {
if got := bs.LeadingZeroes(); got != tt.leading {
t.Errorf("%q leading zeroes = %d, want %d", tt.s, got, tt.leading)
}
if got := bs.TrailingZeroes(); got != tt.trailing {
t.Errorf("%q leading zeroes = %d, want %d", tt.s, got, tt.trailing)
}
// After reversing the bit string, leading and trailing zeroes must be swapped.
bs.Reverse()
if got := bs.LeadingZeroes(); got != tt.trailing {
t.Errorf("reversed %q leading zeroes = %d, want %d", tt.s, got, tt.trailing)
}
if got := bs.TrailingZeroes(); got != tt.leading {
t.Errorf("reversed %q leading zeroes = %d, want %d", tt.s, got, tt.leading)
}
})
}
}