-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase91_1_test.go
442 lines (369 loc) · 10.5 KB
/
base91_1_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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Copyright (c) 2017-2020 Denis Subbotin, Philip Schlump,
// Nika Jones, Steven Allen, MoonFruit
// Copyright (c) 2022 Teal.Finance contributors
//
// This file is a modified copy from https://github.com/mr-tron/base58
// The source code has been adapted to support other bases.
// This file is now part of BaseXX under the terms of the MIT License.
// SPDX-License-Identifier: MIT
// See the LICENSE file or https://opensource.org/licenses/MIT
package base91
import (
"bytes"
"encoding/hex"
"math/rand"
"testing"
equimBase91 "github.com/Equim-chan/base91-go"
bproctorBase91 "github.com/bproctor/base91"
majestrateBase91 "github.com/majestrate/base91"
mtraverBase91 "github.com/mtraver/base91"
)
const (
nn = 1111
nnn = 1024 // power of two to speed up the % modulo
)
var bin [][]byte
const benchChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~'"
var benchEncoding = NewEncoding(benchChars)
var tstEncoding = NewEncoding(btcDigits[:Radix])
// func init() {
// // If we do not seed the prng - it will default to a seed of (1)
// // https://golang.org/pkg/math/rand/#Seed
// rand.Seed(time.Now().UTC().UnixNano())
// }
func setup[T string | []byte](data *[]T, encode func([]byte) T) {
if len(*data) > 0 {
return
}
setupBin()
// pre-make the test pairs, so it doesn't take up benchmark time...
*data = make([]T, 0, nnn)
for i := 0; i < nnn; i++ {
str := encode(bin[i])
*data = append(*data, str)
}
}
func setupBin() {
if len(bin) > 0 {
return
}
bin = make([][]byte, 0, nnn)
for i := 0; i < nnn; i++ {
b := make([]byte, rand.Intn(nn))
rand.Read(b)
bin = append(bin, b)
}
}
func randEncoding() *Encoding {
// Permutes [0, 127] and returns the first XX elements according to the BaseXX.
var randomness [128]byte
rand.Read(randomness[:])
var bts [128]byte
for i, r := range randomness {
j := int(r) % (i + 1)
bts[i] = bts[j]
bts[j] = byte(i)
}
return NewEncoding(string(bts[:Radix]))
}
var btcDigits = "" +
"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" +
" !0OIl()*+[\\]^_`{|}~;:#$<=>%&',-./?@"
func TestNewEncoding_InvalidTooShort(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic on alphabet being too short did not occur")
}
}()
_ = NewEncoding(btcDigits[:Radix-1]) // too short
}
func TestNewEncoding_InvalidTooLong(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic on alphabet being too long did not occur")
}
}()
_ = NewEncoding(btcDigits) // too long
}
func TestNewEncoding_InvalidNon127(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic on alphabet containing non-ascii chars did not occur")
}
}()
_ = NewEncoding("\xFF" + btcDigits[:Radix-1]) // good length
}
func TestNewEncoding_InvalidDup(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic on alphabet containing duplicate chars did not occur")
}
}()
_ = NewEncoding(btcDigits[:1] + btcDigits[:Radix-1]) // good length, but 1st char duplicated
}
func TestEncoding_EncodeToString_DecodeString(t *testing.T) {
for k := 0; k < 10; k++ {
testEncDecLoop(t, randEncoding())
}
testEncDecLoop(t, StdEncoding)
testEncDecLoop(t, tstEncoding)
}
func testEncDecLoop(t *testing.T, enc *Encoding) {
t.Helper()
for j := 1; j < 256; j++ {
b := make([]byte, j)
for i := 0; i < 100; i++ {
rand.Read(b)
fe := enc.EncodeToString(b)
fd, err := enc.DecodeString(fe)
if err != nil {
t.Errorf("fast error: %v", err)
}
if hex.EncodeToString(b) != hex.EncodeToString(fd) {
t.Errorf("decoding err: %s != %s", hex.EncodeToString(b), hex.EncodeToString(fd))
}
}
}
}
var sAscii []string
func TestEncoding_DecodeString(t *testing.T) {
setup(&sAscii, benchEncoding.EncodeToString)
for i := 0; i < nnn; i++ {
b, err := benchEncoding.DecodeString(sAscii[i])
if err != nil {
t.Fatalf("#%d err=%v ascii=%v", i, err, sAscii[i])
}
if !bytes.Equal(b, bin[i]) {
t.Errorf("#%d ascii: %v", i, sAscii[i])
t.Errorf("#%d want: %x", i, bin[i])
t.Errorf("#%d got : %x", i, b)
t.FailNow()
}
}
}
func BenchmarkEncoding_Encode(b *testing.B) {
setupBin()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchEncoding.Encode(bin[i%nnn])
}
}
func BenchmarkEncoding_EncodeToString(b *testing.B) {
setupBin()
b.ResetTimer()
for i := 0; i < b.N; i++ {
benchEncoding.EncodeToString(bin[i%nnn])
}
}
func BenchmarkEncoding_DecodeString(b *testing.B) {
setup(&sAscii, benchEncoding.EncodeToString)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = benchEncoding.DecodeString(sAscii[i%nnn])
}
}
var bproctorAscii [][]byte
/* Just to verify "github.com/bproctor/base91"
func TestBproctorBase91(t *testing.T) {
setup(&bproctorAscii, bproctorBase91.Encode)
for i := 0; i < nnn; i++ {
b := bproctorBase91.Decode(bproctorAscii[i])
if !bytes.Equal(b, bin[i]) {
t.Errorf("#%d ascii: %v", i, bproctorAscii[i])
t.Errorf("#%d want: %x", i, bin[i])
t.Errorf("#%d got : %x", i, b)
t.FailNow()
}
}
}*/
func BenchmarkBproctorBase91_Decode(b *testing.B) {
setup(&bproctorAscii, bproctorBase91.Encode)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = bproctorBase91.Decode(bproctorAscii[i%nnn])
}
}
var equimAscii []string
/* Just to verify "github.com/Equim-chan/base91-go"
func TestEquimBase91(t *testing.T) {
setup(&equimAscii, equimBase91.EncodeToString)
for i := 0; i < nnn; i++ {
b := equimBase91.DecodeString(equimAscii[i])
if !bytes.Equal(b, bin[i]) {
t.Errorf("#%d ascii: %v", i, equimAscii[i])
t.Errorf("#%d want: %x", i, bin[i])
t.Errorf("#%d got : %x", i, b)
t.FailNow()
}
}
}*/
func BenchmarkEquimBase91_Decode(b *testing.B) {
setup(&equimAscii, equimBase91.EncodeToString)
b.ResetTimer()
for i := 0; i < b.N; i++ {
equimBase91.DecodeString(equimAscii[i%nnn])
}
}
var majestrateAscii [][]byte
/* Just to verify "github.com/majestrate/base91"
func TestMajestrateBase91(t *testing.T) {
setup(&majestrateAscii, majestrateBase91.Encode)
for i := 0; i < nnn; i++ {
b, err := majestrateBase91.Decode(majestrateAscii[i])
if err != nil {
t.Fatalf("#%d err=%v ascii=%v", i, err, majestrateAscii[i])
}
if !bytes.Equal(b, bin[i]) {
t.Errorf("#%d ascii: %v", i, majestrateAscii[i])
t.Errorf("#%d want: %x", i, bin[i])
t.Errorf("#%d got : %x", i, b)
t.FailNow()
}
}
}*/
func BenchmarkMajestrateBase91_Decode(b *testing.B) {
setup(&majestrateAscii, majestrateBase91.Encode)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = majestrateBase91.Decode(majestrateAscii[i%nnn])
}
}
var (
mtraverEncoding = mtraverBase91.NewEncoding(benchChars)
mtraverAsciiS []string
mtraverAsciiB [][]byte
)
/* Just to verify "github.com/mtraver/base91"
func TestMtraverBase91(t *testing.T) {
setup(&mtraverAsciiS, mtraverEncoding.EncodeToString)
if len(mtraverAsciiS) == 0 {
t.Fatal("len=0")
}
setup(&mtraverAsciiB, func(src []byte) []byte {
dst := make([]byte, 2*len(src))
n := mtraverEncoding.Encode(dst, src)
return dst[:n]
})
buf := make([]byte, 2*nn)
for i := 0; i < nnn; i++ {
if mtraverAsciiS[i] != string(mtraverAsciiB[i]) {
t.Errorf("#%d asciiS: %d %q", i, len(mtraverAsciiS[i]), mtraverAsciiS[i])
t.Fatalf("#%d asciiB: %d %q", i, len(mtraverAsciiB[i]), mtraverAsciiB[i])
}
b, err := mtraverEncoding.DecodeString(mtraverAsciiS[i])
if err != nil {
t.Fatalf("S #%d err=%v ascii=%v", i, err, mtraverAsciiS[i])
}
if !bytes.Equal(b, bin[i]) {
t.Errorf("S #%d len=%d ascii: %v", i, len(mtraverAsciiS[i]), mtraverAsciiS[i])
t.Errorf("S #%d len=%d want: %x", i, len(bin[i]), bin[i])
t.Errorf("S #%d len=%d got : %x", i, len(b), b)
t.FailNow()
}
n, err := mtraverEncoding.Decode(buf, mtraverAsciiB[i])
b = buf[:n]
if err != nil {
t.Fatalf("B #%d err=%v ascii=%v", i, err, mtraverAsciiS[i])
}
if !bytes.Equal(b, bin[i]) {
t.Errorf("B #%d len=%d ascii: %v", i, len(mtraverAsciiS[i]), mtraverAsciiS[i])
t.Errorf("B #%d len=%d want: %x", i, len(bin[i]), bin[i])
t.Errorf("B #%d len=%d got : %x", i, len(b), b)
t.FailNow()
}
}
}*/
func BenchmarkMtraverBase91_EncodeToString(b *testing.B) {
setupBin()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = mtraverEncoding.EncodeToString(bin[i%nnn])
}
}
func BenchmarkMtraverBase91_Encode(b *testing.B) {
setupBin()
b.ResetTimer()
buf := make([]byte, 2*nn)
for i := 0; i < b.N; i++ {
_ = mtraverEncoding.Encode(buf, bin[i%nnn])
}
}
func BenchmarkMtraverBase91_DecodeString(b *testing.B) {
setup(&mtraverAsciiS, mtraverEncoding.EncodeToString)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = mtraverEncoding.DecodeString(mtraverAsciiS[i%nnn])
}
}
func BenchmarkMtraverBase91_Decode(b *testing.B) {
setup(&mtraverAsciiB, func(src []byte) []byte {
dst := make([]byte, 2*len(src))
n := mtraverEncoding.Encode(dst, src)
return dst[:n]
})
b.ResetTimer()
buf := make([]byte, 2*nn)
for i := 0; i < b.N; i++ {
_, _ = mtraverEncoding.Decode(buf, mtraverAsciiB[i%nnn])
}
}
/* HAVING BUGS:
var acAscii [][]byte
func TestBDecodeACBase91(t *testing.T) {
setup(&acAscii, acBase91.Encode)
for i := 0; i < n; i++ {
b := acBase91.Decode(acAscii[i])
if !bytes.Equal(b, bin[i]) {
t.Errorf("#%d ascii: %v", i, string(acAscii[i]))
t.Errorf("#%d want: %x", i, bin[i])
t.Errorf("#%d got : %x", i, b)
t.FailNow()
}
}
}
func BenchmarkEncodeACBase91(b *testing.B) {
setupBin()
b.ResetTimer()
for i := 0; i < b.N; i++ {
acBase91.Encode(bin[i%n])
}
}
func BenchmarkDecodeACBase91(b *testing.B) {
setup(&dataValues, benchEncoding)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = acBase91.Decode(dataValues[i%n].bAscii)
}
}
var breezechenEncoding = breezechenBase91.StdEncoding
var breezechenAscii [][]byte
func TestBDecodeBreezechenBase91(t *testing.T) {
a := make([]byte, nn*2)
setup(&breezechenAscii, func(b []byte) []byte {
n := breezechenEncoding.Encode(a, b)
var out []byte
copy(out, a[:n])
return out
})
b := make([]byte, nn*2)
for i := 0; i < nnn; i++ {
n, err := breezechenEncoding.Decode(b, breezechenAscii[i])
if err != nil {
t.Fatalf("#%d err=%v ascii=%v", i, err, string(breezechenAscii[i]))
}
if bytes.Compare(b[:n], bin[i]) != 0 {
t.Errorf("#%d ascii: %v", i, breezechenAscii[i])
t.Errorf("#%d want: %x", i, bin[i])
t.Errorf("#%d got : %x", i, b[:n])
t.FailNow()
}
}
}
func BenchmarkDecodeBreezechenBase91(b *testing.B) {
setup(&dataValues, benchEncoding)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = breezechenEncoding.Decode(nil, dataValues[i%n].bAscii)
}
}
*/