-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathbase58_test.go
More file actions
165 lines (136 loc) · 3.52 KB
/
base58_test.go
File metadata and controls
165 lines (136 loc) · 3.52 KB
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
package base58
import (
"encoding/hex"
"math/rand"
"testing"
"time"
)
type testValues struct {
dec []byte
enc string
}
var n = 5000000
var testPairs = make([]testValues, 0, n)
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 initTestPairs() {
if len(testPairs) > 0 {
return
}
// pre-make the test pairs, so it doesn't take up benchmark time...
for i := 0; i < n; i++ {
data := make([]byte, 32)
rand.Read(data)
testPairs = append(testPairs, testValues{dec: data, enc: FastBase58Encoding(data)})
}
}
func randAlphabet() *Alphabet {
// Permutes [0, 127] and returns the first 58 elements.
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 NewAlphabet(string(bts[:58]))
}
var btcDigits = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
func TestInvalidAlphabetTooShort(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic on alphabet being too short did not occur")
}
}()
_ = NewAlphabet(btcDigits[1:])
}
func TestInvalidAlphabetTooLong(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic on alphabet being too long did not occur")
}
}()
_ = NewAlphabet("0" + btcDigits)
}
func TestInvalidAlphabetNon127(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic on alphabet containing non-ascii chars did not occur")
}
}()
_ = NewAlphabet("\xFF" + btcDigits[1:])
}
func TestInvalidAlphabetDup(t *testing.T) {
defer func() {
if r := recover(); r == nil {
t.Errorf("Expected panic on alphabet containing duplicate chars did not occur")
}
}()
_ = NewAlphabet("z" + btcDigits[1:])
}
func TestFastEqTrivialEncodingAndDecoding(t *testing.T) {
for k := 0; k < 10; k++ {
testEncDecLoop(t, randAlphabet())
}
testEncDecLoop(t, BTCAlphabet)
testEncDecLoop(t, FlickrAlphabet)
}
func testEncDecLoop(t *testing.T, alph *Alphabet) {
for j := 1; j < 256; j++ {
var b = make([]byte, j)
for i := 0; i < 100; i++ {
rand.Read(b)
fe := FastBase58EncodingAlphabet(b, alph)
te := TrivialBase58EncodingAlphabet(b, alph)
if fe != te {
t.Errorf("encoding err: %#v", hex.EncodeToString(b))
}
fd, ferr := FastBase58DecodingAlphabet(fe, alph)
if ferr != nil {
t.Errorf("fast error: %v", ferr)
}
td, terr := TrivialBase58DecodingAlphabet(te, alph)
if terr != nil {
t.Errorf("trivial error: %v", terr)
}
if hex.EncodeToString(b) != hex.EncodeToString(td) {
t.Errorf("decoding err: %s != %s", hex.EncodeToString(b), hex.EncodeToString(td))
}
if hex.EncodeToString(b) != hex.EncodeToString(fd) {
t.Errorf("decoding err: %s != %s", hex.EncodeToString(b), hex.EncodeToString(fd))
}
}
}
}
func BenchmarkTrivialBase58Encoding(b *testing.B) {
initTestPairs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
TrivialBase58Encoding([]byte(testPairs[i].dec))
}
}
func BenchmarkFastBase58Encoding(b *testing.B) {
initTestPairs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
FastBase58Encoding(testPairs[i].dec)
}
}
func BenchmarkTrivialBase58Decoding(b *testing.B) {
initTestPairs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
TrivialBase58Decoding(testPairs[i].enc)
}
}
func BenchmarkFastBase58Decoding(b *testing.B) {
initTestPairs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
FastBase58Decoding(testPairs[i].enc)
}
}