-
Notifications
You must be signed in to change notification settings - Fork 294
/
codec_test.go
5362 lines (4694 loc) · 139 KB
/
codec_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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
// Use of this source code is governed by a MIT license found in the LICENSE file.
package codec
// NAMING CONVENTION FOR TESTS
//
// function and variable/const names here fit a simple naming convention
//
// - each test is a doTestXXX(...). TestXXX calls doTestXXX.
// - testXXX are helper functions.
// - doTestXXX are test functions that take an extra arg of a handle.
// - testXXX variables and constants are only used in tests.
// - shared functions/vars/consts are testShared...
// - fnBenchmarkXX and fnTestXXX can be used as needed.
//
// - each TestXXX must only call testSetup once.
// - each test with a prefix __doTest is a dependent helper function,
// which MUST not call testSetup itself.
// doTestXXX or TestXXX may call it.
// if we are testing in parallel,
// then we don't want to share much state: testBytesFreeList, etc.
import (
"bufio"
"bytes"
"encoding/gob"
"errors"
"fmt"
"io"
"io/ioutil"
"math"
"math/rand"
"net"
"net/rpc"
"os"
"os/exec"
"path/filepath"
"reflect"
"strconv"
"strings"
"sync/atomic"
"testing"
"time"
)
func init() {
testPreInitFns = append(testPreInitFns, testInit)
}
const testRecoverPanicToErr = !debugging
// tests which check for errors will fail if testRecoverPanicToErr=false (debugging=true).
// Consequently, skip them.
var testSkipIfNotRecoverPanicToErrMsg = "tests checks for errors, and testRecoverPanicToErr=false"
func testPanicToErr(h errDecorator, err *error) {
// Note: This method MUST be called directly from defer i.e. defer testPanicToErr ...
// else it seems the recover is not fully handled
if x := recover(); x != nil {
panicValToErr(h, x, err)
}
}
var testBytesFreeList bytesFreelist
type testCustomStringT string
// make these mapbyslice
type testMbsT []interface{}
type testMbsArr0T [0]interface{}
type testMbsArr4T [4]interface{}
type testMbsArr5T [5]interface{}
type testMbsCustStrT []testCustomStringT
func (testMbsT) MapBySlice() {}
func (*testMbsArr0T) MapBySlice() {}
func (*testMbsArr4T) MapBySlice() {}
func (testMbsArr5T) MapBySlice() {}
func (testMbsCustStrT) MapBySlice() {}
// type testSelferRecur struct{}
// func (s *testSelferRecur) CodecEncodeSelf(e *Encoder) {
// e.MustEncode(s)
// }
// func (s *testSelferRecur) CodecDecodeSelf(d *Decoder) {
// d.MustDecode(s)
// }
type testIntfMapI interface {
GetIntfMapV() string
}
type testIntfMapT1 struct {
IntfMapV string
}
func (x *testIntfMapT1) GetIntfMapV() string { return x.IntfMapV }
type testIntfMapT2 struct {
IntfMapV string
}
func (x testIntfMapT2) GetIntfMapV() string { return x.IntfMapV }
type testMissingFieldsMap struct {
m map[string]interface{}
}
func (mf *testMissingFieldsMap) CodecMissingField(field []byte, value interface{}) bool {
if mf.m == nil {
mf.m = map[string]interface{}{}
}
(mf.m)[string(field)] = value
return true
}
func (mf *testMissingFieldsMap) CodecMissingFields() map[string]interface{} {
return mf.m
}
var _ MissingFielder = (*testMissingFieldsMap)(nil)
var testErrWriterErr = errors.New("testErrWriterErr")
type testErrWriter struct{}
func (x *testErrWriter) Write(p []byte) (int, error) {
return 0, testErrWriterErr
}
// ----
type testVerifyFlag uint8
const (
_ testVerifyFlag = 1 << iota
testVerifyMapTypeSame
testVerifyMapTypeStrIntf
testVerifyMapTypeIntfIntf
// testVerifySliceIntf
testVerifyForPython
testVerifyDoNil
testVerifyTimeAsInteger
)
func (f testVerifyFlag) isset(v testVerifyFlag) bool {
return f&v == v
}
// const testSkipRPCTests = false
var (
testTableNumPrimitives int
testTableIdxTime int
testTableNumMaps int
// set this when running using bufio, etc
testSkipRPCTests = false
testSkipRPCTestsMsg = "testSkipRPCTests=true"
)
var (
skipVerifyVal interface{} = &(struct{}{})
testMapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
// For Go Time, do not use a descriptive timezone.
// It's unnecessary, and makes it harder to do a reflect.DeepEqual.
// The Offset already tells what the offset should be, if not on UTC and unknown zone name.
timeLoc = time.FixedZone("", -8*60*60) // UTC-08:00 //time.UTC-8
timeToCompare1 = time.Date(2012, 2, 2, 2, 2, 2, 2000, timeLoc).UTC()
timeToCompare2 = time.Date(1900, 2, 2, 2, 2, 2, 2000, timeLoc).UTC()
timeToCompare3 = time.Unix(0, 270).UTC() // use value that must be encoded as uint64 for nanoseconds (for cbor/msgpack comparison)
//timeToCompare4 = time.Time{}.UTC() // does not work well with simple cbor time encoding (overflow)
timeToCompare4 = time.Unix(-2013855848, 4223).UTC()
table []interface{} // main items we encode
// will encode a float32 as float64, or large int as uint
testRpcServer = rpc.NewServer()
testRpcInt = new(TestRpcInt)
)
func init() {
testRpcServer.Register(testRpcInt)
}
var wrapInt64Typ = reflect.TypeOf(wrapInt64(0))
var wrapBytesTyp = reflect.TypeOf(wrapBytes(nil))
var testUintToBytesTyp = reflect.TypeOf(testUintToBytes(0))
var testSelfExtTyp = reflect.TypeOf((*TestSelfExtImpl)(nil)).Elem()
var testSelfExt2Typ = reflect.TypeOf((*TestSelfExtImpl2)(nil)).Elem()
func testByteBuf(in []byte) *bytes.Buffer {
return bytes.NewBuffer(in)
}
type TestABC struct {
A, B, C string
}
func (x *TestABC) MarshalBinary() ([]byte, error) {
return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
}
func (x *TestABC) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
}
func (x *TestABC) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s %s %s"`, x.A, x.B, x.C)), nil
}
func (x *TestABC) UnmarshalBinary(data []byte) (err error) {
ss := strings.Split(string(data), " ")
x.A, x.B, x.C = ss[0], ss[1], ss[2]
return
}
func (x *TestABC) UnmarshalText(data []byte) (err error) {
return x.UnmarshalBinary(data)
}
func (x *TestABC) UnmarshalJSON(data []byte) (err error) {
return x.UnmarshalBinary(data[1 : len(data)-1])
}
type TestABC2 struct {
A, B, C string
}
func (x TestABC2) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
}
func (x *TestABC2) UnmarshalText(data []byte) (err error) {
ss := strings.Split(string(data), " ")
x.A, x.B, x.C = ss[0], ss[1], ss[2]
return
// _, err = fmt.Sscanf(string(data), "%s %s %s", &x.A, &x.B, &x.C)
}
type TestSimplish struct {
Ii int
Ss string
Ar [2]*TestSimplish
Sl []*TestSimplish
Mm map[string]*TestSimplish
}
type TestRpcABC struct {
A, B, C string
}
type TestRpcInt struct {
i int64
}
func (r *TestRpcInt) Update(n int, res *int) error {
atomic.StoreInt64(&r.i, int64(n))
*res = n
return nil
}
func (r *TestRpcInt) Square(ignore int, res *int) error {
i := int(atomic.LoadInt64(&r.i))
*res = i * i
return nil
}
func (r *TestRpcInt) Mult(n int, res *int) error {
*res = int(atomic.LoadInt64(&r.i)) * n
return nil
}
func (r *TestRpcInt) EchoStruct(arg TestRpcABC, res *string) error {
*res = fmt.Sprintf("%#v", arg)
return nil
}
func (r *TestRpcInt) Echo123(args []string, res *string) error {
*res = fmt.Sprintf("%#v", args)
return nil
}
type TestRawValue struct {
R Raw
I int
}
// ----
type testUnixNanoTimeExt struct {
// keep timestamp here, so that do not incur interface-conversion costs
// ts int64
}
func (x *testUnixNanoTimeExt) WriteExt(v interface{}) []byte {
v2 := v.(*time.Time)
bs := make([]byte, 8)
bigenstd.PutUint64(bs, uint64(v2.UnixNano()))
return bs
}
func (x *testUnixNanoTimeExt) ReadExt(v interface{}, bs []byte) {
v2 := v.(*time.Time)
ui := bigenstd.Uint64(bs)
*v2 = time.Unix(0, int64(ui)).UTC()
}
type testUnixNanoTimeInterfaceExt struct{}
func (x testUnixNanoTimeInterfaceExt) ConvertExt(v interface{}) interface{} {
v2 := v.(*time.Time) // structs are encoded by passing the ptr
return v2.UTC().UnixNano()
}
func (x testUnixNanoTimeInterfaceExt) UpdateExt(dest interface{}, v interface{}) {
tt := dest.(*time.Time)
*tt = time.Unix(0, v.(int64)).UTC()
// switch v2 := v.(type) {
// case int64:
// *tt = time.Unix(0, v2).UTC()
// case uint64:
// *tt = time.Unix(0, int64(v2)).UTC()
// //case float64:
// //case string:
// default:
// panic(fmt.Errorf("unsupported format for time conversion: expecting int64/uint64; got %T", v))
// }
}
// ----
type wrapInt64Ext int64
func (x *wrapInt64Ext) WriteExt(v interface{}) []byte {
v2 := uint64(int64(v.(wrapInt64)))
bs := make([]byte, 8)
bigenstd.PutUint64(bs, v2)
return bs
}
func (x *wrapInt64Ext) ReadExt(v interface{}, bs []byte) {
v2 := v.(*wrapInt64)
ui := bigenstd.Uint64(bs)
*v2 = wrapInt64(int64(ui))
}
func (x *wrapInt64Ext) ConvertExt(v interface{}) interface{} {
return int64(v.(wrapInt64))
}
func (x *wrapInt64Ext) UpdateExt(dest interface{}, v interface{}) {
v2 := dest.(*wrapInt64)
*v2 = wrapInt64(v.(int64))
}
// ----
type wrapBytesExt struct{}
func (x *wrapBytesExt) WriteExt(v interface{}) []byte {
return ([]byte)(v.(wrapBytes))
}
func (x *wrapBytesExt) ReadExt(v interface{}, bs []byte) {
v2 := v.(*wrapBytes)
*v2 = wrapBytes(bs)
}
func (x *wrapBytesExt) ConvertExt(v interface{}) interface{} {
return ([]byte)(v.(wrapBytes))
}
func (x *wrapBytesExt) UpdateExt(dest interface{}, v interface{}) {
v2 := dest.(*wrapBytes)
// some formats (e.g. json) cannot nakedly determine []byte from string, so expect both
switch v3 := v.(type) {
case []byte:
*v2 = wrapBytes(v3)
case string:
*v2 = wrapBytes([]byte(v3))
default:
panic(errors.New("UpdateExt for wrapBytesExt expects string or []byte"))
}
// *v2 = wrapBytes(v.([]byte))
}
// ----
// timeExt is an extension handler for time.Time, that uses binc model for encoding/decoding time.
// we used binc model, as that is the only custom time representation that we designed ourselves.
type timeBytesExt struct{}
func (x timeBytesExt) WriteExt(v interface{}) (bs []byte) {
return bincEncodeTime(*(v.(*time.Time)))
// return bincEncodeTime(v.(time.Time))
// switch v2 := v.(type) {
// case time.Time:
// bs = bincEncodeTime(v2)
// case *time.Time:
// bs = bincEncodeTime(*v2)
// default:
// panic(fmt.Errorf("unsupported format for time conversion: expecting time.Time; got %T", v2))
// }
// return
}
func (x timeBytesExt) ReadExt(v interface{}, bs []byte) {
tt, err := bincDecodeTime(bs)
if err != nil {
panic(err)
}
*(v.(*time.Time)) = tt
}
type timeInterfaceExt struct{}
func (x timeInterfaceExt) ConvertExt(v interface{}) interface{} {
return timeBytesExt{}.WriteExt(v)
}
func (x timeInterfaceExt) UpdateExt(v interface{}, src interface{}) {
timeBytesExt{}.ReadExt(v, src.([]byte))
}
// ----
type testUintToBytesExt struct{}
func (x testUintToBytesExt) WriteExt(v interface{}) (bs []byte) {
z := uint32(v.(testUintToBytes))
if z == 0 {
return nil
}
return make([]byte, z)
}
func (x testUintToBytesExt) ReadExt(v interface{}, bs []byte) {
*(v.(*testUintToBytes)) = testUintToBytes(len(bs))
}
func (x testUintToBytesExt) ConvertExt(v interface{}) interface{} {
return x.WriteExt(v)
}
func (x testUintToBytesExt) UpdateExt(v interface{}, src interface{}) {
x.ReadExt(v, src.([]byte))
}
// ----
func testSetupNoop() {}
func testSetup(t *testing.T, h *Handle) (fn func()) {
return testSetupWithChecks(t, h, true)
}
// testSetup will ensure testInitAll is run, and then
// return a function that should be deferred to run at the end
// of the test.
//
// This function can track how much time a test took,
// or recover from panic's and fail the test appropriately.
func testSetupWithChecks(t *testing.T, h *Handle, allowParallel bool) (fn func()) {
testOnce.Do(testInitAll)
if allowParallel && testUseParallel {
t.Parallel()
if h != nil {
*h = testHandleCopy(*h)
}
}
// in case an error is seen, recover it here.
if testRecoverPanicToErr {
fnRecoverPanic := func() {
if x := recover(); x != nil {
var err error
panicValToErr(errDecoratorDef{}, x, &err)
t.Logf("recovered error: %v", err)
t.FailNow()
}
}
fn = fnRecoverPanic
}
if fn == nil {
fn = testSetupNoop
}
return
}
func testBasicHandle(h Handle) *BasicHandle { return h.getBasicHandle() }
func testCodecEncode(ts interface{}, bsIn []byte, fn func([]byte) *bytes.Buffer, h Handle, useMust bool) (bs []byte, err error) {
return testSharedCodecEncode(ts, bsIn, fn, h, testBasicHandle(h), useMust)
}
func testCodecDecode(bs []byte, ts interface{}, h Handle, useMust bool) (err error) {
return testSharedCodecDecode(bs, ts, h, testBasicHandle(h), useMust)
}
func testCheckErr(t *testing.T, err error) {
if err != nil {
t.Logf("err: %v", err)
t.FailNow()
}
}
func testCheckEqual(t *testing.T, v1 interface{}, v2 interface{}, desc string) {
t.Helper()
if err := deepEqual(v1, v2); err != nil {
t.Logf("Not Equal: %s: %v", desc, err)
if testVerbose {
t.Logf("\tv1: %v, v2: %v", v1, v2)
}
t.FailNow()
}
}
func testInit() {
gob.Register(new(TestStrucFlex))
for _, v := range testHandles {
bh := testBasicHandle(v)
bh.clearInited() // so it is reinitialized next time around
// pre-fill them first
bh.EncodeOptions = testEncodeOptions
bh.DecodeOptions = testDecodeOptions
bh.RPCOptions = testRPCOptions
// bh.InterfaceReset = true
// bh.PreferArrayOverSlice = true
// modify from flag'ish things
bh.MaxInitLen = testMaxInitLen
}
var tTimeExt timeBytesExt
var tBytesExt wrapBytesExt
var tI64Ext wrapInt64Ext
var tUintToBytesExt testUintToBytesExt
// create legacy functions suitable for deprecated AddExt functionality,
// and use on some places for testSimpleH e.g. for time.Time and wrapInt64
var (
myExtEncFn = func(x BytesExt, rv reflect.Value) (bs []byte, err error) {
defer testPanicToErr(errDecoratorDef{}, &err)
bs = x.WriteExt(rv.Interface())
return
}
myExtDecFn = func(x BytesExt, rv reflect.Value, bs []byte) (err error) {
defer testPanicToErr(errDecoratorDef{}, &err)
x.ReadExt(rv.Interface(), bs)
return
}
timeExtEncFn = func(rv reflect.Value) (bs []byte, err error) { return myExtEncFn(tTimeExt, rv) }
timeExtDecFn = func(rv reflect.Value, bs []byte) (err error) { return myExtDecFn(tTimeExt, rv, bs) }
wrapInt64ExtEncFn = func(rv reflect.Value) (bs []byte, err error) { return myExtEncFn(&tI64Ext, rv) }
wrapInt64ExtDecFn = func(rv reflect.Value, bs []byte) (err error) { return myExtDecFn(&tI64Ext, rv, bs) }
)
chkErr := func(err error) {
if err != nil {
panic(err)
}
}
// time.Time is a native type, so extensions will have no effect.
// However, we add these here to ensure nothing happens.
chkErr(testSimpleH.AddExt(timeTyp, 1, timeExtEncFn, timeExtDecFn))
// testBincH.SetBytesExt(timeTyp, 1, timeExt{}) // time is builtin for binc
chkErr(testMsgpackH.SetBytesExt(timeTyp, 1, timeBytesExt{}))
chkErr(testCborH.SetInterfaceExt(timeTyp, 1, testUnixNanoTimeInterfaceExt{}))
// testJsonH.SetInterfaceExt(timeTyp, 1, &testUnixNanoTimeExt{})
// Add extensions for the testSelfExt
chkErr(testSimpleH.SetBytesExt(testSelfExtTyp, 78, SelfExt))
chkErr(testMsgpackH.SetBytesExt(testSelfExtTyp, 78, SelfExt))
chkErr(testBincH.SetBytesExt(testSelfExtTyp, 78, SelfExt))
chkErr(testJsonH.SetInterfaceExt(testSelfExtTyp, 78, SelfExt))
chkErr(testCborH.SetInterfaceExt(testSelfExtTyp, 78, SelfExt))
chkErr(testSimpleH.SetBytesExt(testSelfExt2Typ, 79, SelfExt))
chkErr(testMsgpackH.SetBytesExt(testSelfExt2Typ, 79, SelfExt))
chkErr(testBincH.SetBytesExt(testSelfExt2Typ, 79, SelfExt))
chkErr(testJsonH.SetInterfaceExt(testSelfExt2Typ, 79, SelfExt))
chkErr(testCborH.SetInterfaceExt(testSelfExt2Typ, 79, SelfExt))
// Now, add extensions for the type wrapInt64 and wrapBytes,
// so we can execute the Encode/Decode Ext paths.
chkErr(testSimpleH.SetBytesExt(wrapBytesTyp, 32, &tBytesExt))
chkErr(testMsgpackH.SetBytesExt(wrapBytesTyp, 32, &tBytesExt))
chkErr(testBincH.SetBytesExt(wrapBytesTyp, 32, &tBytesExt))
chkErr(testJsonH.SetInterfaceExt(wrapBytesTyp, 32, &tBytesExt))
chkErr(testCborH.SetInterfaceExt(wrapBytesTyp, 32, &tBytesExt))
chkErr(testSimpleH.SetBytesExt(testUintToBytesTyp, 33, &tUintToBytesExt))
chkErr(testMsgpackH.SetBytesExt(testUintToBytesTyp, 33, &tUintToBytesExt))
chkErr(testBincH.SetBytesExt(testUintToBytesTyp, 33, &tUintToBytesExt))
chkErr(testJsonH.SetInterfaceExt(testUintToBytesTyp, 33, &tUintToBytesExt))
chkErr(testCborH.SetInterfaceExt(testUintToBytesTyp, 33, &tUintToBytesExt))
chkErr(testSimpleH.AddExt(wrapInt64Typ, 16, wrapInt64ExtEncFn, wrapInt64ExtDecFn))
// chkErr(testSimpleH.SetBytesExt(wrapInt64Typ, 16, &tI64Ext))
chkErr(testMsgpackH.SetBytesExt(wrapInt64Typ, 16, &tI64Ext))
chkErr(testBincH.SetBytesExt(wrapInt64Typ, 16, &tI64Ext))
chkErr(testJsonH.SetInterfaceExt(wrapInt64Typ, 16, &tI64Ext))
chkErr(testCborH.SetInterfaceExt(wrapInt64Typ, 16, &tI64Ext))
// primitives MUST be an even number, so it can be used as a mapBySlice also.
primitives := []interface{}{
int8(-8),
int16(-1616),
int32(-32323232),
int64(-6464646464646464),
uint8(192),
uint16(1616),
uint32(32323232),
uint64(6464646464646464),
byte(192),
float32(-3232.0),
float64(-6464646464.0),
float32(3232.0),
float64(6464.0),
float64(6464646464.0),
complex64(complex(160.0, 0)),
complex128(complex(1616, 0)),
false,
true,
"null",
nil,
"some&day>some<day",
timeToCompare1,
"",
timeToCompare2,
"bytestring",
timeToCompare3,
"none",
timeToCompare4,
}
maps := []interface{}{
map[string]bool{
"true": true,
"false": false,
},
map[string]interface{}{
"true": "True",
"false": false,
"uint16(1616)": uint16(1616),
},
//add a complex combo map in here. (map has list which has map)
//note that after the first thing, everything else should be generic.
map[string]interface{}{
"list": []interface{}{
int16(1616),
int32(32323232),
true,
float32(-3232.0),
map[string]interface{}{
"TRUE": true,
"FALSE": false,
},
[]interface{}{true, false},
},
"int32": int32(32323232),
"bool": true,
"LONG STRING": `
1234567890 1234567890
1234567890 1234567890
1234567890 1234567890
ABCDEDFGHIJKLMNOPQRSTUVWXYZ
abcdedfghijklmnopqrstuvwxyz
ABCDEDFGHIJKLMNOPQRSTUVWXYZ
abcdedfghijklmnopqrstuvwxyz
"ABCDEDFGHIJKLMNOPQRSTUVWXYZ"
' a tab '
\a\b\c\d\e
\b\f\n\r\t all literally
ugorji
`,
"SHORT STRING": "1234567890",
},
map[interface{}]interface{}{
true: "true",
uint8(138): false,
false: uint8(200),
},
}
testTableNumPrimitives = len(primitives)
testTableIdxTime = testTableNumPrimitives - 8
testTableNumMaps = len(maps)
table = []interface{}{}
table = append(table, primitives...)
table = append(table, primitives)
table = append(table, testMbsT(primitives))
table = append(table, maps...)
table = append(table, newTestStrucFlex(0, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly))
// table = append(table, newTestStrucFlex(0, testNumRepeatString, true, !testSkipIntf, testMapStringKeyOnly))
}
func testTableVerify(f testVerifyFlag, h Handle) (av []interface{}) {
av = make([]interface{}, len(table))
lp := testTableNumPrimitives + 4
// doNil := f & testVerifyDoNil == testVerifyDoNil
// doPython := f & testVerifyForPython == testVerifyForPython
switch {
case f.isset(testVerifyForPython):
for i, v := range table {
if i == testTableNumPrimitives+1 || i > lp { // testTableNumPrimitives+1 is the mapBySlice
av[i] = skipVerifyVal
continue
}
av[i] = testVerifyVal(v, f, h)
}
// only do the python verify up to the maps, skipping the last 2 maps.
av = av[:testTableNumPrimitives+2+testTableNumMaps-2]
case f.isset(testVerifyDoNil):
for i, v := range table {
if i > lp {
av[i] = skipVerifyVal
continue
}
av[i] = testVerifyVal(v, f, h)
}
default:
for i, v := range table {
if i == lp {
av[i] = skipVerifyVal
continue
}
//av[i] = testVerifyVal(v, testVerifyMapTypeSame)
switch v.(type) {
case []interface{}:
av[i] = testVerifyVal(v, f, h)
case testMbsT:
av[i] = testVerifyVal(v, f, h)
case map[string]interface{}:
av[i] = testVerifyVal(v, f, h)
case map[interface{}]interface{}:
av[i] = testVerifyVal(v, f, h)
case time.Time:
av[i] = testVerifyVal(v, f, h)
default:
av[i] = v
}
}
}
return
}
func testVerifyValInt(v int64, isMsgp bool) (v2 interface{}) {
if isMsgp {
if v >= 0 && v <= 127 {
v2 = uint64(v)
} else {
v2 = int64(v)
}
} else if v >= 0 {
v2 = uint64(v)
} else {
v2 = int64(v)
}
return
}
func testVerifyVal(v interface{}, f testVerifyFlag, h Handle) (v2 interface{}) {
//for python msgpack,
// - all positive integers are unsigned 64-bit ints
// - all floats are float64
_, isMsgp := h.(*MsgpackHandle)
_, isCbor := h.(*CborHandle)
switch iv := v.(type) {
case int8:
v2 = testVerifyValInt(int64(iv), isMsgp)
// fmt.Printf(">>>> is msgp: %v, v: %T, %v ==> v2: %T, %v\n", isMsgp, v, v, v2, v2)
case int16:
v2 = testVerifyValInt(int64(iv), isMsgp)
case int32:
v2 = testVerifyValInt(int64(iv), isMsgp)
case int64:
v2 = testVerifyValInt(int64(iv), isMsgp)
case uint8:
v2 = uint64(iv)
case uint16:
v2 = uint64(iv)
case uint32:
v2 = uint64(iv)
case uint64:
v2 = uint64(iv)
case float32:
v2 = float64(iv)
case float64:
v2 = float64(iv)
case complex64:
v2 = float64(float32(real(iv)))
case complex128:
v2 = float64(real(iv))
case []interface{}:
m2 := make([]interface{}, len(iv))
for j, vj := range iv {
m2[j] = testVerifyVal(vj, f, h)
}
v2 = m2
case testMbsT:
m2 := make([]interface{}, len(iv))
for j, vj := range iv {
m2[j] = testVerifyVal(vj, f, h)
}
v2 = testMbsT(m2)
case map[string]bool:
switch {
case f.isset(testVerifyMapTypeSame):
m2 := make(map[string]bool)
for kj, kv := range iv {
m2[kj] = kv
}
v2 = m2
case f.isset(testVerifyMapTypeStrIntf):
m2 := make(map[string]interface{})
for kj, kv := range iv {
m2[kj] = kv
}
v2 = m2
case f.isset(testVerifyMapTypeIntfIntf):
m2 := make(map[interface{}]interface{})
for kj, kv := range iv {
m2[kj] = kv
}
v2 = m2
}
case map[string]interface{}:
switch {
case f.isset(testVerifyMapTypeSame):
m2 := make(map[string]interface{})
for kj, kv := range iv {
m2[kj] = testVerifyVal(kv, f, h)
}
v2 = m2
case f.isset(testVerifyMapTypeStrIntf):
m2 := make(map[string]interface{})
for kj, kv := range iv {
m2[kj] = testVerifyVal(kv, f, h)
}
v2 = m2
case f.isset(testVerifyMapTypeIntfIntf):
m2 := make(map[interface{}]interface{})
for kj, kv := range iv {
m2[kj] = testVerifyVal(kv, f, h)
}
v2 = m2
}
case map[interface{}]interface{}:
m2 := make(map[interface{}]interface{})
for kj, kv := range iv {
m2[testVerifyVal(kj, f, h)] = testVerifyVal(kv, f, h)
}
v2 = m2
case time.Time:
switch {
case f.isset(testVerifyTimeAsInteger):
if iv2 := iv.UnixNano(); iv2 >= 0 {
v2 = uint64(iv2)
} else {
v2 = int64(iv2)
}
case isMsgp:
v2 = iv.UTC()
case isCbor:
// fmt.Printf("%%%% cbor verifier\n")
v2 = iv.UTC().Round(time.Microsecond)
default:
v2 = v
}
default:
v2 = v
}
return
}
func testReleaseBytes(bs []byte) {
if !testUseParallel {
testBytesFreeList.put(bs)
}
}
func testGetBytes() (bs []byte) {
if !testUseParallel {
bs = testBytesFreeList.get(64)
}
return
}
func testHandleCopy(h Handle) (h2 Handle) {
switch v := h.(type) {
case *JsonHandle:
v2 := *v
h2 = &v2
case *CborHandle:
v2 := *v
h2 = &v2
case *MsgpackHandle:
v2 := *v
h2 = &v2
case *SimpleHandle:
v2 := *v
h2 = &v2
case *BincHandle:
v2 := *v
h2 = &v2
}
return
}
func testMarshal(v interface{}, h Handle) (bs []byte, err error) {
// return testCodecEncode(v, nil, testByteBuf, h)
return testCodecEncode(v, testGetBytes(), testByteBuf, h, false)
}
func testUnmarshal(v interface{}, data []byte, h Handle) (err error) {
return testCodecDecode(data, v, h, false)
}
func testMarshalErr(v interface{}, h Handle, t *testing.T, name string) (bs []byte) {
t.Helper()
bs, err := testCodecEncode(v, testGetBytes(), testByteBuf, h, true)
if err != nil {
t.Logf("%s: marshal failed: %v", name, err)
if testVerbose {
t.Logf("Error encoding %s: %v, Err: %v", name, v, err)
}
t.FailNow()
}
return
}
func testUnmarshalErr(v interface{}, data []byte, h Handle, t *testing.T, name string) {
t.Helper()
err := testCodecDecode(data, v, h, true)
if err != nil {
t.Logf("%s: unmarshal failed: %v", name, err)
if testVerbose {
t.Logf("Error Decoding into %s: %v, Err: %v", name, v, err)
}
t.FailNow()
}
}
func testDeepEqualErr(v1, v2 interface{}, t *testing.T, name string) {
t.Helper()
if err := deepEqual(v1, v2); err == nil {
if testVerbose {
t.Logf("%s: values equal", name)
}
} else {
t.Logf("%s: values not equal: %v", name, err)
if testVerbose {
t.Logf("%s: values not equal: %v. 1: %#v, 2: %#v", name, err, v1, v2)
}
t.FailNow()
}
}
func testReadWriteCloser(c io.ReadWriteCloser) io.ReadWriteCloser {
if testRpcBufsize <= 0 && rand.Int63()%2 == 0 {
return c
}
return struct {
io.Closer
*bufio.Reader
*bufio.Writer
}{c, bufio.NewReaderSize(c, testRpcBufsize), bufio.NewWriterSize(c, testRpcBufsize)}
}
// testCodecTableOne allows us test for different variations based on arguments passed.
func testCodecTableOne(t *testing.T, testNil bool, h Handle,
vs []interface{}, vsVerify []interface{}) {
//if testNil, then just test for when a pointer to a nil interface{} is passed. It should work.
//Current setup allows us test (at least manually) the nil interface or typed interface.
if testVerbose {
t.Logf("================ TestNil: %v: %v entries ================\n", testNil, len(vs))
}
if mh, ok := h.(*MsgpackHandle); ok {
defer func(a, b bool) {
mh.RawToString = a
mh.PositiveIntUnsigned = b
}(mh.RawToString, mh.PositiveIntUnsigned)
mh.RawToString = true
mh.PositiveIntUnsigned = false
}
bh := testBasicHandle(h)
for i, v0 := range vs {
if testVerbose {
t.Logf("..............................................")
t.Logf(" Testing: #%d:, %T, %#v\n", i, v0, v0)
}
// if a TestStrucFlex and we are doing a testNil,
// ensure the fields which are not encodeable are set to nil appropriately
// i.e. TestStrucFlex.{MstrUi64TSelf, mapMsu2wss}
var mapMstrUi64TSelf map[stringUint64T]*stringUint64T
var mapMsu2wss map[stringUint64T]wrapStringSlice
// TestStrucFlex.{Msp2ss, Mip2ss} have pointer keys.
// When we encode and the decode back into the same value,
// the length of this map will effectively double, because
// each pointer has equal underlying value, but are separate entries in the map.
//
// Best way to compare is to store them, and then compare them later if needed.
var mapMsp2ss map[*string][]string
var mapMip2ss map[*uint64][]string
tsflex, _ := v0.(*TestStrucFlex)
if tsflex != nil {