This repository was archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_test.go
More file actions
105 lines (83 loc) · 2.26 KB
/
string_test.go
File metadata and controls
105 lines (83 loc) · 2.26 KB
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
package amf0
import (
"bytes"
"io/ioutil"
"testing"
"github.com/stretchr/testify/assert"
)
func strBytes(s AmfType) []byte {
buf := new(bytes.Buffer)
s.Encode(buf)
return buf.Bytes()
}
func TestStringBuildsAndEncodes(t *testing.T) {
s := NewString("hello")
assert.Equal(t, []byte{0x0, 0x5, 0x68, 0x65, 0x6C,
0x6C, 0x6F}, strBytes(s))
}
func TestStringEncodingUtf8(t *testing.T) {
s := NewString("こんにちは")
assert.Equal(t, []byte{0x0, 0xf, 0xe3, 0x81, 0x93,
0xe3, 0x82, 0x93, 0xe3, 0x81, 0xab, 0xe3, 0x81, 0xa1, 0xe3,
0x81, 0xaf}, strBytes(s))
}
func TestStringDecodes(t *testing.T) {
o := new(String)
err := o.Decode(bytes.NewReader([]byte{
0x0, 0xf, 0xe3, 0x81, 0x93, 0xe3, 0x82, 0x93, 0xe3, 0x81, 0xab,
0xe3, 0x81, 0xa1, 0xe3, 0x81, 0xaf,
}))
assert.Nil(t, err)
assert.Equal(t, "こんにちは", string(*o))
}
func BenchmarkStringDecode(b *testing.B) {
in := NewString("hello")
data := strBytes(in)
out := new(String)
for i := 0; i < b.N; i++ {
out.Decode(bytes.NewReader(data))
}
}
func BenchmarkStringEncode(b *testing.B) {
in := NewString("hello")
for i := 0; i < b.N; i++ {
in.Encode(ioutil.Discard)
}
}
func TestLongStringBuildsAndEncodes(t *testing.T) {
s := NewLongString("hello")
assert.Equal(t, []byte{0x0, 0x0, 0x0, 0x5, 0x68,
0x65, 0x6C, 0x6C, 0x6F}, strBytes(s))
}
func TestLongStringEncodingUtf8(t *testing.T) {
s := NewLongString("こんにちは")
assert.Equal(t, []byte{
0x00, 0x00, 0x00, 0xf, 0xe3, 0x81, 0x93, 0xe3, 0x82, 0x93, 0xe3,
0x81, 0xab, 0xe3, 0x81, 0xa1, 0xe3, 0x81, 0xaf,
}, strBytes(s))
}
func TestLongStringDecodes(t *testing.T) {
o := new(LongString)
err := o.Decode(bytes.NewReader([]byte{
0x00, 0x00, 0x00, 0xf, 0xe3, 0x81, 0x93, 0xe3, 0x82, 0x93, 0xe3,
0x81, 0xab, 0xe3, 0x81, 0xa1, 0xe3, 0x81, 0xaf,
}))
assert.Nil(t, err)
assert.Equal(t, "こんにちは", string(*o))
}
func BenchmarkLongStringDecode(b *testing.B) {
data := []byte{
0x00, 0x00, 0x00, 0xf, 0xe3, 0x81, 0x93, 0xe3, 0x82, 0x93, 0xe3,
0x81, 0xab, 0xe3, 0x81, 0xa1, 0xe3, 0x81, 0xaf,
}
out := new(LongString)
for i := 0; i < b.N; i++ {
out.Decode(bytes.NewReader(data))
}
}
func BenchmarkLongStringEncode(b *testing.B) {
in := NewLongString("hello")
for i := 0; i < b.N; i++ {
in.Encode(ioutil.Discard)
}
}