-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathencoder_test.go
113 lines (109 loc) · 2.33 KB
/
encoder_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
package tsid
import (
"testing"
"time"
)
func TestBase64(t *testing.T) {
e := Base64{Aligned: true}
for i := 0; i < 100; i++ {
n := &ID{
Main: time.Now().UnixNano(),
Ext: Rand(byte(i)),
}
d := e.Encode(n)
n2, e := e.Decode(d)
if e != nil {
t.Fatal("want: nothing, got: error ", e)
return
}
if n.Main != n2.Main || n.Ext != n2.Ext {
t.Fatal("want: [", n.Main, ", ", n.Ext, "] got: [", n2.Main, ",", n2.Ext, "]")
return
}
}
}
func TestBase64Zero(t *testing.T) {
e := Base64{Aligned: true}
for i := int64(0); i < 100; i++ {
e.Aligned = i%9 != 0
n := &ID{
Main: i % 10,
Ext: i % 5,
}
d := e.Encode(n)
n2, e := e.Decode(d)
if e != nil {
t.Fatal("want: nothing, got: error ", e)
return
}
if n.Main != n2.Main || n.Ext != n2.Ext {
t.Fatal("want: [", n.Main, ", ", n.Ext, "] got: [", n2.Main, ",", n2.Ext, "]")
return
}
}
}
func TestDecodeError(t *testing.T) {
tests := map[string]decodeErrorType{
"": DecodeErrorEmpty,
"[": DecodeErrorInvalidDigit,
}
en := Base64{Aligned: true}
for n, e := range tests {
_, a := en.Decode(n)
if a != nil {
if x, o := a.(*DecodeError); !o || x.Type != e {
t.Error("unexpected errors occur")
}
} else {
t.Error("the expected error did not occur")
}
}
}
func BenchmarkBase64EncodeMain(b *testing.B) {
e := Base64{Aligned: true}
for i := 0; i < b.N; i++ {
n := &ID{
Main: time.Now().UnixNano(),
}
e.Encode(n)
}
}
func BenchmarkBase64EncodeExt(b *testing.B) {
e := Base64{Aligned: true}
for i := 0; i < b.N; i++ {
n := &ID{
Main: time.Now().UnixNano(),
Ext: time.Now().UnixNano(),
}
e.Encode(n)
}
}
func BenchmarkBase64DecodeMain(b *testing.B) {
e := Base64{Aligned: true}
n := &ID{
Main: time.Now().UnixNano(),
}
s := e.Encode(n)
for i := 0; i < b.N; i++ {
n2, e := e.Decode(s)
if e != nil || n.Main != n2.Main || n.Ext != n2.Ext {
b.Fatal("want: [", n.Main, ", ", n.Ext, "] got: [", n2.Main, ",", n2.Ext, "]")
return
}
}
}
func BenchmarkBase64DecodeExt(b *testing.B) {
e := Base64{Aligned: true}
n := &ID{
Main: time.Now().UnixNano(),
Ext: time.Now().UnixNano(),
}
s := e.Encode(n)
for i := 0; i < b.N; i++ {
n2, e := e.Decode(s)
if e != nil || n.Main != n2.Main || n.Ext != n2.Ext {
b.Fatal("want: [", n.Main, ", ", n.Ext, "] got: [", n2.Main, ",", n2.Ext, "]")
return
}
}
}