-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstruct_encoder_test.go
More file actions
439 lines (392 loc) · 11.3 KB
/
Copy pathstruct_encoder_test.go
File metadata and controls
439 lines (392 loc) · 11.3 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
package msgpck
import (
"testing"
)
const errMsgDecodeFailedFmt = "decode failed: %v"
func TestStructEncoderMethods(t *testing.T) {
type Person struct {
Name string `msgpack:"name"`
Age int `msgpack:"age"`
}
enc := GetStructEncoder[Person]()
p := Person{Name: "Test", Age: 25}
t.Run("Encode", func(t *testing.T) {
b, err := enc.Encode(&p)
if err != nil {
t.Fatal(err)
}
if len(b) == 0 {
t.Error("Encode returned empty")
}
})
t.Run("GetStructEncoder", func(t *testing.T) {
enc := GetStructEncoder[Person]()
b, err := enc.Encode(&p)
if err != nil {
t.Fatal(err)
}
if len(b) == 0 {
t.Error("GetStructEncoder returned empty")
}
})
}
func TestEncodeOmitemptyVariants(t *testing.T) {
type Data struct {
S string `msgpack:"s,omitempty"`
I int `msgpack:"i,omitempty"`
U uint `msgpack:"u,omitempty"`
F float64 `msgpack:"f,omitempty"`
B bool `msgpack:"b,omitempty"`
Sl []int `msgpack:"sl,omitempty"`
M map[string]int `msgpack:"m,omitempty"`
A [0]int `msgpack:"a,omitempty"`
}
d := Data{} // all zero values
b, err := Marshal(d)
if err != nil {
t.Fatal(err)
}
// Should be a map with 0 entries (all omitted)
var decoded any
_ = Unmarshal(b, &decoded)
m := decoded.(map[string]any)
if len(m) != 0 {
t.Errorf("expected empty map, got %d entries", len(m))
}
}
func TestStructEncoderIsEmptyField(t *testing.T) {
type AllTypes struct {
S string `msgpack:"s,omitempty"`
I int `msgpack:"i,omitempty"`
I64 int64 `msgpack:"i64,omitempty"`
I32 int32 `msgpack:"i32,omitempty"`
I16 int16 `msgpack:"i16,omitempty"`
I8 int8 `msgpack:"i8,omitempty"`
U uint `msgpack:"u,omitempty"`
U64 uint64 `msgpack:"u64,omitempty"`
U32 uint32 `msgpack:"u32,omitempty"`
U16 uint16 `msgpack:"u16,omitempty"`
U8 uint8 `msgpack:"u8,omitempty"`
F64 float64 `msgpack:"f64,omitempty"`
F32 float32 `msgpack:"f32,omitempty"`
B bool `msgpack:"b,omitempty"`
Sl []int `msgpack:"sl,omitempty"`
M map[string]string `msgpack:"m,omitempty"`
}
// All zero values should be omitted
enc := GetStructEncoder[AllTypes]()
data := AllTypes{}
b, err := enc.Encode(&data)
if err != nil {
t.Fatal(err)
}
// Should be an empty map
var m any
_ = Unmarshal(b, &m)
mp := m.(map[string]any)
// Map is never empty-checked properly so it may still be included
// Just verify no error and reasonable output
if len(mp) > 1 { // only map should remain (as it returns false for empty check)
t.Errorf("expected mostly empty map, got %d fields", len(mp))
}
// Now test with non-zero values
data2 := AllTypes{
S: "test",
I: 1,
I64: 2,
I32: 3,
I16: 4,
I8: 5,
U: 6,
U64: 7,
U32: 8,
U16: 9,
U8: 10,
F64: 1.1,
F32: 2.2,
B: true,
Sl: []int{1},
M: map[string]string{"k": "v"},
}
b2, err := enc.Encode(&data2)
if err != nil {
t.Fatal(err)
}
var m2 any
_ = Unmarshal(b2, &m2)
mp2 := m2.(map[string]any)
if len(mp2) < 10 {
t.Errorf("expected many fields, got %d", len(mp2))
}
}
func TestStructEncoderGenericTypes(t *testing.T) {
type Data struct {
IntSlice []int `msgpack:"ints"`
AnyMap map[string]int `msgpack:"anymap"`
}
d := Data{
IntSlice: []int{1, 2, 3},
AnyMap: map[string]int{"a": 1, "b": 2},
}
enc := GetStructEncoder[Data]()
b, err := enc.Encode(&d)
if err != nil {
t.Fatal(err)
}
// Just verify it encodes without error
if len(b) == 0 {
t.Error("encode failed")
}
}
func TestStructEncoderEncode(t *testing.T) {
type Data struct {
V int `msgpack:"v"`
}
enc := GetStructEncoder[Data]()
d := Data{V: 42}
b, err := enc.Encode(&d)
if err != nil || len(b) == 0 {
t.Error("Encode failed")
}
}
func TestStructEncoderSliceField(t *testing.T) {
type Data struct {
S []string `msgpack:"s"`
}
enc := GetStructEncoder[Data]()
d := Data{S: []string{"a", "b", "c"}}
b, err := enc.Encode(&d)
if err != nil || len(b) == 0 {
t.Error("slice field encode failed")
}
}
func TestStructEncoderMapField(t *testing.T) {
type Data struct {
M map[string]int `msgpack:"m"`
}
enc := GetStructEncoder[Data]()
d := Data{M: map[string]int{"k": 42}}
b, err := enc.Encode(&d)
if err != nil || len(b) == 0 {
t.Error("map field encode failed")
}
}
// TestStructEncoderEncodeWith tests the EncodeWith method for encoder reuse
func TestStructEncoderEncodeWith(t *testing.T) {
type Person struct {
Name string `msgpack:"name"`
Age int `msgpack:"age"`
}
enc := GetStructEncoder[Person]()
dec := GetStructDecoder[Person](false)
// Use EncodeWith with a user-managed encoder
e := NewEncoder(64)
original := Person{Name: "Alice", Age: 30}
err := enc.EncodeWith(e, &original)
if err != nil {
t.Fatalf("EncodeWith failed: %v", err)
}
// Verify the encoded data is valid
var result Person
err = dec.Decode(e.Bytes(), &result)
if err != nil {
t.Fatalf(errMsgDecodeFailedFmt, err)
}
if result.Name != original.Name || result.Age != original.Age {
t.Errorf("got %+v, want %+v", result, original)
}
// Verify encoder can be reused after Reset
e.Reset()
err = enc.EncodeWith(e, &Person{Name: "Bob", Age: 25})
if err != nil {
t.Fatalf("second EncodeWith failed: %v", err)
}
var result2 Person
dec.Decode(e.Bytes(), &result2)
if result2.Name != "Bob" {
t.Errorf("got name %q, want Bob", result2.Name)
}
}
// TestPreEncodeStringFormats tests pre-encoding of field names of various lengths.
func TestPreEncodeStringFormats(t *testing.T) {
// Test fixstr (0-31 bytes)
short := preEncodeString("abc")
if short[0] != (fixstrPrefix | 3) {
t.Errorf("fixstr format wrong: got %02x", short[0])
}
// Test str8 (32-255 bytes)
str8Name := string(make([]byte, 100))
for i := range str8Name {
str8Name = str8Name[:i] + "a" + str8Name[i+1:]
}
medium := preEncodeString(str8Name)
if medium[0] != formatStr8 {
t.Errorf("str8 format wrong: got %02x", medium[0])
}
// Test str16 (256-65535 bytes)
str16Name := make([]byte, 300)
for i := range str16Name {
str16Name[i] = 'b'
}
long := preEncodeString(string(str16Name))
if long[0] != formatStr16 {
t.Errorf("str16 format wrong: got %02x", long[0])
}
// Test str32 (>65535 bytes)
str32Name := make([]byte, 70000)
for i := range str32Name {
str32Name[i] = 'c'
}
veryLong := preEncodeString(string(str32Name))
if veryLong[0] != formatStr32 {
t.Errorf("str32 format wrong: got %02x", veryLong[0])
}
}
// TestStructEncoderLongStrings tests encoding of strings of various lengths.
func TestStructEncoderLongStrings(t *testing.T) {
type Data struct {
S string `msgpack:"s"`
}
enc := GetStructEncoder[Data]()
dec := GetStructDecoder[Data](false)
// Generate strings of various lengths to cover all string format paths
tests := []struct {
name string
len int
}{
{"fixstr (short)", 10},
{"fixstr (max)", 31},
{"str8 (min)", 32},
{"str8 (mid)", 100},
{"str8 (max)", 255},
{"str16 (min)", 256},
{"str16 (mid)", 1000},
{"str16 (max)", 65535},
{"str32 (min)", 65536},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Create string of specified length
s := make([]byte, tc.len)
for i := range s {
s[i] = 'a' + byte(i%26)
}
original := Data{S: string(s)}
data, err := enc.Encode(&original)
if err != nil {
t.Fatalf("encode failed: %v", err)
}
var result Data
err = dec.Decode(data, &result)
if err != nil {
t.Fatalf(errMsgDecodeFailedFmt, err)
}
if result.S != original.S {
t.Errorf("string mismatch: got len %d, want len %d", len(result.S), len(original.S))
}
})
}
}
// TestStructEncoderAllFieldTypes tests encoding of all supported field types.
func TestStructEncoderAllFieldTypes(t *testing.T) {
type AllTypes struct {
String string `msgpack:"string"`
Int int `msgpack:"int"`
Int64 int64 `msgpack:"int64"`
Int32 int32 `msgpack:"int32"`
Int16 int16 `msgpack:"int16"`
Int8 int8 `msgpack:"int8"`
Uint uint `msgpack:"uint"`
Uint64 uint64 `msgpack:"uint64"`
Uint32 uint32 `msgpack:"uint32"`
Uint16 uint16 `msgpack:"uint16"`
Uint8 uint8 `msgpack:"uint8"`
Float64 float64 `msgpack:"float64"`
Float32 float32 `msgpack:"float32"`
Bool bool `msgpack:"bool"`
Bytes []byte `msgpack:"bytes"`
Strings []string `msgpack:"strings"`
Ints []int `msgpack:"ints"`
Int64s []int64 `msgpack:"int64s"`
Int32s []int32 `msgpack:"int32s"`
Int16s []int16 `msgpack:"int16s"`
Int8s []int8 `msgpack:"int8s"`
Uints []uint `msgpack:"uints"`
Uint64s []uint64 `msgpack:"uint64s"`
Uint32s []uint32 `msgpack:"uint32s"`
Uint16s []uint16 `msgpack:"uint16s"`
Float64s []float64 `msgpack:"float64s"`
Float32s []float32 `msgpack:"float32s"`
StringMap map[string]string `msgpack:"string_map"`
IntMap map[string]int `msgpack:"int_map"`
Int64Map map[string]int64 `msgpack:"int64_map"`
Int32Map map[string]int32 `msgpack:"int32_map"`
Uint64Map map[string]uint64 `msgpack:"uint64_map"`
Uint32Map map[string]uint32 `msgpack:"uint32_map"`
Float64M map[string]float64 `msgpack:"float64_map"`
BoolMap map[string]bool `msgpack:"bool_map"`
AnyMap map[string]any `msgpack:"any_map"`
}
original := AllTypes{
String: "test",
Int: -1,
Int64: -1000000,
Int32: -50000,
Int16: -1000,
Int8: -100,
Uint: 1,
Uint64: 1000000,
Uint32: 50000,
Uint16: 1000,
Uint8: 200,
Float64: 3.14159,
Float32: 2.71828,
Bool: true,
Bytes: []byte{1, 2, 3},
Strings: []string{"a", "b"},
Ints: []int{1, 2, 3},
Int64s: []int64{-1, -2},
Int32s: []int32{-10, -20},
Int16s: []int16{-100, -200},
Int8s: []int8{-1, -2},
Uints: []uint{1, 2},
Uint64s: []uint64{100, 200},
Uint32s: []uint32{10, 20},
Uint16s: []uint16{1, 2},
Float64s: []float64{1.1, 2.2},
Float32s: []float32{1.5, 2.5},
StringMap: map[string]string{"a": "x"},
IntMap: map[string]int{"i": 1},
Int64Map: map[string]int64{"i64": -100},
Int32Map: map[string]int32{"i32": -50},
Uint64Map: map[string]uint64{"u64": 1000},
Uint32Map: map[string]uint32{"u32": 100},
Float64M: map[string]float64{"f": 1.5},
BoolMap: map[string]bool{"b": true},
AnyMap: map[string]any{"str": "hello", "num": 42},
}
enc := GetStructEncoder[AllTypes]()
dec := GetStructDecoder[AllTypes](false)
data, err := enc.Encode(&original)
if err != nil {
t.Fatalf("encode failed: %v", err)
}
var result AllTypes
err = dec.Decode(data, &result)
if err != nil {
t.Fatalf(errMsgDecodeFailedFmt, err)
}
// Verify a sampling of fields
if result.String != original.String {
t.Errorf("String mismatch")
}
if result.Int64 != original.Int64 {
t.Errorf("Int64 mismatch")
}
if len(result.Strings) != len(original.Strings) {
t.Errorf("Strings length mismatch")
}
if result.StringMap["a"] != "x" {
t.Errorf("StringMap mismatch")
}
}