-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathencoder_test.go
108 lines (96 loc) · 2.72 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
package base64
import (
"encoding/base64"
"testing"
"unsafe"
)
func TestEncoder(t *testing.T) {
stdResult := base64.StdEncoding.EncodeToString(nil)
ownResult := StdEncoding.EncodeToString(nil)
if stdResult != ownResult {
t.Log("expected:", stdResult)
t.Log("actual: ", ownResult)
t.Fatal()
}
stdResult = base64.StdEncoding.EncodeToString([]byte{})
ownResult = StdEncoding.EncodeToString([]byte{})
if stdResult != ownResult {
t.Log("expected:", stdResult)
t.Log("actual: ", ownResult)
t.Fatal()
}
for i := 1; i < 200; i++ {
for j := 0; j < 10000; j++ {
bytes := generateRandomBytes(i)
for _, encoding := range []*Encoding{StdEncoding, RawStdEncoding, URLEncoding, RawURLEncoding} {
length := encoding.EncodedLen(len(bytes))
if length == 0 {
continue
}
result := make([]byte, length, length+10)
encoding.encode(result, bytes, uintptr(length))
(*sliceHeader)(unsafe.Pointer(&result)).len = length + 10
for _, b := range result[length:] {
if b != 0 {
t.Fatal("out of bounds")
}
}
}
stdResult = base64.StdEncoding.EncodeToString(bytes)
ownResult = StdEncoding.EncodeToString(bytes)
if stdResult != ownResult {
t.Log("expected:", stdResult)
t.Log("actual: ", ownResult)
t.Fatal()
}
stdResult = base64.RawStdEncoding.EncodeToString(bytes)
ownResult = RawStdEncoding.EncodeToString(bytes)
if stdResult != ownResult {
t.Log("expected:", stdResult)
t.Log("actual: ", ownResult)
t.Fatal()
}
stdResult = base64.URLEncoding.EncodeToString(bytes)
ownResult = URLEncoding.EncodeToString(bytes)
if stdResult != ownResult {
t.Log("expected:", stdResult)
t.Log("actual: ", ownResult)
t.Fatal()
}
stdResult = base64.RawURLEncoding.EncodeToString(bytes)
ownResult = RawURLEncoding.EncodeToString(bytes)
if stdResult != ownResult {
t.Log("expected:", stdResult)
t.Log("actual: ", ownResult)
t.Fatal()
}
}
}
}
func BenchmarkEncoder(b *testing.B) {
b.ReportAllocs()
length := base64.StdEncoding.EncodedLen(len(valueBytes))
byteResult = make([]byte, length)
b.Run("std/Encode", func(b *testing.B) {
for n := 0; n < b.N; n++ {
base64.StdEncoding.Encode(byteResult, valueBytes)
}
})
b.Run("std/EncodeToString", func(b *testing.B) {
for n := 0; n < b.N; n++ {
stringResult = base64.StdEncoding.EncodeToString(valueBytes)
}
})
length = StdEncoding.EncodedLen(len(valueBytes))
byteResult = make([]byte, length)
b.Run("own/Encode", func(b *testing.B) {
for n := 0; n < b.N; n++ {
StdEncoding.Encode(byteResult, valueBytes)
}
})
b.Run("own/EncodeToString", func(b *testing.B) {
for n := 0; n < b.N; n++ {
stringResult = StdEncoding.EncodeToString(valueBytes)
}
})
}