-
Notifications
You must be signed in to change notification settings - Fork 176
/
base62_test.go
245 lines (198 loc) · 5.27 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
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
package ksuid
import (
"bytes"
"sort"
"strings"
"testing"
)
func TestBase10ToBase62AndBack(t *testing.T) {
number := []byte{1, 2, 3, 4}
encoded := base2base(number, 10, 62)
decoded := base2base(encoded, 62, 10)
if bytes.Compare(number, decoded) != 0 {
t.Fatal(number, " != ", decoded)
}
}
func TestBase256ToBase62AndBack(t *testing.T) {
number := []byte{255, 254, 253, 251}
encoded := base2base(number, 256, 62)
decoded := base2base(encoded, 62, 256)
if bytes.Compare(number, decoded) != 0 {
t.Fatal(number, " != ", decoded)
}
}
func TestEncodeAndDecodeBase62(t *testing.T) {
helloWorld := []byte("hello world")
encoded := encodeBase62(helloWorld)
decoded := decodeBase62(encoded)
if len(encoded) < len(helloWorld) {
t.Fatal("length of encoded base62 string", encoded, "should be >= than raw bytes!")
}
if bytes.Compare(helloWorld, decoded) != 0 {
t.Fatal(decoded, " != ", helloWorld)
}
}
func TestLexographicOrdering(t *testing.T) {
unsortedStrings := make([]string, 256)
for i := 0; i < 256; i++ {
s := string(encodeBase62([]byte{0, byte(i)}))
unsortedStrings[i] = strings.Repeat("0", 2-len(s)) + s
}
if !sort.StringsAreSorted(unsortedStrings) {
sortedStrings := make([]string, len(unsortedStrings))
for i, s := range unsortedStrings {
sortedStrings[i] = s
}
sort.Strings(sortedStrings)
t.Fatal("base62 encoder does not produce lexographically sorted output.",
"expected:", sortedStrings,
"actual:", unsortedStrings)
}
}
func TestBase62Value(t *testing.T) {
s := base62Characters
for i := range s {
v := int(base62Value(s[i]))
if v != i {
t.Error("bad value:")
t.Log("<<<", i)
t.Log(">>>", v)
}
}
}
func TestFastAppendEncodeBase62(t *testing.T) {
for i := 0; i != 1000; i++ {
id := New()
b0 := id[:]
b1 := appendEncodeBase62(nil, b0)
b2 := fastAppendEncodeBase62(nil, b0)
s1 := string(leftpad(b1, '0', stringEncodedLength))
s2 := string(b2)
if s1 != s2 {
t.Error("bad base62 representation of", id)
t.Log("<<<", s1, len(s1))
t.Log(">>>", s2, len(s2))
}
}
}
func TestFastAppendDecodeBase62(t *testing.T) {
for i := 0; i != 1000; i++ {
id := New()
b0 := leftpad(encodeBase62(id[:]), '0', stringEncodedLength)
b1 := appendDecodeBase62(nil, []byte(string(b0))) // because it modifies the input buffer
b2 := fastAppendDecodeBase62(nil, b0)
if !bytes.Equal(leftpad(b1, 0, byteLength), b2) {
t.Error("bad binary representation of", string(b0))
t.Log("<<<", b1)
t.Log(">>>", b2)
}
}
}
func BenchmarkAppendEncodeBase62(b *testing.B) {
a := [stringEncodedLength]byte{}
id := New()
for i := 0; i != b.N; i++ {
appendEncodeBase62(a[:0], id[:])
}
}
func BenchmarkAppendFastEncodeBase62(b *testing.B) {
a := [stringEncodedLength]byte{}
id := New()
for i := 0; i != b.N; i++ {
fastAppendEncodeBase62(a[:0], id[:])
}
}
func BenchmarkAppendDecodeBase62(b *testing.B) {
a := [byteLength]byte{}
id := []byte(New().String())
for i := 0; i != b.N; i++ {
b := [stringEncodedLength]byte{}
copy(b[:], id)
appendDecodeBase62(a[:0], b[:])
}
}
func BenchmarkAppendFastDecodeBase62(b *testing.B) {
a := [byteLength]byte{}
id := []byte(New().String())
for i := 0; i != b.N; i++ {
fastAppendDecodeBase62(a[:0], id)
}
}
// The functions bellow were the initial implementation of the base conversion
// algorithms, they were replaced by optimized versions later on. We keep them
// in the test files as a reference to ensure compatibility between the generic
// and optimized implementations.
func appendBase2Base(dst []byte, src []byte, inBase int, outBase int) []byte {
off := len(dst)
bs := src[:]
bq := [stringEncodedLength]byte{}
for len(bs) > 0 {
length := len(bs)
quotient := bq[:0]
remainder := 0
for i := 0; i != length; i++ {
acc := int(bs[i]) + remainder*inBase
d := acc/outBase | 0
remainder = acc % outBase
if len(quotient) > 0 || d > 0 {
quotient = append(quotient, byte(d))
}
}
// Appends in reverse order, the byte slice gets reversed before it's
// returned by the function.
dst = append(dst, byte(remainder))
bs = quotient
}
reverse(dst[off:])
return dst
}
func base2base(src []byte, inBase int, outBase int) []byte {
return appendBase2Base(nil, src, inBase, outBase)
}
func appendEncodeBase62(dst []byte, src []byte) []byte {
off := len(dst)
dst = appendBase2Base(dst, src, 256, 62)
for i, c := range dst[off:] {
dst[off+i] = base62Characters[c]
}
return dst
}
func encodeBase62(in []byte) []byte {
return appendEncodeBase62(nil, in)
}
func appendDecodeBase62(dst []byte, src []byte) []byte {
// Kind of intrusive, we modify the input buffer... it's OK here, it saves
// a memory allocation in Parse.
for i, b := range src {
// O(1)... technically. Has better real-world perf than a map
src[i] = byte(strings.IndexByte(base62Characters, b))
}
return appendBase2Base(dst, src, 62, 256)
}
func decodeBase62(src []byte) []byte {
return appendDecodeBase62(
make([]byte, 0, len(src)*2),
append(make([]byte, 0, len(src)), src...),
)
}
func reverse(b []byte) {
i := 0
j := len(b) - 1
for i < j {
b[i], b[j] = b[j], b[i]
i++
j--
}
}
func leftpad(b []byte, c byte, n int) []byte {
if n -= len(b); n > 0 {
for i := 0; i != n; i++ {
b = append(b, c)
}
copy(b[n:], b)
for i := 0; i != n; i++ {
b[i] = c
}
}
return b
}