-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
173 lines (160 loc) · 3.37 KB
/
util_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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package go_engine_io_parser
import (
"bufio"
"bytes"
"testing"
"testing/quick"
"github.com/stretchr/testify/assert"
)
func TestWriteBinaryLen(t *testing.T) {
at := assert.New(t)
tests := []struct {
l int64
output []byte
}{
{0, []byte{0, 0xff}},
{1, []byte{1, 0xff}},
{9, []byte{9, 0xff}},
{10, []byte{1, 0, 0xff}},
{19, []byte{1, 9, 0xff}},
{23461, []byte{2, 3, 4, 6, 1, 0xff}},
}
for _, test := range tests {
buf := bytes.NewBuffer(nil)
err := writeBinaryLen(test.l, buf)
at.Nil(err)
at.Equal(test.output, buf.Bytes())
}
f := func(l int64) bool {
if l < 0 {
return true
}
buf := bytes.NewBuffer(nil)
_ = writeBinaryLen(l, buf)
r := bufio.NewReader(buf)
out, _ := readBinaryLen(r)
return out == l
}
err := quick.Check(f, nil)
at.Nil(err)
}
func TestWriteStringLen(t *testing.T) {
at := assert.New(t)
tests := []struct {
l int64
output string
}{
{0, "0:"},
{1, "1:"},
{9, "9:"},
{10, "10:"},
{19, "19:"},
{23461, "23461:"},
}
for _, test := range tests {
buf := bytes.NewBuffer(nil)
err := writeTextLen(test.l, buf)
at.Nil(err)
at.Equal(test.output, buf.String())
}
f := func(l int64) bool {
if l < 0 {
return true
}
buf := bytes.NewBuffer(nil)
_ = writeTextLen(l, buf)
r := bufio.NewReader(buf)
out, _ := readTextLen(r)
return out == l
}
err := quick.Check(f, nil)
at.Nil(err)
}
func TestReadBytesLen(t *testing.T) {
at := assert.New(t)
tests := []struct {
data []byte
ok bool
output int64
}{
{[]byte{0xff}, true, 0},
{[]byte{0, 0xff}, true, 0},
{[]byte{1, 0xff}, true, 1},
{[]byte{9, 0xff}, true, 9},
{[]byte{1, 0, 0xff}, true, 10},
{[]byte{1, 9, 0xff}, true, 19},
{[]byte{2, 3, 4, 6, 1, 0xff}, true, 23461},
{[]byte{2, 3, 4, 6, 1}, false, 0},
{[]byte{2, 254, 4, 6, 1}, false, 0},
}
for _, test := range tests {
r := bufio.NewReader(bytes.NewReader(test.data))
l, err := readBinaryLen(r)
at.Equal(test.ok, err == nil)
if !test.ok {
continue
}
at.Equal(test.output, l)
}
}
func TestReadStringLen(t *testing.T) {
at := assert.New(t)
tests := []struct {
data []byte
ok bool
output int64
}{
{[]byte(":"), true, 0},
{[]byte("0:"), true, 0},
{[]byte("1:"), true, 1},
{[]byte("9:"), true, 9},
{[]byte("10:"), true, 10},
{[]byte("19:"), true, 19},
{[]byte("23461:"), true, 23461},
{[]byte("23461"), false, 0},
{[]byte("234ab"), false, 0},
}
for _, test := range tests {
r := bufio.NewReader(bytes.NewReader(test.data))
l, err := readTextLen(r)
at.Equal(test.ok, err == nil)
if !test.ok {
continue
}
at.Equal(test.output, l)
}
}
func BenchmarkWriteStringLen(b *testing.B) {
w := bytes.NewBuffer(nil)
_ = writeTextLen(23461, w)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.Reset()
_ = writeTextLen(23461, w)
}
}
func BenchmarkWriteBinaryLen(b *testing.B) {
w := bytes.NewBuffer(nil)
_ = writeTextLen(23461, w)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.Reset()
_ = writeBinaryLen(23461, w)
}
}
func BenchmarkReadStringLen(b *testing.B) {
bs := bytes.Repeat([]byte("23461:"), b.N)
r := bufio.NewReader(bytes.NewReader(bs))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = readTextLen(r)
}
}
func BenchmarkReadBinaryLen(b *testing.B) {
bs := bytes.Repeat([]byte{2, 3, 4, 6, 1, 0xff}, b.N)
r := bufio.NewReader(bytes.NewReader(bs))
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = readBinaryLen(r)
}
}