-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbase36_test.go
66 lines (52 loc) · 1.45 KB
/
base36_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
package base36
import (
"math"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
var raw = []uint64{0, 50, 100, 999, 1000, 1111, 5959, 99999,
123456789, 5481594952936519619, math.MaxInt64 / 2048, math.MaxInt64 / 512,
math.MaxInt64, math.MaxUint64}
var encoded = []string{"0", "1E", "2S", "RR", "RS", "UV", "4LJ", "255R",
"21I3V9", "15N9Z8L3AU4EB", "18CE53UN18F", "4XDKKFEK4XR",
"1Y2P0IJ32E8E7", "3W5E11264SGSF"}
func TestEncode(t *testing.T) {
for i, v := range raw {
assert.Equal(t, encoded[i], Encode(v))
}
}
func TestDecode(t *testing.T) {
for i, v := range encoded {
assert.Equal(t, raw[i], Decode(v))
assert.Equal(t, raw[i], Decode(strings.ToLower(v)))
}
}
func TestDecodeToBytes(t *testing.T) {
// ensure that lowercase encoded string decodes correctly
// https://github.com/martinlindhe/base36/issues/8
in := []byte("Hello World")
encoded := EncodeBytes(in)
decoded := DecodeToBytes(encoded)
assert.Equal(t, decoded, in)
decodedLower := DecodeToBytes(strings.ToLower(encoded))
assert.Equal(t, decodedLower, in)
}
func BenchmarkEncode(b *testing.B) {
for i := 0; i < b.N; i++ {
Encode(5481594952936519619)
}
}
func BenchmarkEncodeBytesAsBytes(b *testing.B) {
data := []byte{
0x86, 0x4F, 0xD2, 0x6F, 0xB5, 0x59, 0xF7, 0x5B, 0x48, 0x4F, 0x2A, 0x48, 0x4F, 0x2A,
}
for i := 0; i < b.N; i++ {
EncodeBytesAsBytes(data)
}
}
func BenchmarkDecode(b *testing.B) {
for i := 0; i < b.N; i++ {
Decode("1Y2P0IJ32E8E7")
}
}