-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsgpack_test.go
More file actions
303 lines (258 loc) · 7.74 KB
/
msgpack_test.go
File metadata and controls
303 lines (258 loc) · 7.74 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package msgpack
import (
"testing"
"strings"
"reflect"
"bytes"
"log"
"fmt"
)
// Function generates chars into a string build from 0x31 to 0x7e
func generateChar(sb *strings.Builder, max int) {
for i:=0; i<max; i++ {
sb.WriteByte(byte(i+0x31))
}
}
func encodeDebug(t *testing.T, enc *Encoder, buf *bytes.Buffer, v interface{}) {
enc.Encode(v)
t.Logf("Encoded[%d]: \"0x%x\"\n", buf.Len(), buf.Bytes())
}
// Function decodes once
func decodeDebug(t *testing.T, dec *Decoder, v interface{}) {
//Get token
tok, err := dec.Token()
if err != nil {
panic(err)
}
//Token not equal to value
log.Println(v, tok, reflect.TypeOf(v), reflect.TypeOf(tok))
if !reflect.DeepEqual(v, tok) {
log.Panicf("value mismatch (%v)! %v (%v) != %v (%v)", dec.Kind(), v, reflect.TypeOf(v), tok, reflect.TypeOf(tok))
}
t.Logf("Checked out! [%v] %v (%v) == %v (%v)", dec.Kind(), v, reflect.TypeOf(v), tok, reflect.TypeOf(tok))
}
func TestInt(t *testing.T) {
bknown := []byte{ 0xe3, 0xd1, 0x40, 0x74, 0xd2, 0x00, 0x10, 0x00,
0x00, 0xd3, 0x00, 0x33, 0xff, 0xaa, 0xbb, 0xcc,
0xee, 0xff, 0xd3, 0x00, 0x33, 0xff, 0xaa, 0xbb,
0xcc, 0xee, 0xff }
//Native int types
fixint := int8(-3)
integ16 := int16(16500)
integ32 := int32(1<<20)
integ64 := int64(0x0033ffaabbcceeff)
integ := int(0x0033ffaabbcceeff)
//Encode the above native types
buf := bytes.Buffer{}
enc := NewEncoder(&buf)
encodeDebug(t, enc, &buf, fixint)
encodeDebug(t, enc, &buf, integ16)
encodeDebug(t, enc, &buf, integ32)
encodeDebug(t, enc, &buf, integ64)
encodeDebug(t, enc, &buf, integ)
//Check if bytes are same
if bytes.Compare(buf.Bytes(), bknown) != 0 {
panic("Bytes mismatch!")
}
//Decode
dec := NewDecoder(&buf)
//Check if we encoded correctly
decodeDebug(t, dec, fixint)
decodeDebug(t, dec, integ16)
decodeDebug(t, dec, integ32)
decodeDebug(t, dec, integ64)
}
func TestUint(t *testing.T) {
bknown := []byte{ 0xcc, 0xff, 0xcd, 0x40, 0x74, 0xce, 0x00, 0x10,
0x00, 0x00, 0xcf, 0x00, 0x33, 0xff, 0xaa, 0xbb,
0xcc, 0xee, 0xff, 0xcf, 0x00, 0x33, 0xff, 0xaa,
0xbb, 0xcc, 0xee, 0xff }
//Native types
integ8 := uint8(255)
integ16 := uint16(16500)
integ32 := uint32(1<<20)
integ64 := uint64(0x0033ffaabbcceeff)
integ := uint(0x0033ffaabbcceeff)
//Encode the above native types
buf := bytes.Buffer{}
enc := NewEncoder(&buf)
encodeDebug(t, enc, &buf, integ8)
encodeDebug(t, enc, &buf, integ16)
encodeDebug(t, enc, &buf, integ32)
encodeDebug(t, enc, &buf, integ64)
encodeDebug(t, enc, &buf, integ)
// Check if bytes are same
if !bytes.Equal(buf.Bytes(), bknown) {
panic("Bytes mismatch!")
}
//Check if we encoded correctly
dec := NewDecoder(&buf)
decodeDebug(t, dec, integ8)
decodeDebug(t, dec, integ16)
decodeDebug(t, dec, integ32)
decodeDebug(t, dec, integ64)
}
func TestBoolEncoder(t *testing.T) {
buf := bytes.Buffer{}
enc := NewEncoder(&buf)
encodeDebug(t, enc, &buf, true)
encodeDebug(t, enc, &buf, false)
if bytes.Compare(buf.Bytes(), []byte{ 0xc3, 0xc2 }) != 0 {
panic("Bytes mismatch!")
}
}
func TestStringEncoder(t *testing.T) {
fixstr := "test"
buf := bytes.Buffer{}
enc := NewEncoder(&buf)
encodeDebug(t, enc, &buf, fixstr)
//Check fixstr
if bytes.Compare(buf.Bytes(), []byte{ 0xa4, 't', 'e', 's', 't' }) != 0 {
panic("Bytes mismatch!")
}
//Setup 31 > x < 256
//0x21 to 0x7e
sb := strings.Builder{}
sb.Grow(240)
generateChar(&sb, 240)
encodeDebug(t, enc, &buf, sb.String())
//Setup 255 > x < 65536
sb = strings.Builder{}
generateChar(&sb, 59999)
encodeDebug(t, enc, &buf, sb.String())
//Setup >65535
sb = strings.Builder{}
generateChar(&sb, 70321)
encodeDebug(t, enc, &buf, sb.String())
}
// Test array
func TestArray(t *testing.T) {
ints := []int{ -3, 16500, 1<<20, 0x0033ffaabbcceeff }
buf := bytes.Buffer{}
enc := NewEncoder(&buf)
encodeDebug(t, enc, &buf, ints)
}
func TestMap(t *testing.T) {
maps := map[string]int{ "test": 4, "gogo": 4 }
buf := bytes.Buffer{}
enc := NewEncoder(&buf)
encodeDebug(t, enc, &buf, maps)
}
// Test struct by comparing to map
func TestStruct(t *testing.T) {
maps := map[string]int{ "test": 2, "gogo": 5 }
buf := bytes.Buffer{}
enc := NewEncoder(&buf)
encodeDebug(t, enc, &buf, maps)
//Struct
st := struct{ Test int `msgpack:"test"`
Gogo int `msgpack:"gogo"` } { maps["test"], maps["gogo"] }
//Struct buffer
stbuf := bytes.Buffer{}
enc = NewEncoder(&stbuf)
encodeDebug(t, enc, &stbuf, st)
t.Logf("%.*s", stbuf.Len(), stbuf.Bytes())
}
// Test omitempty struct tag keyword
func TestOmitEmpty(t *testing.T) {
mk := "audi"
st := struct{ Omit *string `msgpack:"omit,omitempty"`
Nil *string
Make *string }{ nil, nil, &mk }
//Check omitted
buf := bytes.Buffer{}
enc := NewEncoder(&buf)
encodeDebug(t, enc, &buf, &st)
t.Logf("%.*s", buf.Len(), buf.Bytes())
//Check not omitted
buf.Reset()
omit := "NO Omit!!"
st.Omit = &omit
enc = NewEncoder(&buf)
encodeDebug(t, enc, &buf, &st)
t.Logf("%.*s", buf.Len(), buf.Bytes())
}
// Test float
func TestFloat(t *testing.T) {
f64 := float64(1.32342342341)
f32 := float32(.992)
//Encode floats
buf := bytes.Buffer{}
enc := NewEncoder(&buf)
encodeDebug(t, enc, &buf, f64)
encodeDebug(t, enc, &buf, f32)
t.Logf("%.*s", buf.Len(), buf.Bytes())
}
// Test complicated struct
func TestComplicatedStruct(t *testing.T) {
st := struct{ Make string
Model string
Year int
Properties map[string]string }{ "Audi", "A4", 2018, map[string]string{ "engine": "4-cylinder" } }
buf := bytes.Buffer{}
enc := NewEncoder(&buf)
encodeDebug(t, enc, &buf, &st)
t.Logf("%.*s", buf.Len(), buf.Bytes())
}
// Test with the Marshal function
func TestMarshal(t *testing.T) {
st := struct{ Make string
Model string
Year int
Properties map[string]string }{ "Audi", "A4", 2018, map[string]string{ "engine": "4-cylinder" } }
//Marshal
if buf, err := Marshal(st); err != nil {
panic(fmt.Sprintf("%s", err.Error()))
} else {
t.Logf("%.*s", len(buf), buf)
}
}
// General test function for decoding
func TestUnmarshal(t *testing.T) {
var buf []byte
num := uint64(392423)
if b, err := Marshal(num); err != nil {
panic(err)
} else {
buf = b
}
//Decode
dnum := uint64(0)
if err := Unmarshal(buf, &dnum); err != nil {
panic(err)
}
//Check if match
if dnum != num {
panic(fmt.Sprintf("Decoded numbers are not the same as encoded! %v != %v", num, dnum))
}
//FixUint
num = 12
if b, err := Marshal(uint8(num)); err != nil {
panic(err)
} else {
buf = b
}
//Decode
dnum = 0
if err := Unmarshal(buf, &dnum); err != nil {
panic(err)
}
//Check if match
if dnum != num {
panic(fmt.Sprintf("Decoded numbers are not the same as encoded! %v != %v", num, dnum))
}
//Fixint
inum := -5
if b, err := Marshal(inum); err != nil {
panic(err)
} else {
buf = b
}
//Decode
var dinum int
if err := Unmarshal(buf, &dinum); err != nil {
panic(err)
} else if inum != dinum {
panic(fmt.Sprintf("Decoded numbers are not the same as encoded! %v != %v", inum, dinum))
}
}