-
Notifications
You must be signed in to change notification settings - Fork 5
/
base62_test.go
206 lines (182 loc) · 4.68 KB
/
base62_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
package base62
import (
"bytes"
"crypto/rand"
mathrand "math/rand"
"strings"
"testing"
)
func Test_EncodeDecode(t *testing.T) {
src := []byte("Hello, 世界!")
dst := Encode(src)
got, err := Decode(dst)
if err != nil {
t.Fatalf("failed decode, err = %v", err)
}
if !bytes.Equal(src, got) {
t.Fatalf("failed decode, got = %v, want = %v", got, src)
}
dstStr := EncodeToString(src)
got, _ = DecodeString(dstStr)
if !bytes.Equal(src, got) {
t.Fatalf("failed decode string, got = %v, want = %v", got, src)
}
}
func Test_EncodeDecode_Zeros(t *testing.T) {
for i := 0; i < 1000; i++ {
src := make([]byte, i)
dst := StdEncoding._encodeV1(src)
got, err := Decode(dst)
if err != nil {
t.Fatalf("failed decode: err = %v", err)
}
if !bytes.Equal(src, got) {
t.Fatalf("failed decode, got = %v, want = %v", got, src)
}
// Make sure the new implementation is compatible with the old.
v2Dst := StdEncoding._encodeV2(src)
if !bytes.Equal(dst, v2Dst) {
t.Logf("src= %v\n v1= %v\n v2= %v", src, dst, v2Dst)
t.Fatalf("encode new implementation not equal to v1")
}
}
}
func Test_EncodeDecode_0xFF(t *testing.T) {
for i := 0; i < 1000; i++ {
src := make([]byte, i)
for i := range src {
src[i] = 0xff
}
dst := StdEncoding._encodeV1(src)
got, err := Decode(dst)
if err != nil {
t.Fatalf("failed decode: err = %v", err)
}
if !bytes.Equal(src, got) {
t.Fatalf("failed decode, got = %v, want = %v", got, src)
}
// Make sure the new implementation is compatible with the old.
v2Dst := StdEncoding._encodeV2(src)
if !bytes.Equal(dst, v2Dst) {
t.Logf("src= %v\n v1= %v\n v2= %v", src, dst, v2Dst)
t.Fatalf("encode new implementation not equal to v1")
}
}
}
func Test_EncodeDecode_RandomBytes(t *testing.T) {
for i := 0; i < 1000000; i++ {
src := make([]byte, 32+mathrand.Intn(32))
_, _ = rand.Read(src)
dst := StdEncoding._encodeV1(src)
got, err := Decode(dst)
if err != nil {
t.Fatalf("failed decode, err = %v", err)
}
if !bytes.Equal(src, got) {
t.Fatalf("failed decode, got = %v, want = %v", got, src)
}
// Make sure the new implementation is compatible with the old.
v2Dst := StdEncoding._encodeV2(src)
if !bytes.Equal(dst, v2Dst) {
t.Logf("src= %v\n v1= %v\n v2= %v", src, dst, v2Dst)
t.Fatalf("encode new implementation not equal to v1")
}
}
}
func Test_EncodeToBuf(t *testing.T) {
buf := make([]byte, 0, 1000)
for i := 0; i < 10000; i++ {
src := make([]byte, 32+mathrand.Intn(100))
_, _ = rand.Read(src)
want := Encode(src)
got1 := EncodeToBuf(make([]byte, 0, 2), src)
if !bytes.Equal(want, got1) {
t.Fatal("incorrect result from EncodeToBuf")
}
got2 := EncodeToBuf(buf, src)
if !bytes.Equal(want, got2) {
t.Fatal("incorrect result from EncodeToBuf")
}
}
}
func TestDecodeToBuf(t *testing.T) {
buf := make([]byte, 0, 1000)
for i := 0; i < 10000; i++ {
src := make([]byte, 32+mathrand.Intn(100))
_, _ = rand.Read(src)
encoded := Encode(src)
got1, err := DecodeToBuf(make([]byte, 0, 2), encoded)
if err != nil {
t.Fatalf("failed DecodeToBuf, err = %v", err)
}
if !bytes.Equal(src, got1) {
t.Fatalf("incorrect result from DecodeToBuf, encoded = %v", encoded)
}
got2, err := DecodeToBuf(buf, encoded)
if err != nil {
t.Fatalf("failed DecodeToBuf, err = %v", err)
}
if !bytes.Equal(src, got2) {
t.Fatalf("incorrect result from DecodeToBuf, encoded = %v", encoded)
}
}
}
// ----------
func Test_encoder_next(t *testing.T) {
src := []byte{123, 234, 255}
enc := newEncoder(src)
//for _, w := range src {
// fmt.Printf("%08b", w)
//}
//fmt.Println()
x, hasMore := enc.next()
for {
_ = x
if !hasMore {
break
}
x, hasMore = enc.next()
}
}
// ----------
func Test_NewEncoding_panic(t *testing.T) {
func() {
encoder := "abcdef"
defer func() {
if r := recover(); r == nil {
t.Errorf("NewEncoding did not panic with encoder %q", encoder)
}
}()
_ = NewEncoding(encoder)
}()
func() {
encoder := []byte(encodeStd)
encoder[1] = '\n'
defer func() {
if r := recover(); r == nil {
t.Error("NewEncoding did not panic with encoder contains \\n")
}
}()
_ = NewEncoding(string(encoder))
}()
func() {
encoder := []byte(encodeStd)
encoder[1] = '\r'
defer func() {
if r := recover(); r == nil {
t.Error("NewEncoding did not panic with encoder contains \\r")
}
}()
_ = NewEncoding(string(encoder))
}()
}
func Test_Decode_CorruptInputError(t *testing.T) {
src := make([]byte, 256)
for i := range src {
src[i] = byte(i)
}
_, err := StdEncoding.Decode(src)
if err == nil || !strings.Contains(err.Error(), "illegal base62 data at input byte") {
t.Fatal("decoding invalid data did not return CorruptInputError")
}
}