-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbits_test.go
240 lines (236 loc) · 5.86 KB
/
bits_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
package varint
import (
"fmt"
"math/big"
"testing"
)
func TestBitsNew(t *testing.T) {
test("Combined", t, func(th h) {
table := map[string]struct {
blen int
bytes []uint
n uint
big *big.Int
s string
base int
bits Bits
}{
"nil inputs should produce empty bits": {
blen: 0,
bytes: nil,
n: 0,
big: nil,
s: "0",
base: 3,
bits: Bits{0},
},
"empty inputs should produce empty bits": {
blen: 0,
bytes: []uint{},
n: 0,
big: big.NewInt(0),
s: "0",
base: 3,
bits: Bits{0},
},
"empty len inputs should produce empty bits": {
blen: 0,
bytes: []uint{100},
n: 0,
big: new(big.Int),
s: "0",
base: 4,
bits: Bits{0},
},
"non empty inputs should produce non empty bits": {
blen: 7,
bytes: []uint{100},
n: 100,
big: big.NewInt(100),
s: "100",
base: 10,
bits: Bits{7, 100},
},
"non empty long inputs should produce non empty bits": {
blen: 86,
bytes: []uint{0x1, 0x3B0000},
n: 1,
big: big.NewInt(0).SetBits([]big.Word{0x1, 0x3B0000}),
s: "3B00000000000000000001",
base: 16,
bits: Bits{86, 0x1, 0x3B0000},
},
}
for tname, tcase := range table {
test(tname, th.T, func(h h) {
b := NewBits(tcase.blen, tcase.bytes)
bn := NewBitsUint(tcase.n)
bb := NewBitsBits(tcase.blen, tcase.bits)
bbig := NewBitsBigInt(tcase.big)
brnd := NewBitsRand(tcase.blen, rnd)
bs := NewBitsString(tcase.s, tcase.base)
h.Equal(tcase.bits, b)
h.Equal(tcase.bits.Uint(), bn.Uint())
h.Equal(tcase.bits, bb)
h.Equal(tcase.bits, bbig)
h.Equal(tcase.bits.BitLen(), brnd.BitLen())
h.Equal(tcase.bits, bs)
})
}
})
test("String", t, func(th h) {
table := map[string]struct {
s string
base int
bits Bits
}{
"should produce nil bits for empty string": {
s: "",
base: -1,
},
"should produce nil bits for unsupported characters": {
s: "ABC@EDF",
base: 63,
},
"should produce nil bits for characters out of provided base": {
s: "ABC",
base: 2,
},
"should produce expected bits for long valid string with _": {
s: "abc_def_ghi_jkl_mno_pqr_stu_vwx_yz0",
base: 36,
bits: NewBits(138, []uint{0x393FAFC0785F4B4C, 0x0FDE01C8C1446C9E, 0x372}),
},
}
for tname, tcase := range table {
test(tname, th.T, func(h h) {
h.Equal(tcase.bits, NewBitsString(tcase.s, tcase.base))
})
}
})
}
func TestBits(t *testing.T) {
table := map[string]struct {
bits Bits
blen int
bytes []uint
empty bool
n uint
big *big.Int
s string
base int
bs string
}{
"nil bits should return empty results": {
bits: nil,
bytes: []uint{0},
empty: true,
n: 0,
big: big.NewInt(0),
s: "[0]{0X0}",
base: 0,
bs: "0",
},
"implicit empty bits should return empty results": {
bits: []uint{0},
bytes: []uint{0},
empty: true,
n: 0,
big: big.NewInt(0),
s: "[0]{0X0}",
base: 100,
bs: "0",
},
"explicit empty bits should return empty results": {
bits: []uint{0, 0},
bytes: []uint{0},
empty: true,
n: 0,
big: big.NewInt(0),
s: "[0]{0X0}",
base: -1,
bs: "0",
},
"explicit empty bits should return empty results, even if bytes not empty": {
bits: []uint{0, 10},
bytes: []uint{0},
empty: true,
n: 0,
big: big.NewInt(0),
s: "[0]{0X0}",
base: 0,
bs: "0",
},
"non empty single small word bits should return non empty results": {
bits: []uint{4, 0xF},
blen: 4,
bytes: []uint{0xF},
empty: false,
n: 0xF,
big: big.NewInt(0xF),
s: "[4]{0XF}",
base: 32,
bs: "f",
},
"non empty single word bits should return non empty results": {
bits: []uint{24, 0xFF},
blen: 24,
bytes: []uint{0xFF},
empty: false,
n: 0xFF,
big: big.NewInt(0xFF),
s: "[24]{0XFF}",
base: 32,
bs: "7v",
},
"non empty multi words bits should return non empty results": {
bits: []uint{128, 0xF, 0xAABBCCDD},
blen: 128,
bytes: []uint{0xF, 0xAABBCCDD},
empty: false,
n: 0xF,
big: big.NewInt(0).SetBits([]big.Word{0xF, 0xAABBCCDD}),
s: "[128]{0XAABBCCDD000000000000000F}",
base: 16,
bs: "aabbccdd000000000000000f",
},
}
for tname, tcase := range table {
test(tname, t, func(h h) {
h.Equal(tcase.blen, tcase.bits.BitLen())
h.Equal(tcase.bytes, tcase.bits.Bytes())
h.Equal(tcase.empty, tcase.bits.Empty())
h.Equal(tcase.n, tcase.bits.Uint())
h.Equal(tcase.big, tcase.bits.BigInt())
h.Equal(tcase.s, tcase.bits.String())
h.Equal(tcase.bs, string(tcase.bits.To(tcase.base)))
})
}
}
func FuzzBitsFmt(f *testing.F) {
const base = 62
fuzz(f, func(h h, b62 string) {
// Initialize fuzz original bits from b62 string,
// then do the same for bigint and compare resulting bits.
// Then compare their string formats as well.
// After that get b62 string back from bits, initialize
// another bits and compare them with original again.
b, ok := big.NewInt(0).SetString(b62, base)
bits := NewBitsString(b62, base)
// Skip on empty bits or bigint error.
if bits.Empty() || !ok {
return
}
h.Equal(bits, NewBitsBigInt(b))
h.Equal(fmt.Sprintf("%d", bits), fmt.Sprintf("%d", b))
h.Equal(fmt.Sprintf("%#X", bits), fmt.Sprintf("%#X", b))
h.Equal(fmt.Sprintf("%0b", bits), fmt.Sprintf("%0b", b))
h.Equal(fmt.Sprintf("%#b", bits), fmt.Sprintf("%#b", b))
h.Equal(fmt.Sprintf("%#o", bits), fmt.Sprintf("%#o", b))
h.Equal(fmt.Sprintf("%#x", bits), fmt.Sprintf("%#x", b))
h.Equal(fmt.Sprintf("%100O", bits), fmt.Sprintf("%100O", b))
h.Equal(fmt.Sprintf("%010x", bits), fmt.Sprintf("%010x", b))
h.Equal(bits.String(), fmt.Sprintf("[%d]{%#X}", b.BitLen(), b))
h.Equal(bits, NewBitsString(string(bits.To(base)), base))
})
}