Exemple #1
0
func TestReflect(t *testing.T) {
	var expected1, actual1 int16 = 42, 0
	var expected2, actual2 float32 = 9.2, 0

	b := make([]byte, 6)
	lex.PutInt16(b, expected1)
	lex.PutFloat32(b[2:], expected2)

	lex.Reflect(b, &actual1)
	lex.Reflect(b[2:], &actual2)

	assert.Equal(t, expected1, actual1)
	assert.Equal(t, expected2, actual2)
}
Exemple #2
0
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)
}
Exemple #3
0
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)
}
Exemple #4
0
func TestReflect_string(t *testing.T) {
	var expected, actual string = "howdy", ""

	b := make([]byte, 6)
	lex.PutString(b, expected)

	lex.Reflect(b, &actual)

	assert.Equal(t, expected, actual)
}
Exemple #5
0
func TestReflect_notptr(t *testing.T) {
	var v, actual int = 42, 0

	b := make([]byte, 8)
	lex.PutInt(b, v)

	lex.Reflect(b, actual)

	assert.Equal(t, 0, actual)
}
Exemple #6
0
func TestReflect_alias(t *testing.T) {
	var expected, actual aliasedInt = 42, 0

	b := make([]byte, 8)
	lex.PutInt(b, int(expected))

	lex.Reflect(b, &actual)

	assert.Equal(t, expected, actual)
}
Exemple #7
0
func TestReflect_struct_invalid(t *testing.T) {
	var tests = []interface{}{
		&invalidStruct{},
		&struct{}{},
		&time.Time{},
	}
	b := make([]byte, 16)
	lex.PutInt(b, 42) //just something to decode
	for _, tt := range tests {
		err := lex.Reflect(b, tt)
		assert.NotNil(t, err)
	}
}
Exemple #8
0
func TestReflect_struct(t *testing.T) {
	var expected testStruct = testStruct{42, "hello", 12.5}
	var actual testStruct

	b := make([]byte, 18)
	lex.PutInt(b, expected.A)
	lex.PutString(b[8:], expected.B)
	lex.PutFloat32(b[14:], expected.C)

	lex.Reflect(b, &actual)

	assert.Equal(t, expected, actual)
}
Exemple #9
0
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)
}
Exemple #10
0
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))
	}
}