-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_test.go
More file actions
574 lines (522 loc) · 13.5 KB
/
encoder_test.go
File metadata and controls
574 lines (522 loc) · 13.5 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
package msgpck
import (
"bytes"
"testing"
)
const (
errMsgDecodeInt64ArrayFailed = "DecodeInt64Array failed: %v"
errMsgIndexGotWant = "index %d: got %d, want %d"
)
func TestEncoderBufferGrowth(t *testing.T) {
e := NewEncoder(1) // Start with tiny buffer
// Write something larger than initial buffer
bigString := string(make([]byte, 100))
e.EncodeString(bigString)
if len(e.Bytes()) == 0 {
t.Error("encoder should have grown buffer")
}
}
func TestEncoderStringFormats(t *testing.T) {
e := NewEncoder(256)
// fixstr (0-31 bytes)
e.Reset()
e.EncodeString("hi")
if e.Bytes()[0]&0xe0 != 0xa0 {
t.Error("short string should use fixstr")
}
// str8 (32-255 bytes)
e.Reset()
e.EncodeString(string(make([]byte, 50)))
if e.Bytes()[0] != formatStr8 {
t.Error("medium string should use str8")
}
// str16 (256-65535 bytes)
e.Reset()
e.EncodeString(string(make([]byte, 300)))
if e.Bytes()[0] != formatStr16 {
t.Error("large string should use str16")
}
}
func TestEncoderBinaryFormats(t *testing.T) {
e := NewEncoder(256)
// bin8 (0-255 bytes)
e.Reset()
e.EncodeBinary(make([]byte, 50))
if e.Bytes()[0] != formatBin8 {
t.Error("small binary should use bin8")
}
// bin16 (256-65535 bytes)
e.Reset()
e.EncodeBinary(make([]byte, 300))
if e.Bytes()[0] != formatBin16 {
t.Error("medium binary should use bin16")
}
}
func TestEncoderMapHeader(t *testing.T) {
e := NewEncoder(32)
// fixmap (0-15)
e.Reset()
e.EncodeMapHeader(5)
if e.Bytes()[0]&0xf0 != 0x80 {
t.Error("small map should use fixmap")
}
// map16 (16-65535)
e.Reset()
e.EncodeMapHeader(20)
if e.Bytes()[0] != formatMap16 {
t.Error("medium map should use map16")
}
}
func TestEncoderArrayHeader(t *testing.T) {
e := NewEncoder(32)
// fixarray (0-15)
e.Reset()
e.EncodeArrayHeader(5)
if e.Bytes()[0]&0xf0 != 0x90 {
t.Error("small array should use fixarray")
}
// array16 (16-65535)
e.Reset()
e.EncodeArrayHeader(20)
if e.Bytes()[0] != formatArray16 {
t.Error("medium array should use array16")
}
}
// TestEncoderIntFormats tests integer encoding formats
func TestEncoderIntFormats(t *testing.T) {
tests := []struct {
val int64
expect byte
}{
{0, 0x00},
{127, 0x7f},
{-1, 0xff},
{-32, 0xe0},
{-33, formatInt8},
{128, formatUint8},
{256, formatUint16},
{65536, formatUint32},
{-129, formatInt16},
{-32769, formatInt32},
}
for _, tc := range tests {
e := NewEncoder(16)
e.EncodeInt(tc.val)
if e.Bytes()[0] != tc.expect {
t.Errorf("EncodeInt(%d): got format 0x%x, want 0x%x", tc.val, e.Bytes()[0], tc.expect)
}
}
}
// TestEncoderUintFormats tests unsigned integer encoding formats
func TestEncoderUintFormats(t *testing.T) {
tests := []struct {
val uint64
expect byte
}{
{0, 0x00},
{127, 0x7f},
{128, formatUint8},
{256, formatUint16},
{65536, formatUint32},
{1 << 32, formatUint64},
}
for _, tc := range tests {
e := NewEncoder(16)
e.EncodeUint(tc.val)
if e.Bytes()[0] != tc.expect {
t.Errorf("EncodeUint(%d): got format 0x%x, want 0x%x", tc.val, e.Bytes()[0], tc.expect)
}
}
}
// TestEncoderStringFormatsTable tests string encoding formats with table-driven tests
func TestEncoderStringFormatsTable(t *testing.T) {
tests := []struct {
len int
expect byte
}{
{0, 0xa0},
{31, 0xbf},
{32, formatStr8},
{255, formatStr8},
{256, formatStr16},
{65535, formatStr16},
}
for _, tc := range tests {
e := NewEncoder(tc.len + 10)
e.EncodeString(string(make([]byte, tc.len)))
if e.Bytes()[0] != tc.expect {
t.Errorf("EncodeString(len=%d): got format 0x%x, want 0x%x", tc.len, e.Bytes()[0], tc.expect)
}
}
}
// TestEncoderBinaryFormatsTable tests binary encoding formats with table-driven tests
func TestEncoderBinaryFormatsTable(t *testing.T) {
tests := []struct {
len int
expect byte
}{
{0, formatBin8},
{255, formatBin8},
{256, formatBin16},
{65535, formatBin16},
}
for _, tc := range tests {
e := NewEncoder(tc.len + 10)
e.EncodeBinary(make([]byte, tc.len))
if e.Bytes()[0] != tc.expect {
t.Errorf("EncodeBinary(len=%d): got format 0x%x, want 0x%x", tc.len, e.Bytes()[0], tc.expect)
}
}
}
// TestEncoderArrayFormatsTable tests array header encoding formats
func TestEncoderArrayFormatsTable(t *testing.T) {
tests := []struct {
len int
expect byte
}{
{0, 0x90},
{15, 0x9f},
{16, formatArray16},
{65535, formatArray16},
}
for _, tc := range tests {
e := NewEncoder(16)
e.EncodeArrayHeader(tc.len)
if e.Bytes()[0] != tc.expect {
t.Errorf("EncodeArrayHeader(%d): got format 0x%x, want 0x%x", tc.len, e.Bytes()[0], tc.expect)
}
}
}
// TestEncoderMapFormatsTable tests map header encoding formats
func TestEncoderMapFormatsTable(t *testing.T) {
tests := []struct {
len int
expect byte
}{
{0, 0x80},
{15, 0x8f},
{16, formatMap16},
{65535, formatMap16},
}
for _, tc := range tests {
e := NewEncoder(16)
e.EncodeMapHeader(tc.len)
if e.Bytes()[0] != tc.expect {
t.Errorf("EncodeMapHeader(%d): got format 0x%x, want 0x%x", tc.len, e.Bytes()[0], tc.expect)
}
}
}
// TestEncoderFloatFormats tests float encoding formats
func TestEncoderFloatFormats(t *testing.T) {
t.Run("float32", func(t *testing.T) {
e := NewEncoder(16)
e.EncodeFloat32(3.14)
if e.Bytes()[0] != formatFloat32 {
t.Error("EncodeFloat32 format wrong")
}
})
t.Run("float64", func(t *testing.T) {
e := NewEncoder(16)
e.EncodeFloat64(3.14)
if e.Bytes()[0] != formatFloat64 {
t.Error("EncodeFloat64 format wrong")
}
})
}
// TestEncodeValue tests encoding Value types
func TestEncodeValue(t *testing.T) {
tests := []struct {
name string
v Value
}{
{"nil", Value{Type: TypeNil}},
{"bool true", Value{Type: TypeBool, Bool: true}},
{"bool false", Value{Type: TypeBool, Bool: false}},
{"int", Value{Type: TypeInt, Int: -42}},
{"uint", Value{Type: TypeUint, Uint: 42}},
{"float32", Value{Type: TypeFloat32, Float32: 3.14}},
{"float64", Value{Type: TypeFloat64, Float64: 3.14159}},
{"string", Value{Type: TypeString, Bytes: []byte("hello")}},
{"binary", Value{Type: TypeBinary, Bytes: []byte{1, 2, 3}}},
{"array", Value{Type: TypeArray, Array: []Value{{Type: TypeInt, Int: 1}}}},
{"map", Value{Type: TypeMap, Map: []KV{{Key: []byte("k"), Value: Value{Type: TypeInt, Int: 1}}}}},
{"ext", Value{Type: TypeExt, Ext: Ext{Type: 1, Data: []byte{1, 2, 3, 4}}}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
e := NewEncoder(64)
e.EncodeValue(&tc.v)
if len(e.Bytes()) == 0 {
t.Error("EncodeValue produced no output")
}
})
}
}
// TestNewEncoderBuffer tests NewEncoderBuffer
func TestNewEncoderBuffer(t *testing.T) {
buf := make([]byte, 0, 100)
e := NewEncoderBuffer(buf)
e.EncodeString("test")
if e.Len() == 0 {
t.Error("NewEncoderBuffer failed")
}
}
// TestExtension tests extension type encoding/decoding
func TestExtension(t *testing.T) {
e := NewEncoder(32)
e.EncodeExt(42, []byte{1, 2, 3, 4})
d := NewDecoder(e.Bytes())
v, err := d.Decode()
if err != nil {
t.Fatalf("Decode failed: %v", err)
}
if v.Type != TypeExt {
t.Fatalf("expected TypeExt, got %v", v.Type)
}
if v.Ext.Type != 42 {
t.Errorf("ext type = %d, want 42", v.Ext.Type)
}
if !bytes.Equal(v.Ext.Data, []byte{1, 2, 3, 4}) {
t.Errorf("ext data = %v, want [1 2 3 4]", v.Ext.Data)
}
}
// TestEncoderBatchArrays tests batch array encoding functions
func TestEncoderBatchArrays(t *testing.T) {
t.Run("EncodeInt64Array", func(t *testing.T) {
e := NewEncoder(64)
arr := []int64{1, -2, 127, -128, 32767}
e.EncodeInt64Array(arr)
d := NewDecoder(e.Bytes())
result, err := d.DecodeInt64Array()
if err != nil {
t.Fatalf(errMsgDecodeInt64ArrayFailed, err)
}
if len(result) != len(arr) {
t.Errorf(errMsgLengthMismatch, len(result), len(arr))
}
for i := range arr {
if result[i] != arr[i] {
t.Errorf(errMsgIndexGotWant, i, result[i], arr[i])
}
}
})
t.Run("EncodeUint64Array", func(t *testing.T) {
e := NewEncoder(64)
arr := []uint64{0, 127, 255, 65535, 1 << 32}
e.EncodeUint64Array(arr)
d := NewDecoder(e.Bytes())
result, err := d.DecodeUint64Array()
if err != nil {
t.Fatalf("DecodeUint64Array failed: %v", err)
}
if len(result) != len(arr) {
t.Errorf(errMsgLengthMismatch, len(result), len(arr))
}
for i := range arr {
if result[i] != arr[i] {
t.Errorf(errMsgIndexGotWant, i, result[i], arr[i])
}
}
})
t.Run("EncodeFloat64Array", func(t *testing.T) {
e := NewEncoder(64)
arr := []float64{0.0, 1.5, -3.14159, 1e10}
e.EncodeFloat64Array(arr)
d := NewDecoder(e.Bytes())
result, err := d.DecodeFloat64Array()
if err != nil {
t.Fatalf("DecodeFloat64Array failed: %v", err)
}
if len(result) != len(arr) {
t.Errorf(errMsgLengthMismatch, len(result), len(arr))
}
for i := range arr {
if result[i] != arr[i] {
t.Errorf("index %d: got %f, want %f", i, result[i], arr[i])
}
}
})
t.Run("EncodeStringArray", func(t *testing.T) {
e := NewEncoder(128)
arr := []string{"hello", "world", "", "test123"}
e.EncodeStringArray(arr)
d := NewDecoder(e.Bytes())
result, err := d.DecodeStringArray()
if err != nil {
t.Fatalf("DecodeStringArray failed: %v", err)
}
if len(result) != len(arr) {
t.Errorf(errMsgLengthMismatch, len(result), len(arr))
}
for i := range arr {
if result[i] != arr[i] {
t.Errorf("index %d: got %q, want %q", i, result[i], arr[i])
}
}
})
}
// TestEncoderLargeArrays tests array16/array32 format encoding.
func TestEncoderLargeArrays(t *testing.T) {
t.Run("array16 format", func(t *testing.T) {
e := NewEncoder(1024)
// Create array with 20 elements (triggers array16 format, not fixarray)
arr := make([]int64, 20)
for i := range arr {
arr[i] = int64(i * 10)
}
e.EncodeInt64Array(arr)
d := NewDecoder(e.Bytes())
result, err := d.DecodeInt64Array()
if err != nil {
t.Fatalf(errMsgDecodeInt64ArrayFailed, err)
}
if len(result) != 20 {
t.Errorf("length mismatch: got %d, want 20", len(result))
}
})
t.Run("array with various int formats", func(t *testing.T) {
e := NewEncoder(256)
// Mix of different integer formats
arr := []int64{
0, // positive fixint
127, // positive fixint max
-1, // negative fixint
-32, // negative fixint min
200, // uint8
-100, // int8
30000, // uint16/int16
-30000, // int16
70000, // uint32
-70000, // int32
5000000000, // int64
-5000000000, // int64
}
e.EncodeInt64Array(arr)
d := NewDecoder(e.Bytes())
result, err := d.DecodeInt64Array()
if err != nil {
t.Fatalf(errMsgDecodeInt64ArrayFailed, err)
}
for i := range arr {
if result[i] != arr[i] {
t.Errorf(errMsgIndexGotWant, i, result[i], arr[i])
}
}
})
t.Run("large array header", func(t *testing.T) {
e := NewEncoder(8192)
// Create 100 element array
arr := make([]int64, 100)
for i := range arr {
arr[i] = int64(i)
}
e.EncodeInt64Array(arr)
d := NewDecoder(e.Bytes())
result, err := d.DecodeInt64Array()
if err != nil {
t.Fatalf(errMsgDecodeInt64ArrayFailed, err)
}
if len(result) != 100 {
t.Errorf("length mismatch: got %d, want 100", len(result))
}
})
}
// TestEncoderLargeMaps tests map16/map32 format encoding.
func TestEncoderLargeMaps(t *testing.T) {
t.Run("map16 format", func(t *testing.T) {
e := NewEncoder(2048)
// Create map with 20 entries (triggers map16 format)
e.EncodeMapHeader(20)
for i := 0; i < 20; i++ {
e.EncodeString("key" + string(rune('a'+i)))
e.EncodeInt(int64(i * 100))
}
d := NewDecoder(e.Bytes())
v, err := d.Decode()
if err != nil {
t.Fatalf("Decode failed: %v", err)
}
if len(v.Map) != 20 {
t.Errorf("map length mismatch: got %d, want 20", len(v.Map))
}
})
}
// TestDecodeFloatFromIntegers tests decoding various integer formats into float64.
func TestDecodeFloatFromIntegers(t *testing.T) {
type FloatData struct {
F float64 `msgpack:"f"`
}
tests := []struct {
name string
encode func(*Encoder)
want float64
}{
{"positive fixint", func(e *Encoder) {
e.EncodeMapHeader(1)
e.EncodeString("f")
e.writeByte(42) // positive fixint
}, 42.0},
{"negative fixint", func(e *Encoder) {
e.EncodeMapHeader(1)
e.EncodeString("f")
e.writeByte(0xff) // -1 as negative fixint
}, -1.0},
{"uint8", func(e *Encoder) {
e.EncodeMapHeader(1)
e.EncodeString("f")
e.writeByte(formatUint8)
e.writeByte(200)
}, 200.0},
{"uint16", func(e *Encoder) {
e.EncodeMapHeader(1)
e.EncodeString("f")
e.writeByte(formatUint16)
e.writeUint16(40000)
}, 40000.0},
{"uint32", func(e *Encoder) {
e.EncodeMapHeader(1)
e.EncodeString("f")
e.writeByte(formatUint32)
e.writeUint32(100000)
}, 100000.0},
{"int8", func(e *Encoder) {
e.EncodeMapHeader(1)
e.EncodeString("f")
e.writeByte(formatInt8)
e.writeByte(0x80) // -128
}, -128.0},
{"int16", func(e *Encoder) {
e.EncodeMapHeader(1)
e.EncodeString("f")
e.writeByte(formatInt16)
e.writeUint16(0x8000) // -32768
}, -32768.0},
{"int32", func(e *Encoder) {
e.EncodeMapHeader(1)
e.EncodeString("f")
e.writeByte(formatInt32)
e.writeUint32(0xFFFFFF00) // -256
}, -256.0},
{"int64", func(e *Encoder) {
e.EncodeMapHeader(1)
e.EncodeString("f")
e.writeByte(formatInt64)
e.writeUint64(0xFFFFFFFFFFFFFC18) // -1000
}, -1000.0},
}
dec := GetStructDecoder[FloatData](false)
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
e := NewEncoder(64)
tc.encode(e)
var result FloatData
err := dec.Decode(e.Bytes(), &result)
if err != nil {
t.Fatalf("decode failed: %v", err)
}
if result.F != tc.want {
t.Errorf("got %f, want %f", result.F, tc.want)
}
})
}
}