-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdecode_test.go
75 lines (63 loc) · 1.56 KB
/
decode_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
package base91
import (
"bytes"
"crypto/rand"
"io"
"io/ioutil"
"strings"
"testing"
)
var (
decodeSpec = map[string]string{
"xA": "1",
">OwJh>Io0Tv!8PE": "Hello World!",
"tC??dBPUBX|xqnB@VEC%qCXQ{+WB|9~5]PIlN+\";B`%tx34t0c.;[Gf6W0WBUG": "「さやかちゃん、大好きだ!(*^ω^*)」",
"5fNOkLP/rav": "\x3e\xeb\xa0\x34\x10\x01\x9d\x96\x5e",
"EquimSayaka~A": "\xf2\x8e\x88\x31\x1a\xf0\x68\xce\x7a\x3f",
}
)
func TestDecode(t *testing.T) {
for k, v := range decodeSpec {
if actual := string(DecodeString(k)); actual != v {
t.Fatalf("expected `%s`, got `%s`", v, actual)
}
}
}
func TestDecoder(t *testing.T) {
for k, v := range decodeSpec {
d := NewDecoder(bytes.NewReader([]byte(k)))
actual, err := ioutil.ReadAll(d)
if err != nil {
t.Fatal(err)
}
if string(actual) != v {
t.Fatalf("expected `%s`, got `%s`", v, actual)
}
}
}
func BenchmarkDecode(b *testing.B) {
s := make([]byte, 1024*1024)
if _, err := rand.Read(s); err != nil {
b.Fatal(err)
}
encoded := EncodeToString(s)
b.ResetTimer()
for i := 0; i < b.N; i++ {
DecodeString(encoded)
}
}
func BenchmarkDecoder(b *testing.B) {
s := make([]byte, 1024*1024)
if _, err := rand.Read(s); err != nil {
b.Fatal(err)
}
encoded := EncodeToString(s)
b.SetBytes(int64(len(encoded)))
b.ResetTimer()
for i := 0; i < b.N; i++ {
d := NewDecoder(strings.NewReader(encoded))
if _, err := io.CopyN(ioutil.Discard, d, 1024*1024); err != nil {
b.Fatal(err)
}
}
}