func TestPutReflect(t *testing.T) { var a1 int16 = 42 var a2 float32 = 9.2 expected := make([]byte, 6) lex.PutInt16(expected, a1) lex.PutFloat32(expected[2:], a2) actual := make([]byte, 6) lex.PutReflect(actual, a1) lex.PutReflect(actual[2:], a2) assert.True(t, bytes.Equal(expected, actual)) }
func BenchmarkPutReflectInt(b *testing.B) { v := 64 bs := make([]byte, 8) for n := 0; n < b.N; n++ { lex.PutReflect(bs, v) } }
func BenchmarkPutReflectString(b *testing.B) { s := "hello world" bs := make([]byte, 12) for n := 0; n < b.N; n++ { lex.PutReflect(bs, s) } }
func TestSizeReflectPutReflect_invalidnilptr(t *testing.T) { b := make([]byte, 8) var m map[string]int assert.Equal(t, -1, lex.Size(&m)) lex.Reflect(b, &m) lex.PutReflect(b, &m) }
func TestSizeReflectPutReflect_nilptr(t *testing.T) { b := make([]byte, 8) var p *int = nil assert.Equal(t, -1, lex.Size(p)) lex.Reflect(b, p) lex.PutReflect(b, p) }
func TestPutReflect_alias(t *testing.T) { var a1 aliasedInt = 42 expected := make([]byte, 8) lex.PutInt(expected, int(a1)) actual := make([]byte, 8) lex.PutReflect(actual, a1) assert.True(t, bytes.Equal(expected, actual)) }
func TestPutReflect_struct_invalid(t *testing.T) { var tests = []interface{}{ invalidStruct{}, struct{}{}, time.Time{}, } b := make([]byte, 16) for _, tt := range tests { err := lex.PutReflect(b, tt) assert.NotNil(t, err) } }
func TestPutReflect_struct(t *testing.T) { var a1 testStruct = testStruct{42, "hello", 12.5} expected := make([]byte, 18) lex.PutInt(expected, a1.A) lex.PutString(expected[8:], a1.B) lex.PutFloat32(expected[14:], a1.C) actual := make([]byte, 18) lex.PutReflect(actual, a1) assert.True(t, bytes.Equal(expected, actual)) }
func TestSizeReflectPutReflect_nil(t *testing.T) { b := make([]byte, 8) assert.Equal(t, -1, lex.Size(nil)) lex.Reflect(b, nil) lex.PutReflect(b, nil) }
func TestSizeReflectPutReflect(t *testing.T) { v01 := string("howdy") v02 := bool(true) v03 := int8(2) v04 := int16(3) v05 := int32(5) v06 := int64(8) v07 := uint8(2) v08 := uint16(3) v09 := uint32(5) v10 := uint64(8) v11 := float32(23) v12 := float64(46) v13 := complex(float32(1), float32(2)) v14 := complex(float64(1), float64(2)) v15 := byte(24) v16 := rune(24) v17 := int(42) v18 := uint(42) a01 := mystring("howdy") a02 := mybool(true) a03 := myint8(2) a04 := myint16(3) a05 := myint32(5) a06 := myint64(8) a07 := myuint8(2) a08 := myuint16(3) a09 := myuint32(5) a10 := myuint64(8) a11 := myfloat32(23) a12 := myfloat64(46) a13 := mycomplex64(complex(float32(1), float32(2))) a14 := mycomplex128(complex(float64(1), float64(2))) a15 := mybyte(24) a16 := myrune(24) a17 := myint(42) a18 := myuint(42) s01 := mystruct1{24, 42} s02 := mystruct2{1.5, 5.0} s03 := mystruct3{ mystruct1{1, 2}, mystruct2{3.0, 4.0}, } var tests = []struct { val, ptr interface{} size int }{ //builtin {v01, &v01, len(v01) + 1}, {v02, &v02, 1}, {v03, &v03, 1}, {v04, &v04, 2}, {v05, &v05, 4}, {v06, &v06, 8}, {v07, &v07, 1}, {v08, &v08, 2}, {v09, &v09, 4}, {v10, &v10, 8}, {v11, &v11, 4}, {v12, &v12, 8}, {v13, &v13, 8}, {v14, &v14, 16}, {v15, &v15, 1}, {v16, &v16, 4}, {v17, &v17, 8}, {v18, &v18, 8}, //aliased {a01, &a01, len(a01) + 1}, {a02, &a02, 1}, {a03, &a03, 1}, {a04, &a04, 2}, {a05, &a05, 4}, {a06, &a06, 8}, {a07, &a07, 1}, {a08, &a08, 2}, {a09, &a09, 4}, {a10, &a10, 8}, {a11, &a11, 4}, {a12, &a12, 8}, {a13, &a13, 8}, {a14, &a14, 16}, {a15, &a15, 1}, {a16, &a16, 4}, {a17, &a17, 8}, {a18, &a18, 8}, //structs {s01, &s01, 8}, {s02, &s02, 8}, {s03, &s03, 16}, } for _, tt := range tests { assert.Equal(t, tt.size, lex.Size(tt.val), "(%v) %v", reflect.ValueOf(tt.val).Kind(), tt.val) assert.Equal(t, tt.size, lex.Size(tt.ptr), "(%v ptr)", reflect.ValueOf(tt.val).Kind()) b1 := make([]byte, tt.size) b2 := make([]byte, tt.size) lex.PutReflect(b1, tt.val) lex.Reflect(b2, tt.ptr) //underneath, val == *ptr, so we need to change the value of ptr lex.PutReflect(b2, tt.ptr) //we're cheating a little and just using Reflect/PutReflect as a means assert.False(t, bytes.Equal(b1, b2)) //to change the underlying value, avoiding a bunch of type checks lex.Reflect(b1, tt.ptr) lex.PutReflect(b2, tt.ptr) assert.True(t, bytes.Equal(b1, b2)) } }