diff --git a/codec_test.go b/codec_test.go index a36aa6d..5067234 100644 --- a/codec_test.go +++ b/codec_test.go @@ -70,48 +70,96 @@ func TestMarshalInt32Slice(t *testing.T) { }) } +func TestMarshalInt32SliceNil(t *testing.T) { + testMarshalBasicSlice(t, []int32{}, func(v []byte) (interface{}, error) { + return ToInt32Slice(v) + }) +} + func TestMarshalUInt32Slice(t *testing.T) { testMarshalBasicSlice(t, []uint32{11, 22}, func(v []byte) (interface{}, error) { return ToUInt32Slice(v) }) } +func TestMarshalUInt32SliceNil(t *testing.T) { + testMarshalBasicSlice(t, []uint32{}, func(v []byte) (interface{}, error) { + return ToUInt32Slice(v) + }) +} + func TestMarshalInt64Slice(t *testing.T) { testMarshalBasicSlice(t, []int64{-4097, 255}, func(v []byte) (interface{}, error) { return ToInt64Slice(v) }) } +func TestMarshalInt64SliceNil(t *testing.T) { + testMarshalBasicSlice(t, []int64{}, func(v []byte) (interface{}, error) { + return ToInt64Slice(v) + }) +} + func TestMarshalUInt64Slice(t *testing.T) { testMarshalBasicSlice(t, []uint64{0, 1}, func(v []byte) (interface{}, error) { return ToUInt64Slice(v) }) } +func TestMarshalUInt64SliceNil(t *testing.T) { + testMarshalBasicSlice(t, []uint64{}, func(v []byte) (interface{}, error) { + return ToUInt64Slice(v) + }) +} + func TestMarshalFloat32Slice(t *testing.T) { testMarshalBasicSlice(t, []float32{0.25, 0.375}, func(v []byte) (interface{}, error) { return ToFloat32Slice(v) }) } +func TestMarshalFloat32SliceNil(t *testing.T) { + testMarshalBasicSlice(t, []float32{}, func(v []byte) (interface{}, error) { + return ToFloat32Slice(v) + }) +} + func TestMarshalFloat64Slice(t *testing.T) { testMarshalBasicSlice(t, []float64{0.12, 0.45}, func(v []byte) (interface{}, error) { return ToFloat64Slice(v) }) } +func TestMarshalFloat64SliceNil(t *testing.T) { + testMarshalBasicSlice(t, []float64{}, func(v []byte) (interface{}, error) { + return ToFloat64Slice(v) + }) +} + func TestMarshalBoolSlice(t *testing.T) { testMarshalBasicSlice(t, []bool{true, false}, func(v []byte) (interface{}, error) { return ToBoolSlice(v) }) } +func TestMarshalBoolSliceNil(t *testing.T) { + testMarshalBasicSlice(t, []bool{}, func(v []byte) (interface{}, error) { + return ToBoolSlice(v) + }) +} + func TestMarshalUTF8StringSlice(t *testing.T) { testMarshalBasicSlice(t, []string{"a", "b"}, func(v []byte) (interface{}, error) { return ToUTF8StringSlice(v) }) } +func TestMarshalUTF8StringSliceNil(t *testing.T) { + testMarshalBasicSlice(t, []string{}, func(v []byte) (interface{}, error) { + return ToUTF8StringSlice(v) + }) +} + func testMarshalBasicSlice(t *testing.T, expected interface{}, converter func(v []byte) (interface{}, error)) { flag := false @@ -263,6 +311,32 @@ func TestMarshalObjectSlice(t *testing.T) { } } +func TestMarshalObjectSliceNil(t *testing.T) { + flag := false + input := exampleSliceNil{ + Names: []string{}, + } + + codec := NewCodec(0x30) + inputBuf, _ := codec.Marshal(input) + testPrintf("inputBuf=%v\n", utils.FormatBytes(inputBuf)) + + testDecoder(0x10, inputBuf, func(v []byte) (interface{}, error) { + flag = true + testPrintf("v=%#x\n", v) + var mold []string + err := ToObject(v, &mold) + assert.NoError(t, err, fmt.Sprintf("decode error:%v", err)) + testPrintf("mold=%v\n", mold) + assert.Equal(t, 0, len(mold), fmt.Sprintf("value len does not match(%v): %v", 0, len(mold))) + return mold, err + }) + + if !flag { + t.Errorf("Observable does not listen to values") + } +} + type exampleData struct { Name string `y3:"0x10"` Noise float32 `y3:"0x11"` @@ -277,3 +351,7 @@ type thermometer struct { type exampleSlice struct { Therms []thermometer `y3:"0x12"` } + +type exampleSliceNil struct { + Names []string `y3:"0x10"` +}