This repository has been archived by the owner on Sep 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_test.go
276 lines (235 loc) · 8.3 KB
/
encode_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package gosmile
import (
"log"
"math"
"runtime"
"testing"
)
func TestZigZagInt(t *testing.T) {
expect(0, zigzagEncodeInt(0), t, "zigzag1")
expect(1, zigzagEncodeInt(-1), t, "zigzag1")
expect(2, zigzagEncodeInt(1), t, "zigzag2")
expect(int(0xffffffff), zigzagEncodeInt(math.MinInt32), t, "zigzagintmin")
expect(int(0xfffffffe), zigzagEncodeInt(math.MaxInt32), t, "zigzagintmax")
}
func TestZigZagLong(t *testing.T) {
expect(int64(0), zigzagEncodeLong(0), t, "zigzag0")
expect(int64(-2), zigzagEncodeLong(int64(math.MaxInt64)), t, "zigzag-longmax")
expect(int64(-1), zigzagEncodeLong(math.MinInt64), t, "zigzag-longmin")
}
func TestEncodeHeader(t *testing.T) {
e := NewEncodeConf()
e.Version = 3
content, err := Marshal(e, 1)
if err != nil || len(content) < 4 {
t.Fatal("err here", err, "content size:", len(content))
}
expect(byte(':'), content[0], t, "testencodeheader-1")
expect(byte(')'), content[1], t, "testencodeheader-2")
expect(byte('\n'), content[2], t, "testencodeheader-3")
varByte := content[3]
expect(byte(3)<<4, varByte&0xf0, t, "testencodeheader-version")
expect(byte(0x00), varByte&0x04, t, "testencodeheader-rawbinary")
expect(byte(0x00), varByte&0x02, t, "testencodeheader-sharedstringvalue")
expect(byte(0x01), varByte&0x01, t, "testencodeheader-sharedpropname")
e = NewEncodeConf()
e.SharedStringValueEnabled = true
e.SharedPropertyNameEnabled = false
content, err = Marshal(e, 1)
if err != nil || len(content) < 4 {
t.Fatal("err here", err, "content size:", len(content))
}
varByte = content[3]
expect(byte(1)<<1, varByte&0x02, t, "testencodeheader-sharedstringvalue2")
expect(byte(0x00), varByte&0x01, t, "testencodeheader-sharedpropname2")
}
func TestEncodeSmallInt(t *testing.T) {
e := NewEncodeConf()
e.IncludeHeader = false
assertSmallInt := func(n int, name string) {
c, err := Marshal(e, n)
if err != nil {
t.Fatal("error", err)
}
expect(1, len(c), t, name+"length")
expect(byte(token_prefix_small_int+zigzagEncodeInt(n)), c[0], t, name)
}
assertSmallInt(3, "encode3")
assertSmallInt(0, "encode0")
assertSmallInt(-6, "encode-6")
assertSmallInt(15, "encode15")
assertSmallInt(-16, "encode-16")
}
func TestOtherInts(t *testing.T) {
e := NewEncodeConf()
e.IncludeHeader = false
assertInt := func(n int, length int, name string) {
c, err := Marshal(e, n)
if err != nil {
t.Fatal(err)
}
expect(length, len(c), t, name+"length")
}
assertInt(16, 2, "encode16")
assertInt(-17, 2, "encode-17")
assertInt(0xfff, 3, "encode0xfff")
assertInt(-4096, 3, "encode-4096")
assertInt(0x1000, 4, "encode0x1000")
assertInt(500000, 4, "encode500000")
assertInt(math.MaxInt32, 6, "encodemaxint")
assertInt(math.MinInt32, 6, "encodeminint")
}
func TestFloat32(t *testing.T) {
e := NewEncodeConf()
e.IncludeHeader = false
c, err := Marshal(e, float32(0.125))
if err != nil || c == nil {
t.Fatal("encode float failed, content:", c, "error:", err)
}
if len(c) != 6 {
t.Fatal("encode float failed, expected length is 6 but it was:", len(c))
}
}
func TestFloat64(t *testing.T) {
e := NewEncodeConf()
e.IncludeHeader = false
c, err := Marshal(e, float64(0.125))
if err != nil || c == nil {
t.Fatal("encode float64 failed, content:", c, "error:", err)
}
if len(c) != 11 {
t.Fatal("encode float64 failed, expected length is 11 but it was:", len(c))
}
}
func TestBool(t *testing.T) {
e := NewEncodeConf()
e.IncludeHeader = false
c, err := Marshal(e, false)
if err != nil || c == nil || len(c) != 1 || c[0] != token_literal_false {
t.Fatal("encode false failed, content:", c, "error:", err)
}
c, err = Marshal(e, true)
if err != nil || c == nil || len(c) != 1 || c[0] != token_literal_true {
t.Fatal("encode true failed, content:", c, "error:", err)
}
}
func TestTinyString(t *testing.T) {
e := NewEncodeConf()
e.IncludeHeader = false
originalVal := "strtest"
val := originalVal
c, err := Marshal(e, val)
if err != nil || c == nil || len(c) != 8 || (c[0] != (token_prefix_tiny_ascii+7)) && string(c[1:]) != val {
t.Fatal("encode short ascii string failed, err:", err, "content:", c)
}
val = originalVal + "œ"
c, err = Marshal(e, val)
if err != nil || c == nil || len(c) != 10 || (c[0] != (token_prefix_tiny_unicode+10)) && string(c[1:]) != val {
t.Fatal("encode short unicode 2-byte string failed, err:", err, "content:", c)
}
val = originalVal + "≈"
c, err = Marshal(e, val)
if err != nil || c == nil || len(c) != 11 || (c[0] != (token_prefix_tiny_unicode+11)) && string(c[1:]) != val {
t.Fatal("encode short unicode 3-byte string failed, err:", err, "content:", c)
}
}
func TestLongString(t *testing.T) {
e := NewEncodeConf()
e.IncludeHeader = false
originalVal := "benim adim insanlarin hizasina yazilmistir\n" +
"her gun yepyeni ruyalarla odenebilecek bir ceza bu\n"
val := originalVal
c, err := Marshal(e, val)
if err != nil || c == nil || len(c) != 96 || (c[0] != token_byte_long_string_ascii) && string(c[1:]) != val && c[len(c)-1] != byte_marker_end_of_string {
t.Fatal("encode long ascii string failed, err:", err, "content len:", len(c), "first byte:", c[0], "last byte:", c[len(c)-1])
}
val = originalVal + "œ"
c, err = Marshal(e, val)
if err != nil || c == nil || len(c) != 98 || (c[0] != token_misc_long_text_unicode) && string(c[1:]) != val && c[len(c)-1] != byte_marker_end_of_string {
t.Fatal("encode long unicode 2-byte string failed, err:", err, "content len:", len(c), "first byte:", c[0], "last byte:", c[len(c)-1])
}
val = originalVal + "≈"
c, err = Marshal(e, val)
if err != nil || c == nil || len(c) != 99 || (c[0] != token_misc_long_text_unicode) && string(c[1:]) != val && c[len(c)-1] != byte_marker_end_of_string {
t.Fatal("encode long unicode 3-byte string failed, err:", err, "content len:", len(c), "first byte:", c[0], "last byte:", c[len(c)-1])
}
}
func TestSimpleSlice(t *testing.T) {
e := NewEncodeConf()
e.IncludeHeader = false
val := []int{1, 2, 16}
c, err := Marshal(e, val)
if err != nil || len(c) != 6 || c[0] != token_literal_start_array || c[5] != token_literal_end_array {
t.Fatal("encode int slice failed, err:", err, "len(c):", len(c))
}
strSlice := []string{"strtest", "strtestœ"}
c, err = Marshal(e, strSlice)
if err != nil || len(c) != 20 || c[0] != token_literal_start_array || c[19] != token_literal_end_array {
t.Fatal("encode string slice failed, err:", err, "len(c):", len(c))
}
}
func TestSimpleArray(t *testing.T) {
e := NewEncodeConf()
e.IncludeHeader = false
val := [3]int{1, 2, 16}
c, err := Marshal(e, val)
if err != nil || len(c) != 6 || c[0] != token_literal_start_array || c[5] != token_literal_end_array {
t.Fatal("encode int array failed, err:", err, "len(c):", len(c))
}
}
func TestSimpleMap(t *testing.T) {
v := make(map[string]string)
v["key"] = "value"
e := NewEncodeConf()
e.IncludeHeader = false
c, err := Marshal(e, v)
if err != nil || len(c) != 12 || c[0] != token_literal_start_object || c[11] != token_literal_end_object {
t.Fatal("encode simple map failed, err:", err, "len(c):", len(c))
}
}
type simplestruct struct {
key string
}
type simpleinterface interface {
Foo() string
}
func (s *simplestruct) Foo() string {
return "foo"
}
func TestSimpleStruct(t *testing.T) {
v := simplestruct{"value"}
e := NewEncodeConf()
e.IncludeHeader = false
c, err := Marshal(e, v)
if err != nil || len(c) != 12 || c[0] != token_literal_start_object || c[11] != token_literal_end_object {
t.Fatal("encode simple map failed, err:", err, "len(c):", len(c))
}
}
func TestSimpleStructPointer(t *testing.T) {
v := simplestruct{"value"}
e := NewEncodeConf()
e.IncludeHeader = false
c, err := Marshal(e, &v)
if err != nil || len(c) != 12 || c[0] != token_literal_start_object || c[11] != token_literal_end_object {
t.Fatal("encode simple map failed, err:", err, "len(c):", len(c))
}
}
func TestSimpleInterface(t *testing.T) {
s := simplestruct{"value"}
var v simpleinterface
v = &s
e := NewEncodeConf()
e.IncludeHeader = false
c, err := Marshal(e, simpleinterface(v))
if err != nil || len(c) != 12 || c[0] != token_literal_start_object || c[11] != token_literal_end_object {
t.Fatal("encode simple map failed, err:", err, "len(c):", len(c))
}
}
func expect(expected interface{}, got interface{}, t *testing.T, test string) {
if got != expected {
var stack [4096]byte
runtime.Stack(stack[:], false)
log.Printf("%s\n", stack[:])
t.Fatal(test, "expected:", expected, "got:", got)
}
}