github.com/ugorji/go/codec@v1.2.13-0.20240307214044-07c54c229a5a/codec_test.go (about)

     1  // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved.
     2  // Use of this source code is governed by a MIT license found in the LICENSE file.
     3  
     4  package codec
     5  
     6  // NAMING CONVENTION FOR TESTS
     7  //
     8  // function and variable/const names here fit a simple naming convention
     9  //
    10  // - each test is a doTestXXX(...). TestXXX calls doTestXXX.
    11  // - testXXX are helper functions.
    12  // - doTestXXX are test functions that take an extra arg of a handle.
    13  // - testXXX variables and constants are only used in tests.
    14  // - shared functions/vars/consts are testShared...
    15  // - fnBenchmarkXX and fnTestXXX can be used as needed.
    16  //
    17  // - each TestXXX must only call testSetup once.
    18  // - each test with a prefix __doTest is a dependent helper function,
    19  //   which MUST not call testSetup itself.
    20  //   doTestXXX or TestXXX may call it.
    21  
    22  // if we are testing in parallel,
    23  // then we don't want to share much state: testBytesFreeList, etc.
    24  
    25  import (
    26  	"bufio"
    27  	"bytes"
    28  	"encoding/gob"
    29  	"errors"
    30  	"fmt"
    31  	"io"
    32  	"io/ioutil"
    33  	"math"
    34  	"math/rand"
    35  	"net"
    36  	"net/rpc"
    37  	"os"
    38  	"os/exec"
    39  	"path/filepath"
    40  	"reflect"
    41  	"strconv"
    42  	"strings"
    43  	"sync/atomic"
    44  	"testing"
    45  	"time"
    46  )
    47  
    48  func init() {
    49  	testPreInitFns = append(testPreInitFns, testInit)
    50  }
    51  
    52  const testRecoverPanicToErr = !debugging
    53  
    54  // tests which check for errors will fail if testRecoverPanicToErr=false (debugging=true).
    55  // Consequently, skip them.
    56  var testSkipIfNotRecoverPanicToErrMsg = "tests checks for errors, and testRecoverPanicToErr=false"
    57  
    58  func testPanicToErr(h errDecorator, err *error) {
    59  	// Note: This method MUST be called directly from defer i.e. defer testPanicToErr ...
    60  	// else it seems the recover is not fully handled
    61  	if x := recover(); x != nil {
    62  		panicValToErr(h, x, err)
    63  	}
    64  }
    65  
    66  var testBytesFreeList bytesFreelist
    67  
    68  type testCustomStringT string
    69  
    70  // make these mapbyslice
    71  type testMbsT []interface{}
    72  type testMbsArr0T [0]interface{}
    73  type testMbsArr4T [4]interface{}
    74  type testMbsArr5T [5]interface{}
    75  type testMbsCustStrT []testCustomStringT
    76  
    77  func (testMbsT) MapBySlice()        {}
    78  func (*testMbsArr0T) MapBySlice()   {}
    79  func (*testMbsArr4T) MapBySlice()   {}
    80  func (testMbsArr5T) MapBySlice()    {}
    81  func (testMbsCustStrT) MapBySlice() {}
    82  
    83  // type testSelferRecur struct{}
    84  
    85  // func (s *testSelferRecur) CodecEncodeSelf(e *Encoder) {
    86  // 	e.MustEncode(s)
    87  // }
    88  // func (s *testSelferRecur) CodecDecodeSelf(d *Decoder) {
    89  // 	d.MustDecode(s)
    90  // }
    91  
    92  type testIntfMapI interface {
    93  	GetIntfMapV() string
    94  }
    95  
    96  type testIntfMapT1 struct {
    97  	IntfMapV string
    98  }
    99  
   100  func (x *testIntfMapT1) GetIntfMapV() string { return x.IntfMapV }
   101  
   102  type testIntfMapT2 struct {
   103  	IntfMapV string
   104  }
   105  
   106  func (x testIntfMapT2) GetIntfMapV() string { return x.IntfMapV }
   107  
   108  type testMissingFieldsMap struct {
   109  	m map[string]interface{}
   110  }
   111  
   112  func (mf *testMissingFieldsMap) CodecMissingField(field []byte, value interface{}) bool {
   113  	if mf.m == nil {
   114  		mf.m = map[string]interface{}{}
   115  	}
   116  
   117  	(mf.m)[string(field)] = value
   118  
   119  	return true
   120  }
   121  
   122  func (mf *testMissingFieldsMap) CodecMissingFields() map[string]interface{} {
   123  	return mf.m
   124  }
   125  
   126  var _ MissingFielder = (*testMissingFieldsMap)(nil)
   127  
   128  var testErrWriterErr = errors.New("testErrWriterErr")
   129  
   130  type testErrWriter struct{}
   131  
   132  func (x *testErrWriter) Write(p []byte) (int, error) {
   133  	return 0, testErrWriterErr
   134  }
   135  
   136  // ----
   137  
   138  type testVerifyFlag uint8
   139  
   140  const (
   141  	_ testVerifyFlag = 1 << iota
   142  	testVerifyMapTypeSame
   143  	testVerifyMapTypeStrIntf
   144  	testVerifyMapTypeIntfIntf
   145  	// testVerifySliceIntf
   146  	testVerifyForPython
   147  	testVerifyDoNil
   148  	testVerifyTimeAsInteger
   149  )
   150  
   151  func (f testVerifyFlag) isset(v testVerifyFlag) bool {
   152  	return f&v == v
   153  }
   154  
   155  // const testSkipRPCTests = false
   156  
   157  var (
   158  	testTableNumPrimitives int
   159  	testTableIdxTime       int
   160  	testTableNumMaps       int
   161  
   162  	// set this when running using bufio, etc
   163  	testSkipRPCTests = false
   164  
   165  	testSkipRPCTestsMsg = "testSkipRPCTests=true"
   166  )
   167  
   168  var (
   169  	skipVerifyVal interface{} = &(struct{}{})
   170  
   171  	testMapStrIntfTyp = reflect.TypeOf(map[string]interface{}(nil))
   172  
   173  	// For Go Time, do not use a descriptive timezone.
   174  	// It's unnecessary, and makes it harder to do a reflect.DeepEqual.
   175  	// The Offset already tells what the offset should be, if not on UTC and unknown zone name.
   176  	timeLoc        = time.FixedZone("", -8*60*60) // UTC-08:00 //time.UTC-8
   177  	timeToCompare1 = time.Date(2012, 2, 2, 2, 2, 2, 2000, timeLoc).UTC()
   178  	timeToCompare2 = time.Date(1900, 2, 2, 2, 2, 2, 2000, timeLoc).UTC()
   179  	timeToCompare3 = time.Unix(0, 270).UTC() // use value that must be encoded as uint64 for nanoseconds (for cbor/msgpack comparison)
   180  	//timeToCompare4 = time.Time{}.UTC() // does not work well with simple cbor time encoding (overflow)
   181  	timeToCompare4 = time.Unix(-2013855848, 4223).UTC()
   182  
   183  	table []interface{} // main items we encode
   184  	// will encode a float32 as float64, or large int as uint
   185  
   186  	testRpcServer = rpc.NewServer()
   187  	testRpcInt    = new(TestRpcInt)
   188  )
   189  
   190  func init() {
   191  	testRpcServer.Register(testRpcInt)
   192  }
   193  
   194  var wrapInt64Typ = reflect.TypeOf(wrapInt64(0))
   195  var wrapBytesTyp = reflect.TypeOf(wrapBytes(nil))
   196  
   197  var testUintToBytesTyp = reflect.TypeOf(testUintToBytes(0))
   198  var testSelfExtTyp = reflect.TypeOf((*TestSelfExtImpl)(nil)).Elem()
   199  var testSelfExt2Typ = reflect.TypeOf((*TestSelfExtImpl2)(nil)).Elem()
   200  
   201  func testByteBuf(in []byte) *bytes.Buffer {
   202  	return bytes.NewBuffer(in)
   203  }
   204  
   205  type TestABC struct {
   206  	A, B, C string
   207  }
   208  
   209  func (x *TestABC) MarshalBinary() ([]byte, error) {
   210  	return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
   211  }
   212  func (x *TestABC) MarshalText() ([]byte, error) {
   213  	return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
   214  }
   215  func (x *TestABC) MarshalJSON() ([]byte, error) {
   216  	return []byte(fmt.Sprintf(`"%s %s %s"`, x.A, x.B, x.C)), nil
   217  }
   218  
   219  func (x *TestABC) UnmarshalBinary(data []byte) (err error) {
   220  	ss := strings.Split(string(data), " ")
   221  	x.A, x.B, x.C = ss[0], ss[1], ss[2]
   222  	return
   223  }
   224  func (x *TestABC) UnmarshalText(data []byte) (err error) {
   225  	return x.UnmarshalBinary(data)
   226  }
   227  func (x *TestABC) UnmarshalJSON(data []byte) (err error) {
   228  	return x.UnmarshalBinary(data[1 : len(data)-1])
   229  }
   230  
   231  type TestABC2 struct {
   232  	A, B, C string
   233  }
   234  
   235  func (x TestABC2) MarshalText() ([]byte, error) {
   236  	return []byte(fmt.Sprintf("%s %s %s", x.A, x.B, x.C)), nil
   237  }
   238  func (x *TestABC2) UnmarshalText(data []byte) (err error) {
   239  	ss := strings.Split(string(data), " ")
   240  	x.A, x.B, x.C = ss[0], ss[1], ss[2]
   241  	return
   242  	// _, err = fmt.Sscanf(string(data), "%s %s %s", &x.A, &x.B, &x.C)
   243  }
   244  
   245  type TestSimplish struct {
   246  	Ii int
   247  	Ss string
   248  	Ar [2]*TestSimplish
   249  	Sl []*TestSimplish
   250  	Mm map[string]*TestSimplish
   251  }
   252  
   253  type TestRpcABC struct {
   254  	A, B, C string
   255  }
   256  
   257  type TestRpcInt struct {
   258  	i int64
   259  }
   260  
   261  func (r *TestRpcInt) Update(n int, res *int) error {
   262  	atomic.StoreInt64(&r.i, int64(n))
   263  	*res = n
   264  	return nil
   265  }
   266  func (r *TestRpcInt) Square(ignore int, res *int) error {
   267  	i := int(atomic.LoadInt64(&r.i))
   268  	*res = i * i
   269  	return nil
   270  }
   271  func (r *TestRpcInt) Mult(n int, res *int) error {
   272  	*res = int(atomic.LoadInt64(&r.i)) * n
   273  	return nil
   274  }
   275  func (r *TestRpcInt) EchoStruct(arg TestRpcABC, res *string) error {
   276  	*res = fmt.Sprintf("%#v", arg)
   277  	return nil
   278  }
   279  func (r *TestRpcInt) Echo123(args []string, res *string) error {
   280  	*res = fmt.Sprintf("%#v", args)
   281  	return nil
   282  }
   283  
   284  type TestRawValue struct {
   285  	R Raw
   286  	I int
   287  }
   288  
   289  // ----
   290  
   291  type testUnixNanoTimeExt struct {
   292  	// keep timestamp here, so that do not incur interface-conversion costs
   293  	// ts int64
   294  }
   295  
   296  func (x *testUnixNanoTimeExt) WriteExt(v interface{}) []byte {
   297  	v2 := v.(*time.Time)
   298  	bs := make([]byte, 8)
   299  	bigenstd.PutUint64(bs, uint64(v2.UnixNano()))
   300  	return bs
   301  }
   302  func (x *testUnixNanoTimeExt) ReadExt(v interface{}, bs []byte) {
   303  	v2 := v.(*time.Time)
   304  	ui := bigenstd.Uint64(bs)
   305  	*v2 = time.Unix(0, int64(ui)).UTC()
   306  }
   307  
   308  type testUnixNanoTimeInterfaceExt struct{}
   309  
   310  func (x testUnixNanoTimeInterfaceExt) ConvertExt(v interface{}) interface{} {
   311  	v2 := v.(*time.Time) // structs are encoded by passing the ptr
   312  	return v2.UTC().UnixNano()
   313  }
   314  
   315  func (x testUnixNanoTimeInterfaceExt) UpdateExt(dest interface{}, v interface{}) {
   316  	tt := dest.(*time.Time)
   317  	*tt = time.Unix(0, v.(int64)).UTC()
   318  	// switch v2 := v.(type) {
   319  	// case int64:
   320  	// 	*tt = time.Unix(0, v2).UTC()
   321  	// case uint64:
   322  	// 	*tt = time.Unix(0, int64(v2)).UTC()
   323  	// //case float64:
   324  	// //case string:
   325  	// default:
   326  	// 	panic(fmt.Errorf("unsupported format for time conversion: expecting int64/uint64; got %T", v))
   327  	// }
   328  }
   329  
   330  // ----
   331  
   332  type wrapInt64Ext int64
   333  
   334  func (x *wrapInt64Ext) WriteExt(v interface{}) []byte {
   335  	v2 := uint64(int64(v.(wrapInt64)))
   336  	bs := make([]byte, 8)
   337  	bigenstd.PutUint64(bs, v2)
   338  	return bs
   339  }
   340  func (x *wrapInt64Ext) ReadExt(v interface{}, bs []byte) {
   341  	v2 := v.(*wrapInt64)
   342  	ui := bigenstd.Uint64(bs)
   343  	*v2 = wrapInt64(int64(ui))
   344  }
   345  func (x *wrapInt64Ext) ConvertExt(v interface{}) interface{} {
   346  	return int64(v.(wrapInt64))
   347  }
   348  func (x *wrapInt64Ext) UpdateExt(dest interface{}, v interface{}) {
   349  	v2 := dest.(*wrapInt64)
   350  	*v2 = wrapInt64(v.(int64))
   351  }
   352  
   353  // ----
   354  
   355  type wrapBytesExt struct{}
   356  
   357  func (x *wrapBytesExt) WriteExt(v interface{}) []byte {
   358  	return ([]byte)(v.(wrapBytes))
   359  }
   360  func (x *wrapBytesExt) ReadExt(v interface{}, bs []byte) {
   361  	v2 := v.(*wrapBytes)
   362  	*v2 = wrapBytes(bs)
   363  }
   364  func (x *wrapBytesExt) ConvertExt(v interface{}) interface{} {
   365  	return ([]byte)(v.(wrapBytes))
   366  }
   367  func (x *wrapBytesExt) UpdateExt(dest interface{}, v interface{}) {
   368  	v2 := dest.(*wrapBytes)
   369  	// some formats (e.g. json) cannot nakedly determine []byte from string, so expect both
   370  	switch v3 := v.(type) {
   371  	case []byte:
   372  		*v2 = wrapBytes(v3)
   373  	case string:
   374  		*v2 = wrapBytes([]byte(v3))
   375  	default:
   376  		panic(errors.New("UpdateExt for wrapBytesExt expects string or []byte"))
   377  	}
   378  	// *v2 = wrapBytes(v.([]byte))
   379  }
   380  
   381  // ----
   382  
   383  // timeExt is an extension handler for time.Time, that uses binc model for encoding/decoding time.
   384  // we used binc model, as that is the only custom time representation that we designed ourselves.
   385  type timeBytesExt struct{}
   386  
   387  func (x timeBytesExt) WriteExt(v interface{}) (bs []byte) {
   388  	return bincEncodeTime(*(v.(*time.Time)))
   389  	// return bincEncodeTime(v.(time.Time))
   390  	// switch v2 := v.(type) {
   391  	// case time.Time:
   392  	// 	bs = bincEncodeTime(v2)
   393  	// case *time.Time:
   394  	// 	bs = bincEncodeTime(*v2)
   395  	// default:
   396  	// 	panic(fmt.Errorf("unsupported format for time conversion: expecting time.Time; got %T", v2))
   397  	// }
   398  	// return
   399  }
   400  func (x timeBytesExt) ReadExt(v interface{}, bs []byte) {
   401  	tt, err := bincDecodeTime(bs)
   402  	if err != nil {
   403  		panic(err)
   404  	}
   405  	*(v.(*time.Time)) = tt
   406  }
   407  
   408  type timeInterfaceExt struct{}
   409  
   410  func (x timeInterfaceExt) ConvertExt(v interface{}) interface{} {
   411  	return timeBytesExt{}.WriteExt(v)
   412  }
   413  func (x timeInterfaceExt) UpdateExt(v interface{}, src interface{}) {
   414  	timeBytesExt{}.ReadExt(v, src.([]byte))
   415  }
   416  
   417  // ----
   418  type testUintToBytesExt struct{}
   419  
   420  func (x testUintToBytesExt) WriteExt(v interface{}) (bs []byte) {
   421  	z := uint32(v.(testUintToBytes))
   422  	if z == 0 {
   423  		return nil
   424  	}
   425  	return make([]byte, z)
   426  }
   427  func (x testUintToBytesExt) ReadExt(v interface{}, bs []byte) {
   428  	*(v.(*testUintToBytes)) = testUintToBytes(len(bs))
   429  }
   430  func (x testUintToBytesExt) ConvertExt(v interface{}) interface{} {
   431  	return x.WriteExt(v)
   432  }
   433  func (x testUintToBytesExt) UpdateExt(v interface{}, src interface{}) {
   434  	x.ReadExt(v, src.([]byte))
   435  }
   436  
   437  // ----
   438  
   439  func testSetupNoop() {}
   440  
   441  func testSetup(t *testing.T, h *Handle) (fn func()) {
   442  	return testSetupWithChecks(t, h, true)
   443  }
   444  
   445  // testSetup will ensure testInitAll is run, and then
   446  // return a function that should be deferred to run at the end
   447  // of the test.
   448  //
   449  // This function can track how much time a test took,
   450  // or recover from panic's and fail the test appropriately.
   451  func testSetupWithChecks(t *testing.T, h *Handle, allowParallel bool) (fn func()) {
   452  	testOnce.Do(testInitAll)
   453  	if allowParallel && testUseParallel {
   454  		t.Parallel()
   455  		if h != nil {
   456  			*h = testHandleCopy(*h)
   457  		}
   458  	}
   459  	// in case an error is seen, recover it here.
   460  	if testRecoverPanicToErr {
   461  		fnRecoverPanic := func() {
   462  			if x := recover(); x != nil {
   463  				var err error
   464  				panicValToErr(errDecoratorDef{}, x, &err)
   465  				t.Logf("recovered error: %v", err)
   466  				t.FailNow()
   467  			}
   468  		}
   469  		fn = fnRecoverPanic
   470  	}
   471  	if fn == nil {
   472  		fn = testSetupNoop
   473  	}
   474  	return
   475  }
   476  
   477  func testBasicHandle(h Handle) *BasicHandle { return h.getBasicHandle() }
   478  
   479  func testCodecEncode(ts interface{}, bsIn []byte, fn func([]byte) *bytes.Buffer, h Handle, useMust bool) (bs []byte, err error) {
   480  	return testSharedCodecEncode(ts, bsIn, fn, h, testBasicHandle(h), useMust)
   481  }
   482  
   483  func testCodecDecode(bs []byte, ts interface{}, h Handle, useMust bool) (err error) {
   484  	return testSharedCodecDecode(bs, ts, h, testBasicHandle(h), useMust)
   485  }
   486  
   487  func testCheckErr(t *testing.T, err error) {
   488  	if err != nil {
   489  		t.Logf("err: %v", err)
   490  		t.FailNow()
   491  	}
   492  }
   493  
   494  func testCheckEqual(t *testing.T, v1 interface{}, v2 interface{}, desc string) {
   495  	t.Helper()
   496  	if err := deepEqual(v1, v2); err != nil {
   497  		t.Logf("Not Equal: %s: %v", desc, err)
   498  		if testVerbose {
   499  			t.Logf("\tv1: %v, v2: %v", v1, v2)
   500  		}
   501  		t.FailNow()
   502  	}
   503  }
   504  
   505  func testInit() {
   506  	gob.Register(new(TestStrucFlex))
   507  
   508  	for _, v := range testHandles {
   509  		bh := testBasicHandle(v)
   510  		bh.clearInited() // so it is reinitialized next time around
   511  		// pre-fill them first
   512  		bh.EncodeOptions = testEncodeOptions
   513  		bh.DecodeOptions = testDecodeOptions
   514  		bh.RPCOptions = testRPCOptions
   515  		// bh.InterfaceReset = true
   516  		// bh.PreferArrayOverSlice = true
   517  		// modify from flag'ish things
   518  		bh.MaxInitLen = testMaxInitLen
   519  	}
   520  
   521  	var tTimeExt timeBytesExt
   522  	var tBytesExt wrapBytesExt
   523  	var tI64Ext wrapInt64Ext
   524  	var tUintToBytesExt testUintToBytesExt
   525  
   526  	// create legacy functions suitable for deprecated AddExt functionality,
   527  	// and use on some places for testSimpleH e.g. for time.Time and wrapInt64
   528  	var (
   529  		myExtEncFn = func(x BytesExt, rv reflect.Value) (bs []byte, err error) {
   530  			defer testPanicToErr(errDecoratorDef{}, &err)
   531  			bs = x.WriteExt(rv.Interface())
   532  			return
   533  		}
   534  		myExtDecFn = func(x BytesExt, rv reflect.Value, bs []byte) (err error) {
   535  			defer testPanicToErr(errDecoratorDef{}, &err)
   536  			x.ReadExt(rv.Interface(), bs)
   537  			return
   538  		}
   539  		timeExtEncFn      = func(rv reflect.Value) (bs []byte, err error) { return myExtEncFn(tTimeExt, rv) }
   540  		timeExtDecFn      = func(rv reflect.Value, bs []byte) (err error) { return myExtDecFn(tTimeExt, rv, bs) }
   541  		wrapInt64ExtEncFn = func(rv reflect.Value) (bs []byte, err error) { return myExtEncFn(&tI64Ext, rv) }
   542  		wrapInt64ExtDecFn = func(rv reflect.Value, bs []byte) (err error) { return myExtDecFn(&tI64Ext, rv, bs) }
   543  	)
   544  
   545  	chkErr := func(err error) {
   546  		if err != nil {
   547  			panic(err)
   548  		}
   549  	}
   550  
   551  	// time.Time is a native type, so extensions will have no effect.
   552  	// However, we add these here to ensure nothing happens.
   553  	chkErr(testSimpleH.AddExt(timeTyp, 1, timeExtEncFn, timeExtDecFn))
   554  	// testBincH.SetBytesExt(timeTyp, 1, timeExt{}) // time is builtin for binc
   555  	chkErr(testMsgpackH.SetBytesExt(timeTyp, 1, timeBytesExt{}))
   556  	chkErr(testCborH.SetInterfaceExt(timeTyp, 1, testUnixNanoTimeInterfaceExt{}))
   557  	// testJsonH.SetInterfaceExt(timeTyp, 1, &testUnixNanoTimeExt{})
   558  
   559  	// Add extensions for the testSelfExt
   560  	chkErr(testSimpleH.SetBytesExt(testSelfExtTyp, 78, SelfExt))
   561  	chkErr(testMsgpackH.SetBytesExt(testSelfExtTyp, 78, SelfExt))
   562  	chkErr(testBincH.SetBytesExt(testSelfExtTyp, 78, SelfExt))
   563  	chkErr(testJsonH.SetInterfaceExt(testSelfExtTyp, 78, SelfExt))
   564  	chkErr(testCborH.SetInterfaceExt(testSelfExtTyp, 78, SelfExt))
   565  
   566  	chkErr(testSimpleH.SetBytesExt(testSelfExt2Typ, 79, SelfExt))
   567  	chkErr(testMsgpackH.SetBytesExt(testSelfExt2Typ, 79, SelfExt))
   568  	chkErr(testBincH.SetBytesExt(testSelfExt2Typ, 79, SelfExt))
   569  	chkErr(testJsonH.SetInterfaceExt(testSelfExt2Typ, 79, SelfExt))
   570  	chkErr(testCborH.SetInterfaceExt(testSelfExt2Typ, 79, SelfExt))
   571  
   572  	// Now, add extensions for the type wrapInt64 and wrapBytes,
   573  	// so we can execute the Encode/Decode Ext paths.
   574  
   575  	chkErr(testSimpleH.SetBytesExt(wrapBytesTyp, 32, &tBytesExt))
   576  	chkErr(testMsgpackH.SetBytesExt(wrapBytesTyp, 32, &tBytesExt))
   577  	chkErr(testBincH.SetBytesExt(wrapBytesTyp, 32, &tBytesExt))
   578  	chkErr(testJsonH.SetInterfaceExt(wrapBytesTyp, 32, &tBytesExt))
   579  	chkErr(testCborH.SetInterfaceExt(wrapBytesTyp, 32, &tBytesExt))
   580  
   581  	chkErr(testSimpleH.SetBytesExt(testUintToBytesTyp, 33, &tUintToBytesExt))
   582  	chkErr(testMsgpackH.SetBytesExt(testUintToBytesTyp, 33, &tUintToBytesExt))
   583  	chkErr(testBincH.SetBytesExt(testUintToBytesTyp, 33, &tUintToBytesExt))
   584  	chkErr(testJsonH.SetInterfaceExt(testUintToBytesTyp, 33, &tUintToBytesExt))
   585  	chkErr(testCborH.SetInterfaceExt(testUintToBytesTyp, 33, &tUintToBytesExt))
   586  
   587  	chkErr(testSimpleH.AddExt(wrapInt64Typ, 16, wrapInt64ExtEncFn, wrapInt64ExtDecFn))
   588  	// chkErr(testSimpleH.SetBytesExt(wrapInt64Typ, 16, &tI64Ext))
   589  	chkErr(testMsgpackH.SetBytesExt(wrapInt64Typ, 16, &tI64Ext))
   590  	chkErr(testBincH.SetBytesExt(wrapInt64Typ, 16, &tI64Ext))
   591  	chkErr(testJsonH.SetInterfaceExt(wrapInt64Typ, 16, &tI64Ext))
   592  	chkErr(testCborH.SetInterfaceExt(wrapInt64Typ, 16, &tI64Ext))
   593  
   594  	// primitives MUST be an even number, so it can be used as a mapBySlice also.
   595  	primitives := []interface{}{
   596  		int8(-8),
   597  		int16(-1616),
   598  		int32(-32323232),
   599  		int64(-6464646464646464),
   600  		uint8(192),
   601  		uint16(1616),
   602  		uint32(32323232),
   603  		uint64(6464646464646464),
   604  		byte(192),
   605  		float32(-3232.0),
   606  		float64(-6464646464.0),
   607  		float32(3232.0),
   608  		float64(6464.0),
   609  		float64(6464646464.0),
   610  		complex64(complex(160.0, 0)),
   611  		complex128(complex(1616, 0)),
   612  		false,
   613  		true,
   614  		"null",
   615  		nil,
   616  		"some&day>some<day",
   617  		timeToCompare1,
   618  		"",
   619  		timeToCompare2,
   620  		"bytestring",
   621  		timeToCompare3,
   622  		"none",
   623  		timeToCompare4,
   624  	}
   625  
   626  	maps := []interface{}{
   627  		map[string]bool{
   628  			"true":  true,
   629  			"false": false,
   630  		},
   631  		map[string]interface{}{
   632  			"true":         "True",
   633  			"false":        false,
   634  			"uint16(1616)": uint16(1616),
   635  		},
   636  		//add a complex combo map in here. (map has list which has map)
   637  		//note that after the first thing, everything else should be generic.
   638  		map[string]interface{}{
   639  			"list": []interface{}{
   640  				int16(1616),
   641  				int32(32323232),
   642  				true,
   643  				float32(-3232.0),
   644  				map[string]interface{}{
   645  					"TRUE":  true,
   646  					"FALSE": false,
   647  				},
   648  				[]interface{}{true, false},
   649  			},
   650  			"int32": int32(32323232),
   651  			"bool":  true,
   652  			"LONG STRING": `
   653  1234567890 1234567890 
   654  1234567890 1234567890 
   655  1234567890 1234567890 
   656  ABCDEDFGHIJKLMNOPQRSTUVWXYZ 
   657  abcdedfghijklmnopqrstuvwxyz 
   658  ABCDEDFGHIJKLMNOPQRSTUVWXYZ 
   659  abcdedfghijklmnopqrstuvwxyz 
   660  "ABCDEDFGHIJKLMNOPQRSTUVWXYZ" 
   661  '	a tab	'
   662  \a\b\c\d\e
   663  \b\f\n\r\t all literally
   664  ugorji
   665  `,
   666  			"SHORT STRING": "1234567890",
   667  		},
   668  		map[interface{}]interface{}{
   669  			true:       "true",
   670  			uint8(138): false,
   671  			false:      uint8(200),
   672  		},
   673  	}
   674  
   675  	testTableNumPrimitives = len(primitives)
   676  	testTableIdxTime = testTableNumPrimitives - 8
   677  	testTableNumMaps = len(maps)
   678  
   679  	table = []interface{}{}
   680  	table = append(table, primitives...)
   681  	table = append(table, primitives)
   682  	table = append(table, testMbsT(primitives))
   683  	table = append(table, maps...)
   684  	table = append(table, newTestStrucFlex(0, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly))
   685  	// table = append(table, newTestStrucFlex(0, testNumRepeatString, true, !testSkipIntf, testMapStringKeyOnly))
   686  }
   687  
   688  func testTableVerify(f testVerifyFlag, h Handle) (av []interface{}) {
   689  	av = make([]interface{}, len(table))
   690  	lp := testTableNumPrimitives + 4
   691  	// doNil := f & testVerifyDoNil == testVerifyDoNil
   692  	// doPython := f & testVerifyForPython == testVerifyForPython
   693  	switch {
   694  	case f.isset(testVerifyForPython):
   695  		for i, v := range table {
   696  			if i == testTableNumPrimitives+1 || i > lp { // testTableNumPrimitives+1 is the mapBySlice
   697  				av[i] = skipVerifyVal
   698  				continue
   699  			}
   700  			av[i] = testVerifyVal(v, f, h)
   701  		}
   702  		// only do the python verify up to the maps, skipping the last 2 maps.
   703  		av = av[:testTableNumPrimitives+2+testTableNumMaps-2]
   704  	case f.isset(testVerifyDoNil):
   705  		for i, v := range table {
   706  			if i > lp {
   707  				av[i] = skipVerifyVal
   708  				continue
   709  			}
   710  			av[i] = testVerifyVal(v, f, h)
   711  		}
   712  	default:
   713  		for i, v := range table {
   714  			if i == lp {
   715  				av[i] = skipVerifyVal
   716  				continue
   717  			}
   718  			//av[i] = testVerifyVal(v, testVerifyMapTypeSame)
   719  			switch v.(type) {
   720  			case []interface{}:
   721  				av[i] = testVerifyVal(v, f, h)
   722  			case testMbsT:
   723  				av[i] = testVerifyVal(v, f, h)
   724  			case map[string]interface{}:
   725  				av[i] = testVerifyVal(v, f, h)
   726  			case map[interface{}]interface{}:
   727  				av[i] = testVerifyVal(v, f, h)
   728  			case time.Time:
   729  				av[i] = testVerifyVal(v, f, h)
   730  			default:
   731  				av[i] = v
   732  			}
   733  		}
   734  	}
   735  	return
   736  }
   737  
   738  func testVerifyValInt(v int64, isMsgp bool) (v2 interface{}) {
   739  	if isMsgp {
   740  		if v >= 0 && v <= 127 {
   741  			v2 = uint64(v)
   742  		} else {
   743  			v2 = int64(v)
   744  		}
   745  	} else if v >= 0 {
   746  		v2 = uint64(v)
   747  	} else {
   748  		v2 = int64(v)
   749  	}
   750  	return
   751  }
   752  
   753  func testVerifyVal(v interface{}, f testVerifyFlag, h Handle) (v2 interface{}) {
   754  	//for python msgpack,
   755  	//  - all positive integers are unsigned 64-bit ints
   756  	//  - all floats are float64
   757  	_, isMsgp := h.(*MsgpackHandle)
   758  	_, isCbor := h.(*CborHandle)
   759  	switch iv := v.(type) {
   760  	case int8:
   761  		v2 = testVerifyValInt(int64(iv), isMsgp)
   762  		// fmt.Printf(">>>> is msgp: %v, v: %T, %v ==> v2: %T, %v\n", isMsgp, v, v, v2, v2)
   763  	case int16:
   764  		v2 = testVerifyValInt(int64(iv), isMsgp)
   765  	case int32:
   766  		v2 = testVerifyValInt(int64(iv), isMsgp)
   767  	case int64:
   768  		v2 = testVerifyValInt(int64(iv), isMsgp)
   769  	case uint8:
   770  		v2 = uint64(iv)
   771  	case uint16:
   772  		v2 = uint64(iv)
   773  	case uint32:
   774  		v2 = uint64(iv)
   775  	case uint64:
   776  		v2 = uint64(iv)
   777  	case float32:
   778  		v2 = float64(iv)
   779  	case float64:
   780  		v2 = float64(iv)
   781  	case complex64:
   782  		v2 = float64(float32(real(iv)))
   783  	case complex128:
   784  		v2 = float64(real(iv))
   785  	case []interface{}:
   786  		m2 := make([]interface{}, len(iv))
   787  		for j, vj := range iv {
   788  			m2[j] = testVerifyVal(vj, f, h)
   789  		}
   790  		v2 = m2
   791  	case testMbsT:
   792  		m2 := make([]interface{}, len(iv))
   793  		for j, vj := range iv {
   794  			m2[j] = testVerifyVal(vj, f, h)
   795  		}
   796  		v2 = testMbsT(m2)
   797  	case map[string]bool:
   798  		switch {
   799  		case f.isset(testVerifyMapTypeSame):
   800  			m2 := make(map[string]bool)
   801  			for kj, kv := range iv {
   802  				m2[kj] = kv
   803  			}
   804  			v2 = m2
   805  		case f.isset(testVerifyMapTypeStrIntf):
   806  			m2 := make(map[string]interface{})
   807  			for kj, kv := range iv {
   808  				m2[kj] = kv
   809  			}
   810  			v2 = m2
   811  		case f.isset(testVerifyMapTypeIntfIntf):
   812  			m2 := make(map[interface{}]interface{})
   813  			for kj, kv := range iv {
   814  				m2[kj] = kv
   815  			}
   816  			v2 = m2
   817  		}
   818  	case map[string]interface{}:
   819  		switch {
   820  		case f.isset(testVerifyMapTypeSame):
   821  			m2 := make(map[string]interface{})
   822  			for kj, kv := range iv {
   823  				m2[kj] = testVerifyVal(kv, f, h)
   824  			}
   825  			v2 = m2
   826  		case f.isset(testVerifyMapTypeStrIntf):
   827  			m2 := make(map[string]interface{})
   828  			for kj, kv := range iv {
   829  				m2[kj] = testVerifyVal(kv, f, h)
   830  			}
   831  			v2 = m2
   832  		case f.isset(testVerifyMapTypeIntfIntf):
   833  			m2 := make(map[interface{}]interface{})
   834  			for kj, kv := range iv {
   835  				m2[kj] = testVerifyVal(kv, f, h)
   836  			}
   837  			v2 = m2
   838  		}
   839  	case map[interface{}]interface{}:
   840  		m2 := make(map[interface{}]interface{})
   841  		for kj, kv := range iv {
   842  			m2[testVerifyVal(kj, f, h)] = testVerifyVal(kv, f, h)
   843  		}
   844  		v2 = m2
   845  	case time.Time:
   846  		switch {
   847  		case f.isset(testVerifyTimeAsInteger):
   848  			if iv2 := iv.UnixNano(); iv2 >= 0 {
   849  				v2 = uint64(iv2)
   850  			} else {
   851  				v2 = int64(iv2)
   852  			}
   853  		case isMsgp:
   854  			v2 = iv.UTC()
   855  		case isCbor:
   856  			// fmt.Printf("%%%% cbor verifier\n")
   857  			v2 = iv.UTC().Round(time.Microsecond)
   858  		default:
   859  			v2 = v
   860  		}
   861  	default:
   862  		v2 = v
   863  	}
   864  	return
   865  }
   866  
   867  func testReleaseBytes(bs []byte) {
   868  	if !testUseParallel {
   869  		testBytesFreeList.put(bs)
   870  	}
   871  }
   872  
   873  func testGetBytes() (bs []byte) {
   874  	if !testUseParallel {
   875  		bs = testBytesFreeList.get(64)
   876  	}
   877  	return
   878  }
   879  
   880  func testHandleCopy(h Handle) (h2 Handle) {
   881  	switch v := h.(type) {
   882  	case *JsonHandle:
   883  		v2 := *v
   884  		h2 = &v2
   885  	case *CborHandle:
   886  		v2 := *v
   887  		h2 = &v2
   888  	case *MsgpackHandle:
   889  		v2 := *v
   890  		h2 = &v2
   891  	case *SimpleHandle:
   892  		v2 := *v
   893  		h2 = &v2
   894  	case *BincHandle:
   895  		v2 := *v
   896  		h2 = &v2
   897  	}
   898  	return
   899  }
   900  
   901  func testMarshal(v interface{}, h Handle) (bs []byte, err error) {
   902  	// return testCodecEncode(v, nil, testByteBuf, h)
   903  	return testCodecEncode(v, testGetBytes(), testByteBuf, h, false)
   904  }
   905  
   906  func testUnmarshal(v interface{}, data []byte, h Handle) (err error) {
   907  	return testCodecDecode(data, v, h, false)
   908  }
   909  
   910  func testMarshalErr(v interface{}, h Handle, t *testing.T, name string) (bs []byte) {
   911  	t.Helper()
   912  	bs, err := testCodecEncode(v, testGetBytes(), testByteBuf, h, true)
   913  	if err != nil {
   914  		t.Logf("%s: marshal failed: %v", name, err)
   915  		if testVerbose {
   916  			t.Logf("Error encoding %s: %v, Err: %v", name, v, err)
   917  		}
   918  		t.FailNow()
   919  	}
   920  	return
   921  }
   922  
   923  func testUnmarshalErr(v interface{}, data []byte, h Handle, t *testing.T, name string) {
   924  	t.Helper()
   925  	err := testCodecDecode(data, v, h, true)
   926  	if err != nil {
   927  		t.Logf("%s: unmarshal failed: %v", name, err)
   928  		if testVerbose {
   929  			t.Logf("Error Decoding into %s: %v, Err: %v", name, v, err)
   930  		}
   931  		t.FailNow()
   932  	}
   933  }
   934  
   935  func testDeepEqualErr(v1, v2 interface{}, t *testing.T, name string) {
   936  	t.Helper()
   937  	if err := deepEqual(v1, v2); err == nil {
   938  		if testVerbose {
   939  			t.Logf("%s: values equal", name)
   940  		}
   941  	} else {
   942  		t.Logf("%s: values not equal: %v", name, err)
   943  		if testVerbose {
   944  			t.Logf("%s: values not equal: %v. 1: %#v, 2: %#v", name, err, v1, v2)
   945  		}
   946  		t.FailNow()
   947  	}
   948  }
   949  
   950  func testReadWriteCloser(c io.ReadWriteCloser) io.ReadWriteCloser {
   951  	if testRpcBufsize <= 0 && rand.Int63()%2 == 0 {
   952  		return c
   953  	}
   954  	return struct {
   955  		io.Closer
   956  		*bufio.Reader
   957  		*bufio.Writer
   958  	}{c, bufio.NewReaderSize(c, testRpcBufsize), bufio.NewWriterSize(c, testRpcBufsize)}
   959  }
   960  
   961  // testCodecTableOne allows us test for different variations based on arguments passed.
   962  func testCodecTableOne(t *testing.T, testNil bool, h Handle,
   963  	vs []interface{}, vsVerify []interface{}) {
   964  	//if testNil, then just test for when a pointer to a nil interface{} is passed. It should work.
   965  	//Current setup allows us test (at least manually) the nil interface or typed interface.
   966  	if testVerbose {
   967  		t.Logf("================ TestNil: %v: %v entries ================\n", testNil, len(vs))
   968  	}
   969  
   970  	if mh, ok := h.(*MsgpackHandle); ok {
   971  		defer func(a, b bool) {
   972  			mh.RawToString = a
   973  			mh.PositiveIntUnsigned = b
   974  		}(mh.RawToString, mh.PositiveIntUnsigned)
   975  		mh.RawToString = true
   976  		mh.PositiveIntUnsigned = false
   977  	}
   978  
   979  	bh := testBasicHandle(h)
   980  	for i, v0 := range vs {
   981  		if testVerbose {
   982  			t.Logf("..............................................")
   983  			t.Logf("         Testing: #%d:, %T, %#v\n", i, v0, v0)
   984  		}
   985  		// if a TestStrucFlex and we are doing a testNil,
   986  		// ensure the fields which are not encodeable are set to nil appropriately
   987  		// i.e. TestStrucFlex.{MstrUi64TSelf, mapMsu2wss}
   988  		var mapMstrUi64TSelf map[stringUint64T]*stringUint64T
   989  		var mapMsu2wss map[stringUint64T]wrapStringSlice
   990  		// TestStrucFlex.{Msp2ss, Mip2ss} have pointer keys.
   991  		// When we encode and the decode back into the same value,
   992  		// the length of this map will effectively double, because
   993  		// each pointer has equal underlying value, but are separate entries in the map.
   994  		//
   995  		// Best way to compare is to store them, and then compare them later if needed.
   996  		var mapMsp2ss map[*string][]string
   997  		var mapMip2ss map[*uint64][]string
   998  
   999  		tsflex, _ := v0.(*TestStrucFlex)
  1000  		if tsflex != nil {
  1001  			mapMstrUi64TSelf = tsflex.MstrUi64TSelf
  1002  			mapMsu2wss = tsflex.Msu2wss
  1003  			mapMsp2ss = tsflex.Msp2ss
  1004  			mapMip2ss = tsflex.Mip2ss
  1005  			if testNil {
  1006  				tsflex.MstrUi64TSelf = nil
  1007  				tsflex.Msu2wss = nil
  1008  			}
  1009  		}
  1010  		b0 := testMarshalErr(v0, h, t, "v0")
  1011  		var b1 = b0
  1012  		if len(b0) > 1024 {
  1013  			b1 = b0[:1024]
  1014  		}
  1015  		bytesorstr := "string"
  1016  		if h.isBinary() {
  1017  			bytesorstr = "bytes"
  1018  			if len(b0) > 256 {
  1019  				b1 = b0[:256]
  1020  			}
  1021  		}
  1022  		if testVerbose {
  1023  			t.Logf("         Encoded %s: type: %T, len/cap: %v/%v, %v, %s\n", bytesorstr, v0, len(b0), cap(b0), b1, "...")
  1024  		}
  1025  		// TestStrucFlex has many fields which will encode differently if SignedInteger - so skip
  1026  		if _, ok := v0.(*TestStrucFlex); ok && bh.SignedInteger {
  1027  			continue
  1028  		}
  1029  
  1030  		var v1 interface{}
  1031  		var err error
  1032  
  1033  		if tsflex != nil {
  1034  			if testNil {
  1035  				tsflex.MstrUi64TSelf = mapMstrUi64TSelf
  1036  				tsflex.Msu2wss = mapMsu2wss
  1037  			}
  1038  			tsflex.Msp2ss = nil
  1039  			tsflex.Mip2ss = nil
  1040  		}
  1041  
  1042  		if testNil {
  1043  			err = testUnmarshal(&v1, b0, h)
  1044  		} else if v0 != nil {
  1045  			v0rt := reflect.TypeOf(v0) // ptr
  1046  			if v0rt.Kind() == reflect.Ptr {
  1047  				err = testUnmarshal(v0, b0, h)
  1048  				v1 = v0
  1049  			} else {
  1050  				rv1 := reflect.New(v0rt)
  1051  				err = testUnmarshal(rv1.Interface(), b0, h)
  1052  				v1 = rv1.Elem().Interface()
  1053  				// v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface()
  1054  			}
  1055  		}
  1056  
  1057  		if tsflex != nil {
  1058  			// MARKER: consider compare tsflex.{Msp2ss, Mip2ss} to map{Msp2ss, Mip2ss}
  1059  			_, _ = mapMsp2ss, mapMip2ss
  1060  		}
  1061  
  1062  		if testVerbose {
  1063  			t.Logf("         v1 returned: %T, %v %#v", v1, v1, v1)
  1064  		}
  1065  		// if v1 != nil {
  1066  		//	t.Logf("         v1 returned: %T, %#v", v1, v1)
  1067  		//	//we always indirect, because ptr to typed value may be passed (if not testNil)
  1068  		//	v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface()
  1069  		// }
  1070  		if err != nil {
  1071  			t.Logf("-------- Error: %v", err)
  1072  			if testVerbose {
  1073  				t.Logf("-------- Partial return: %v", v1)
  1074  			}
  1075  			t.FailNow()
  1076  		}
  1077  		v0check := vsVerify[i]
  1078  		if v0check == skipVerifyVal || bh.SignedInteger {
  1079  			if testVerbose {
  1080  				t.Logf("        Nil Check skipped: Decoded: %T, %#v\n", v1, v1)
  1081  			}
  1082  			continue
  1083  		}
  1084  		if err = deepEqual(v0check, v1); err == nil {
  1085  			if testVerbose {
  1086  				t.Logf("++++++++ Before and After marshal matched\n")
  1087  			}
  1088  		} else {
  1089  			// t.Logf("-------- Before and After marshal do not match: Error: %v"+
  1090  			// 	" ====> GOLDEN: (%T) %#v, DECODED: (%T) %#v\n", err, v0check, v0check, v1, v1)
  1091  			t.Logf("-------- FAIL: Before and After marshal do not match: Error: %v", err)
  1092  			if testVerbose {
  1093  				t.Logf("    ....... GOLDEN:  (%T) %v %#v", v0check, v0check, v0check)
  1094  				t.Logf("    ....... DECODED: (%T) %v %#v", v1, v1, v1)
  1095  			}
  1096  			t.FailNow()
  1097  		}
  1098  		testReleaseBytes(b0)
  1099  	}
  1100  }
  1101  
  1102  func doTestCodecTableOne(t *testing.T, h Handle) {
  1103  	// since this test modifies maps (and slices?), it should not be run in parallel,
  1104  	// else we may get "concurrent modification/range/set" errors.
  1105  
  1106  	defer testSetupWithChecks(t, &h, false)()
  1107  
  1108  	numPrim, numMap, idxTime, idxMap := testTableNumPrimitives, testTableNumMaps, testTableIdxTime, testTableNumPrimitives+2
  1109  
  1110  	tableVerify := testTableVerify(testVerifyMapTypeSame, h)
  1111  	tableTestNilVerify := testTableVerify(testVerifyDoNil|testVerifyMapTypeStrIntf, h)
  1112  	switch v := h.(type) {
  1113  	case *MsgpackHandle:
  1114  		oldWriteExt := v.WriteExt
  1115  		v.WriteExt = true
  1116  		testCodecTableOne(t, false, h, table, tableVerify)
  1117  		v.WriteExt = oldWriteExt
  1118  	case *JsonHandle:
  1119  		//skip []interface{} containing time.Time, as it encodes as a number, but cannot decode back to time.Time.
  1120  		//As there is no real support for extension tags in json, this must be skipped.
  1121  		testCodecTableOne(t, false, h, table[:numPrim], tableVerify[:numPrim])
  1122  		testCodecTableOne(t, false, h, table[idxMap:], tableVerify[idxMap:])
  1123  	default:
  1124  		testCodecTableOne(t, false, h, table, tableVerify)
  1125  	}
  1126  	// func TestMsgpackAll(t *testing.T) {
  1127  
  1128  	// //skip []interface{} containing time.Time
  1129  	// testCodecTableOne(t, false, h, table[:numPrim], tableVerify[:numPrim])
  1130  	// testCodecTableOne(t, false, h, table[numPrim+1:], tableVerify[numPrim+1:])
  1131  	// func TestMsgpackNilStringMap(t *testing.T) {
  1132  	var oldMapType reflect.Type
  1133  	v := testBasicHandle(h)
  1134  
  1135  	oldMapType, v.MapType = v.MapType, testMapStrIntfTyp
  1136  	// defer func() { v.MapType = oldMapType }()
  1137  	//skip time.Time, []interface{} containing time.Time, last map, and newStruc
  1138  	testCodecTableOne(t, true, h, table[:idxTime], tableTestNilVerify[:idxTime])
  1139  	testCodecTableOne(t, true, h, table[idxMap:idxMap+numMap-1], tableTestNilVerify[idxMap:idxMap+numMap-1]) // failing one for msgpack
  1140  	v.MapType = oldMapType
  1141  	// func TestMsgpackNilIntf(t *testing.T) {
  1142  
  1143  	//do last map and newStruc
  1144  	idx2 := idxMap + numMap - 1
  1145  	testCodecTableOne(t, true, h, table[idx2:], tableTestNilVerify[idx2:])
  1146  	//testCodecTableOne(t, true, h, table[17:18], tableTestNilVerify[17:18]) // do we need this?
  1147  }
  1148  
  1149  func doTestCodecMiscOne(t *testing.T, h Handle) {
  1150  	defer testSetup(t, &h)()
  1151  	var err error
  1152  	bh := testBasicHandle(h)
  1153  	b := testMarshalErr(32, h, t, "32")
  1154  	// Cannot do this nil one, because faster type assertion decoding will panic
  1155  	// var i *int32
  1156  	// if err = testUnmarshal(b, i, nil); err == nil {
  1157  	// 	t.Logf("------- Expecting error because we cannot unmarshal to int32 nil ptr")
  1158  	// 	t.FailNow()
  1159  	// }
  1160  	var i2 int32
  1161  	testUnmarshalErr(&i2, b, h, t, "int32-ptr")
  1162  	if i2 != int32(32) {
  1163  		t.Logf("------- didn't unmarshal to 32: Received: %d", i2)
  1164  		t.FailNow()
  1165  	}
  1166  
  1167  	// func TestMsgpackDecodePtr(t *testing.T) {
  1168  	ts := newTestStrucFlex(testDepth, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly)
  1169  	testReleaseBytes(b)
  1170  	b = testMarshalErr(ts, h, t, "pointer-to-struct")
  1171  	if len(b) < 40 {
  1172  		t.Logf("------- Size must be > 40. Size: %d", len(b))
  1173  		t.FailNow()
  1174  	}
  1175  	var b1 = b
  1176  	if len(b1) > 256 {
  1177  		b1 = b1[:256]
  1178  	}
  1179  	if testVerbose {
  1180  		if h.isBinary() {
  1181  			t.Logf("------- b: size: %v, value: %v", len(b), b1)
  1182  		} else {
  1183  			t.Logf("------- b: size: %v, value: %s", len(b), b1)
  1184  		}
  1185  	}
  1186  
  1187  	// ts2 := testEmptyTestStrucFlex()
  1188  	var ts2 = new(TestStrucFlex)
  1189  	// we initialize and start draining the chan, so that we can decode into it without it blocking due to no consumer
  1190  	ts2.Chstr = make(chan string, teststrucflexChanCap)
  1191  	go func() {
  1192  		for range ts2.Chstr {
  1193  		}
  1194  	}() // drain it
  1195  
  1196  	testUnmarshalErr(ts2, b, h, t, "pointer-to-struct")
  1197  	if ts2.I64 != math.MaxInt64*2/3 {
  1198  		t.Logf("------- Unmarshal wrong. Expect I64 = 64. Got: %v", ts2.I64)
  1199  		t.FailNow()
  1200  	}
  1201  	close(ts2.Chstr)
  1202  	testReleaseBytes(b)
  1203  
  1204  	// Note: These will not work with SliceElementReset or InterfaceReset=true, so handle that.
  1205  	defer func(a, b bool) {
  1206  		bh.SliceElementReset = a
  1207  		bh.InterfaceReset = b
  1208  	}(bh.SliceElementReset, bh.InterfaceReset)
  1209  
  1210  	bh.SliceElementReset = false
  1211  	bh.InterfaceReset = false
  1212  
  1213  	m := map[string]int{"A": 2, "B": 3}
  1214  	p := []interface{}{m}
  1215  	bs := testMarshalErr(p, h, t, "p")
  1216  
  1217  	m2 := map[string]int{}
  1218  	p2 := []interface{}{m2}
  1219  	testUnmarshalErr(&p2, bs, h, t, "&p2")
  1220  
  1221  	if m2["A"] != 2 || m2["B"] != 3 {
  1222  		t.Logf("FAIL: m2 not as expected: expecting: %v, got: %v", m, m2)
  1223  		t.FailNow()
  1224  	}
  1225  
  1226  	testCheckEqual(t, p, p2, "p=p2")
  1227  	testCheckEqual(t, m, m2, "m=m2")
  1228  	if err = deepEqual(p, p2); err == nil {
  1229  		if testVerbose {
  1230  			t.Logf("p and p2 match")
  1231  		}
  1232  	} else {
  1233  		t.Logf("Not Equal: %v. p: %v, p2: %v", err, p, p2)
  1234  		t.FailNow()
  1235  	}
  1236  	if err = deepEqual(m, m2); err == nil {
  1237  		if testVerbose {
  1238  			t.Logf("m and m2 match")
  1239  		}
  1240  	} else {
  1241  		t.Logf("Not Equal: %v. m: %v, m2: %v", err, m, m2)
  1242  		t.FailNow()
  1243  	}
  1244  	testReleaseBytes(bs)
  1245  
  1246  	// func TestMsgpackDecodeStructSubset(t *testing.T) {
  1247  	// test that we can decode a subset of the stream
  1248  	mm := map[string]interface{}{"A": 5, "B": 99, "C": 333}
  1249  	bs = testMarshalErr(mm, h, t, "mm")
  1250  	type ttt struct {
  1251  		A uint8
  1252  		C int32
  1253  	}
  1254  	var t2 ttt
  1255  	testUnmarshalErr(&t2, bs, h, t, "t2")
  1256  	t3 := ttt{5, 333}
  1257  	testCheckEqual(t, t2, t3, "t2=t3")
  1258  	testReleaseBytes(bs)
  1259  
  1260  	// println(">>>>>")
  1261  	// test simple arrays, non-addressable arrays, slices
  1262  	type tarr struct {
  1263  		A int64
  1264  		B [3]int64
  1265  		C []byte
  1266  		D [3]byte
  1267  	}
  1268  	var tarr0 = tarr{1, [3]int64{2, 3, 4}, []byte{4, 5, 6}, [3]byte{7, 8, 9}}
  1269  	// test both pointer and non-pointer (value)
  1270  	for _, tarr1 := range []interface{}{tarr0, &tarr0} {
  1271  		bs = testMarshalErr(tarr1, h, t, "tarr1")
  1272  		if _, ok := h.(*JsonHandle); ok {
  1273  			if testVerbose {
  1274  				t.Logf("Marshal as: %s", bs)
  1275  			}
  1276  		}
  1277  		var tarr2 tarr
  1278  		testUnmarshalErr(&tarr2, bs, h, t, "tarr2")
  1279  		testCheckEqual(t, tarr0, tarr2, "tarr0=tarr2")
  1280  		testReleaseBytes(bs)
  1281  	}
  1282  
  1283  	// test byte array, even if empty (msgpack only)
  1284  	if _, ok := h.(*MsgpackHandle); ok {
  1285  		type ystruct struct {
  1286  			Anarray []byte
  1287  		}
  1288  		var ya = ystruct{}
  1289  		testUnmarshalErr(&ya, []byte{0x91, 0x90}, h, t, "ya")
  1290  	}
  1291  
  1292  	var tt1, tt2 time.Time
  1293  	tt2 = time.Now()
  1294  	bs = testMarshalErr(tt1, h, t, "zero-time-enc")
  1295  	testUnmarshalErr(&tt2, bs, h, t, "zero-time-dec")
  1296  	testDeepEqualErr(tt1, tt2, t, "zero-time-eq")
  1297  	testReleaseBytes(bs)
  1298  
  1299  	// test encoding a slice of byte (but not []byte) and decoding into a []byte
  1300  	var sw = []wrapUint8{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'}
  1301  	var bw []byte // ("ABCDEFGHIJ")
  1302  	bs = testMarshalErr(sw, h, t, "wrap-bytes-enc")
  1303  	testUnmarshalErr(&bw, bs, h, t, "wrap-bytes-dec")
  1304  	testDeepEqualErr(bw, []byte("ABCDEFGHIJ"), t, "wrap-bytes-eq")
  1305  	testReleaseBytes(bs)
  1306  }
  1307  
  1308  func doTestCodecEmbeddedPointer(t *testing.T, h Handle) {
  1309  	defer testSetup(t, &h)()
  1310  	type Z int
  1311  	type A struct {
  1312  		AnInt int
  1313  	}
  1314  	type B struct {
  1315  		*Z
  1316  		*A
  1317  		MoreInt int
  1318  	}
  1319  	var z Z = 4
  1320  	x1 := &B{&z, &A{5}, 6}
  1321  	bs := testMarshalErr(x1, h, t, "x1")
  1322  	var x2 = new(B)
  1323  	testUnmarshalErr(x2, bs, h, t, "x2")
  1324  	testCheckEqual(t, x1, x2, "x1=x2")
  1325  	testReleaseBytes(bs)
  1326  }
  1327  
  1328  func testCodecUnderlyingType(t *testing.T, h Handle) {
  1329  	defer testSetup(t, &h)()
  1330  	// Manual Test.
  1331  	// Run by hand, with accompanying printf.statements in fast-path.go
  1332  	// to ensure that the fast functions are called.
  1333  	type T1 map[string]string
  1334  	v := T1{"1": "1s", "2": "2s"}
  1335  	var bs []byte
  1336  	var err error
  1337  	NewEncoderBytes(&bs, h).MustEncode(v)
  1338  	if err != nil {
  1339  		t.Logf("Error during encode: %v", err)
  1340  		t.FailNow()
  1341  	}
  1342  	var v2 T1
  1343  	NewDecoderBytes(bs, h).MustDecode(&v2)
  1344  	if err != nil {
  1345  		t.Logf("Error during decode: %v", err)
  1346  		t.FailNow()
  1347  	}
  1348  }
  1349  
  1350  func doTestCodecChan(t *testing.T, h Handle) {
  1351  	defer testSetup(t, &h)()
  1352  	// - send a slice []*int64 (sl1) into an chan (ch1) with cap > len(s1)
  1353  	// - encode ch1 as a stream array
  1354  	// - decode a chan (ch2), with cap > len(s1) from the stream array
  1355  	// - receive from ch2 into slice sl2
  1356  	// - compare sl1 and sl2
  1357  
  1358  	{
  1359  		if testVerbose {
  1360  			t.Logf("*int64")
  1361  		}
  1362  		sl1 := make([]*int64, 4)
  1363  		for i := range sl1 {
  1364  			var j int64 = int64(i)
  1365  			sl1[i] = &j
  1366  		}
  1367  		ch1 := make(chan *int64, 4)
  1368  		for _, j := range sl1 {
  1369  			ch1 <- j
  1370  		}
  1371  		var bs []byte
  1372  		NewEncoderBytes(&bs, h).MustEncode(ch1)
  1373  		ch2 := make(chan *int64, 8)
  1374  		NewDecoderBytes(bs, h).MustDecode(&ch2)
  1375  		close(ch2)
  1376  		var sl2 []*int64
  1377  		for j := range ch2 {
  1378  			sl2 = append(sl2, j)
  1379  		}
  1380  		if err := deepEqual(sl1, sl2); err != nil {
  1381  			t.Logf("FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
  1382  			if testVerbose {
  1383  				t.Logf("sl1: %#v, sl2: %#v", sl1, sl2)
  1384  			}
  1385  			t.FailNow()
  1386  		}
  1387  	}
  1388  
  1389  	{
  1390  		if testVerbose {
  1391  			t.Logf("testBytesT []byte - input []byte")
  1392  		}
  1393  		type testBytesT []byte
  1394  		sl1 := make([]testBytesT, 4)
  1395  		for i := range sl1 {
  1396  			var j = []byte(strings.Repeat(strconv.FormatInt(int64(i), 10), i))
  1397  			sl1[i] = j
  1398  		}
  1399  		ch1 := make(chan testBytesT, 4)
  1400  		for _, j := range sl1 {
  1401  			ch1 <- j
  1402  		}
  1403  		var bs []byte
  1404  		NewEncoderBytes(&bs, h).MustEncode(ch1)
  1405  		ch2 := make(chan testBytesT, 8)
  1406  		NewDecoderBytes(bs, h).MustDecode(&ch2)
  1407  		close(ch2)
  1408  		var sl2 []testBytesT
  1409  		for j := range ch2 {
  1410  			// t.Logf(">>>> from chan: is nil? %v, %v", j == nil, j)
  1411  			sl2 = append(sl2, j)
  1412  		}
  1413  		if err := deepEqual(sl1, sl2); err != nil {
  1414  			t.Logf("FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
  1415  			if testVerbose {
  1416  				t.Logf("sl1: %#v, sl2: %#v", sl1, sl2)
  1417  			}
  1418  			t.FailNow()
  1419  		}
  1420  	}
  1421  	{
  1422  		if testVerbose {
  1423  			t.Logf("testBytesT byte - input string/testBytesT")
  1424  		}
  1425  		type testBytesT byte
  1426  		sl1 := make([]testBytesT, 4)
  1427  		for i := range sl1 {
  1428  			var j = strconv.FormatInt(int64(i), 10)[0]
  1429  			sl1[i] = testBytesT(j)
  1430  		}
  1431  		ch1 := make(chan testBytesT, 4)
  1432  		for _, j := range sl1 {
  1433  			ch1 <- j
  1434  		}
  1435  		var bs []byte
  1436  		NewEncoderBytes(&bs, h).MustEncode(ch1)
  1437  		ch2 := make(chan testBytesT, 8)
  1438  		NewDecoderBytes(bs, h).MustDecode(&ch2)
  1439  		close(ch2)
  1440  		var sl2 []testBytesT
  1441  		for j := range ch2 {
  1442  			sl2 = append(sl2, j)
  1443  		}
  1444  		if err := deepEqual(sl1, sl2); err != nil {
  1445  			t.Logf("FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
  1446  			t.FailNow()
  1447  		}
  1448  	}
  1449  
  1450  	{
  1451  		if testVerbose {
  1452  			t.Logf("*[]byte")
  1453  		}
  1454  		sl1 := make([]byte, 4)
  1455  		for i := range sl1 {
  1456  			var j = strconv.FormatInt(int64(i), 10)[0]
  1457  			sl1[i] = byte(j)
  1458  		}
  1459  		ch1 := make(chan byte, 4)
  1460  		for _, j := range sl1 {
  1461  			ch1 <- j
  1462  		}
  1463  		var bs []byte
  1464  		NewEncoderBytes(&bs, h).MustEncode(ch1)
  1465  		ch2 := make(chan byte, 8)
  1466  		NewDecoderBytes(bs, h).MustDecode(&ch2)
  1467  		close(ch2)
  1468  		var sl2 []byte
  1469  		for j := range ch2 {
  1470  			sl2 = append(sl2, j)
  1471  		}
  1472  		if err := deepEqual(sl1, sl2); err != nil {
  1473  			t.Logf("FAIL: Not Match: %v; len: %v, %v", err, len(sl1), len(sl2))
  1474  			t.FailNow()
  1475  		}
  1476  	}
  1477  }
  1478  
  1479  func doTestCodecRpcOne(t *testing.T, rr Rpc, h Handle, doRequest bool, exitSleepMs time.Duration) (port int) {
  1480  	defer testSetup(t, &h)()
  1481  	if testSkipRPCTests {
  1482  		t.Skip(testSkipRPCTestsMsg)
  1483  	}
  1484  	if !testRecoverPanicToErr {
  1485  		t.Skip(testSkipIfNotRecoverPanicToErrMsg)
  1486  	}
  1487  
  1488  	if mh, ok := h.(*MsgpackHandle); ok && mh.SliceElementReset {
  1489  		t.Skipf("skipping ... MsgpackRpcSpec does not handle SliceElementReset - needs investigation")
  1490  	}
  1491  
  1492  	if jsonH, ok := h.(*JsonHandle); ok && !jsonH.TermWhitespace {
  1493  		jsonH.TermWhitespace = true
  1494  		defer func() { jsonH.TermWhitespace = false }()
  1495  	}
  1496  
  1497  	// srv := rpc.NewServer()
  1498  	// srv.Register(testRpcInt)
  1499  	srv := testRpcServer
  1500  
  1501  	ln, err := net.Listen("tcp", "127.0.0.1:0") // listen on ipv4 localhost
  1502  	testCheckErr(t, err)
  1503  	port = (ln.Addr().(*net.TCPAddr)).Port
  1504  	if testVerbose {
  1505  		t.Logf("connFn: addr: %v, network: %v, port: %v", ln.Addr(), ln.Addr().Network(), port)
  1506  	}
  1507  	// var opts *DecoderOptions
  1508  	// opts := testDecOpts
  1509  	// opts.MapType = mapStrIntfTyp
  1510  	serverExitChan := make(chan bool, 1)
  1511  	var serverExitFlag uint64
  1512  	serverFn := func() {
  1513  		var conns []net.Conn
  1514  		var svrcodecs []rpc.ServerCodec
  1515  		defer func() {
  1516  			for i := range conns {
  1517  				svrcodecs[i].Close()
  1518  				conns[i].Close()
  1519  			}
  1520  			ln.Close()
  1521  			serverExitChan <- true
  1522  		}()
  1523  		for {
  1524  			conn1, err1 := ln.Accept()
  1525  			if atomic.LoadUint64(&serverExitFlag) == 1 {
  1526  				if conn1 != nil {
  1527  					conn1.Close()
  1528  				}
  1529  				break
  1530  			}
  1531  			if err1 != nil {
  1532  				// fmt.Printf("accept err1: %v\n", err1)
  1533  				if testVerbose {
  1534  					t.Logf("rpc error accepting connection: %v", err1)
  1535  				}
  1536  				continue
  1537  			}
  1538  			if conn1 != nil {
  1539  				sc := rr.ServerCodec(testReadWriteCloser(conn1), h)
  1540  				conns = append(conns, conn1)
  1541  				svrcodecs = append(svrcodecs, sc)
  1542  				go srv.ServeCodec(sc)
  1543  			}
  1544  		}
  1545  	}
  1546  
  1547  	connFn := func() (bs net.Conn) {
  1548  		bs, err2 := net.Dial(ln.Addr().Network(), ln.Addr().String())
  1549  		testCheckErr(t, err2)
  1550  		return
  1551  	}
  1552  
  1553  	exitFn := func() {
  1554  		if atomic.LoadUint64(&serverExitFlag) == 1 {
  1555  			return
  1556  		}
  1557  		atomic.StoreUint64(&serverExitFlag, 1)
  1558  		bs := connFn()
  1559  		defer bs.Close()
  1560  		<-serverExitChan
  1561  		// atomic.StoreUint64(&serverExitFlag, 0)
  1562  		// serverExitChan <- true
  1563  	}
  1564  
  1565  	go serverFn()
  1566  
  1567  	// runtime.Gosched()
  1568  
  1569  	if exitSleepMs == 0 {
  1570  		defer exitFn()
  1571  	} else {
  1572  		go func() {
  1573  			time.Sleep(exitSleepMs)
  1574  			exitFn()
  1575  		}()
  1576  	}
  1577  
  1578  	if doRequest {
  1579  		func() {
  1580  			bs := connFn()
  1581  			defer bs.Close()
  1582  			cc := rr.ClientCodec(testReadWriteCloser(bs), h)
  1583  			defer cc.Close()
  1584  			cl := rpc.NewClientWithCodec(cc)
  1585  			defer cl.Close()
  1586  			var up, sq, mult int
  1587  			var rstr string
  1588  			testCheckErr(t, cl.Call("TestRpcInt.Update", 5, &up))
  1589  			testCheckEqual(t, atomic.LoadInt64(&testRpcInt.i), int64(5), "testRpcInt.i=5")
  1590  			testCheckEqual(t, up, 5, "up=5")
  1591  			testCheckErr(t, cl.Call("TestRpcInt.Square", 1, &sq))
  1592  			testCheckEqual(t, sq, 25, "sq=25")
  1593  			testCheckErr(t, cl.Call("TestRpcInt.Mult", 20, &mult))
  1594  			testCheckEqual(t, mult, 100, "mult=100")
  1595  			testCheckErr(t, cl.Call("TestRpcInt.EchoStruct", TestRpcABC{"Aa", "Bb", "Cc"}, &rstr))
  1596  			testCheckEqual(t, rstr, fmt.Sprintf("%#v", TestRpcABC{"Aa", "Bb", "Cc"}), "rstr=")
  1597  			testCheckErr(t, cl.Call("TestRpcInt.Echo123", []string{"A1", "B2", "C3"}, &rstr))
  1598  			testCheckEqual(t, rstr, fmt.Sprintf("%#v", []string{"A1", "B2", "C3"}), "rstr=")
  1599  		}()
  1600  	}
  1601  	return
  1602  }
  1603  
  1604  func doTestMapEncodeForCanonical(t *testing.T, h Handle) {
  1605  	defer testSetup(t, &h)()
  1606  	// println("doTestMapEncodeForCanonical")
  1607  	v1 := map[stringUint64T]interface{}{
  1608  		stringUint64T{"a", 1}: 1,
  1609  		stringUint64T{"b", 2}: "hello",
  1610  		stringUint64T{"c", 3}: map[string]interface{}{
  1611  			"c/a": 1,
  1612  			"c/b": "world",
  1613  			"c/c": []int{1, 2, 3, 4},
  1614  			"c/d": map[string]interface{}{
  1615  				"c/d/a": "fdisajfoidsajfopdjsaopfjdsapofda",
  1616  				"c/d/b": "fdsafjdposakfodpsakfopdsakfpodsakfpodksaopfkdsopafkdopsa",
  1617  				"c/d/c": "poir02  ir30qif4p03qir0pogjfpoaerfgjp ofke[padfk[ewapf kdp[afep[aw",
  1618  				"c/d/d": "fdsopafkd[sa f-32qor-=4qeof -afo-erfo r-eafo 4e-  o r4-qwo ag",
  1619  				"c/d/e": "kfep[a sfkr0[paf[a foe-[wq  ewpfao-q ro3-q ro-4qof4-qor 3-e orfkropzjbvoisdb",
  1620  				"c/d/f": "",
  1621  			},
  1622  			"c/e": map[int]string{
  1623  				1:     "1",
  1624  				22:    "22",
  1625  				333:   "333",
  1626  				4444:  "4444",
  1627  				55555: "55555",
  1628  			},
  1629  			"c/f": map[string]int{
  1630  				"1":     1,
  1631  				"22":    22,
  1632  				"333":   333,
  1633  				"4444":  4444,
  1634  				"55555": 55555,
  1635  			},
  1636  			"c/g": map[bool]int{
  1637  				false: 0,
  1638  				true:  1,
  1639  			},
  1640  			"c/t": map[time.Time]int64{
  1641  				time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC): time.Date(2019, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano(),
  1642  				time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC): time.Date(2018, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano(),
  1643  				time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC): time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC).UnixNano(),
  1644  			},
  1645  		},
  1646  	}
  1647  	var v2 map[stringUint64T]interface{}
  1648  	var b1, b2, b3 []byte
  1649  
  1650  	// encode v1 into b1, decode b1 into v2, encode v2 into b2, and compare b1 and b2.
  1651  	// OR
  1652  	// encode v1 into b1, decode b1 into v2, encode v2 into b2 and b3, and compare b2 and b3.
  1653  	//   e.g. when doing cbor indefinite, we may haveto use out-of-band encoding
  1654  	//   where each key is encoded as an indefinite length string, which makes it not the same
  1655  	//   order as the strings were lexicographically ordered before.
  1656  
  1657  	var cborIndef bool
  1658  	if ch, ok := h.(*CborHandle); ok {
  1659  		cborIndef = ch.IndefiniteLength
  1660  	}
  1661  
  1662  	// if ch, ok := h.(*BincHandle); ok && ch.AsSymbols != 2 {
  1663  	// 	defer func(u uint8) { ch.AsSymbols = u }(ch.AsSymbols)
  1664  	// 	ch.AsSymbols = 2
  1665  	// }
  1666  
  1667  	bh := testBasicHandle(h)
  1668  
  1669  	defer func(c, si bool) {
  1670  		bh.Canonical = c
  1671  		bh.SignedInteger = si
  1672  	}(bh.Canonical, bh.SignedInteger)
  1673  	bh.Canonical = true
  1674  	// bh.SignedInteger = true
  1675  
  1676  	e1 := NewEncoderBytes(&b1, h)
  1677  	e1.MustEncode(v1)
  1678  	d1 := NewDecoderBytes(b1, h)
  1679  	d1.MustDecode(&v2)
  1680  	// testDeepEqualErr(v1, v2, t, "huh?")
  1681  	e2 := NewEncoderBytes(&b2, h)
  1682  	e2.MustEncode(v2)
  1683  	var b1t, b2t = b1, b2
  1684  	if cborIndef {
  1685  		e2 = NewEncoderBytes(&b3, h)
  1686  		e2.MustEncode(v2)
  1687  		b1t, b2t = b2, b3
  1688  	}
  1689  
  1690  	if !bytes.Equal(b1t, b2t) {
  1691  		t.Logf("Unequal bytes of length: %v vs %v", len(b1t), len(b2t))
  1692  		if testVerbose {
  1693  			t.Logf("Unequal bytes: \n\t%v \n\tVS \n\t%v", b1t, b2t)
  1694  		}
  1695  		t.FailNow()
  1696  	}
  1697  }
  1698  
  1699  func doTestStdEncIntf(t *testing.T, h Handle) {
  1700  	defer testSetup(t, &h)()
  1701  	args := [][2]interface{}{
  1702  		{&TestABC{"A", "BB", "CCC"}, new(TestABC)},
  1703  		{&TestABC2{"AAA", "BB", "C"}, new(TestABC2)},
  1704  	}
  1705  	for _, a := range args {
  1706  		var b []byte
  1707  		e := NewEncoderBytes(&b, h)
  1708  		e.MustEncode(a[0])
  1709  		d := NewDecoderBytes(b, h)
  1710  		d.MustDecode(a[1])
  1711  		if err := deepEqual(a[0], a[1]); err == nil {
  1712  			if testVerbose {
  1713  				t.Logf("++++ Objects match")
  1714  			}
  1715  		} else {
  1716  			t.Logf("---- FAIL: Objects do not match: y0: %v, y1: %v, err: %v", a[0], a[1], err)
  1717  			t.FailNow()
  1718  		}
  1719  	}
  1720  }
  1721  
  1722  func doTestEncCircularRef(t *testing.T, h Handle) {
  1723  	if !testRecoverPanicToErr {
  1724  		t.Skip(testSkipIfNotRecoverPanicToErrMsg)
  1725  	}
  1726  	defer testSetup(t, &h)()
  1727  	type T1 struct {
  1728  		S string
  1729  		B bool
  1730  		T interface{}
  1731  	}
  1732  	type T2 struct {
  1733  		S string
  1734  		T *T1
  1735  	}
  1736  	type T3 struct {
  1737  		S string
  1738  		T *T2
  1739  	}
  1740  	t1 := T1{"t1", true, nil}
  1741  	t2 := T2{"t2", &t1}
  1742  	t3 := T3{"t3", &t2}
  1743  	t1.T = &t3
  1744  
  1745  	var bs []byte
  1746  	var err error
  1747  
  1748  	bh := testBasicHandle(h)
  1749  	if !bh.CheckCircularRef {
  1750  		bh.CheckCircularRef = true
  1751  		defer func() { bh.CheckCircularRef = false }()
  1752  	}
  1753  	err = NewEncoderBytes(&bs, h).Encode(&t3)
  1754  	if err == nil || !strings.Contains(err.Error(), "circular reference found") {
  1755  		t.Logf("expect circular reference error, got: %v", err)
  1756  		t.FailNow()
  1757  	}
  1758  	if x := err.Error(); strings.Contains(x, "circular") || strings.Contains(x, "cyclic") {
  1759  		if testVerbose {
  1760  			t.Logf("error detected as expected: %v", x)
  1761  		}
  1762  	} else {
  1763  		t.Logf("FAIL: error detected was not as expected: %v", x)
  1764  		t.FailNow()
  1765  	}
  1766  }
  1767  
  1768  // TestAnonCycleT{1,2,3} types are used to test anonymous cycles.
  1769  // They are top-level, so that they can have circular references.
  1770  type (
  1771  	TestAnonCycleT1 struct {
  1772  		S string
  1773  		TestAnonCycleT2
  1774  	}
  1775  	TestAnonCycleT2 struct {
  1776  		S2 string
  1777  		TestAnonCycleT3
  1778  	}
  1779  	TestAnonCycleT3 struct {
  1780  		*TestAnonCycleT1
  1781  	}
  1782  )
  1783  
  1784  func doTestAnonCycle(t *testing.T, h Handle) {
  1785  	defer testSetup(t, &h)()
  1786  	var x TestAnonCycleT1
  1787  	x.S = "hello"
  1788  	x.TestAnonCycleT2.S2 = "hello.2"
  1789  	x.TestAnonCycleT2.TestAnonCycleT3.TestAnonCycleT1 = &x
  1790  
  1791  	// just check that you can get typeInfo for T1
  1792  	rt := reflect.TypeOf((*TestAnonCycleT1)(nil)).Elem()
  1793  	rtid := rt2id(rt)
  1794  	pti := testBasicHandle(h).getTypeInfo(rtid, rt)
  1795  	if testVerbose {
  1796  		t.Logf("[%s] pti: %v", h.Name(), pti)
  1797  	}
  1798  }
  1799  
  1800  func doTestAllErrWriter(t *testing.T, hh ...Handle) {
  1801  	if !testRecoverPanicToErr {
  1802  		t.Skip(testSkipIfNotRecoverPanicToErrMsg)
  1803  	}
  1804  	defer testSetup(t, nil)()
  1805  	for _, h := range hh {
  1806  		if testUseParallel {
  1807  			h = testHandleCopy(h)
  1808  		}
  1809  		__doTestErrWriter(t, h)
  1810  	}
  1811  }
  1812  
  1813  func __doTestErrWriter(t *testing.T, h Handle) {
  1814  	name := h.Name()
  1815  	var ew testErrWriter
  1816  	w := bufio.NewWriterSize(&ew, 4)
  1817  	enc := NewEncoder(w, h)
  1818  	for i := 0; i < 4; i++ {
  1819  		err := enc.Encode("ugorji")
  1820  		if ev, ok := err.(*codecError); ok {
  1821  			err = ev.Cause()
  1822  		}
  1823  		if err != testErrWriterErr {
  1824  			t.Logf("%s: expecting err: %v, received: %v", name, testErrWriterErr, err)
  1825  			t.FailNow()
  1826  		}
  1827  	}
  1828  }
  1829  
  1830  func __doTestJsonLargeInteger(t *testing.T, v interface{}, ias uint8, jh *JsonHandle) {
  1831  	if testVerbose {
  1832  		t.Logf("Running TestJsonLargeInteger: v: %#v, ias: %c", v, ias)
  1833  	}
  1834  	oldIAS := jh.IntegerAsString
  1835  	defer func() { jh.IntegerAsString = oldIAS }()
  1836  	jh.IntegerAsString = ias
  1837  
  1838  	var vu uint
  1839  	var vi int
  1840  	var vb bool
  1841  	var b []byte
  1842  	e := NewEncoderBytes(&b, jh)
  1843  	e.MustEncode(v)
  1844  	e.MustEncode(true)
  1845  	d := NewDecoderBytes(b, jh)
  1846  	// below, we validate that the json string or number was encoded,
  1847  	// then decode, and validate that the correct value was decoded.
  1848  	fnStrChk := func() {
  1849  		// check that output started with '"', and ended with '"true'
  1850  		// if !(len(b) >= 7 && b[0] == '"' && string(b[len(b)-7:]) == `" true `) {
  1851  		if !(len(b) >= 5 && b[0] == '"' && string(b[len(b)-5:]) == `"true`) {
  1852  			t.Logf("Expecting a JSON string, got: '%s'", b)
  1853  			t.FailNow()
  1854  		}
  1855  	}
  1856  
  1857  	switch ias {
  1858  	case 'L':
  1859  		switch v2 := v.(type) {
  1860  		case int:
  1861  			v2n := int64(v2) // done to work with 32-bit OS
  1862  			if v2n > 1<<53 || (v2n < 0 && -v2n > 1<<53) {
  1863  				fnStrChk()
  1864  			}
  1865  		case uint:
  1866  			v2n := uint64(v2) // done to work with 32-bit OS
  1867  			if v2n > 1<<53 {
  1868  				fnStrChk()
  1869  			}
  1870  		}
  1871  	case 'A':
  1872  		fnStrChk()
  1873  	default:
  1874  		// check that output doesn't contain " at all
  1875  		for _, i := range b {
  1876  			if i == '"' {
  1877  				t.Logf("Expecting a JSON Number without quotation: got: %s", b)
  1878  				t.FailNow()
  1879  			}
  1880  		}
  1881  	}
  1882  	switch v2 := v.(type) {
  1883  	case int:
  1884  		d.MustDecode(&vi)
  1885  		d.MustDecode(&vb)
  1886  		// check that vb = true, and vi == v2
  1887  		if !(vb && vi == v2) {
  1888  			t.Logf("Expecting equal values from %s: got golden: %v, decoded: %v", b, v2, vi)
  1889  			t.FailNow()
  1890  		}
  1891  	case uint:
  1892  		d.MustDecode(&vu)
  1893  		d.MustDecode(&vb)
  1894  		// check that vb = true, and vi == v2
  1895  		if !(vb && vu == v2) {
  1896  			t.Logf("Expecting equal values from %s: got golden: %v, decoded: %v", b, v2, vu)
  1897  			t.FailNow()
  1898  		}
  1899  	}
  1900  }
  1901  
  1902  func doTestRawValue(t *testing.T, h Handle) {
  1903  	defer testSetup(t, &h)()
  1904  	bh := testBasicHandle(h)
  1905  	if !bh.Raw {
  1906  		bh.Raw = true
  1907  		defer func() { bh.Raw = false }()
  1908  	}
  1909  
  1910  	var i, i2 int
  1911  	var v, v2 TestRawValue
  1912  	var bs, bs2 []byte
  1913  
  1914  	i = 1234 //1234567890
  1915  	v = TestRawValue{I: i}
  1916  	e := NewEncoderBytes(&bs, h)
  1917  	e.MustEncode(v.I)
  1918  	if testVerbose {
  1919  		t.Logf(">>> raw: %v, %s\n", bs, bs)
  1920  	}
  1921  
  1922  	v.R = Raw(bs)
  1923  	e.ResetBytes(&bs2)
  1924  	e.MustEncode(v)
  1925  
  1926  	if testVerbose {
  1927  		t.Logf(">>> bs2: %v, %s\n", bs2, bs2)
  1928  	}
  1929  	d := NewDecoderBytes(bs2, h)
  1930  	d.MustDecode(&v2)
  1931  	d.ResetBytes(v2.R)
  1932  	if testVerbose {
  1933  		t.Logf(">>> v2.R: %v, %s\n", ([]byte)(v2.R), ([]byte)(v2.R))
  1934  	}
  1935  	d.MustDecode(&i2)
  1936  
  1937  	if testVerbose {
  1938  		t.Logf(">>> Encoded %v, decoded %v\n", i, i2)
  1939  	}
  1940  	// t.Logf("Encoded %v, decoded %v", i, i2)
  1941  	if i != i2 {
  1942  		t.Logf("Error: encoded %v, decoded %v", i, i2)
  1943  		t.FailNow()
  1944  	}
  1945  }
  1946  
  1947  // Comprehensive testing that generates data encoded from python handle (cbor, msgpack),
  1948  // and validates that our code can read and write it out accordingly.
  1949  // We keep this unexported here, and put actual test in ext_dep_test.go.
  1950  // This way, it can be excluded by excluding file completely.
  1951  func doTestPythonGenStreams(t *testing.T, h Handle) {
  1952  	defer testSetup(t, &h)()
  1953  
  1954  	// time0 := time.Now()
  1955  	// defer func() { xdebugf("python-gen-streams: %s: took: %v", h.Name(), time.Since(time0)) }()
  1956  
  1957  	name := h.Name()
  1958  	if testVerbose {
  1959  		t.Logf("TestPythonGenStreams-%v", name)
  1960  	}
  1961  	tmpdir, err := ioutil.TempDir("", "golang-"+name+"-test")
  1962  	if err != nil {
  1963  		t.Logf("-------- Unable to create temp directory\n")
  1964  		t.FailNow()
  1965  	}
  1966  	defer os.RemoveAll(tmpdir)
  1967  	if testVerbose {
  1968  		t.Logf("tmpdir: %v", tmpdir)
  1969  	}
  1970  	cmd := exec.Command("python", "test.py", "testdata", tmpdir)
  1971  	//cmd.Stdin = strings.NewReader("some input")
  1972  	//cmd.Stdout = &out
  1973  	var cmdout []byte
  1974  	if cmdout, err = cmd.CombinedOutput(); err != nil {
  1975  		t.Logf("-------- Error running test.py testdata. Err: %v", err)
  1976  		t.Logf("         %v", string(cmdout))
  1977  		t.FailNow()
  1978  	}
  1979  
  1980  	bh := testBasicHandle(h)
  1981  
  1982  	defer func(tt reflect.Type) { bh.MapType = tt }(bh.MapType)
  1983  	defer func(b bool) { bh.RawToString = b }(bh.RawToString)
  1984  
  1985  	// msgpack python needs Raw converted to string
  1986  	bh.RawToString = true
  1987  
  1988  	oldMapType := bh.MapType
  1989  	tablePythonVerify := testTableVerify(testVerifyForPython|testVerifyTimeAsInteger|testVerifyMapTypeStrIntf, h)
  1990  	for i, v := range tablePythonVerify {
  1991  		// if v == uint64(0) && h is a *MsgpackHandle: v = int64(0)
  1992  		bh.MapType = oldMapType
  1993  		//load up the golden file based on number
  1994  		//decode it
  1995  		//compare to in-mem object
  1996  		//encode it again
  1997  		//compare to output stream
  1998  		if testVerbose {
  1999  			t.Logf("..............................................")
  2000  			t.Logf("         Testing: #%d: %T, %#v\n", i, v, v)
  2001  		}
  2002  		var bss []byte
  2003  		bss, err = ioutil.ReadFile(filepath.Join(tmpdir, strconv.Itoa(i)+"."+name+".golden"))
  2004  		if err != nil {
  2005  			t.Logf("-------- Error reading golden file: %d. Err: %v", i, err)
  2006  			t.FailNow()
  2007  			continue
  2008  		}
  2009  		bh.MapType = testMapStrIntfTyp
  2010  
  2011  		var v1 interface{}
  2012  		if err = testUnmarshal(&v1, bss, h); err != nil {
  2013  			t.Logf("-------- Error decoding stream: %d: Err: %v", i, err)
  2014  			t.FailNow()
  2015  			continue
  2016  		}
  2017  		if v == skipVerifyVal {
  2018  			continue
  2019  		}
  2020  		//no need to indirect, because we pass a nil ptr, so we already have the value
  2021  		//if v1 != nil { v1 = reflect.Indirect(reflect.ValueOf(v1)).Interface() }
  2022  		if err = deepEqual(v, v1); err == nil {
  2023  			if testVerbose {
  2024  				t.Logf("++++++++ Objects match: %T, %v", v, v)
  2025  			}
  2026  		} else {
  2027  			t.Logf("-------- FAIL: Objects do not match: %v. Source: %T. Decoded: %T", err, v, v1)
  2028  			if testVerbose {
  2029  				t.Logf("--------   GOLDEN: %#v", v)
  2030  				// t.Logf("--------   DECODED: %#v <====> %#v", v1, reflect.Indirect(reflect.ValueOf(v1)).Interface())
  2031  				t.Logf("--------   DECODED: %#v <====> %#v", v1, reflect.Indirect(reflect.ValueOf(v1)).Interface())
  2032  			}
  2033  			t.FailNow()
  2034  		}
  2035  		bsb, err := testMarshal(v1, h)
  2036  		if err != nil {
  2037  			t.Logf("Error encoding to stream: %d: Err: %v", i, err)
  2038  			t.FailNow()
  2039  			continue
  2040  		}
  2041  		if err = deepEqual(bsb, bss); err == nil {
  2042  			if testVerbose {
  2043  				t.Logf("++++++++ Bytes match")
  2044  			}
  2045  		} else {
  2046  			xs := "--------"
  2047  			if reflect.ValueOf(v).Kind() == reflect.Map {
  2048  				xs = "        "
  2049  				if testVerbose {
  2050  					t.Logf("%s FAIL - bytes do not match, but it's a map (ok - dependent on ordering): %v", xs, err)
  2051  				}
  2052  			} else {
  2053  				t.Logf("%s FAIL - bytes do not match and is not a map (bad): %v", xs, err)
  2054  				t.FailNow()
  2055  			}
  2056  			if testVerbose {
  2057  				t.Logf("%s   FROM_FILE: %4d] %v", xs, len(bss), bss)
  2058  				t.Logf("%s     ENCODED: %4d] %v", xs, len(bsb), bsb)
  2059  			}
  2060  		}
  2061  		testReleaseBytes(bsb)
  2062  	}
  2063  	bh.MapType = oldMapType
  2064  }
  2065  
  2066  // To test MsgpackSpecRpc, we test 3 scenarios:
  2067  //    - Go Client to Go RPC Service (contained within TestMsgpackRpcSpec)
  2068  //    - Go client to Python RPC Service (contained within doTestMsgpackRpcSpecGoClientToPythonSvc)
  2069  //    - Python Client to Go RPC Service (contained within doTestMsgpackRpcSpecPythonClientToGoSvc)
  2070  //
  2071  // This allows us test the different calling conventions
  2072  //    - Go Service requires only one argument
  2073  //    - Python Service allows multiple arguments
  2074  
  2075  func doTestMsgpackRpcSpecGoClientToPythonSvc(t *testing.T, h Handle) {
  2076  	if testSkipRPCTests {
  2077  		t.Skip(testSkipRPCTestsMsg)
  2078  	}
  2079  	defer testSetup(t, &h)()
  2080  
  2081  	// openPorts are between 6700 and 6800
  2082  	r := rand.New(rand.NewSource(time.Now().UnixNano()))
  2083  	openPort := strconv.FormatInt(6700+r.Int63n(99), 10)
  2084  	// openPort := "6792"
  2085  	cmd := exec.Command("python", "test.py", "rpc-server", openPort, "4")
  2086  	testCheckErr(t, cmd.Start())
  2087  	bs, err2 := net.Dial("tcp", ":"+openPort)
  2088  	maxSleepTime := 500 * time.Millisecond
  2089  	iterSleepTime := 5 * time.Millisecond
  2090  	for i := 0; i < int(maxSleepTime/iterSleepTime) && err2 != nil; i++ {
  2091  		time.Sleep(iterSleepTime) // time for python rpc server to start
  2092  		bs, err2 = net.Dial("tcp", ":"+openPort)
  2093  	}
  2094  	testCheckErr(t, err2)
  2095  	cc := MsgpackSpecRpc.ClientCodec(testReadWriteCloser(bs), h)
  2096  	cl := rpc.NewClientWithCodec(cc)
  2097  	defer cl.Close()
  2098  	var rstr string
  2099  	testCheckErr(t, cl.Call("EchoStruct", TestRpcABC{"Aa", "Bb", "Cc"}, &rstr))
  2100  	//testCheckEqual(t, rstr, "{'A': 'Aa', 'B': 'Bb', 'C': 'Cc'}")
  2101  	var mArgs MsgpackSpecRpcMultiArgs = []interface{}{"A1", "B2", "C3"}
  2102  	testCheckErr(t, cl.Call("Echo123", mArgs, &rstr))
  2103  	testCheckEqual(t, rstr, "1:A1 2:B2 3:C3", "rstr=")
  2104  	cmd.Process.Kill()
  2105  }
  2106  
  2107  func doTestMsgpackRpcSpecPythonClientToGoSvc(t *testing.T, h Handle) {
  2108  	if testSkipRPCTests {
  2109  		t.Skip(testSkipRPCTestsMsg)
  2110  	}
  2111  	defer testSetup(t, &h)()
  2112  	// seems 10ms is not enough for test run; set high to 1 second, and client will close when done
  2113  	exitSleepDur := 1000 * time.Millisecond
  2114  	port := doTestCodecRpcOne(t, MsgpackSpecRpc, h, false, exitSleepDur)
  2115  	cmd := exec.Command("python", "test.py", "rpc-client-go-service", strconv.Itoa(port))
  2116  	var cmdout []byte
  2117  	var err error
  2118  	if cmdout, err = cmd.CombinedOutput(); err != nil {
  2119  		t.Logf("-------- Error running test.py rpc-client-go-service. Err: %v", err)
  2120  		t.Logf("         %v", string(cmdout))
  2121  		t.FailNow()
  2122  	}
  2123  	testCheckEqual(t, string(cmdout),
  2124  		fmt.Sprintf("%#v\n%#v\n", []string{"A1", "B2", "C3"}, TestRpcABC{"Aa", "Bb", "Cc"}), "cmdout=")
  2125  }
  2126  
  2127  func doTestSwallowAndZero(t *testing.T, h Handle) {
  2128  	defer testSetup(t, &h)()
  2129  	v1 := newTestStrucFlex(testDepth, testNumRepeatString, false, false, testMapStringKeyOnly)
  2130  	var b1 []byte
  2131  
  2132  	e1 := NewEncoderBytes(&b1, h)
  2133  	e1.MustEncode(v1)
  2134  	d1 := NewDecoderBytes(b1, h)
  2135  	d1.swallow()
  2136  	if d1.r().numread() != uint(len(b1)) {
  2137  		t.Logf("swallow didn't consume all encoded bytes: %v out of %v", d1.r().numread(), len(b1))
  2138  		t.FailNow()
  2139  	}
  2140  	setZero(v1)
  2141  	testDeepEqualErr(v1, &TestStrucFlex{}, t, "filled-and-zeroed")
  2142  }
  2143  
  2144  func doTestRawExt(t *testing.T, h Handle) {
  2145  	defer testSetup(t, &h)()
  2146  	var b []byte
  2147  	var v RawExt // interface{}
  2148  	_, isJson := h.(*JsonHandle)
  2149  	_, isCbor := h.(*CborHandle)
  2150  	bh := testBasicHandle(h)
  2151  	// isValuer := isJson || isCbor
  2152  	// _ = isValuer
  2153  	for _, r := range []RawExt{
  2154  		{Tag: 99, Value: "9999", Data: []byte("9999")},
  2155  	} {
  2156  		e := NewEncoderBytes(&b, h)
  2157  		e.MustEncode(&r)
  2158  		// fmt.Printf(">>>> rawext: isnil? %v, %d - %v\n", b == nil, len(b), b)
  2159  		d := NewDecoderBytes(b, h)
  2160  		d.MustDecode(&v)
  2161  		var r2 = r
  2162  		switch {
  2163  		case isJson:
  2164  			r2.Tag = 0
  2165  			r2.Data = nil
  2166  		case isCbor:
  2167  			r2.Data = nil
  2168  		default:
  2169  			r2.Value = nil
  2170  		}
  2171  		testDeepEqualErr(v, r2, t, "rawext-default")
  2172  		// switch h.(type) {
  2173  		// case *JsonHandle:
  2174  		// 	testDeepEqualErr(r.Value, v, t, "rawext-json")
  2175  		// default:
  2176  		// 	var r2 = r
  2177  		// 	if isValuer {
  2178  		// 		r2.Data = nil
  2179  		// 	} else {
  2180  		// 		r2.Value = nil
  2181  		// 	}
  2182  		// 	testDeepEqualErr(v, r2, t, "rawext-default")
  2183  		// }
  2184  	}
  2185  
  2186  	// Add testing for Raw also
  2187  	if b != nil {
  2188  		b = b[:0]
  2189  	}
  2190  	oldRawMode := bh.Raw
  2191  	defer func() { bh.Raw = oldRawMode }()
  2192  	bh.Raw = true
  2193  
  2194  	var v2 Raw
  2195  	for _, s := range []string{
  2196  		"goodbye",
  2197  		"hello",
  2198  	} {
  2199  		e := NewEncoderBytes(&b, h)
  2200  		e.MustEncode(&s)
  2201  		// fmt.Printf(">>>> rawext: isnil? %v, %d - %v\n", b == nil, len(b), b)
  2202  		var r Raw = make([]byte, len(b))
  2203  		copy(r, b)
  2204  		d := NewDecoderBytes(b, h)
  2205  		d.MustDecode(&v2)
  2206  		testDeepEqualErr(v2, r, t, "raw-default")
  2207  	}
  2208  
  2209  }
  2210  
  2211  // func doTestTimeExt(t *testing.T, h Handle) {
  2212  // 	var t = time.Now()
  2213  // 	// add time ext to the handle
  2214  // }
  2215  
  2216  func doTestMapStructKey(t *testing.T, h Handle) {
  2217  	defer testSetup(t, &h)()
  2218  	var b []byte
  2219  	var v interface{} // map[stringUint64T]wrapUint64Slice // interface{}
  2220  	bh := testBasicHandle(h)
  2221  	m := map[stringUint64T]wrapUint64Slice{
  2222  		stringUint64T{"55555", 55555}: []wrapUint64{12345},
  2223  		stringUint64T{"333", 333}:     []wrapUint64{123},
  2224  	}
  2225  	oldCanonical := bh.Canonical
  2226  	oldMapType := bh.MapType
  2227  	defer func() {
  2228  		bh.Canonical = oldCanonical
  2229  		bh.MapType = oldMapType
  2230  	}()
  2231  
  2232  	bh.MapType = reflect.TypeOf((*map[stringUint64T]wrapUint64Slice)(nil)).Elem()
  2233  	for _, bv := range [2]bool{true, false} {
  2234  		b, v = nil, nil
  2235  		bh.Canonical = bv
  2236  		e := NewEncoderBytes(&b, h)
  2237  		e.MustEncode(m)
  2238  		d := NewDecoderBytes(b, h)
  2239  		d.MustDecode(&v)
  2240  		testDeepEqualErr(v, m, t, "map-structkey")
  2241  	}
  2242  }
  2243  
  2244  func doTestDecodeNilMapValue(t *testing.T, h Handle) {
  2245  	defer testSetup(t, &h)()
  2246  	type Struct struct {
  2247  		Field map[uint16]map[uint32]struct{}
  2248  	}
  2249  
  2250  	bh := testBasicHandle(h)
  2251  	defer func(t reflect.Type) {
  2252  		bh.MapType = t
  2253  	}(bh.MapType)
  2254  
  2255  	// this test expects that nil doesn't result in deleting entries
  2256  
  2257  	_, isJsonHandle := h.(*JsonHandle)
  2258  
  2259  	toEncode := Struct{Field: map[uint16]map[uint32]struct{}{
  2260  		1: nil,
  2261  	}}
  2262  
  2263  	bs := testMarshalErr(toEncode, h, t, "-")
  2264  	if isJsonHandle && testVerbose {
  2265  		t.Logf("json encoded: %s\n", bs)
  2266  	}
  2267  
  2268  	var decoded Struct
  2269  	testUnmarshalErr(&decoded, bs, h, t, "-")
  2270  	if !reflect.DeepEqual(decoded, toEncode) {
  2271  		t.Logf("Decoded value %#v != %#v", decoded, toEncode)
  2272  		t.FailNow()
  2273  	}
  2274  	testReleaseBytes(bs)
  2275  
  2276  	__doTestDecodeNilMapEntryValue(t, h)
  2277  }
  2278  
  2279  func __doTestDecodeNilMapEntryValue(t *testing.T, h Handle) {
  2280  	type Entry struct{}
  2281  	type Entries struct {
  2282  		Map map[string]*Entry
  2283  	}
  2284  
  2285  	c := Entries{
  2286  		Map: map[string]*Entry{
  2287  			"nil":   nil,
  2288  			"empty": &Entry{},
  2289  		},
  2290  	}
  2291  
  2292  	bs, err := testMarshal(&c, h)
  2293  	if err != nil {
  2294  		t.Logf("failed to encode: %v", err)
  2295  		t.FailNow()
  2296  	}
  2297  
  2298  	var f Entries
  2299  	err = testUnmarshal(&f, bs, h)
  2300  	if err != nil {
  2301  		t.Logf("failed to decode: %v", err)
  2302  		t.FailNow()
  2303  	}
  2304  
  2305  	if !reflect.DeepEqual(c, f) {
  2306  		t.Logf("roundtrip encoding doesn't match\nexpected: %v\nfound:    %v\n\n"+
  2307  			"empty value: %#+v\nnil value:   %#+v",
  2308  			c, f, f.Map["empty"], f.Map["nil"])
  2309  		t.FailNow()
  2310  	}
  2311  	testReleaseBytes(bs)
  2312  }
  2313  
  2314  func doTestEmbeddedFieldPrecedence(t *testing.T, h Handle) {
  2315  	defer testSetup(t, &h)()
  2316  	type Embedded struct {
  2317  		Field byte
  2318  	}
  2319  	type Struct struct {
  2320  		Field byte
  2321  		Embedded
  2322  	}
  2323  	toEncode := Struct{
  2324  		Field:    1,
  2325  		Embedded: Embedded{Field: 2},
  2326  	}
  2327  	_, isJsonHandle := h.(*JsonHandle)
  2328  	handle := testBasicHandle(h)
  2329  	oldMapType := handle.MapType
  2330  	defer func() { handle.MapType = oldMapType }()
  2331  
  2332  	handle.MapType = reflect.TypeOf(map[interface{}]interface{}(nil))
  2333  
  2334  	bs, err := testMarshal(toEncode, h)
  2335  	if err != nil {
  2336  		t.Logf("Error encoding: %v, Err: %v", toEncode, err)
  2337  		t.FailNow()
  2338  	}
  2339  
  2340  	var decoded Struct
  2341  	err = testUnmarshal(&decoded, bs, h)
  2342  	if err != nil {
  2343  		t.Logf("Error decoding: %v", err)
  2344  		t.FailNow()
  2345  	}
  2346  
  2347  	if decoded.Field != toEncode.Field {
  2348  		t.Logf("Decoded result %v != %v", decoded.Field, toEncode.Field) // hex to look at what was encoded
  2349  		if isJsonHandle {
  2350  			t.Logf("JSON encoded as: %s", bs) // hex to look at what was encoded
  2351  		}
  2352  		t.FailNow()
  2353  	}
  2354  	testReleaseBytes(bs)
  2355  }
  2356  
  2357  func doTestLargeContainerLen(t *testing.T, h Handle) {
  2358  	defer testSetup(t, &h)()
  2359  
  2360  	// This test can take a while if run multiple times in a loop, as it creates
  2361  	// large maps/slices. Use t.Short() appropriately to limit its execution time.
  2362  
  2363  	// Note that this test does not make sense for formats which do not pre-record
  2364  	// the length, like json, or cbor with indefinite length.
  2365  	if _, ok := h.(*JsonHandle); ok {
  2366  		t.Skipf("skipping as json doesn't support prefixed lengths")
  2367  	}
  2368  	if c, ok := h.(*CborHandle); ok && c.IndefiniteLength {
  2369  		t.Skipf("skipping as cbor Indefinite Length doesn't use prefixed lengths")
  2370  	}
  2371  
  2372  	bh := testBasicHandle(h)
  2373  
  2374  	var sizes []int
  2375  	// sizes = []int{
  2376  	// 	0, 1,
  2377  	// 	math.MaxInt8, math.MaxInt8 + 4, math.MaxInt8 - 4,
  2378  	// 	math.MaxInt16, math.MaxInt16 + 4, math.MaxInt16 - 4,
  2379  	// 	math.MaxInt32, math.MaxInt32 - 4,
  2380  	// 	// math.MaxInt32 + 4, // bombs on 32-bit
  2381  	// 	// math.MaxInt64, math.MaxInt64 - 4, // bombs on 32-bit
  2382  
  2383  	// 	math.MaxUint8, math.MaxUint8 + 4, math.MaxUint8 - 4,
  2384  	// 	math.MaxUint16, math.MaxUint16 + 4, math.MaxUint16 - 4,
  2385  	// 	// math.MaxUint32, math.MaxUint32 + 4, math.MaxUint32 - 4, // bombs on 32-bit
  2386  	// }
  2387  	sizes = []int{
  2388  		// ensure in ascending order (as creating mm below requires it)
  2389  		0,
  2390  		1,
  2391  		math.MaxInt8 + 4,
  2392  		math.MaxUint8 + 4,
  2393  	}
  2394  	if !testing.Short() {
  2395  		sizes = append(sizes, math.MaxUint16+4) // math.MaxInt16+4, math.MaxUint16+4
  2396  	}
  2397  
  2398  	m := make(map[int][]struct{})
  2399  	for _, i := range sizes {
  2400  		m[i] = make([]struct{}, i)
  2401  	}
  2402  
  2403  	bs := testMarshalErr(m, h, t, "-slices")
  2404  	var m2 = make(map[int][]struct{})
  2405  	testUnmarshalErr(m2, bs, h, t, "-slices")
  2406  	testDeepEqualErr(m, m2, t, "-slices")
  2407  
  2408  	d, x := testSharedCodecDecoder(bs, h, bh)
  2409  	bs2 := d.d.nextValueBytes([]byte{})
  2410  	testSharedCodecDecoderAfter(d, x, bh)
  2411  	testDeepEqualErr(bs, bs2, t, "nextvaluebytes-slices")
  2412  	// if len(bs2) != 0 || len(bs2) != len(bs) { }
  2413  	testReleaseBytes(bs)
  2414  
  2415  	// requires sizes to be in ascending order
  2416  	mm := make(map[int]struct{})
  2417  	for _, i := range sizes {
  2418  		for j := len(mm); j < i; j++ {
  2419  			mm[j] = struct{}{}
  2420  		}
  2421  		bs = testMarshalErr(mm, h, t, "-map")
  2422  		var mm2 = make(map[int]struct{})
  2423  		testUnmarshalErr(mm2, bs, h, t, "-map")
  2424  		testDeepEqualErr(mm, mm2, t, "-map")
  2425  
  2426  		d, x = testSharedCodecDecoder(bs, h, bh)
  2427  		bs2 = d.d.nextValueBytes([]byte{})
  2428  		testSharedCodecDecoderAfter(d, x, bh)
  2429  		testDeepEqualErr(bs, bs2, t, "nextvaluebytes-map")
  2430  		testReleaseBytes(bs)
  2431  	}
  2432  
  2433  	// do same tests for large strings (encoded as symbols or not)
  2434  	// skip if 32-bit or not using unsafe mode
  2435  	if safeMode || (32<<(^uint(0)>>63)) < 64 {
  2436  		return
  2437  	}
  2438  
  2439  	// now, want to do tests for large strings, which
  2440  	// could be encoded as symbols.
  2441  	// to do this, we create a simple one-field struct,
  2442  	// use use flags to switch from symbols to non-symbols
  2443  
  2444  	hbinc, okbinc := h.(*BincHandle)
  2445  	if okbinc {
  2446  		oldAsSymbols := hbinc.AsSymbols
  2447  		defer func() { hbinc.AsSymbols = oldAsSymbols }()
  2448  	}
  2449  	inOutLen := math.MaxUint16 * 3 / 2
  2450  	if testing.Short() {
  2451  		inOutLen = math.MaxUint8 * 2 // math.MaxUint16 / 16
  2452  	}
  2453  	var out = make([]byte, 0, inOutLen)
  2454  	var in []byte = make([]byte, inOutLen)
  2455  	for i := range in {
  2456  		in[i] = 'A'
  2457  	}
  2458  
  2459  	e := NewEncoder(nil, h)
  2460  
  2461  	sizes = []int{
  2462  		0, 1, 4, 8, 12, 16, 28, 32, 36,
  2463  		math.MaxInt8 - 4, math.MaxInt8, math.MaxInt8 + 4,
  2464  		math.MaxUint8, math.MaxUint8 + 4, math.MaxUint8 - 4,
  2465  	}
  2466  	if !testing.Short() {
  2467  		sizes = append(sizes,
  2468  			math.MaxInt16-4, math.MaxInt16, math.MaxInt16+4,
  2469  			math.MaxUint16, math.MaxUint16+4, math.MaxUint16-4)
  2470  	}
  2471  
  2472  	for _, i := range sizes {
  2473  		var m1, m2 map[string]bool
  2474  		m1 = make(map[string]bool, 1)
  2475  		// var s1 = stringView(in[:i])
  2476  		var s1 = string(in[:i])
  2477  		// fmt.Printf("testcontainerlen: large string: i: %v, |%s|\n", i, s1)
  2478  		m1[s1] = true
  2479  
  2480  		if okbinc {
  2481  			hbinc.AsSymbols = 2
  2482  		}
  2483  		out = out[:0]
  2484  		e.ResetBytes(&out)
  2485  		e.MustEncode(m1)
  2486  		// bs, _ = testMarshalErr(m1, h, t, "-")
  2487  		m2 = make(map[string]bool, 1)
  2488  		testUnmarshalErr(m2, out, h, t, "no-symbols-string")
  2489  		testDeepEqualErr(m1, m2, t, "no-symbols-string")
  2490  
  2491  		d, x = testSharedCodecDecoder(out, h, bh)
  2492  		bs2 = d.d.nextValueBytes([]byte{})
  2493  		testSharedCodecDecoderAfter(d, x, bh)
  2494  		testDeepEqualErr(out, bs2, t, "nextvaluebytes-no-symbols-string")
  2495  
  2496  		if okbinc {
  2497  			// now, do as symbols
  2498  			hbinc.AsSymbols = 1
  2499  			out = out[:0]
  2500  			e.ResetBytes(&out)
  2501  			e.MustEncode(m1)
  2502  			// bs, _ = testMarshalErr(m1, h, t, "-")
  2503  			m2 = make(map[string]bool, 1)
  2504  			testUnmarshalErr(m2, out, h, t, "symbols-string")
  2505  			testDeepEqualErr(m1, m2, t, "symbols-string")
  2506  
  2507  			d, x = testSharedCodecDecoder(out, h, bh)
  2508  			bs2 = d.d.nextValueBytes([]byte{})
  2509  			testSharedCodecDecoderAfter(d, x, bh)
  2510  			testDeepEqualErr(out, bs2, t, "nextvaluebytes-symbols-string")
  2511  			hbinc.AsSymbols = 2
  2512  		}
  2513  	}
  2514  
  2515  	// test out extensions with large output
  2516  	var xl, xl2 testUintToBytes
  2517  	xl = testUintToBytes(inOutLen)
  2518  	bs = testMarshalErr(xl, h, t, "-large-extension-bytes")
  2519  	testUnmarshalErr(&xl2, bs, h, t, "-large-extension-bytes")
  2520  	testDeepEqualErr(xl, xl2, t, "-large-extension-bytes")
  2521  
  2522  	d, x = testSharedCodecDecoder(bs, h, bh)
  2523  	bs2 = d.d.nextValueBytes([]byte{})
  2524  	testSharedCodecDecoderAfter(d, x, bh)
  2525  	testDeepEqualErr(bs, bs2, t, "nextvaluebytes-large-extension-bytes")
  2526  
  2527  	xl = testUintToBytes(0) // so it's WriteExt returns nil
  2528  	bs = testMarshalErr(xl, h, t, "-large-extension-bytes")
  2529  	testUnmarshalErr(&xl2, bs, h, t, "-large-extension-bytes")
  2530  	testDeepEqualErr(xl, xl2, t, "-large-extension-bytes")
  2531  }
  2532  
  2533  func testRandomFillRV(v reflect.Value) {
  2534  	fneg := func() int64 {
  2535  		i := rand.Intn(2)
  2536  		if i == 1 {
  2537  			return 1
  2538  		}
  2539  		return -1
  2540  	}
  2541  
  2542  	if v.Type() == timeTyp {
  2543  		v.Set(reflect.ValueOf(timeToCompare1))
  2544  		return
  2545  	}
  2546  
  2547  	switch v.Kind() {
  2548  	case reflect.Invalid:
  2549  	case reflect.Ptr:
  2550  		if v.IsNil() {
  2551  			v.Set(reflect.New(v.Type().Elem()))
  2552  		}
  2553  		testRandomFillRV(v.Elem())
  2554  	case reflect.Interface:
  2555  		if v.IsNil() {
  2556  			v.Set(reflect.ValueOf("nothing"))
  2557  		} else {
  2558  			testRandomFillRV(v.Elem())
  2559  		}
  2560  	case reflect.Struct:
  2561  		for i, n := 0, v.NumField(); i < n; i++ {
  2562  			testRandomFillRV(v.Field(i))
  2563  		}
  2564  	case reflect.Slice:
  2565  		if v.IsNil() {
  2566  			v.Set(reflect.MakeSlice(v.Type(), 4, 4))
  2567  		}
  2568  		fallthrough
  2569  	case reflect.Array:
  2570  		for i, n := 0, v.Len(); i < n; i++ {
  2571  			testRandomFillRV(v.Index(i))
  2572  		}
  2573  	case reflect.Map:
  2574  		if v.IsNil() {
  2575  			v.Set(reflect.MakeMap(v.Type()))
  2576  		}
  2577  		if v.Len() == 0 {
  2578  			kt, vt := v.Type().Key(), v.Type().Elem()
  2579  			for i := 0; i < 4; i++ {
  2580  				k0 := reflect.New(kt).Elem()
  2581  				v0 := reflect.New(vt).Elem()
  2582  				testRandomFillRV(k0)
  2583  				testRandomFillRV(v0)
  2584  				v.SetMapIndex(k0, v0)
  2585  			}
  2586  		} else {
  2587  			for _, k := range v.MapKeys() {
  2588  				testRandomFillRV(v.MapIndex(k))
  2589  			}
  2590  		}
  2591  	case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  2592  		v.SetInt(fneg() * rand.Int63n(127))
  2593  	case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  2594  		v.SetUint(uint64(rand.Int63n(255)))
  2595  	case reflect.Bool:
  2596  		v.SetBool(fneg() == 1)
  2597  	case reflect.Float32, reflect.Float64:
  2598  		v.SetFloat(float64(fneg()) * float64(rand.Float32()))
  2599  	case reflect.Complex64, reflect.Complex128:
  2600  		v.SetComplex(complex(float64(fneg())*float64(rand.Float32()), 0))
  2601  	case reflect.String:
  2602  		// ensure this string can test the extent of json string decoding
  2603  		v.SetString(strings.Repeat(strconv.FormatInt(rand.Int63n(99), 10), rand.Intn(8)) +
  2604  			"- ABC \x41=\x42 \u2318 - \r \b \f - \u2028 and \u2029 .")
  2605  	default:
  2606  		panic(fmt.Errorf("testRandomFillRV: unsupported type: %v", v.Kind()))
  2607  	}
  2608  }
  2609  
  2610  func doTestTime(t *testing.T, h Handle) {
  2611  	defer testSetup(t, &h)()
  2612  	name := h.Name()
  2613  	// test time which uses the time.go implementation (ie Binc)
  2614  	var tt, tt2 time.Time
  2615  	// time in 1990
  2616  	tt = time.Unix(20*366*24*60*60, 1000*900).In(time.FixedZone("UGO", -5*60*60))
  2617  	// fmt.Printf("time tt: %v\n", tt)
  2618  	b := testMarshalErr(tt, h, t, "time-"+name)
  2619  	testUnmarshalErr(&tt2, b, h, t, "time-"+name)
  2620  	// per go documentation, test time with .Equal not ==
  2621  	if !tt2.Equal(tt) {
  2622  		t.Logf("%s: values not equal: 1: %v, 2: %v", name, tt2, tt)
  2623  		t.FailNow()
  2624  	}
  2625  	// testDeepEqualErr(tt.UTC(), tt2, t, "time-"+name)
  2626  	testReleaseBytes(b)
  2627  }
  2628  
  2629  func doTestUintToInt(t *testing.T, h Handle) {
  2630  	defer testSetup(t, &h)()
  2631  	name := h.Name()
  2632  	var golden = [...]int64{
  2633  		0, 1, 22, 333, 4444, 55555, 666666,
  2634  		// msgpack ones
  2635  		24, 128,
  2636  		// standard ones
  2637  		math.MaxUint8, math.MaxUint8 + 4, math.MaxUint8 - 4,
  2638  		math.MaxUint16, math.MaxUint16 + 4, math.MaxUint16 - 4,
  2639  		math.MaxUint32, math.MaxUint32 + 4, math.MaxUint32 - 4,
  2640  		math.MaxInt8, math.MaxInt8 + 4, math.MaxInt8 - 4,
  2641  		math.MaxInt16, math.MaxInt16 + 4, math.MaxInt16 - 4,
  2642  		math.MaxInt32, math.MaxInt32 + 4, math.MaxInt32 - 4,
  2643  		math.MaxInt64, math.MaxInt64 - 4,
  2644  	}
  2645  	var i int64
  2646  	var ui, ui2 uint64
  2647  	var fi float64
  2648  	var b []byte
  2649  	for _, v := range golden {
  2650  		i = v
  2651  		ui = 0
  2652  		b = testMarshalErr(i, h, t, "int2uint-"+name)
  2653  		testUnmarshalErr(&ui, b, h, t, "int2uint-"+name)
  2654  		if ui != uint64(i) {
  2655  			t.Logf("%s: values not equal: %v, %v", name, ui, uint64(i))
  2656  			t.FailNow()
  2657  		}
  2658  		testReleaseBytes(b)
  2659  
  2660  		ui = uint64(i)
  2661  		i = 0
  2662  		b = testMarshalErr(ui, h, t, "uint2int-"+name)
  2663  		testUnmarshalErr(&i, b, h, t, "uint2int-"+name)
  2664  		if i != int64(ui) {
  2665  			t.Logf("%s: values not equal: %v, %v", name, i, int64(ui))
  2666  			t.FailNow()
  2667  		}
  2668  		testReleaseBytes(b)
  2669  
  2670  		if v == math.MaxInt64 {
  2671  			ui = uint64(-(v - 1))
  2672  		} else {
  2673  			ui = uint64(-v)
  2674  		}
  2675  
  2676  		b = testMarshalErr(ui, h, t, "negint2uint-"+name)
  2677  		testUnmarshalErr(&ui2, b, h, t, "negint2uint-"+name)
  2678  		if ui2 != ui {
  2679  			t.Logf("%s: values not equal: %v, %v", name, ui2, ui)
  2680  			t.FailNow()
  2681  		}
  2682  		testReleaseBytes(b)
  2683  
  2684  		fi = 0
  2685  		b = testMarshalErr(i, h, t, "int2float-"+name)
  2686  		testUnmarshalErr(&fi, b, h, t, "int2float-"+name)
  2687  		testReleaseBytes(b)
  2688  		if fi != float64(i) {
  2689  			t.Logf("%s: values not equal: %v, %v", name, fi, float64(i))
  2690  			t.FailNow()
  2691  		}
  2692  	}
  2693  }
  2694  
  2695  func doTestDifferentMapOrSliceType(t *testing.T, h Handle) {
  2696  	if !testRecoverPanicToErr {
  2697  		t.Skip(testSkipIfNotRecoverPanicToErrMsg)
  2698  	}
  2699  	defer testSetup(t, &h)()
  2700  
  2701  	if mh, ok := h.(*MsgpackHandle); ok {
  2702  		defer func(b bool) { mh.RawToString = b }(mh.RawToString)
  2703  		mh.RawToString = true
  2704  	}
  2705  
  2706  	name := h.Name()
  2707  
  2708  	// - maptype, slicetype: diff from map[string]intf, map[intf]intf or []intf, etc
  2709  	//   include map[interface{}]string where some keys are []byte.
  2710  	//   To test, take a sequence of []byte and string, and decode into []string and []interface.
  2711  	//   Also, decode into map[string]string, map[string]interface{}, map[interface{}]string
  2712  
  2713  	bh := testBasicHandle(h)
  2714  	defer func(oldM, oldS reflect.Type) { bh.MapType, bh.SliceType = oldM, oldS }(bh.MapType, bh.SliceType)
  2715  
  2716  	// test for arrays first
  2717  	fnArr := func() {
  2718  		defer func(b1, b2 bool) { bh.StringToRaw, _ = b1, b2 }(bh.StringToRaw, false)
  2719  		bh.StringToRaw = false
  2720  		vi := []interface{}{
  2721  			"hello 1",
  2722  			float64(111.0),
  2723  			"hello 3",
  2724  			float64(333.0),
  2725  			"hello 5",
  2726  		}
  2727  
  2728  		var mi, mi2 map[string]float64
  2729  		mi = make(map[string]float64)
  2730  
  2731  		mi[vi[0].(string)] = vi[1].(float64)
  2732  		mi[vi[2].(string)] = vi[3].(float64)
  2733  
  2734  		var v4a, v4a2 testMbsArr4T
  2735  		copy(v4a[:], vi)
  2736  
  2737  		b := testMarshalErr(v4a, h, t, "-")
  2738  		testUnmarshalErr(&mi2, b, h, t, "-")
  2739  		testDeepEqualErr(mi2, mi, t, "-")
  2740  		testUnmarshalErr(&v4a2, b, h, t, "-")
  2741  		testDeepEqualErr(v4a2, v4a, t, "-")
  2742  		testReleaseBytes(b)
  2743  
  2744  		var v0a, v0a2 testMbsArr0T
  2745  		copy(v0a[:], vi)
  2746  
  2747  		mi2 = nil
  2748  		b = testMarshalErr(v0a, h, t, "-")
  2749  		testUnmarshalErr(&mi2, b, h, t, "-")
  2750  		testDeepEqualErr(mi2, map[string]float64{}, t, "-")
  2751  		testUnmarshalErr(&v0a2, b, h, t, "-")
  2752  		testDeepEqualErr(v0a2, v0a, t, "-")
  2753  		testReleaseBytes(b)
  2754  
  2755  		{
  2756  			var v5a testMbsArr5T
  2757  			copy(v5a[:], vi)
  2758  			b, err := testMarshal(v5a, h)
  2759  			testReleaseBytes(b)
  2760  			if err == nil || !strings.Contains(err.Error(), "mapBySlice requires even slice length") {
  2761  				t.Logf("mapBySlice for odd length array fail: expected mapBySlice error, got: %v", err)
  2762  				t.FailNow()
  2763  			}
  2764  		}
  2765  	}
  2766  	fnArr()
  2767  
  2768  	var b []byte
  2769  
  2770  	var vi = []interface{}{
  2771  		"hello 1",
  2772  		[]byte("hello 2"),
  2773  		"hello 3",
  2774  		[]byte("hello 4"),
  2775  		"hello 5",
  2776  	}
  2777  
  2778  	var vs []string
  2779  	var v2i, v2s testMbsT
  2780  	var v2ss testMbsCustStrT
  2781  
  2782  	// encode it as a map or as a slice
  2783  	for i, v := range vi {
  2784  		vv, ok := v.(string)
  2785  		if !ok {
  2786  			vv = string(v.([]byte))
  2787  		}
  2788  		vs = append(vs, vv)
  2789  		v2i = append(v2i, v, strconv.FormatInt(int64(i+1), 10))
  2790  		v2s = append(v2s, vv, strconv.FormatInt(int64(i+1), 10))
  2791  		v2ss = append(v2ss, testCustomStringT(vv), testCustomStringT(strconv.FormatInt(int64(i+1), 10)))
  2792  	}
  2793  
  2794  	var v2d interface{}
  2795  
  2796  	// encode vs as a list, and decode into a list and compare
  2797  	var goldSliceS = []string{"hello 1", "hello 2", "hello 3", "hello 4", "hello 5"}
  2798  	var goldSliceI = []interface{}{"hello 1", "hello 2", "hello 3", "hello 4", "hello 5"}
  2799  	var goldSlice = []interface{}{goldSliceS, goldSliceI}
  2800  	for j, g := range goldSlice {
  2801  		bh.SliceType = reflect.TypeOf(g)
  2802  		name := fmt.Sprintf("slice-%s-%v", name, j+1)
  2803  		b = testMarshalErr(vs, h, t, name)
  2804  		v2d = nil
  2805  		// v2d = reflect.New(bh.SliceType).Elem().Interface()
  2806  		testUnmarshalErr(&v2d, b, h, t, name)
  2807  		testDeepEqualErr(v2d, goldSlice[j], t, name)
  2808  		testReleaseBytes(b)
  2809  	}
  2810  
  2811  	// to ensure that we do not use fast-path for map[intf]string, use a custom string type (for goldMapIS).
  2812  	// this will allow us to test out the path that sees a []byte where a map has an interface{} type,
  2813  	// and convert it to a string for the decoded map key.
  2814  
  2815  	// encode v2i as a map, and decode into a map and compare
  2816  	var goldMapSS = map[string]string{"hello 1": "1", "hello 2": "2", "hello 3": "3", "hello 4": "4", "hello 5": "5"}
  2817  	var goldMapSI = map[string]interface{}{"hello 1": "1", "hello 2": "2", "hello 3": "3", "hello 4": "4", "hello 5": "5"}
  2818  	var goldMapIS = map[interface{}]testCustomStringT{"hello 1": "1", "hello 2": "2", "hello 3": "3", "hello 4": "4", "hello 5": "5"}
  2819  	var goldMap = []interface{}{goldMapSS, goldMapSI, goldMapIS}
  2820  	for j, g := range goldMap {
  2821  		bh.MapType = reflect.TypeOf(g)
  2822  		name := fmt.Sprintf("map-%s-%v", name, j+1)
  2823  		// for formats that clearly differentiate binary from string, use v2i
  2824  		// else use the v2s (with all strings, no []byte)
  2825  		v2d = nil
  2826  		// v2d = reflect.New(bh.MapType).Elem().Interface()
  2827  		switch h.(type) {
  2828  		case *MsgpackHandle, *BincHandle, *CborHandle:
  2829  			b = testMarshalErr(v2i, h, t, name)
  2830  			testUnmarshalErr(&v2d, b, h, t, name)
  2831  			testDeepEqualErr(v2d, goldMap[j], t, name)
  2832  			testReleaseBytes(b)
  2833  		default:
  2834  			b = testMarshalErr(v2s, h, t, name)
  2835  			testUnmarshalErr(&v2d, b, h, t, name)
  2836  			testDeepEqualErr(v2d, goldMap[j], t, name)
  2837  			testReleaseBytes(b)
  2838  			b = testMarshalErr(v2ss, h, t, name)
  2839  			v2d = nil
  2840  			testUnmarshalErr(&v2d, b, h, t, name)
  2841  			testDeepEqualErr(v2d, goldMap[j], t, name)
  2842  			testReleaseBytes(b)
  2843  		}
  2844  	}
  2845  
  2846  	// encode []byte and decode into one with len < cap.
  2847  	// get the slices > decDefSliceCap*2, so things like json can go much higher
  2848  	{
  2849  		var bs1 = []byte("abcdefghijklmnopqrstuvwxyz")
  2850  		b := testMarshalErr(bs1, h, t, "enc-bytes")
  2851  
  2852  		var bs2 = make([]byte, 32, 32)
  2853  		testUnmarshalErr(bs2, b, h, t, "dec-bytes")
  2854  		testDeepEqualErr(bs1, bs2[:len(bs1)], t, "cmp-enc-dec-bytes")
  2855  
  2856  		testUnmarshalErr(&bs2, b, h, t, "dec-bytes-2")
  2857  		testDeepEqualErr(bs1, bs2, t, "cmp-enc-dec-bytes-2")
  2858  
  2859  		bs2 = bs2[:2:4]
  2860  		testUnmarshalErr(&bs2, b, h, t, "dec-bytes-2")
  2861  		testDeepEqualErr(bs1, bs2, t, "cmp-enc-dec-bytes-2")
  2862  
  2863  		bs2 = nil
  2864  		testUnmarshalErr(&bs2, b, h, t, "dec-bytes-2")
  2865  		testDeepEqualErr(bs1, bs2, t, "cmp-enc-dec-bytes-2")
  2866  
  2867  		bs1 = []byte{}
  2868  		b = testMarshalErr(bs1, h, t, "enc-bytes")
  2869  
  2870  		bs2 = nil
  2871  		testUnmarshalErr(&bs2, b, h, t, "dec-bytes-2")
  2872  		testDeepEqualErr(bs1, bs2, t, "cmp-enc-dec-bytes-2")
  2873  
  2874  		type Ti32 int32
  2875  		v1 := []Ti32{
  2876  			9, 99, 999, 9999, 99999, 999999,
  2877  			9, 99, 999, 9999, 99999, 999999,
  2878  			9, 99, 999, 9999, 99999, 999999,
  2879  		}
  2880  		b = testMarshalErr(v1, h, t, "enc-Ti32")
  2881  
  2882  		v2 := make([]Ti32, 20, 20)
  2883  		testUnmarshalErr(v2, b, h, t, "dec-Ti32")
  2884  		testDeepEqualErr(v1, v2[:len(v1)], t, "cmp-enc-dec-Ti32")
  2885  
  2886  		testUnmarshalErr(&v2, b, h, t, "dec-Ti32-2")
  2887  		testDeepEqualErr(v1, v2, t, "cmp-enc-dec-Ti32-2")
  2888  
  2889  		v2 = v2[:1:3]
  2890  		testUnmarshalErr(&v2, b, h, t, "dec-Ti32-2")
  2891  		testDeepEqualErr(v1, v2, t, "cmp-enc-dec-Ti32-2")
  2892  
  2893  		v2 = nil
  2894  		testUnmarshalErr(&v2, b, h, t, "dec-Ti32-2")
  2895  		testDeepEqualErr(v1, v2, t, "cmp-enc-dec-Ti32-2")
  2896  
  2897  		v1 = []Ti32{}
  2898  		b = testMarshalErr(v1, h, t, "enc-Ti32")
  2899  
  2900  		v2 = nil
  2901  		testUnmarshalErr(&v2, b, h, t, "dec-Ti32-2")
  2902  		testDeepEqualErr(v1, v2, t, "cmp-enc-dec-Ti32-2")
  2903  	}
  2904  }
  2905  
  2906  func doTestScalars(t *testing.T, h Handle) {
  2907  	defer testSetup(t, &h)()
  2908  
  2909  	if mh, ok := h.(*MsgpackHandle); ok {
  2910  		defer func(b bool) { mh.RawToString = b }(mh.RawToString)
  2911  		mh.RawToString = true
  2912  	}
  2913  
  2914  	// for each scalar:
  2915  	// - encode its ptr
  2916  	// - encode it (non-ptr)
  2917  	// - check that bytes are same
  2918  	// - make a copy (using reflect)
  2919  	// - check that same
  2920  	// - set zero on it
  2921  	// - check that its equal to 0 value
  2922  	// - decode into new
  2923  	// - compare to original
  2924  
  2925  	bh := testBasicHandle(h)
  2926  	if !bh.Canonical {
  2927  		bh.Canonical = true
  2928  		defer func() { bh.Canonical = false }()
  2929  	}
  2930  
  2931  	var bzero = testMarshalErr(nil, h, t, "nil-enc")
  2932  
  2933  	vi := []interface{}{
  2934  		int(0),
  2935  		int8(0),
  2936  		int16(0),
  2937  		int32(0),
  2938  		int64(0),
  2939  		uint(0),
  2940  		uint8(0),
  2941  		uint16(0),
  2942  		uint32(0),
  2943  		uint64(0),
  2944  		uintptr(0),
  2945  		float32(0),
  2946  		float64(0),
  2947  		bool(false),
  2948  		time.Time{},
  2949  		string(""),
  2950  		[]byte(nil),
  2951  	}
  2952  	for _, v := range fastpathAv {
  2953  		vi = append(vi, reflect.Zero(v.rt).Interface())
  2954  	}
  2955  	for _, v := range vi {
  2956  		rv := reflect.New(reflect.TypeOf(v)).Elem()
  2957  		testRandomFillRV(rv)
  2958  		v = rv.Interface()
  2959  
  2960  		rv2 := reflect.New(rv.Type())
  2961  		rv2.Elem().Set(rv)
  2962  		vp := rv2.Interface()
  2963  
  2964  		var tname string
  2965  		switch rv.Kind() {
  2966  		case reflect.Map:
  2967  			tname = "map[" + rv.Type().Key().Name() + "]" + rv.Type().Elem().Name()
  2968  		case reflect.Slice:
  2969  			tname = "[]" + rv.Type().Elem().Name()
  2970  		default:
  2971  			tname = rv.Type().Name()
  2972  		}
  2973  
  2974  		var b, b1, b2 []byte
  2975  		b1 = testMarshalErr(v, h, t, tname+"-enc")
  2976  		// store b1 into b, as b1 slice is reused for next marshal
  2977  		b = make([]byte, len(b1))
  2978  		copy(b, b1)
  2979  		b2 = testMarshalErr(vp, h, t, tname+"-enc-ptr")
  2980  		testDeepEqualErr(b1, b2, t, tname+"-enc-eq")
  2981  
  2982  		// decode the nil value into rv2, and test that it is the zero value
  2983  		setZero(vp)
  2984  		testDeepEqualErr(rv2.Elem().Interface(), reflect.Zero(rv.Type()).Interface(), t, tname+"-enc-eq-zero-ref")
  2985  
  2986  		testUnmarshalErr(vp, b, h, t, tname+"-dec")
  2987  		testDeepEqualErr(rv2.Elem().Interface(), v, t, tname+"-dec-eq")
  2988  
  2989  		// test that we can decode an encoded nil into it
  2990  		testUnmarshalErr(vp, bzero, h, t, tname+"-dec-from-enc-nil")
  2991  		testDeepEqualErr(rv2.Elem().Interface(), reflect.Zero(rv.Type()).Interface(), t, tname+"-dec-from-enc-nil")
  2992  		testReleaseBytes(b1)
  2993  		testReleaseBytes(b2)
  2994  	}
  2995  
  2996  	// test setZero for *Raw and reflect.Value
  2997  	var r0 Raw
  2998  	var r = Raw([]byte("hello"))
  2999  	setZero(&r)
  3000  	testDeepEqualErr(r, r0, t, "raw-zeroed")
  3001  
  3002  	r = Raw([]byte("hello"))
  3003  	var rv = reflect.ValueOf(&r)
  3004  	setZero(rv)
  3005  	// note: we cannot test reflect.Value's because they might point to different pointers
  3006  	// and reflect.DeepEqual doesn't honor that.
  3007  	// testDeepEqualErr(rv, reflect.ValueOf(&r0), t, "raw-reflect-zeroed")
  3008  	testDeepEqualErr(rv.Interface(), &r0, t, "raw-reflect-zeroed")
  3009  	testReleaseBytes(bzero)
  3010  }
  3011  
  3012  func doTestIntfMapping(t *testing.T, h Handle) {
  3013  	defer testSetup(t, &h)()
  3014  	name := h.Name()
  3015  	rti := reflect.TypeOf((*testIntfMapI)(nil)).Elem()
  3016  	defer func() { testBasicHandle(h).Intf2Impl(rti, nil) }()
  3017  
  3018  	type T9 struct {
  3019  		I testIntfMapI
  3020  	}
  3021  
  3022  	for i, v := range []testIntfMapI{
  3023  		// Use a valid string to test some extents of json string decoding
  3024  		&testIntfMapT1{"ABC \x41=\x42 \u2318 - \r \b \f - \u2028 and \u2029 ."},
  3025  		testIntfMapT2{"DEF"},
  3026  	} {
  3027  		if err := testBasicHandle(h).Intf2Impl(rti, reflect.TypeOf(v)); err != nil {
  3028  			t.Logf("Error mapping %v to %T", rti, v)
  3029  			t.FailNow()
  3030  		}
  3031  		var v1, v2 T9
  3032  		v1 = T9{v}
  3033  		b := testMarshalErr(v1, h, t, name+"-enc-"+strconv.Itoa(i))
  3034  		testUnmarshalErr(&v2, b, h, t, name+"-dec-"+strconv.Itoa(i))
  3035  		testDeepEqualErr(v1, v2, t, name+"-dec-eq-"+strconv.Itoa(i))
  3036  		testReleaseBytes(b)
  3037  	}
  3038  }
  3039  
  3040  func doTestOmitempty(t *testing.T, h Handle) {
  3041  	defer testSetup(t, &h)()
  3042  	name := h.Name()
  3043  	if testBasicHandle(h).StructToArray {
  3044  		t.Skipf("skipping OmitEmpty test when StructToArray=true")
  3045  	}
  3046  	type T1 struct {
  3047  		A int  `codec:"a"`
  3048  		B *int `codec:"b,omitempty"`
  3049  		C int  `codec:"c,omitempty"`
  3050  	}
  3051  	type T2 struct {
  3052  		A int `codec:"a"`
  3053  	}
  3054  	var v1 T1
  3055  	var v2 T2
  3056  	b1 := testMarshalErr(v1, h, t, name+"-omitempty")
  3057  	b2 := testMarshalErr(v2, h, t, name+"-no-omitempty-trunc")
  3058  	testDeepEqualErr(b1, b2, t, name+"-omitempty-cmp")
  3059  	testReleaseBytes(b1)
  3060  	testReleaseBytes(b2)
  3061  }
  3062  
  3063  func doTestMissingFields(t *testing.T, h Handle) {
  3064  	defer testSetup(t, &h)()
  3065  	name := h.Name()
  3066  	if codecgen {
  3067  		t.Skipf("skipping Missing Fields tests as it is not honored by codecgen")
  3068  	}
  3069  	if testBasicHandle(h).StructToArray {
  3070  		t.Skipf("skipping Missing Fields test when StructToArray=true")
  3071  	}
  3072  	// encode missingFielderT2, decode into missingFielderT1, encode it out again, decode into new missingFielderT2, compare
  3073  	v1 := missingFielderT2{S: "true seven eight", B: true, F: 777.0, I: -888}
  3074  	b1 := testMarshalErr(v1, h, t, name+"-missing-enc-2")
  3075  
  3076  	var v2 missingFielderT1
  3077  	testUnmarshalErr(&v2, b1, h, t, name+"-missing-dec-1")
  3078  
  3079  	b2 := testMarshalErr(&v2, h, t, name+"-missing-enc-1")
  3080  
  3081  	var v3 missingFielderT2
  3082  	testUnmarshalErr(&v3, b2, h, t, name+"-missing-dec-2")
  3083  
  3084  	testDeepEqualErr(v1, v3, t, name+"-missing-cmp-2")
  3085  
  3086  	testReleaseBytes(b1)
  3087  	testReleaseBytes(b2)
  3088  
  3089  	v4 := missingFielderT11{s1: "s111", S2: "S222"}
  3090  	b1 = testMarshalErr(v4, h, t, name+"-missing-enc-11")
  3091  	var m4 map[string]string
  3092  	testUnmarshalErr(&m4, b1, h, t, name+"-missing-dec-11")
  3093  	testDeepEqualErr(m4, map[string]string{"s1": "s111", "S2": "S222"}, t, name+"-missing-cmp-11")
  3094  
  3095  	testReleaseBytes(b1)
  3096  
  3097  	// test canonical interaction - with structs having some missing fields and some regular fields
  3098  	bh := testBasicHandle(h)
  3099  
  3100  	defer func(c bool) {
  3101  		bh.Canonical = c
  3102  	}(bh.Canonical)
  3103  	bh.Canonical = true
  3104  
  3105  	b1 = nil
  3106  
  3107  	var s1 = struct {
  3108  		A int
  3109  		B int
  3110  		C int
  3111  	}{1, 2, 3}
  3112  
  3113  	NewEncoderBytes(&b1, h).MustEncode(&s1)
  3114  
  3115  	var s2 = struct {
  3116  		C int
  3117  		testMissingFieldsMap
  3118  	}{C: 3}
  3119  	s2.testMissingFieldsMap = testMissingFieldsMap{
  3120  		m: map[string]interface{}{
  3121  			"A": 1,
  3122  			"B": 2,
  3123  		},
  3124  	}
  3125  
  3126  	for i := 0; i < 16; i++ {
  3127  		b2 = nil
  3128  		NewEncoderBytes(&b2, h).MustEncode(&s2)
  3129  
  3130  		if !bytes.Equal(b1, b2) {
  3131  			t.Fatalf("bytes differed:'%s' vs '%s'", b1, b2)
  3132  		}
  3133  	}
  3134  }
  3135  
  3136  func doTestMaxDepth(t *testing.T, h Handle) {
  3137  	if !testRecoverPanicToErr {
  3138  		t.Skip(testSkipIfNotRecoverPanicToErrMsg)
  3139  	}
  3140  	defer testSetup(t, &h)()
  3141  	name := h.Name()
  3142  	type T struct {
  3143  		I interface{} // value to encode
  3144  		M int16       // maxdepth
  3145  		S bool        // use swallow (decode into typed struct with only A1)
  3146  		E interface{} // error to find
  3147  	}
  3148  	type T1 struct {
  3149  		A1 *T1
  3150  	}
  3151  	var table []T
  3152  	var sfunc = func(n int) (s [1]interface{}, s1 *[1]interface{}) {
  3153  		s1 = &s
  3154  		for i := 0; i < n; i++ {
  3155  			var s0 [1]interface{}
  3156  			s1[0] = &s0
  3157  			s1 = &s0
  3158  		}
  3159  
  3160  		return
  3161  		// var s []interface{}
  3162  		// s = append(s, []interface{})
  3163  		// s[0] = append(s[0], []interface{})
  3164  		// s[0][0] = append(s[0][0], []interface{})
  3165  		// s[0][0][0] = append(s[0][0][0], []interface{})
  3166  		// s[0][0][0][0] = append(s[0][0][0][0], []interface{})
  3167  		// return s
  3168  	}
  3169  	var mfunc = func(n int) (m map[string]interface{}, mlast map[string]interface{}) {
  3170  		m = make(map[string]interface{})
  3171  		mlast = make(map[string]interface{})
  3172  		m["A0"] = mlast
  3173  		for i := 1; i < n; i++ {
  3174  			m0 := make(map[string]interface{})
  3175  			mlast["A"+strconv.FormatInt(int64(i), 10)] = m0
  3176  			mlast = m0
  3177  		}
  3178  
  3179  		return
  3180  	}
  3181  	s, s1 := sfunc(5)
  3182  	m, _ := mfunc(5)
  3183  	m99, _ := mfunc(99)
  3184  
  3185  	s1[0] = m
  3186  
  3187  	table = append(table, T{s, 0, false, nil})
  3188  	table = append(table, T{s, 256, false, nil})
  3189  	table = append(table, T{s, 7, false, errMaxDepthExceeded})
  3190  	table = append(table, T{s, 15, false, nil})
  3191  	table = append(table, T{m99, 15, true, errMaxDepthExceeded})
  3192  	table = append(table, T{m99, 215, true, nil})
  3193  
  3194  	defer func(n int16) {
  3195  		testBasicHandle(h).MaxDepth = n
  3196  	}(testBasicHandle(h).MaxDepth)
  3197  
  3198  	for i, v := range table {
  3199  		testBasicHandle(h).MaxDepth = v.M
  3200  		b1 := testMarshalErr(v.I, h, t, name+"-maxdepth-enc"+strconv.FormatInt(int64(i), 10))
  3201  
  3202  		var err error
  3203  		v.S = false // MARKER: 20200925: swallow doesn't track depth anymore
  3204  		if v.S {
  3205  			var v2 T1
  3206  			err = testUnmarshal(&v2, b1, h)
  3207  		} else {
  3208  			var v2 interface{}
  3209  			err = testUnmarshal(&v2, b1, h)
  3210  		}
  3211  		var err0 interface{} = err
  3212  		if err1, ok := err.(*codecError); ok {
  3213  			err0 = err1.err
  3214  		}
  3215  		if err0 != v.E {
  3216  			t.Logf("Unexpected error testing max depth for depth %d: expected %v, received %v", v.M, v.E, err)
  3217  			t.FailNow()
  3218  		}
  3219  
  3220  		// decode into something that just triggers swallow
  3221  		testReleaseBytes(b1)
  3222  	}
  3223  }
  3224  
  3225  func doTestMultipleEncDec(t *testing.T, h Handle) {
  3226  	defer testSetup(t, &h)()
  3227  	name := h.Name()
  3228  	// encode a string multiple times.
  3229  	// decode it multiple times.
  3230  	// ensure we get the value each time
  3231  	var s1 = "ugorji"
  3232  	var s2 = "nwoke"
  3233  	var s11, s21 string
  3234  	var buf bytes.Buffer
  3235  	e := NewEncoder(&buf, h)
  3236  	e.MustEncode(s1)
  3237  	e.MustEncode(s2)
  3238  	d := NewDecoder(&buf, h)
  3239  	d.MustDecode(&s11)
  3240  	d.MustDecode(&s21)
  3241  	testDeepEqualErr(s1, s11, t, name+"-multiple-encode")
  3242  	testDeepEqualErr(s2, s21, t, name+"-multiple-encode")
  3243  }
  3244  
  3245  func doTestSelfExt(t *testing.T, h Handle) {
  3246  	defer testSetup(t, &h)()
  3247  	name := h.Name()
  3248  	var ts TestSelfExtImpl
  3249  	ts.S = "ugorji"
  3250  	ts.I = 5678
  3251  	ts.B = true
  3252  	var ts2 TestSelfExtImpl
  3253  
  3254  	bs := testMarshalErr(&ts, h, t, name)
  3255  	testUnmarshalErr(&ts2, bs, h, t, name)
  3256  	testDeepEqualErr(&ts, &ts2, t, name)
  3257  	testReleaseBytes(bs)
  3258  }
  3259  
  3260  func doTestBytesEncodedAsArray(t *testing.T, h Handle) {
  3261  	defer testSetup(t, &h)()
  3262  	name := h.Name()
  3263  	// Need to test edge case where bytes are encoded as an array
  3264  	// (not using optimized []byte native format)
  3265  
  3266  	// encode []int8 (or int32 or any numeric type) with all positive numbers
  3267  	// decode it into []byte
  3268  	var in = make([]int32, 128)
  3269  	var un = make([]uint8, 128)
  3270  	for i := range in {
  3271  		in[i] = int32(i)
  3272  		un[i] = uint8(i)
  3273  	}
  3274  	var out []byte
  3275  	bs := testMarshalErr(&in, h, t, name)
  3276  	testUnmarshalErr(&out, bs, h, t, name)
  3277  
  3278  	testDeepEqualErr(un, out, t, name)
  3279  	testReleaseBytes(bs)
  3280  }
  3281  
  3282  func doTestStrucEncDec(t *testing.T, h Handle) {
  3283  	defer testSetup(t, &h)()
  3284  	name := h.Name()
  3285  
  3286  	{
  3287  		var ts1 = newTestStruc(2, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly)
  3288  		var ts2 TestStruc
  3289  		bs := testMarshalErr(ts1, h, t, name)
  3290  		testUnmarshalErr(&ts2, bs, h, t, name)
  3291  		testDeepEqualErr(ts1, &ts2, t, name)
  3292  		testReleaseBytes(bs)
  3293  	}
  3294  
  3295  	// Note: We cannot use TestStrucFlex because it has omitempty values,
  3296  	// Meaning that sometimes, encoded and decoded will not be same.
  3297  	// {
  3298  	// 	var ts1 = newTestStrucFlex(2, testNumRepeatString, false, !testSkipIntf, testMapStringKeyOnly)
  3299  	// 	var ts2 TestStrucFlex
  3300  	// 	bs := testMarshalErr(ts1, h, t, name)
  3301  	// 	fmt.Printf("\n\n%s\n\n", bs)
  3302  	// 	testUnmarshalErr(&ts2, bs, h, t, name)
  3303  	// 	testDeepEqualErr(ts1, &ts2, t, name)
  3304  	// }
  3305  }
  3306  
  3307  func doTestStructKeyType(t *testing.T, h Handle) {
  3308  	defer testSetup(t, &h)()
  3309  	name := h.Name()
  3310  
  3311  	mh, mok := h.(*MsgpackHandle)
  3312  
  3313  	bch, bcok := h.(*BincHandle)
  3314  
  3315  	bh := testBasicHandle(h)
  3316  	s2a := bh.StructToArray
  3317  	bh.StructToArray = false
  3318  	ir := bh.InterfaceReset
  3319  	bh.InterfaceReset = false
  3320  	var mfx bool
  3321  	if mok {
  3322  		mfx = mh.NoFixedNum
  3323  		mh.NoFixedNum = false
  3324  	}
  3325  	var bcsym uint8
  3326  	if bcok {
  3327  		bcsym = bch.AsSymbols
  3328  		bch.AsSymbols = 2 // MARKER: should be 0 but binc symbols do not work well
  3329  	}
  3330  	defer func() {
  3331  		bh.StructToArray = s2a
  3332  		bh.InterfaceReset = ir
  3333  		if mok {
  3334  			mh.NoFixedNum = mfx
  3335  		}
  3336  		if bcok {
  3337  			bch.AsSymbols = bcsym
  3338  		}
  3339  	}()
  3340  
  3341  	var bs1, bs2 []byte
  3342  
  3343  	var m = make(map[interface{}]interface{})
  3344  
  3345  	fn := func(v interface{}) {
  3346  		v1 := reflect.New(reflect.TypeOf(v)).Elem().Interface()
  3347  		bs1 = testMarshalErr(v, h, t, "")
  3348  		testUnmarshalErr(&v1, bs1, h, t, "")
  3349  		testDeepEqualErr(v, v1, t, name+"")
  3350  		bs2 = testMarshalErr(m, h, t, "")
  3351  		// if _, ok := h.(*JsonHandle); ok {
  3352  		// 	xdebugf("bs1: %s, bs2: %s", bs1, bs2)
  3353  		// }
  3354  		// if bcok {
  3355  		// 	xdebug2f("-------------")
  3356  		// 	xdebugf("v: %#v, m: %#v", v, m)
  3357  		// 	xdebugf("bs1: %v", bs1)
  3358  		// 	xdebugf("bs2: %v", bs2)
  3359  		// 	xdebugf("bs1==bs2: %v", bytes.Equal(bs1, bs2))
  3360  		// 	testDeepEqualErr(bs1, bs2, t, name+"")
  3361  		// 	xdebug2f("-------------")
  3362  		// 	return
  3363  		// }
  3364  		testDeepEqualErr(bs1, bs2, t, name+"")
  3365  		testReleaseBytes(bs1)
  3366  		testReleaseBytes(bs2)
  3367  	}
  3368  
  3369  	fnclr := func() {
  3370  		for k := range m {
  3371  			delete(m, k)
  3372  		}
  3373  	}
  3374  
  3375  	m["F"] = 90
  3376  	fn(&testStrucKeyTypeT0{F: 90})
  3377  	fnclr()
  3378  
  3379  	m["FFFF"] = 100
  3380  	fn(&testStrucKeyTypeT1{F: 100})
  3381  	fnclr()
  3382  
  3383  	m[int64(-1)] = 200
  3384  	fn(&testStrucKeyTypeT2{F: 200})
  3385  	fnclr()
  3386  
  3387  	m[int64(1)] = 300
  3388  	fn(&testStrucKeyTypeT3{F: 300})
  3389  	fnclr()
  3390  
  3391  	m[float64(2.5)] = 400
  3392  	fn(&testStrucKeyTypeT4{F: 400})
  3393  	fnclr()
  3394  }
  3395  
  3396  func doTestRawToStringToRawEtc(t *testing.T, h Handle) {
  3397  	defer testSetup(t, &h)()
  3398  	// name := h.Name()
  3399  
  3400  	// Tests:
  3401  	// - RawToString
  3402  	// - StringToRaw
  3403  	// - MapValueReset
  3404  	// - DeleteOnMapValue (skipped - no longer supported)
  3405  
  3406  	bh := testBasicHandle(h)
  3407  
  3408  	r2s := bh.RawToString
  3409  	s2r := bh.StringToRaw
  3410  	can := bh.Canonical
  3411  	mvr := bh.MapValueReset
  3412  
  3413  	mh, mok := h.(*MsgpackHandle)
  3414  
  3415  	_, jok := h.(*JsonHandle)
  3416  
  3417  	defer func() {
  3418  		bh.RawToString = r2s
  3419  		bh.StringToRaw = s2r
  3420  		bh.Canonical = can
  3421  		bh.MapValueReset = mvr
  3422  	}()
  3423  
  3424  	bh.Canonical = false
  3425  
  3426  	var bs1, bs2 []byte
  3427  
  3428  	// encode: StringToRaw
  3429  	// decode: RawToString
  3430  
  3431  	// compare encoded v1 to encoded v2, while setting StringToRaw to b
  3432  	fne := func(v1, v2 interface{}, b bool) {
  3433  		bh.StringToRaw = b
  3434  		bs1 = testMarshalErr(v1, h, t, "")
  3435  		// bs1 = []byte(string(bs1))
  3436  		bs2 = testMarshalErr(v2, h, t, "")
  3437  		testDeepEqualErr(bs1, bs2, t, "")
  3438  		testReleaseBytes(bs1)
  3439  		testReleaseBytes(bs2)
  3440  	}
  3441  
  3442  	// encoded v1, decode naked and compare to v2
  3443  	fnd := func(v1, v2 interface{}, bs2r, br2s, bwext bool) {
  3444  		bh.RawToString = br2s
  3445  		bh.StringToRaw = bs2r
  3446  		if mok {
  3447  			mh.RawToString = bwext
  3448  		}
  3449  		bs1 = testMarshalErr(v1, h, t, "")
  3450  		var vn interface{}
  3451  		testUnmarshalErr(&vn, bs1, h, t, "")
  3452  		testDeepEqualErr(vn, v2, t, "")
  3453  		testReleaseBytes(bs1)
  3454  	}
  3455  
  3456  	sv0 := "value"
  3457  	bv0 := []byte(sv0)
  3458  
  3459  	sv1 := sv0
  3460  	bv1 := []byte(sv1)
  3461  
  3462  	m1 := map[string]*string{"key": &sv1}
  3463  	m2 := map[string]*[]byte{"key": &bv1}
  3464  
  3465  	// m3 := map[[3]byte]string{[3]byte{'k', 'e', 'y'}: sv0}
  3466  	m4 := map[[3]byte][]byte{[3]byte{'k', 'e', 'y'}: bv0}
  3467  	m5 := map[string][]byte{"key": bv0}
  3468  	// m6 := map[string]string{"key": sv0}
  3469  
  3470  	m7 := map[interface{}]interface{}{"key": sv0}
  3471  	m8 := map[interface{}]interface{}{"key": bv0}
  3472  
  3473  	// StringToRaw=true
  3474  	fne(m1, m4, true)
  3475  
  3476  	// StringToRaw=false
  3477  	// compare encoded m2 to encoded m5
  3478  	fne(m2, m5, false)
  3479  
  3480  	// json doesn't work well with StringToRaw and RawToString
  3481  	// when dealing with interfaces, because it cannot decipher
  3482  	// that a string should be treated as base64.
  3483  	if jok {
  3484  		goto MAP_VALUE_RESET
  3485  	}
  3486  
  3487  	// if msgpack, always set WriteExt = RawToString
  3488  
  3489  	// StringToRaw=true (RawToString=true)
  3490  	// encoded m1, decode naked and compare to m5
  3491  	fnd(m2, m7, true, true, true)
  3492  
  3493  	// StringToRaw=true (RawToString=false)
  3494  	// encoded m1, decode naked and compare to m6
  3495  	fnd(m1, m8, true, false, false)
  3496  
  3497  	// StringToRaw=false, RawToString=true
  3498  	// encode m1, decode naked, and compare to m6
  3499  	fnd(m2, m7, false, true, true)
  3500  
  3501  MAP_VALUE_RESET:
  3502  	// set MapValueReset, and then decode i
  3503  	sv2 := "value-new"
  3504  	m9 := map[string]*string{"key": &sv2}
  3505  
  3506  	bs1 = testMarshalErr(m1, h, t, "")
  3507  
  3508  	bh.MapValueReset = false
  3509  	testUnmarshalErr(&m9, bs1, h, t, "")
  3510  	// if !(m9["key"] == m1["key"]
  3511  	testDeepEqualErr(sv2, "value", t, "")
  3512  	testDeepEqualErr(&sv2, m9["key"], t, "")
  3513  
  3514  	sv2 = "value-new"
  3515  	m9["key"] = &sv2
  3516  	bh.MapValueReset = true
  3517  	testUnmarshalErr(&m9, bs1, h, t, "")
  3518  	testDeepEqualErr(sv2, "value-new", t, "")
  3519  	testDeepEqualErr("value", *(m9["key"]), t, "")
  3520  
  3521  	// t1 = struct {
  3522  	// 	key string
  3523  	// }{ key: sv0 }
  3524  	// t2 := struct {
  3525  	// 	key []byte
  3526  	// }{ key: bv1 }
  3527  	testReleaseBytes(bs1)
  3528  
  3529  }
  3530  
  3531  // -----------------
  3532  
  3533  func doTestJsonDecodeNonStringScalarInStringContext(t *testing.T, h Handle) {
  3534  	defer testSetup(t, &h)()
  3535  	var b = `{"s.true": "true", "b.true": true, "s.false": "false", "b.false": false, "s.10": "10", "i.10": 10, "i.-10": -10}`
  3536  	var golden = map[string]string{"s.true": "true", "b.true": "true", "s.false": "false", "b.false": "false", "s.10": "10", "i.10": "10", "i.-10": "-10"}
  3537  
  3538  	var m map[string]string
  3539  	d := NewDecoderBytes([]byte(b), h)
  3540  	d.MustDecode(&m)
  3541  	if err := deepEqual(golden, m); err == nil {
  3542  		if testVerbose {
  3543  			t.Logf("++++ match: decoded: %#v", m)
  3544  		}
  3545  	} else {
  3546  		t.Logf("---- mismatch: %v ==> golden: %#v, decoded: %#v", err, golden, m)
  3547  		t.FailNow()
  3548  	}
  3549  
  3550  	jh := h.(*JsonHandle)
  3551  
  3552  	oldMapKeyAsString := jh.MapKeyAsString
  3553  	oldPreferFloat := jh.PreferFloat
  3554  	oldSignedInteger := jh.SignedInteger
  3555  	oldHTMLCharAsIs := jh.HTMLCharsAsIs
  3556  	oldMapType := jh.MapType
  3557  
  3558  	defer func() {
  3559  		jh.MapKeyAsString = oldMapKeyAsString
  3560  		jh.PreferFloat = oldPreferFloat
  3561  		jh.SignedInteger = oldSignedInteger
  3562  		jh.HTMLCharsAsIs = oldHTMLCharAsIs
  3563  		jh.MapType = oldMapType
  3564  	}()
  3565  
  3566  	jh.MapType = mapIntfIntfTyp
  3567  	jh.HTMLCharsAsIs = false
  3568  	jh.MapKeyAsString = true
  3569  	jh.PreferFloat = false
  3570  	jh.SignedInteger = false
  3571  
  3572  	// Also test out decoding string values into naked interface
  3573  	b = `{"true": true, "false": false, null: null, "7700000000000000000": 7700000000000000000}`
  3574  	const num = 7700000000000000000 // 77
  3575  	var golden2 = map[interface{}]interface{}{
  3576  		true:  true,
  3577  		false: false,
  3578  		nil:   nil,
  3579  		// uint64(num):uint64(num),
  3580  	}
  3581  
  3582  	fn := func() {
  3583  		d.ResetBytes([]byte(b))
  3584  		var mf interface{}
  3585  		d.MustDecode(&mf)
  3586  		if err := deepEqual(golden2, mf); err != nil {
  3587  			t.Logf("---- mismatch: %v ==> golden: %#v, decoded: %#v", err, golden2, mf)
  3588  			t.FailNow()
  3589  		}
  3590  	}
  3591  
  3592  	golden2[uint64(num)] = uint64(num)
  3593  	fn()
  3594  	delete(golden2, uint64(num))
  3595  
  3596  	jh.SignedInteger = true
  3597  	golden2[int64(num)] = int64(num)
  3598  	fn()
  3599  	delete(golden2, int64(num))
  3600  	jh.SignedInteger = false
  3601  
  3602  	jh.PreferFloat = true
  3603  	golden2[float64(num)] = float64(num)
  3604  	fn()
  3605  	delete(golden2, float64(num))
  3606  	jh.PreferFloat = false
  3607  }
  3608  
  3609  func doTestJsonEncodeIndent(t *testing.T, h Handle) {
  3610  	defer testSetup(t, &h)()
  3611  	v := TestSimplish{
  3612  		Ii: -794,
  3613  		Ss: `A Man is
  3614  after the new line
  3615  	after new line and tab
  3616  `,
  3617  	}
  3618  	v2 := v
  3619  	v.Mm = make(map[string]*TestSimplish)
  3620  	for i := 0; i < len(v.Ar); i++ {
  3621  		v3 := v2
  3622  		v3.Ii += (i * 4)
  3623  		v3.Ss = fmt.Sprintf("%d - %s", v3.Ii, v3.Ss)
  3624  		if i%2 == 0 {
  3625  			v.Ar[i] = &v3
  3626  		}
  3627  		// v3 = v2
  3628  		v.Sl = append(v.Sl, &v3)
  3629  		v.Mm[strconv.FormatInt(int64(i), 10)] = &v3
  3630  	}
  3631  	jh := h.(*JsonHandle)
  3632  
  3633  	oldcan := jh.Canonical
  3634  	oldIndent := jh.Indent
  3635  	oldS2A := jh.StructToArray
  3636  	defer func() {
  3637  		jh.Canonical = oldcan
  3638  		jh.Indent = oldIndent
  3639  		jh.StructToArray = oldS2A
  3640  	}()
  3641  	jh.Canonical = true
  3642  	jh.Indent = -1
  3643  	jh.StructToArray = false
  3644  	var bs []byte
  3645  	NewEncoderBytes(&bs, jh).MustEncode(&v)
  3646  	txt1Tab := string(bs)
  3647  	bs = nil
  3648  	jh.Indent = 120
  3649  	NewEncoderBytes(&bs, jh).MustEncode(&v)
  3650  	txtSpaces := string(bs)
  3651  	// fmt.Printf("\n-----------\n%s\n------------\n%s\n-------------\n", txt1Tab, txtSpaces)
  3652  
  3653  	goldenResultTab := `{
  3654  	"Ar": [
  3655  		{
  3656  			"Ar": [
  3657  				null,
  3658  				null
  3659  			],
  3660  			"Ii": -794,
  3661  			"Mm": null,
  3662  			"Sl": null,
  3663  			"Ss": "-794 - A Man is\nafter the new line\n\tafter new line and tab\n"
  3664  		},
  3665  		null
  3666  	],
  3667  	"Ii": -794,
  3668  	"Mm": {
  3669  		"0": {
  3670  			"Ar": [
  3671  				null,
  3672  				null
  3673  			],
  3674  			"Ii": -794,
  3675  			"Mm": null,
  3676  			"Sl": null,
  3677  			"Ss": "-794 - A Man is\nafter the new line\n\tafter new line and tab\n"
  3678  		},
  3679  		"1": {
  3680  			"Ar": [
  3681  				null,
  3682  				null
  3683  			],
  3684  			"Ii": -790,
  3685  			"Mm": null,
  3686  			"Sl": null,
  3687  			"Ss": "-790 - A Man is\nafter the new line\n\tafter new line and tab\n"
  3688  		}
  3689  	},
  3690  	"Sl": [
  3691  		{
  3692  			"Ar": [
  3693  				null,
  3694  				null
  3695  			],
  3696  			"Ii": -794,
  3697  			"Mm": null,
  3698  			"Sl": null,
  3699  			"Ss": "-794 - A Man is\nafter the new line\n\tafter new line and tab\n"
  3700  		},
  3701  		{
  3702  			"Ar": [
  3703  				null,
  3704  				null
  3705  			],
  3706  			"Ii": -790,
  3707  			"Mm": null,
  3708  			"Sl": null,
  3709  			"Ss": "-790 - A Man is\nafter the new line\n\tafter new line and tab\n"
  3710  		}
  3711  	],
  3712  	"Ss": "A Man is\nafter the new line\n\tafter new line and tab\n"
  3713  }`
  3714  
  3715  	if txt1Tab != goldenResultTab {
  3716  		t.Logf("decoded indented with tabs != expected: \nexpected: %s\nencoded: %s", goldenResultTab, txt1Tab)
  3717  		t.FailNow()
  3718  	}
  3719  	if txtSpaces != strings.Replace(goldenResultTab, "\t", strings.Repeat(" ", 120), -1) {
  3720  		t.Logf("decoded indented with spaces != expected: \nexpected: %s\nencoded: %s", goldenResultTab, txtSpaces)
  3721  		t.FailNow()
  3722  	}
  3723  }
  3724  
  3725  func doTestPreferArrayOverSlice(t *testing.T, h Handle) {
  3726  	defer testSetup(t, &h)()
  3727  	// encode a slice, decode it with PreferArrayOverSlice
  3728  	// if codecgen, skip the test (as codecgen doesn't work with PreferArrayOverSlice)
  3729  	if codecgen {
  3730  		t.Skip("skipping ... prefer array over slice is not supported in codecgen mode")
  3731  	}
  3732  	bh := testBasicHandle(h)
  3733  	paos := bh.PreferArrayOverSlice
  3734  	styp := bh.SliceType
  3735  	defer func() {
  3736  		bh.PreferArrayOverSlice = paos
  3737  		bh.SliceType = styp
  3738  	}()
  3739  	bh.PreferArrayOverSlice = true
  3740  	bh.SliceType = reflect.TypeOf(([]bool)(nil))
  3741  
  3742  	s2 := [4]bool{true, false, true, false}
  3743  	s := s2[:]
  3744  	var v interface{}
  3745  	bs := testMarshalErr(s, h, t, t.Name())
  3746  	testUnmarshalErr(&v, bs, h, t, t.Name())
  3747  	testDeepEqualErr(s2, v, t, t.Name())
  3748  	testReleaseBytes(bs)
  3749  }
  3750  
  3751  func doTestZeroCopyBytes(t *testing.T, h Handle) {
  3752  	defer testSetup(t, &h)()
  3753  	// jsonhandle and cborhandle with indefiniteLength do not support inline bytes, so skip them.
  3754  	if _, ok := h.(*JsonHandle); ok {
  3755  		t.Skipf("skipping ... zero copy bytes not supported by json handle")
  3756  	}
  3757  	if ch, ok := h.(*CborHandle); ok && ch.IndefiniteLength {
  3758  		t.Skipf("skipping ... zero copy bytes not supported by cbor handle with IndefiniteLength=true")
  3759  	}
  3760  
  3761  	bh := testBasicHandle(h)
  3762  	zc := bh.ZeroCopy
  3763  	defer func() {
  3764  		bh.ZeroCopy = zc
  3765  	}()
  3766  	bh.ZeroCopy = true
  3767  
  3768  	s := []byte("hello")
  3769  	var v []byte
  3770  	bs := testMarshalErr(s, h, t, t.Name())
  3771  
  3772  	// Note: this test only works for decoding from []byte, so cannot use testUnmarshalErr
  3773  	NewDecoderBytes(bs, h).MustDecode(&v)
  3774  	// testUnmarshalErr(&v, bs, h, t, t.Name())
  3775  
  3776  	// validate that bs and s points into the bs stream
  3777  	for i := range bs {
  3778  		if &bs[i] == &v[0] {
  3779  			return
  3780  		}
  3781  	}
  3782  
  3783  	// if not match, then a failure happened.
  3784  	if len(bs) > 0 && len(v) > 0 {
  3785  		t.Logf("%s: ZeroCopy=true, but decoded (%p) is not slice of input: (%p)", h.Name(), &v[0], &bs[0])
  3786  	} else {
  3787  		t.Logf("%s: ZeroCopy=true, but decoded OR input slice is empty: %v, %v", h.Name(), v, bs)
  3788  	}
  3789  	testReleaseBytes(bs)
  3790  	t.FailNow()
  3791  }
  3792  
  3793  func doTestNextValueBytes(t *testing.T, h Handle) {
  3794  	defer testSetup(t, &h)()
  3795  
  3796  	bh := testBasicHandle(h)
  3797  
  3798  	// - encode uint, int, float, bool, struct, map, slice, string - all separated by nil
  3799  	// - use nextvaluebytes to grab he's got each one, and decode it, and compare
  3800  	var inputs = []interface{}{
  3801  		uint64(7777),
  3802  		int64(9999),
  3803  		float64(12.25),
  3804  		true,
  3805  		false,
  3806  		map[string]uint64{"1": 1, "22": 22, "333": 333, "4444": 4444},
  3807  		[]string{"1", "22", "333", "4444"},
  3808  		// use *TestStruc, not *TestStrucFlex, as *TestStrucFlex is harder to compare with deep equal
  3809  		// Remember: *TestStruc was separated for this reason, affording comparing against other libraries
  3810  		newTestStruc(testDepth, testNumRepeatString, false, false, true),
  3811  		"1223334444",
  3812  	}
  3813  	var out []byte
  3814  
  3815  	for i, v := range inputs {
  3816  		_ = i
  3817  		bs := testMarshalErr(v, h, t, "nextvaluebytes")
  3818  		out = append(out, bs...)
  3819  		bs2 := testMarshalErr(nil, h, t, "nextvaluebytes")
  3820  		out = append(out, bs2...)
  3821  		testReleaseBytes(bs)
  3822  		testReleaseBytes(bs2)
  3823  	}
  3824  	// out = append(out, []byte("----")...)
  3825  
  3826  	var valueBytes = make([][]byte, len(inputs)*2)
  3827  
  3828  	d, oldReadBufferSize := testSharedCodecDecoder(out, h, testBasicHandle(h))
  3829  	for i := 0; i < len(inputs)*2; i++ {
  3830  		valueBytes[i] = d.d.nextValueBytes([]byte{})
  3831  		// bs := d.d.nextValueBytes([]byte{})
  3832  		// valueBytes[i] = make([]byte, len(bs))
  3833  		// copy(valueBytes[i], bs)
  3834  	}
  3835  	if testUseIoEncDec >= 0 {
  3836  		bh.ReaderBufferSize = oldReadBufferSize
  3837  	}
  3838  
  3839  	defer func(b bool) { bh.InterfaceReset = b }(bh.InterfaceReset)
  3840  	bh.InterfaceReset = false
  3841  
  3842  	var result interface{}
  3843  	for i := 0; i < len(inputs); i++ {
  3844  		// result = reflect.New(reflect.TypeOf(inputs[i])).Elem().Interface()
  3845  		result = reflect.Zero(reflect.TypeOf(inputs[i])).Interface()
  3846  		testUnmarshalErr(&result, valueBytes[i*2], h, t, "nextvaluebytes")
  3847  		testDeepEqualErr(inputs[i], result, t, "nextvaluebytes-1")
  3848  		result = nil
  3849  		testUnmarshalErr(&result, valueBytes[(i*2)+1], h, t, "nextvaluebytes")
  3850  		testDeepEqualErr(nil, result, t, "nextvaluebytes-2")
  3851  	}
  3852  }
  3853  
  3854  func doTestNumbers(t *testing.T, h Handle) {
  3855  	defer testSetup(t, &h)()
  3856  	__doTestIntegers(t, h)
  3857  	__doTestFloats(t, h)
  3858  	__doTestIntegerFloatConversions(t, h)
  3859  }
  3860  
  3861  func __doTestIntegers(t *testing.T, h Handle) {
  3862  	// handle SignedInteger=true|false
  3863  	// decode into an interface{}
  3864  
  3865  	bh := testBasicHandle(h)
  3866  
  3867  	oldSignedInteger := bh.SignedInteger
  3868  	var oldPreferFloat bool
  3869  	var oldNoFixedNum bool
  3870  	jh, jok := h.(*JsonHandle)
  3871  	mh, mok := h.(*MsgpackHandle)
  3872  	if jok {
  3873  		oldPreferFloat = jh.PreferFloat
  3874  	}
  3875  	if mok {
  3876  		oldNoFixedNum = mh.NoFixedNum
  3877  		mh.NoFixedNum = true
  3878  	}
  3879  
  3880  	defer func() {
  3881  		bh.SignedInteger = oldSignedInteger
  3882  		if jok {
  3883  			jh.PreferFloat = oldPreferFloat
  3884  		}
  3885  		if mok {
  3886  			mh.NoFixedNum = oldNoFixedNum
  3887  		}
  3888  	}()
  3889  
  3890  	// var vi int64
  3891  	// var ui uint64
  3892  	var ii interface{}
  3893  
  3894  	for _, v := range testUintsToParse {
  3895  		if jok {
  3896  			jh.PreferFloat = false
  3897  		}
  3898  		b := testMarshalErr(v, h, t, "test-integers")
  3899  		ii = nil
  3900  		bh.SignedInteger = true
  3901  		testUnmarshalErr(&ii, b, h, t, "test-integers")
  3902  		testDeepEqualErr(ii, int64(v), t, "test-integers-signed")
  3903  		ii = nil
  3904  		bh.SignedInteger = false
  3905  		testUnmarshalErr(&ii, b, h, t, "test-integers")
  3906  		testDeepEqualErr(ii, uint64(v), t, "test-integers-unsigned")
  3907  		ii = nil
  3908  		if jok {
  3909  			jh.PreferFloat = true
  3910  			testUnmarshalErr(&ii, b, h, t, "test-integers")
  3911  			testDeepEqualErr(ii, float64(v), t, "test-integers-float")
  3912  		}
  3913  		testReleaseBytes(b)
  3914  	}
  3915  }
  3916  
  3917  var testUintsToParse = []uint64{
  3918  	// Note: use large integers, as some formats store small integers in an agnostic
  3919  	// way, where its not clear if signed or unsigned.
  3920  	2,
  3921  	2048,
  3922  	1<<63 - 4,
  3923  	1800000000e-2,
  3924  	18000000e+2,
  3925  	4.56e+4, // tests float32 exact parsing
  3926  	4.56e+16,
  3927  }
  3928  
  3929  var testFloatsToParse = []float64{
  3930  	3,
  3931  	math.NaN(),
  3932  	math.Inf(1),
  3933  	math.Inf(-1),
  3934  	4.56e+4,  // tests float32 exact parsing
  3935  	4.56e+18, // float32 parsing - exp > 10
  3936  	4.56e+10,
  3937  	4.56e+30,
  3938  	1.01234567890123456789e+30,
  3939  	1.32e+5,
  3940  	1.32e-5,
  3941  	0e+01234567890123456789,
  3942  	1.7976931348623157e308,
  3943  	-1.7976931348623157e+308,
  3944  	1e308,
  3945  	1e-308,
  3946  	1.694649e-317,
  3947  	// 1e-4294967296,
  3948  	2.2250738585072012e-308,
  3949  	4.630813248087435e+307,
  3950  	1.00000000000000011102230246251565404236316680908203125,
  3951  	1.00000000000000033306690738754696212708950042724609375,
  3952  }
  3953  
  3954  func __doTestFloats(t *testing.T, h Handle) {
  3955  	_, jok := h.(*JsonHandle)
  3956  
  3957  	f64s := testFloatsToParse
  3958  	const unusedVal = 9999 // use this as a marker
  3959  
  3960  	// marshall it, unmarshal it, compare to original
  3961  	// Note: JSON encodes NaN, inf, -inf as null, which is decoded as zero value (ie 0).
  3962  	for _, f64 := range f64s {
  3963  		{
  3964  			f := f64
  3965  			var w float64 = unusedVal
  3966  			b := testMarshalErr(f, h, t, "test-floats-enc")
  3967  			testUnmarshalErr(&w, b, h, t, "test-floats-dec")
  3968  			// we only check json for float64, as it doesn't differentiate
  3969  			if (jok && (math.IsNaN(f64) || math.IsInf(f64, 0)) && w != 0) ||
  3970  				(!jok && w != f && !math.IsNaN(float64(f))) {
  3971  				t.Logf("error testing float64: %v, decoded as: %v", f, w)
  3972  				t.FailNow()
  3973  			}
  3974  			var wi interface{}
  3975  			testUnmarshalErr(&wi, b, h, t, "test-floats-dec")
  3976  			if (jok && (math.IsNaN(f64) || math.IsInf(f64, 0)) && wi != nil) ||
  3977  				(!jok && wi.(float64) != f && !math.IsNaN(float64(f))) {
  3978  				t.Logf("error testing float64: %v, decoded as: %v", f, wi)
  3979  				t.FailNow()
  3980  			}
  3981  			testReleaseBytes(b)
  3982  		}
  3983  		{
  3984  			f := float32(f64)
  3985  			var w float32 = unusedVal
  3986  			b := testMarshalErr(f, h, t, "test-floats-enc")
  3987  			// xdebug2f("test float: of %v, encoded as: %s", f, b)
  3988  			testUnmarshalErr(&w, b, h, t, "test-floats-dec")
  3989  			if (jok && (math.IsNaN(f64) || math.IsInf(f64, 0)) && w != 0) ||
  3990  				(!jok && w != f && !math.IsNaN(float64(f))) {
  3991  				t.Logf("error testing float32: %v, decoded as: %v", f, w)
  3992  				t.FailNow()
  3993  			}
  3994  			testReleaseBytes(b)
  3995  		}
  3996  	}
  3997  }
  3998  
  3999  func __doTestIntegerFloatConversions(t *testing.T, h Handle) {
  4000  	if !testRecoverPanicToErr {
  4001  		t.Skip(testSkipIfNotRecoverPanicToErrMsg)
  4002  	}
  4003  	type tI struct{ N int }
  4004  	type tU struct{ N uint }
  4005  	type tF struct{ N float64 }
  4006  
  4007  	type elem struct {
  4008  		in  interface{}
  4009  		out interface{}
  4010  		err string
  4011  	}
  4012  	tests := []elem{
  4013  		// good
  4014  		{tI{5}, tF{5.0}, ""},
  4015  		{tU{5}, tF{5.0}, ""},
  4016  		{tF{5.0}, tU{5}, ""},
  4017  		{tF{5.0}, tI{5}, ""},
  4018  		{tF{-5.0}, tI{-5}, ""},
  4019  		// test negative number into unsigned integer
  4020  		{tI{-5}, tU{5}, "error"},
  4021  		{tF{-5.0}, tU{5}, "error"},
  4022  		// test fractional float into integer
  4023  		{tF{-5.7}, tU{5}, "error"},
  4024  		{tF{-5.7}, tI{5}, "error"},
  4025  	}
  4026  	for _, v := range tests {
  4027  		r := reflect.New(reflect.TypeOf(v.out))
  4028  		b := testMarshalErr(v.in, h, t, "")
  4029  		err := testUnmarshal(r.Interface(), b, h)
  4030  		if v.err == "" {
  4031  			testDeepEqualErr(err, nil, t, "")
  4032  			testDeepEqualErr(r.Elem().Interface(), v.out, t, "")
  4033  		} else if err == nil {
  4034  			t.Logf("expecting an error but didn't receive any, with in: %v, out: %v, expecting err matching: %v", v.in, v.out, v.err)
  4035  			t.FailNow()
  4036  		}
  4037  	}
  4038  }
  4039  
  4040  func doTestStructFieldInfoToArray(t *testing.T, h Handle) {
  4041  	if !testRecoverPanicToErr {
  4042  		t.Skip(testSkipIfNotRecoverPanicToErrMsg)
  4043  	}
  4044  	defer testSetup(t, &h)()
  4045  	bh := testBasicHandle(h)
  4046  
  4047  	defer func(b bool) { bh.CheckCircularRef = b }(bh.CheckCircularRef)
  4048  	bh.CheckCircularRef = true
  4049  
  4050  	var vs = Sstructsmall{A: 99}
  4051  	var vb = Sstructbig{
  4052  		A:         77,
  4053  		B:         true,
  4054  		c:         "ccc 3 ccc",
  4055  		Ssmallptr: &vs,
  4056  		Ssmall:    vs,
  4057  	}
  4058  	vb.Sptr = &vb
  4059  
  4060  	vba := SstructbigToArray{
  4061  		A:         vb.A,
  4062  		B:         vb.B,
  4063  		c:         vb.c,
  4064  		Ssmallptr: vb.Ssmallptr,
  4065  		Ssmall:    vb.Ssmall,
  4066  		Sptr:      vb.Sptr,
  4067  	}
  4068  
  4069  	var b []byte
  4070  	var err error
  4071  	if !codecgen {
  4072  		// codecgen doesn't support CheckCircularRef, and these types are codecgen'ed
  4073  		b, err = testMarshal(&vba, h)
  4074  		testReleaseBytes(b)
  4075  		if err == nil || !strings.Contains(err.Error(), "circular reference found") {
  4076  			t.Logf("expect circular reference error, got: %v", err)
  4077  			t.FailNow()
  4078  		}
  4079  	}
  4080  
  4081  	vb2 := vb
  4082  	vb.Sptr = nil // so we stop having the circular reference error
  4083  	vba.Sptr = &vb2
  4084  
  4085  	var ss []interface{}
  4086  
  4087  	// bh.CheckCircularRef = false
  4088  	b = testMarshalErr(&vba, h, t, "-")
  4089  	testUnmarshalErr(&ss, b, h, t, "-")
  4090  	testDeepEqualErr(ss[1], true, t, "-")
  4091  	testReleaseBytes(b)
  4092  }
  4093  
  4094  func doTestDesc(t *testing.T, h Handle, m map[byte]string) {
  4095  	defer testSetup(t, &h)()
  4096  	for k, v := range m {
  4097  		if s := h.desc(k); s != v {
  4098  			t.Logf("error describing descriptor: '%q' i.e. 0x%x, expected '%s', got '%s'", k, k, v, s)
  4099  			t.FailNow()
  4100  		}
  4101  	}
  4102  }
  4103  
  4104  func TestAtomic(t *testing.T) {
  4105  	defer testSetup(t, nil)()
  4106  	// load, store, load, confirm
  4107  	if true {
  4108  		var a atomicTypeInfoSlice
  4109  		l := a.load()
  4110  		if l != nil {
  4111  			t.Logf("atomic fail: %T, expected load return nil, received: %v", a, l)
  4112  			t.FailNow()
  4113  		}
  4114  		l = append(l, rtid2ti{})
  4115  		a.store(l)
  4116  		l = a.load()
  4117  		if len(l) != 1 {
  4118  			t.Logf("atomic fail: %T, expected load to have length 1, received: %d", a, len(l))
  4119  			t.FailNow()
  4120  		}
  4121  	}
  4122  	if true {
  4123  		var a atomicRtidFnSlice
  4124  		l := a.load()
  4125  		if l != nil {
  4126  			t.Logf("atomic fail: %T, expected load return nil, received: %v", a, l)
  4127  			t.FailNow()
  4128  		}
  4129  		l = append(l, codecRtidFn{})
  4130  		a.store(l)
  4131  		l = a.load()
  4132  		if len(l) != 1 {
  4133  			t.Logf("atomic fail: %T, expected load to have length 1, received: %d", a, len(l))
  4134  			t.FailNow()
  4135  		}
  4136  	}
  4137  	if true {
  4138  		var a atomicClsErr
  4139  		l := a.load()
  4140  		if l.err != nil {
  4141  			t.Logf("atomic fail: %T, expected load return clsErr = nil, received: %v", a, l.err)
  4142  			t.FailNow()
  4143  		}
  4144  		l.err = io.EOF
  4145  		a.store(l)
  4146  		l = a.load()
  4147  		if l.err != io.EOF {
  4148  			t.Logf("atomic fail: %T, expected clsErr = io.EOF, received: %v", a, l.err)
  4149  			t.FailNow()
  4150  		}
  4151  	}
  4152  }
  4153  
  4154  // -----------
  4155  
  4156  func doTestJsonLargeInteger(t *testing.T, h Handle) {
  4157  	if !testRecoverPanicToErr {
  4158  		t.Skip(testSkipIfNotRecoverPanicToErrMsg)
  4159  	}
  4160  	defer testSetup(t, &h)()
  4161  	jh := h.(*JsonHandle)
  4162  	for _, i := range []uint8{'L', 'A', 0} {
  4163  		for _, j := range []interface{}{
  4164  			int64(1 << 60),
  4165  			-int64(1 << 60),
  4166  			0,
  4167  			1 << 20,
  4168  			-(1 << 20),
  4169  			uint64(1 << 60),
  4170  			uint(0),
  4171  			uint(1 << 20),
  4172  			int64(1840000e-2),
  4173  			uint64(1840000e+2),
  4174  		} {
  4175  			__doTestJsonLargeInteger(t, j, i, jh)
  4176  		}
  4177  	}
  4178  
  4179  	oldIAS := jh.IntegerAsString
  4180  	defer func() { jh.IntegerAsString = oldIAS }()
  4181  	jh.IntegerAsString = 0
  4182  
  4183  	type tt struct {
  4184  		s           string
  4185  		canI, canUi bool
  4186  		i           int64
  4187  		ui          uint64
  4188  	}
  4189  
  4190  	var i int64
  4191  	var ui uint64
  4192  	var err error
  4193  	var d *Decoder = NewDecoderBytes(nil, jh)
  4194  	for _, v := range []tt{
  4195  		{"0", true, true, 0, 0},
  4196  		{"0000", false, false, 0, 0},
  4197  		{"0.00e+2", true, true, 0, 0},
  4198  		{"000e-2", false, false, 0, 0},
  4199  		{"0.00e-2", true, true, 0, 0},
  4200  
  4201  		{"9223372036854775807", true, true, math.MaxInt64, math.MaxInt64},                             // maxint64
  4202  		{"92233720368547758.07e+2", true, true, math.MaxInt64, math.MaxInt64},                         // maxint64
  4203  		{"922337203685477580700e-2", true, true, math.MaxInt64, math.MaxInt64},                        // maxint64
  4204  		{"9223372.036854775807E+12", true, true, math.MaxInt64, math.MaxInt64},                        // maxint64
  4205  		{"9223372036854775807000000000000E-12", true, true, math.MaxInt64, math.MaxInt64},             // maxint64
  4206  		{"0.9223372036854775807E+19", true, true, math.MaxInt64, math.MaxInt64},                       // maxint64
  4207  		{"92233720368547758070000000000000000000E-19", true, true, math.MaxInt64, math.MaxInt64},      // maxint64
  4208  		{"0.000009223372036854775807E+24", true, true, math.MaxInt64, math.MaxInt64},                  // maxint64
  4209  		{"9223372036854775807000000000000000000000000E-24", true, true, math.MaxInt64, math.MaxInt64}, // maxint64
  4210  
  4211  		{"-9223372036854775808", true, false, math.MinInt64, 0},                             // minint64
  4212  		{"-92233720368547758.08e+2", true, false, math.MinInt64, 0},                         // minint64
  4213  		{"-922337203685477580800E-2", true, false, math.MinInt64, 0},                        // minint64
  4214  		{"-9223372.036854775808e+12", true, false, math.MinInt64, 0},                        // minint64
  4215  		{"-9223372036854775808000000000000E-12", true, false, math.MinInt64, 0},             // minint64
  4216  		{"-0.9223372036854775808e+19", true, false, math.MinInt64, 0},                       // minint64
  4217  		{"-92233720368547758080000000000000000000E-19", true, false, math.MinInt64, 0},      // minint64
  4218  		{"-0.000009223372036854775808e+24", true, false, math.MinInt64, 0},                  // minint64
  4219  		{"-9223372036854775808000000000000000000000000E-24", true, false, math.MinInt64, 0}, // minint64
  4220  
  4221  		{"18446744073709551615", false, true, 0, math.MaxUint64},                             // maxuint64
  4222  		{"18446744.073709551615E+12", false, true, 0, math.MaxUint64},                        // maxuint64
  4223  		{"18446744073709551615000000000000E-12", false, true, 0, math.MaxUint64},             // maxuint64
  4224  		{"0.000018446744073709551615E+24", false, true, 0, math.MaxUint64},                   // maxuint64
  4225  		{"18446744073709551615000000000000000000000000E-24", false, true, 0, math.MaxUint64}, // maxuint64
  4226  
  4227  		// Add test for limit of uint64 where last digit is 0
  4228  		{"18446744073709551610", false, true, 0, math.MaxUint64 - 5},                             // maxuint64
  4229  		{"18446744.073709551610E+12", false, true, 0, math.MaxUint64 - 5},                        // maxuint64
  4230  		{"18446744073709551610000000000000E-12", false, true, 0, math.MaxUint64 - 5},             // maxuint64
  4231  		{"0.000018446744073709551610E+24", false, true, 0, math.MaxUint64 - 5},                   // maxuint64
  4232  		{"18446744073709551610000000000000000000000000E-24", false, true, 0, math.MaxUint64 - 5}, // maxuint64
  4233  		// {"", true, true},
  4234  	} {
  4235  		if v.s == "" {
  4236  			continue
  4237  		}
  4238  		d.ResetBytes([]byte(v.s))
  4239  		err = d.Decode(&ui)
  4240  		if (v.canUi && err != nil) || (!v.canUi && err == nil) || (v.canUi && err == nil && v.ui != ui) {
  4241  			t.Logf("Failing to decode %s (as unsigned): %v", v.s, err)
  4242  			t.FailNow()
  4243  		}
  4244  
  4245  		d.ResetBytes([]byte(v.s))
  4246  		err = d.Decode(&i)
  4247  		if (v.canI && err != nil) || (!v.canI && err == nil) || (v.canI && err == nil && v.i != i) {
  4248  			t.Logf("Failing to decode %s (as signed): %v", v.s, err)
  4249  			t.FailNow()
  4250  		}
  4251  	}
  4252  }
  4253  
  4254  func doTestJsonInvalidUnicode(t *testing.T, h Handle) {
  4255  	if !testRecoverPanicToErr {
  4256  		t.Skip(testSkipIfNotRecoverPanicToErrMsg)
  4257  	}
  4258  	defer testSetup(t, &h)()
  4259  	// t.Skipf("new json implementation does not handle bad unicode robustly")
  4260  	jh := h.(*JsonHandle)
  4261  
  4262  	var err error
  4263  
  4264  	// ---- test unmarshal ---
  4265  	var m = map[string]string{
  4266  		`"\udc49\u0430abc"`: "\uFFFDabc",
  4267  		`"\udc49\u0430"`:    "\uFFFD",
  4268  		`"\udc49abc"`:       "\uFFFDabc",
  4269  		`"\udc49"`:          "\uFFFD",
  4270  		`"\udZ49\u0430abc"`: "\uFFFD\u0430abc",
  4271  		`"\udcG9\u0430"`:    "\uFFFD\u0430",
  4272  		`"\uHc49abc"`:       "\uFFFDabc",
  4273  		`"\uKc49"`:          "\uFFFD",
  4274  	}
  4275  
  4276  	for k, v := range m {
  4277  		// call testUnmarshal directly, so we can check for EOF
  4278  		// testUnmarshalErr(&s, []byte(k), jh, t, "-")
  4279  		// t.Logf("%s >> %s", k, v)
  4280  		var s string
  4281  		err = testUnmarshal(&s, []byte(k), jh)
  4282  		if err != nil {
  4283  			if err == io.EOF || err == io.ErrUnexpectedEOF {
  4284  				continue
  4285  			}
  4286  			t.Logf("%s: unmarshal failed: %v", "-", err)
  4287  			t.FailNow()
  4288  		}
  4289  
  4290  		if s != v {
  4291  			t.Logf("unmarshal: not equal: %q, %q", v, s)
  4292  			t.FailNow()
  4293  		}
  4294  	}
  4295  
  4296  	// test some valid edge cases
  4297  	m = map[string]string{
  4298  		`"az\uD834\udD1E"`: "az𝄞",
  4299  		`"n\ud834\uDD1en"`: "n\U0001D11En", // "\uf09d849e", // "\UD834DD1E" // U+1DD1E g clef
  4300  
  4301  		`"a\\\"\/\"\b\f\n\r\"\tz"`: "a\\\"/\"\b\f\n\r\"\tz",
  4302  	}
  4303  	for k, v := range m {
  4304  		var s string
  4305  		err = testUnmarshal(&s, []byte(k), jh)
  4306  		if err != nil {
  4307  			t.Logf("%s: unmarshal failed: %v", "-", err)
  4308  			t.FailNow()
  4309  		}
  4310  
  4311  		if s != v {
  4312  			t.Logf("unmarshal: not equal: %q, %q", v, s)
  4313  			t.FailNow()
  4314  		}
  4315  	}
  4316  
  4317  	// ---- test marshal ---
  4318  	var b = []byte{'"', 0xef, 0xbf, 0xbd} // this is " and unicode.ReplacementChar (as bytes)
  4319  	var m2 = map[string][]byte{
  4320  		string([]byte{0xef, 0xbf, 0xbd}):           append(b, '"'),
  4321  		string([]byte{0xef, 0xbf, 0xbd, 0x0, 0x0}): append(b, `\u0000\u0000"`...),
  4322  
  4323  		"a\\\"/\"\b\f\n\r\"\tz": []byte(`"a\\\"/\"\b\f\n\r\"\tz"`),
  4324  
  4325  		// our encoder doesn't support encoding using only ascii ... so need to use the utf-8 version
  4326  		// "n\U0001D11En": []byte(`"n\uD834\uDD1En"`),
  4327  		// "az𝄞": []byte(`"az\uD834\uDD1E"`),
  4328  		"n\U0001D11En": []byte(`"n𝄞n"`),
  4329  		"az𝄞":          []byte(`"az𝄞"`),
  4330  
  4331  		string([]byte{129, 129}): []byte(`"\uFFFD\uFFFD"`),
  4332  	}
  4333  
  4334  	for k, v := range m2 {
  4335  		b, err = testMarshal(k, jh)
  4336  		if err != nil {
  4337  			t.Logf("%s: marshal failed: %v", "-", err)
  4338  			t.FailNow()
  4339  		}
  4340  
  4341  		if !bytes.Equal(b, v) {
  4342  			t.Logf("marshal: not equal: %q, %q", v, b)
  4343  			t.FailNow()
  4344  		}
  4345  		testReleaseBytes(b)
  4346  	}
  4347  }
  4348  
  4349  func doTestJsonNumberParsing(t *testing.T, h Handle) {
  4350  	defer testSetup(t, &h)()
  4351  	parseInt64_reader := func(r readFloatResult) (v int64, fail bool) {
  4352  		u, fail := parseUint64_reader(r)
  4353  		if fail {
  4354  			return
  4355  		}
  4356  		if r.neg {
  4357  			v = -int64(u)
  4358  		} else {
  4359  			v = int64(u)
  4360  		}
  4361  		return
  4362  	}
  4363  	for _, f64 := range testFloatsToParse {
  4364  		// using large prec might make a non-exact float ..., while small prec might make lose some precision.
  4365  		// However, we can do a check, and only check if the (u)int64 and float64 can be converted to one another
  4366  		// without losing any precision. Then we can use any precision for the tests.
  4367  		var precs = [...]int{32, -1}
  4368  		var r readFloatResult
  4369  		var fail bool
  4370  		var fs uint64
  4371  		var fsi int64
  4372  		var fint, ffrac float64
  4373  		_ = ffrac
  4374  		var bv []byte
  4375  		for _, prec := range precs {
  4376  			f := f64
  4377  			if math.IsNaN(f64) || math.IsInf(f64, 0) {
  4378  				goto F32
  4379  			}
  4380  			bv = strconv.AppendFloat(nil, f, 'E', prec, 64)
  4381  			if fs, err := parseFloat64_custom(bv); err != nil || fs != f {
  4382  				t.Logf("float64 -> float64 error (prec: %v) parsing '%s', got %v, expected %v: %v", prec, bv, fs, f, err)
  4383  				t.FailNow()
  4384  			}
  4385  			// try decoding it a uint64 or int64
  4386  			fint, ffrac = math.Modf(f)
  4387  			bv = strconv.AppendFloat(bv[:0], fint, 'E', prec, 64)
  4388  			if f < 0 || fint != float64(uint64(fint)) {
  4389  				goto F64i
  4390  			}
  4391  			r = readFloat(bv, fi64u)
  4392  			fail = !r.ok
  4393  			if r.ok {
  4394  				fs, fail = parseUint64_reader(r)
  4395  			}
  4396  			if fail || fs != uint64(fint) {
  4397  				t.Logf("float64 -> uint64 error (prec: %v, fail: %v) parsing '%s', got %v, expected %v", prec, fail, bv, fs, uint64(fint))
  4398  				t.FailNow()
  4399  			}
  4400  		F64i:
  4401  			if fint != float64(int64(fint)) {
  4402  				goto F32
  4403  			}
  4404  			r = readFloat(bv, fi64u)
  4405  			fail = !r.ok
  4406  			if r.ok {
  4407  				fsi, fail = parseInt64_reader(r)
  4408  			}
  4409  			if fail || fsi != int64(fint) {
  4410  				t.Logf("float64 -> int64 error (prec: %v, fail: %v) parsing '%s', got %v, expected %v", prec, fail, bv, fsi, int64(fint))
  4411  				t.FailNow()
  4412  			}
  4413  		F32:
  4414  			f32 := float32(f64)
  4415  			f64n := float64(f32)
  4416  			if !(math.IsNaN(f64n) || math.IsInf(f64n, 0)) {
  4417  				bv := strconv.AppendFloat(nil, f64n, 'E', prec, 32)
  4418  				if fs, err := parseFloat32_custom(bv); err != nil || fs != f32 {
  4419  					t.Logf("float32 -> float32 error (prec: %v) parsing '%s', got %v, expected %v: %v", prec, bv, fs, f32, err)
  4420  					t.FailNow()
  4421  				}
  4422  			}
  4423  		}
  4424  	}
  4425  
  4426  	for _, v := range testUintsToParse {
  4427  		const prec int = 32    // test with precisions of 32 so formatting doesn't truncate what doesn't fit into float
  4428  		v = uint64(float64(v)) // use a value that when converted back and forth, remains the same
  4429  		bv := strconv.AppendFloat(nil, float64(v), 'E', prec, 64)
  4430  		r := readFloat(bv, fi64u)
  4431  		if r.ok {
  4432  			if fs, fail := parseUint64_reader(r); fail || fs != v {
  4433  				t.Logf("uint64 -> uint64 error (prec: %v, fail: %v) parsing '%s', got %v, expected %v", prec, fail, bv, fs, v)
  4434  				t.FailNow()
  4435  			}
  4436  		} else {
  4437  			t.Logf("uint64 -> uint64 not ok (prec: %v) parsing '%s', expected %v", prec, bv, v)
  4438  			t.FailNow()
  4439  		}
  4440  		vi := int64(v)
  4441  		bv = strconv.AppendFloat(nil, float64(vi), 'E', prec, 64)
  4442  		r = readFloat(bv, fi64u)
  4443  		if r.ok {
  4444  			if fs, fail := parseInt64_reader(r); fail || fs != vi {
  4445  				t.Logf("int64 -> int64 error (prec: %v, fail: %v) parsing '%s', got %v, expected %v", prec, fail, bv, fs, vi)
  4446  				t.FailNow()
  4447  			}
  4448  		} else {
  4449  			t.Logf("int64 -> int64 not ok (prec: %v) parsing '%s', expected %v", prec, bv, vi)
  4450  			t.FailNow()
  4451  		}
  4452  	}
  4453  	{
  4454  		var f64 float64 = 64.0
  4455  		var f32 float32 = 32.0
  4456  		var i int = 11
  4457  		var u64 uint64 = 128
  4458  
  4459  		for _, s := range []string{`""`, `null`} {
  4460  			b := []byte(s)
  4461  			testUnmarshalErr(&f64, b, h, t, "")
  4462  			testDeepEqualErr(f64, float64(0), t, "")
  4463  			testUnmarshalErr(&f32, b, h, t, "")
  4464  			testDeepEqualErr(f32, float32(0), t, "")
  4465  			testUnmarshalErr(&i, b, h, t, "")
  4466  			testDeepEqualErr(i, int(0), t, "")
  4467  			testUnmarshalErr(&u64, b, h, t, "")
  4468  			testDeepEqualErr(u64, uint64(0), t, "")
  4469  			testUnmarshalErr(&u64, b, h, t, "")
  4470  			testDeepEqualErr(u64, uint64(0), t, "")
  4471  		}
  4472  	}
  4473  }
  4474  
  4475  func doTestMsgpackDecodeMapAndExtSizeMismatch(t *testing.T, h Handle) {
  4476  	if !testRecoverPanicToErr {
  4477  		t.Skip(testSkipIfNotRecoverPanicToErrMsg)
  4478  	}
  4479  	defer testSetup(t, &h)()
  4480  	fn := func(t *testing.T, b []byte, v interface{}) {
  4481  		if err := NewDecoderBytes(b, h).Decode(v); err != io.EOF && err != io.ErrUnexpectedEOF {
  4482  			t.Fatalf("expected EOF or ErrUnexpectedEOF, got %v", err)
  4483  		}
  4484  	}
  4485  
  4486  	// a map claiming to have 0x10eeeeee KV pairs, but only has 1.
  4487  	var b = []byte{0xdf, 0x10, 0xee, 0xee, 0xee, 0x1, 0xa1, 0x1}
  4488  	var m1 map[int]string
  4489  	var m2 map[int][]byte
  4490  	fn(t, b, &m1)
  4491  	fn(t, b, &m2)
  4492  
  4493  	// an extension claiming to have 0x7fffffff bytes, but only has 1.
  4494  	b = []byte{0xc9, 0x7f, 0xff, 0xff, 0xff, 0xda, 0x1}
  4495  	var a interface{}
  4496  	fn(t, b, &a)
  4497  
  4498  	// b = []byte{0x00}
  4499  	// var s testSelferRecur
  4500  	// fn(t, b, &s)
  4501  }
  4502  
  4503  func TestMapRangeIndex(t *testing.T) {
  4504  	defer testSetup(t, nil)()
  4505  	// t.Skip()
  4506  	type T struct {
  4507  		I int
  4508  		S string
  4509  		B bool
  4510  		M map[int]T
  4511  	}
  4512  
  4513  	t1 := T{I: 1, B: true, S: "11", M: map[int]T{11: T{I: 11}}}
  4514  	t2 := T{I: 1, B: true, S: "12", M: map[int]T{12: T{I: 12}}}
  4515  
  4516  	// ------
  4517  
  4518  	var m1 = map[string]*T{
  4519  		"11": &t1,
  4520  		"12": &t2,
  4521  	}
  4522  	var m1c = make(map[string]T)
  4523  	for k, v := range m1 {
  4524  		m1c[k] = *v
  4525  	}
  4526  
  4527  	fnrv := func(r1, r2 reflect.Value) reflect.Value {
  4528  		if r1.IsValid() {
  4529  			return r1
  4530  		}
  4531  		return r2
  4532  	}
  4533  
  4534  	// var vx reflect.Value
  4535  
  4536  	mt := reflect.TypeOf(m1)
  4537  	rvk := mapAddrLoopvarRV(mt.Key(), mt.Key().Kind())
  4538  	rvv := mapAddrLoopvarRV(mt.Elem(), mt.Elem().Kind())
  4539  	var it mapIter
  4540  	mapRange(&it, reflect.ValueOf(m1), rvk, rvv, true)
  4541  	for it.Next() {
  4542  		k := fnrv(it.Key(), rvk).Interface().(string)
  4543  		v := fnrv(it.Value(), rvv).Interface().(*T)
  4544  		testDeepEqualErr(m1[k], v, t, "map-key-eq-it-key")
  4545  		if _, ok := m1c[k]; ok {
  4546  			delete(m1c, k)
  4547  		} else {
  4548  			t.Logf("unexpected key in map: %v", k)
  4549  			t.FailNow()
  4550  		}
  4551  	}
  4552  	it.Done()
  4553  	testDeepEqualErr(len(m1c), 0, t, "all-keys-not-consumed")
  4554  
  4555  	// ------
  4556  
  4557  	var m2 = map[*T]T{
  4558  		&t1: t1,
  4559  		&t2: t2,
  4560  	}
  4561  	var m2c = make(map[*T]*T)
  4562  	for k := range m2 {
  4563  		m2c[k] = k
  4564  	}
  4565  
  4566  	mt = reflect.TypeOf(m2)
  4567  	rvk = mapAddrLoopvarRV(mt.Key(), mt.Key().Kind())
  4568  	rvv = mapAddrLoopvarRV(mt.Elem(), mt.Elem().Kind())
  4569  	it = mapIter{} // zero it out first, before calling mapRange
  4570  	mapRange(&it, reflect.ValueOf(m2), rvk, rvv, true)
  4571  	for it.Next() {
  4572  		k := fnrv(it.Key(), rvk).Interface().(*T)
  4573  		v := fnrv(it.Value(), rvv).Interface().(T)
  4574  		testDeepEqualErr(m2[k], v, t, "map-key-eq-it-key")
  4575  		if _, ok := m2c[k]; ok {
  4576  			delete(m2c, k)
  4577  		} else {
  4578  			t.Logf("unexpected key in map: %v", k)
  4579  			t.FailNow()
  4580  		}
  4581  	}
  4582  	it.Done()
  4583  	testDeepEqualErr(len(m2c), 0, t, "all-keys-not-consumed")
  4584  
  4585  	// ---- test mapGet
  4586  
  4587  	fnTestMapIndex := func(mi ...interface{}) {
  4588  		for _, m0 := range mi {
  4589  			m := reflect.ValueOf(m0)
  4590  			mkt := m.Type().Key()
  4591  			mvt := m.Type().Elem()
  4592  			kfast := mapKeyFastKindFor(mkt.Kind())
  4593  			rvv := mapAddrLoopvarRV(mvt, mvt.Kind())
  4594  			visindirect := mapStoresElemIndirect(mvt.Size())
  4595  			visref := refBitset.isset(byte(mvt.Kind()))
  4596  
  4597  			for _, k := range m.MapKeys() {
  4598  				mg := mapGet(m, k, rvv, kfast, visindirect, visref).Interface()
  4599  				testDeepEqualErr(m.MapIndex(k).Interface(), mg, t, "map-index-eq")
  4600  			}
  4601  		}
  4602  	}
  4603  
  4604  	fnTestMapIndex(m1, m1c, m2, m2c)
  4605  
  4606  	// var s string = "hello"
  4607  	// var tt = &T{I: 3}
  4608  	// ttTyp := reflect.TypeOf(tt)
  4609  	// _, _ = tt, ttTyp
  4610  	// mv := reflect.ValueOf(m)
  4611  	// it := mapRange(mv, reflect.ValueOf(&s).Elem(), reflect.ValueOf(&tt).Elem(), true) //ok
  4612  	// it := mapRange(mv, reflect.New(reflect.TypeOf(s)).Elem(), reflect.New(reflect.TypeOf(T{})).Elem(), true) // ok
  4613  	// it := mapRange(mv, reflect.New(reflect.TypeOf(s)).Elem(), reflect.New(ttTyp.Elem()), true) // !ok
  4614  	// it := mapRange(mv, reflect.New(reflect.TypeOf(s)).Elem(), reflect.New(reflect.TypeOf(T{})), true) !ok
  4615  	// it := mapRange(mv, reflect.New(reflect.TypeOf(s)).Elem(), reflect.New(reflect.TypeOf(T{})).Elem(), true) // ok
  4616  
  4617  	// fmt.Printf("key: %#v\n", it.Key())
  4618  	// fmt.Printf("exp: %#v\n", mv.MapIndex(it.Key()))
  4619  	// fmt.Printf("val: %#v\n", it.Value())
  4620  	// testDeepEqualErr(mv.MapIndex(it.Key()), it.Value().Interface()
  4621  }
  4622  
  4623  // ----------
  4624  
  4625  func TestBincCodecsTable(t *testing.T) {
  4626  	doTestCodecTableOne(t, testBincH)
  4627  }
  4628  
  4629  func TestBincCodecsMisc(t *testing.T) {
  4630  	doTestCodecMiscOne(t, testBincH)
  4631  }
  4632  
  4633  func TestBincCodecsEmbeddedPointer(t *testing.T) {
  4634  	doTestCodecEmbeddedPointer(t, testBincH)
  4635  }
  4636  
  4637  func TestBincStdEncIntf(t *testing.T) {
  4638  	doTestStdEncIntf(t, testBincH)
  4639  }
  4640  
  4641  func TestSimpleCodecsTable(t *testing.T) {
  4642  	doTestCodecTableOne(t, testSimpleH)
  4643  }
  4644  
  4645  func TestSimpleCodecsMisc(t *testing.T) {
  4646  	doTestCodecMiscOne(t, testSimpleH)
  4647  }
  4648  
  4649  func TestSimpleCodecsEmbeddedPointer(t *testing.T) {
  4650  	doTestCodecEmbeddedPointer(t, testSimpleH)
  4651  }
  4652  
  4653  func TestSimpleStdEncIntf(t *testing.T) {
  4654  	doTestStdEncIntf(t, testSimpleH)
  4655  }
  4656  
  4657  func TestMsgpackCodecsTable(t *testing.T) {
  4658  	doTestCodecTableOne(t, testMsgpackH)
  4659  }
  4660  
  4661  func TestMsgpackCodecsMisc(t *testing.T) {
  4662  	doTestCodecMiscOne(t, testMsgpackH)
  4663  }
  4664  
  4665  func TestMsgpackCodecsEmbeddedPointer(t *testing.T) {
  4666  	doTestCodecEmbeddedPointer(t, testMsgpackH)
  4667  }
  4668  
  4669  func TestMsgpackStdEncIntf(t *testing.T) {
  4670  	doTestStdEncIntf(t, testMsgpackH)
  4671  }
  4672  
  4673  func TestCborCodecsTable(t *testing.T) {
  4674  	doTestCodecTableOne(t, testCborH)
  4675  }
  4676  
  4677  func TestCborCodecsMisc(t *testing.T) {
  4678  	doTestCodecMiscOne(t, testCborH)
  4679  }
  4680  
  4681  func TestCborCodecsEmbeddedPointer(t *testing.T) {
  4682  	doTestCodecEmbeddedPointer(t, testCborH)
  4683  }
  4684  
  4685  func TestCborCodecChan(t *testing.T) {
  4686  	doTestCodecChan(t, testCborH)
  4687  }
  4688  
  4689  func TestCborStdEncIntf(t *testing.T) {
  4690  	doTestStdEncIntf(t, testCborH)
  4691  }
  4692  
  4693  func TestJsonCodecsTable(t *testing.T) {
  4694  	doTestCodecTableOne(t, testJsonH)
  4695  }
  4696  
  4697  func TestJsonCodecsMisc(t *testing.T) {
  4698  	doTestCodecMiscOne(t, testJsonH)
  4699  }
  4700  
  4701  func TestJsonCodecsEmbeddedPointer(t *testing.T) {
  4702  	doTestCodecEmbeddedPointer(t, testJsonH)
  4703  }
  4704  
  4705  func TestJsonCodecChan(t *testing.T) {
  4706  	doTestCodecChan(t, testJsonH)
  4707  }
  4708  
  4709  func TestJsonStdEncIntf(t *testing.T) {
  4710  	doTestStdEncIntf(t, testJsonH)
  4711  }
  4712  
  4713  // ----- Raw ---------
  4714  func TestJsonRaw(t *testing.T) {
  4715  	doTestRawValue(t, testJsonH)
  4716  }
  4717  func TestBincRaw(t *testing.T) {
  4718  	doTestRawValue(t, testBincH)
  4719  }
  4720  func TestMsgpackRaw(t *testing.T) {
  4721  	doTestRawValue(t, testMsgpackH)
  4722  }
  4723  func TestSimpleRaw(t *testing.T) {
  4724  	doTestRawValue(t, testSimpleH)
  4725  }
  4726  func TestCborRaw(t *testing.T) {
  4727  	doTestRawValue(t, testCborH)
  4728  }
  4729  
  4730  // ----- ALL (framework based) -----
  4731  
  4732  func TestAllEncCircularRef(t *testing.T) {
  4733  	doTestEncCircularRef(t, testCborH)
  4734  }
  4735  
  4736  func TestAllAnonCycle(t *testing.T) {
  4737  	doTestAnonCycle(t, testCborH)
  4738  }
  4739  
  4740  func TestAllErrWriter(t *testing.T) {
  4741  	doTestAllErrWriter(t, testCborH, testJsonH)
  4742  }
  4743  
  4744  // ----- RPC custom -----
  4745  
  4746  func TestMsgpackRpcSpec(t *testing.T) {
  4747  	doTestCodecRpcOne(t, MsgpackSpecRpc, testMsgpackH, true, 0)
  4748  }
  4749  
  4750  // ----- RPC -----
  4751  
  4752  func TestBincRpcGo(t *testing.T) {
  4753  	doTestCodecRpcOne(t, GoRpc, testBincH, true, 0)
  4754  }
  4755  
  4756  func TestSimpleRpcGo(t *testing.T) {
  4757  	doTestCodecRpcOne(t, GoRpc, testSimpleH, true, 0)
  4758  }
  4759  
  4760  func TestMsgpackRpcGo(t *testing.T) {
  4761  	doTestCodecRpcOne(t, GoRpc, testMsgpackH, true, 0)
  4762  }
  4763  
  4764  func TestCborRpcGo(t *testing.T) {
  4765  	doTestCodecRpcOne(t, GoRpc, testCborH, true, 0)
  4766  }
  4767  
  4768  func TestJsonRpcGo(t *testing.T) {
  4769  	doTestCodecRpcOne(t, GoRpc, testJsonH, true, 0)
  4770  }
  4771  
  4772  // ----- OTHERS -----
  4773  
  4774  func TestBincMapEncodeForCanonical(t *testing.T) {
  4775  	t.Skipf("skipping ... needs investigation") // MARKER: testing fails??? Need to research
  4776  	doTestMapEncodeForCanonical(t, testBincH)
  4777  }
  4778  
  4779  func TestSimpleMapEncodeForCanonical(t *testing.T) {
  4780  	doTestMapEncodeForCanonical(t, testSimpleH)
  4781  }
  4782  
  4783  func TestMsgpackMapEncodeForCanonical(t *testing.T) {
  4784  	doTestMapEncodeForCanonical(t, testMsgpackH)
  4785  }
  4786  
  4787  func TestCborMapEncodeForCanonical(t *testing.T) {
  4788  	doTestMapEncodeForCanonical(t, testCborH)
  4789  }
  4790  
  4791  func TestJsonMapEncodeForCanonical(t *testing.T) {
  4792  	doTestMapEncodeForCanonical(t, testJsonH)
  4793  }
  4794  
  4795  func TestBincUnderlyingType(t *testing.T) {
  4796  	testCodecUnderlyingType(t, testBincH)
  4797  }
  4798  
  4799  func TestJsonSwallowAndZero(t *testing.T) {
  4800  	doTestSwallowAndZero(t, testJsonH)
  4801  }
  4802  
  4803  func TestCborSwallowAndZero(t *testing.T) {
  4804  	doTestSwallowAndZero(t, testCborH)
  4805  }
  4806  
  4807  func TestMsgpackSwallowAndZero(t *testing.T) {
  4808  	doTestSwallowAndZero(t, testMsgpackH)
  4809  }
  4810  
  4811  func TestBincSwallowAndZero(t *testing.T) {
  4812  	doTestSwallowAndZero(t, testBincH)
  4813  }
  4814  
  4815  func TestSimpleSwallowAndZero(t *testing.T) {
  4816  	doTestSwallowAndZero(t, testSimpleH)
  4817  }
  4818  
  4819  func TestJsonRawExt(t *testing.T) {
  4820  	doTestRawExt(t, testJsonH)
  4821  }
  4822  
  4823  func TestCborRawExt(t *testing.T) {
  4824  	doTestRawExt(t, testCborH)
  4825  }
  4826  
  4827  func TestMsgpackRawExt(t *testing.T) {
  4828  	doTestRawExt(t, testMsgpackH)
  4829  }
  4830  
  4831  func TestBincRawExt(t *testing.T) {
  4832  	doTestRawExt(t, testBincH)
  4833  }
  4834  
  4835  func TestSimpleRawExt(t *testing.T) {
  4836  	doTestRawExt(t, testSimpleH)
  4837  }
  4838  
  4839  func TestJsonMapStructKey(t *testing.T) {
  4840  	doTestMapStructKey(t, testJsonH)
  4841  }
  4842  
  4843  func TestCborMapStructKey(t *testing.T) {
  4844  	doTestMapStructKey(t, testCborH)
  4845  }
  4846  
  4847  func TestMsgpackMapStructKey(t *testing.T) {
  4848  	doTestMapStructKey(t, testMsgpackH)
  4849  }
  4850  
  4851  func TestBincMapStructKey(t *testing.T) {
  4852  	doTestMapStructKey(t, testBincH)
  4853  }
  4854  
  4855  func TestSimpleMapStructKey(t *testing.T) {
  4856  	doTestMapStructKey(t, testSimpleH)
  4857  }
  4858  
  4859  func TestJsonDecodeNilMapValue(t *testing.T) {
  4860  	doTestDecodeNilMapValue(t, testJsonH)
  4861  }
  4862  
  4863  func TestCborDecodeNilMapValue(t *testing.T) {
  4864  	doTestDecodeNilMapValue(t, testCborH)
  4865  }
  4866  
  4867  func TestMsgpackDecodeNilMapValue(t *testing.T) {
  4868  	doTestDecodeNilMapValue(t, testMsgpackH)
  4869  }
  4870  
  4871  func TestBincDecodeNilMapValue(t *testing.T) {
  4872  	doTestDecodeNilMapValue(t, testBincH)
  4873  }
  4874  
  4875  func TestSimpleDecodeNilMapValue(t *testing.T) {
  4876  	doTestDecodeNilMapValue(t, testSimpleH)
  4877  }
  4878  
  4879  func TestJsonEmbeddedFieldPrecedence(t *testing.T) {
  4880  	doTestEmbeddedFieldPrecedence(t, testJsonH)
  4881  }
  4882  
  4883  func TestCborEmbeddedFieldPrecedence(t *testing.T) {
  4884  	doTestEmbeddedFieldPrecedence(t, testCborH)
  4885  }
  4886  
  4887  func TestMsgpackEmbeddedFieldPrecedence(t *testing.T) {
  4888  	doTestEmbeddedFieldPrecedence(t, testMsgpackH)
  4889  }
  4890  
  4891  func TestBincEmbeddedFieldPrecedence(t *testing.T) {
  4892  	doTestEmbeddedFieldPrecedence(t, testBincH)
  4893  }
  4894  
  4895  func TestSimpleEmbeddedFieldPrecedence(t *testing.T) {
  4896  	doTestEmbeddedFieldPrecedence(t, testSimpleH)
  4897  }
  4898  
  4899  func TestJsonLargeContainerLen(t *testing.T) {
  4900  	doTestLargeContainerLen(t, testJsonH)
  4901  }
  4902  
  4903  func TestCborLargeContainerLen(t *testing.T) {
  4904  	doTestLargeContainerLen(t, testCborH)
  4905  }
  4906  
  4907  func TestMsgpackLargeContainerLen(t *testing.T) {
  4908  	doTestLargeContainerLen(t, testMsgpackH)
  4909  }
  4910  
  4911  func TestBincLargeContainerLen(t *testing.T) {
  4912  	doTestLargeContainerLen(t, testBincH)
  4913  }
  4914  
  4915  func TestSimpleLargeContainerLen(t *testing.T) {
  4916  	doTestLargeContainerLen(t, testSimpleH)
  4917  }
  4918  
  4919  func TestJsonTime(t *testing.T) {
  4920  	doTestTime(t, testJsonH)
  4921  }
  4922  
  4923  func TestCborTime(t *testing.T) {
  4924  	doTestTime(t, testCborH)
  4925  }
  4926  
  4927  func TestMsgpackTime(t *testing.T) {
  4928  	doTestTime(t, testMsgpackH)
  4929  }
  4930  
  4931  func TestBincTime(t *testing.T) {
  4932  	doTestTime(t, testBincH)
  4933  }
  4934  
  4935  func TestSimpleTime(t *testing.T) {
  4936  	doTestTime(t, testSimpleH)
  4937  }
  4938  
  4939  func TestJsonUintToInt(t *testing.T) {
  4940  	doTestUintToInt(t, testJsonH)
  4941  }
  4942  
  4943  func TestCborUintToInt(t *testing.T) {
  4944  	doTestUintToInt(t, testCborH)
  4945  }
  4946  
  4947  func TestMsgpackUintToInt(t *testing.T) {
  4948  	doTestUintToInt(t, testMsgpackH)
  4949  }
  4950  
  4951  func TestBincUintToInt(t *testing.T) {
  4952  	doTestUintToInt(t, testBincH)
  4953  }
  4954  
  4955  func TestSimpleUintToInt(t *testing.T) {
  4956  	doTestUintToInt(t, testSimpleH)
  4957  }
  4958  
  4959  func TestJsonDifferentMapOrSliceType(t *testing.T) {
  4960  	doTestDifferentMapOrSliceType(t, testJsonH)
  4961  }
  4962  
  4963  func TestCborDifferentMapOrSliceType(t *testing.T) {
  4964  	doTestDifferentMapOrSliceType(t, testCborH)
  4965  }
  4966  
  4967  func TestMsgpackDifferentMapOrSliceType(t *testing.T) {
  4968  	doTestDifferentMapOrSliceType(t, testMsgpackH)
  4969  }
  4970  
  4971  func TestBincDifferentMapOrSliceType(t *testing.T) {
  4972  	doTestDifferentMapOrSliceType(t, testBincH)
  4973  }
  4974  
  4975  func TestSimpleDifferentMapOrSliceType(t *testing.T) {
  4976  	doTestDifferentMapOrSliceType(t, testSimpleH)
  4977  }
  4978  
  4979  func TestJsonScalars(t *testing.T) {
  4980  	doTestScalars(t, testJsonH)
  4981  }
  4982  
  4983  func TestCborScalars(t *testing.T) {
  4984  	doTestScalars(t, testCborH)
  4985  }
  4986  
  4987  func TestMsgpackScalars(t *testing.T) {
  4988  	doTestScalars(t, testMsgpackH)
  4989  }
  4990  
  4991  func TestBincScalars(t *testing.T) {
  4992  	doTestScalars(t, testBincH)
  4993  }
  4994  
  4995  func TestSimpleScalars(t *testing.T) {
  4996  	doTestScalars(t, testSimpleH)
  4997  }
  4998  
  4999  func TestJsonOmitempty(t *testing.T) {
  5000  	doTestOmitempty(t, testJsonH)
  5001  }
  5002  
  5003  func TestCborOmitempty(t *testing.T) {
  5004  	doTestOmitempty(t, testCborH)
  5005  }
  5006  
  5007  func TestMsgpackOmitempty(t *testing.T) {
  5008  	doTestOmitempty(t, testMsgpackH)
  5009  }
  5010  
  5011  func TestBincOmitempty(t *testing.T) {
  5012  	doTestOmitempty(t, testBincH)
  5013  }
  5014  
  5015  func TestSimpleOmitempty(t *testing.T) {
  5016  	doTestOmitempty(t, testSimpleH)
  5017  }
  5018  
  5019  func TestJsonIntfMapping(t *testing.T) {
  5020  	doTestIntfMapping(t, testJsonH)
  5021  }
  5022  
  5023  func TestCborIntfMapping(t *testing.T) {
  5024  	doTestIntfMapping(t, testCborH)
  5025  }
  5026  
  5027  func TestMsgpackIntfMapping(t *testing.T) {
  5028  	doTestIntfMapping(t, testMsgpackH)
  5029  }
  5030  
  5031  func TestBincIntfMapping(t *testing.T) {
  5032  	doTestIntfMapping(t, testBincH)
  5033  }
  5034  
  5035  func TestSimpleIntfMapping(t *testing.T) {
  5036  	doTestIntfMapping(t, testSimpleH)
  5037  }
  5038  
  5039  func TestJsonMissingFields(t *testing.T) {
  5040  	doTestMissingFields(t, testJsonH)
  5041  }
  5042  
  5043  func TestCborMissingFields(t *testing.T) {
  5044  	doTestMissingFields(t, testCborH)
  5045  }
  5046  
  5047  func TestMsgpackMissingFields(t *testing.T) {
  5048  	doTestMissingFields(t, testMsgpackH)
  5049  }
  5050  
  5051  func TestBincMissingFields(t *testing.T) {
  5052  	doTestMissingFields(t, testBincH)
  5053  }
  5054  
  5055  func TestSimpleMissingFields(t *testing.T) {
  5056  	doTestMissingFields(t, testSimpleH)
  5057  }
  5058  
  5059  func TestJsonMaxDepth(t *testing.T) {
  5060  	doTestMaxDepth(t, testJsonH)
  5061  }
  5062  
  5063  func TestCborMaxDepth(t *testing.T) {
  5064  	doTestMaxDepth(t, testCborH)
  5065  }
  5066  
  5067  func TestMsgpackMaxDepth(t *testing.T) {
  5068  	doTestMaxDepth(t, testMsgpackH)
  5069  }
  5070  
  5071  func TestBincMaxDepth(t *testing.T) {
  5072  	doTestMaxDepth(t, testBincH)
  5073  }
  5074  
  5075  func TestSimpleMaxDepth(t *testing.T) {
  5076  	doTestMaxDepth(t, testSimpleH)
  5077  }
  5078  
  5079  func TestJsonSelfExt(t *testing.T) {
  5080  	doTestSelfExt(t, testJsonH)
  5081  }
  5082  
  5083  func TestCborSelfExt(t *testing.T) {
  5084  	doTestSelfExt(t, testCborH)
  5085  }
  5086  
  5087  func TestMsgpackSelfExt(t *testing.T) {
  5088  	doTestSelfExt(t, testMsgpackH)
  5089  }
  5090  
  5091  func TestBincSelfExt(t *testing.T) {
  5092  	doTestSelfExt(t, testBincH)
  5093  }
  5094  
  5095  func TestSimpleSelfExt(t *testing.T) {
  5096  	doTestSelfExt(t, testSimpleH)
  5097  }
  5098  
  5099  func TestJsonBytesEncodedAsArray(t *testing.T) {
  5100  	doTestBytesEncodedAsArray(t, testJsonH)
  5101  }
  5102  
  5103  func TestCborBytesEncodedAsArray(t *testing.T) {
  5104  	doTestBytesEncodedAsArray(t, testCborH)
  5105  }
  5106  
  5107  func TestMsgpackBytesEncodedAsArray(t *testing.T) {
  5108  	doTestBytesEncodedAsArray(t, testMsgpackH)
  5109  }
  5110  
  5111  func TestBincBytesEncodedAsArray(t *testing.T) {
  5112  	doTestBytesEncodedAsArray(t, testBincH)
  5113  }
  5114  
  5115  func TestSimpleBytesEncodedAsArray(t *testing.T) {
  5116  	doTestBytesEncodedAsArray(t, testSimpleH)
  5117  }
  5118  
  5119  func TestJsonStrucEncDec(t *testing.T) {
  5120  	doTestStrucEncDec(t, testJsonH)
  5121  }
  5122  
  5123  func TestCborStrucEncDec(t *testing.T) {
  5124  	doTestStrucEncDec(t, testCborH)
  5125  }
  5126  
  5127  func TestMsgpackStrucEncDec(t *testing.T) {
  5128  	doTestStrucEncDec(t, testMsgpackH)
  5129  }
  5130  
  5131  func TestBincStrucEncDec(t *testing.T) {
  5132  	doTestStrucEncDec(t, testBincH)
  5133  }
  5134  
  5135  func TestSimpleStrucEncDec(t *testing.T) {
  5136  	doTestStrucEncDec(t, testSimpleH)
  5137  }
  5138  
  5139  func TestJsonRawToStringToRawEtc(t *testing.T) {
  5140  	doTestRawToStringToRawEtc(t, testJsonH)
  5141  }
  5142  
  5143  func TestCborRawToStringToRawEtc(t *testing.T) {
  5144  	doTestRawToStringToRawEtc(t, testCborH)
  5145  }
  5146  
  5147  func TestMsgpackRawToStringToRawEtc(t *testing.T) {
  5148  	doTestRawToStringToRawEtc(t, testMsgpackH)
  5149  }
  5150  
  5151  func TestBincRawToStringToRawEtc(t *testing.T) {
  5152  	doTestRawToStringToRawEtc(t, testBincH)
  5153  }
  5154  
  5155  func TestSimpleRawToStringToRawEtc(t *testing.T) {
  5156  	doTestRawToStringToRawEtc(t, testSimpleH)
  5157  }
  5158  
  5159  func TestJsonStructKeyType(t *testing.T) {
  5160  	doTestStructKeyType(t, testJsonH)
  5161  }
  5162  
  5163  func TestCborStructKeyType(t *testing.T) {
  5164  	doTestStructKeyType(t, testCborH)
  5165  }
  5166  
  5167  func TestMsgpackStructKeyType(t *testing.T) {
  5168  	doTestStructKeyType(t, testMsgpackH)
  5169  }
  5170  
  5171  func TestBincStructKeyType(t *testing.T) {
  5172  	doTestStructKeyType(t, testBincH)
  5173  }
  5174  
  5175  func TestSimpleStructKeyType(t *testing.T) {
  5176  	doTestStructKeyType(t, testSimpleH)
  5177  }
  5178  
  5179  func TestJsonPreferArrayOverSlice(t *testing.T) {
  5180  	doTestPreferArrayOverSlice(t, testJsonH)
  5181  }
  5182  
  5183  func TestCborPreferArrayOverSlice(t *testing.T) {
  5184  	doTestPreferArrayOverSlice(t, testCborH)
  5185  }
  5186  
  5187  func TestMsgpackPreferArrayOverSlice(t *testing.T) {
  5188  	doTestPreferArrayOverSlice(t, testMsgpackH)
  5189  }
  5190  
  5191  func TestBincPreferArrayOverSlice(t *testing.T) {
  5192  	doTestPreferArrayOverSlice(t, testBincH)
  5193  }
  5194  
  5195  func TestSimplePreferArrayOverSlice(t *testing.T) {
  5196  	doTestPreferArrayOverSlice(t, testSimpleH)
  5197  }
  5198  
  5199  func TestJsonZeroCopyBytes(t *testing.T) {
  5200  	doTestZeroCopyBytes(t, testJsonH)
  5201  }
  5202  
  5203  func TestCborZeroCopyBytes(t *testing.T) {
  5204  	doTestZeroCopyBytes(t, testCborH)
  5205  }
  5206  
  5207  func TestMsgpackZeroCopyBytes(t *testing.T) {
  5208  	doTestZeroCopyBytes(t, testMsgpackH)
  5209  }
  5210  
  5211  func TestBincZeroCopyBytes(t *testing.T) {
  5212  	doTestZeroCopyBytes(t, testBincH)
  5213  }
  5214  
  5215  func TestSimpleZeroCopyBytes(t *testing.T) {
  5216  	doTestZeroCopyBytes(t, testSimpleH)
  5217  }
  5218  
  5219  func TestJsonNextValueBytes(t *testing.T) {
  5220  	doTestNextValueBytes(t, testJsonH)
  5221  }
  5222  
  5223  func TestCborNextValueBytes(t *testing.T) {
  5224  	// x := testCborH.IndefiniteLength
  5225  	// defer func() { testCborH.IndefiniteLength = x }()
  5226  
  5227  	// xdebugf(">>>>> TestCborNextValueBytes: IndefiniteLength = false")
  5228  	// testCborH.IndefiniteLength = false
  5229  	// doTestNextValueBytes(t, testCborH)
  5230  	// xdebugf(">>>>> TestCborNextValueBytes: IndefiniteLength = true")
  5231  	// testCborH.IndefiniteLength = true
  5232  	doTestNextValueBytes(t, testCborH)
  5233  }
  5234  
  5235  func TestMsgpackNextValueBytes(t *testing.T) {
  5236  	doTestNextValueBytes(t, testMsgpackH)
  5237  }
  5238  
  5239  func TestBincNextValueBytes(t *testing.T) {
  5240  	doTestNextValueBytes(t, testBincH)
  5241  }
  5242  
  5243  func TestSimpleNextValueBytes(t *testing.T) {
  5244  	doTestNextValueBytes(t, testSimpleH)
  5245  }
  5246  
  5247  func TestJsonNumbers(t *testing.T) {
  5248  	doTestNumbers(t, testJsonH)
  5249  }
  5250  
  5251  func TestCborNumbers(t *testing.T) {
  5252  	doTestNumbers(t, testCborH)
  5253  }
  5254  
  5255  func TestMsgpackNumbers(t *testing.T) {
  5256  	doTestNumbers(t, testMsgpackH)
  5257  }
  5258  
  5259  func TestBincNumbers(t *testing.T) {
  5260  	doTestNumbers(t, testBincH)
  5261  }
  5262  
  5263  func TestSimpleNumbers(t *testing.T) {
  5264  	doTestNumbers(t, testSimpleH)
  5265  }
  5266  
  5267  func TestJsonDesc(t *testing.T) {
  5268  	doTestDesc(t, testJsonH, map[byte]string{'"': `"`, '{': `{`, '}': `}`, '[': `[`, ']': `]`})
  5269  }
  5270  
  5271  func TestCborDesc(t *testing.T) {
  5272  	m := make(map[byte]string)
  5273  	for k, v := range cbordescMajorNames {
  5274  		// if k == cborMajorSimpleOrFloat { m[k<<5] = "nil" }
  5275  		m[k<<5] = v
  5276  	}
  5277  	for k, v := range cbordescSimpleNames {
  5278  		m[k] = v
  5279  	}
  5280  	delete(m, cborMajorSimpleOrFloat<<5)
  5281  	doTestDesc(t, testCborH, m)
  5282  }
  5283  
  5284  func TestMsgpackDesc(t *testing.T) {
  5285  	m := make(map[byte]string)
  5286  	for k, v := range mpdescNames {
  5287  		m[k] = v
  5288  	}
  5289  	m[mpPosFixNumMin] = "int"
  5290  	m[mpFixStrMin] = "string|bytes"
  5291  	m[mpFixArrayMin] = "array"
  5292  	m[mpFixMapMin] = "map"
  5293  	m[mpFixExt1] = "ext"
  5294  
  5295  	doTestDesc(t, testMsgpackH, m)
  5296  }
  5297  
  5298  func TestBincDesc(t *testing.T) {
  5299  	m := make(map[byte]string)
  5300  	for k, v := range bincdescVdNames {
  5301  		m[k<<4] = v
  5302  	}
  5303  	for k, v := range bincdescSpecialVsNames {
  5304  		m[k] = v
  5305  	}
  5306  	delete(m, bincVdSpecial<<4)
  5307  	doTestDesc(t, testBincH, m)
  5308  }
  5309  
  5310  func TestSimpleDesc(t *testing.T) {
  5311  	doTestDesc(t, testSimpleH, simpledescNames)
  5312  }
  5313  
  5314  func TestJsonStructFieldInfoToArray(t *testing.T) {
  5315  	doTestStructFieldInfoToArray(t, testJsonH)
  5316  }
  5317  
  5318  func TestCborStructFieldInfoToArray(t *testing.T) {
  5319  	doTestStructFieldInfoToArray(t, testCborH)
  5320  }
  5321  
  5322  func TestMsgpackStructFieldInfoToArray(t *testing.T) {
  5323  	doTestStructFieldInfoToArray(t, testMsgpackH)
  5324  }
  5325  
  5326  func TestBincStructFieldInfoToArray(t *testing.T) {
  5327  	doTestStructFieldInfoToArray(t, testBincH)
  5328  }
  5329  
  5330  func TestSimpleStructFieldInfoToArray(t *testing.T) {
  5331  	doTestStructFieldInfoToArray(t, testSimpleH)
  5332  }
  5333  
  5334  // --------
  5335  
  5336  func TestMultipleEncDec(t *testing.T) {
  5337  	doTestMultipleEncDec(t, testJsonH)
  5338  }
  5339  
  5340  func TestJsonEncodeIndent(t *testing.T) {
  5341  	doTestJsonEncodeIndent(t, testJsonH)
  5342  }
  5343  
  5344  func TestJsonDecodeNonStringScalarInStringContext(t *testing.T) {
  5345  	doTestJsonDecodeNonStringScalarInStringContext(t, testJsonH)
  5346  }
  5347  
  5348  func TestJsonLargeInteger(t *testing.T) {
  5349  	doTestJsonLargeInteger(t, testJsonH)
  5350  }
  5351  
  5352  func TestJsonInvalidUnicode(t *testing.T) {
  5353  	doTestJsonInvalidUnicode(t, testJsonH)
  5354  }
  5355  
  5356  func TestJsonNumberParsing(t *testing.T) {
  5357  	doTestJsonNumberParsing(t, testJsonH)
  5358  }
  5359  
  5360  func TestMsgpackDecodeMapAndExtSizeMismatch(t *testing.T) {
  5361  	doTestMsgpackDecodeMapAndExtSizeMismatch(t, testMsgpackH)
  5362  }