Skip to content

Commit

Permalink
add more decoding slice tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shamaton committed Aug 8, 2024
1 parent 5dc8e94 commit 62005b8
Showing 1 changed file with 384 additions and 0 deletions.
384 changes: 384 additions & 0 deletions internal/stream/decoding/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,134 @@ func Test_asFixedSlice_Int(t *testing.T) {
tu.EqualSlice(t, *v1, []int{3})
}

func Test_asFixedSlice_Int8(t *testing.T) {
run := func(t *testing.T, v any) {
method := func(d *decoder) (bool, error) {
rv := reflect.ValueOf(v)
return d.asFixedSlice(rv.Elem(), 1)
}

testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{},
Error: io.EOF,
MethodAsCustom: method,
},
{
Name: "ok",
Data: []byte{def.PositiveFixIntMin + 4},
Expected: true,
ReadCount: 1,
MethodAsCustom: method,
},
}
for _, tc := range testcases {
tc.Run(t)
}
}

v1 := new([]int8)
run(t, v1)
tu.EqualSlice(t, *v1, []int8{4})
}

func Test_asFixedSlice_Int16(t *testing.T) {
run := func(t *testing.T, v any) {
method := func(d *decoder) (bool, error) {
rv := reflect.ValueOf(v)
return d.asFixedSlice(rv.Elem(), 1)
}

testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{},
Error: io.EOF,
MethodAsCustom: method,
},
{
Name: "ok",
Data: []byte{def.PositiveFixIntMin + 5},
Expected: true,
ReadCount: 1,
MethodAsCustom: method,
},
}
for _, tc := range testcases {
tc.Run(t)
}
}

v1 := new([]int16)
run(t, v1)
tu.EqualSlice(t, *v1, []int16{5})
}

func Test_asFixedSlice_Int32(t *testing.T) {
run := func(t *testing.T, v any) {
method := func(d *decoder) (bool, error) {
rv := reflect.ValueOf(v)
return d.asFixedSlice(rv.Elem(), 1)
}

testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{},
Error: io.EOF,
MethodAsCustom: method,
},
{
Name: "ok",
Data: []byte{def.PositiveFixIntMin + 6},
Expected: true,
ReadCount: 1,
MethodAsCustom: method,
},
}
for _, tc := range testcases {
tc.Run(t)
}
}

v1 := new([]int32)
run(t, v1)
tu.EqualSlice(t, *v1, []int32{6})
}

func Test_asFixedSlice_Int64(t *testing.T) {
run := func(t *testing.T, v any) {
method := func(d *decoder) (bool, error) {
rv := reflect.ValueOf(v)
return d.asFixedSlice(rv.Elem(), 1)
}

testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{},
Error: io.EOF,
MethodAsCustom: method,
},
{
Name: "ok",
Data: []byte{def.PositiveFixIntMin + 7},
Expected: true,
ReadCount: 1,
MethodAsCustom: method,
},
}
for _, tc := range testcases {
tc.Run(t)
}
}

v1 := new([]int64)
run(t, v1)
tu.EqualSlice(t, *v1, []int64{7})
}

func Test_asFixedSlice_Uint(t *testing.T) {
run := func(t *testing.T, v any) {
method := func(d *decoder) (bool, error) {
Expand Down Expand Up @@ -129,3 +257,259 @@ func Test_asFixedSlice_Uint(t *testing.T) {
run(t, v1)
tu.EqualSlice(t, *v1, []uint{5})
}

func Test_asFixedSlice_Uint8(t *testing.T) {
run := func(t *testing.T, v any) {
method := func(d *decoder) (bool, error) {
rv := reflect.ValueOf(v)
return d.asFixedSlice(rv.Elem(), 1)
}

testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{},
Error: io.EOF,
MethodAsCustom: method,
},
{
Name: "ok",
Data: []byte{def.PositiveFixIntMin + 6},
Expected: true,
ReadCount: 1,
MethodAsCustom: method,
},
}
for _, tc := range testcases {
tc.Run(t)
}
}

v1 := new([]uint8)
run(t, v1)
tu.EqualSlice(t, *v1, []uint8{6})
}

func Test_asFixedSlice_Uint16(t *testing.T) {
run := func(t *testing.T, v any) {
method := func(d *decoder) (bool, error) {
rv := reflect.ValueOf(v)
return d.asFixedSlice(rv.Elem(), 1)
}

testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{},
Error: io.EOF,
MethodAsCustom: method,
},
{
Name: "ok",
Data: []byte{def.PositiveFixIntMin + 7},
Expected: true,
ReadCount: 1,
MethodAsCustom: method,
},
}
for _, tc := range testcases {
tc.Run(t)
}
}

v1 := new([]uint16)
run(t, v1)
tu.EqualSlice(t, *v1, []uint16{7})
}

func Test_asFixedSlice_Uint32(t *testing.T) {
run := func(t *testing.T, v any) {
method := func(d *decoder) (bool, error) {
rv := reflect.ValueOf(v)
return d.asFixedSlice(rv.Elem(), 1)
}

testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{},
Error: io.EOF,
MethodAsCustom: method,
},
{
Name: "ok",
Data: []byte{def.PositiveFixIntMin + 8},
Expected: true,
ReadCount: 1,
MethodAsCustom: method,
},
}
for _, tc := range testcases {
tc.Run(t)
}
}

v1 := new([]uint32)
run(t, v1)
tu.EqualSlice(t, *v1, []uint32{8})
}

func Test_asFixedSlice_Uint64(t *testing.T) {
run := func(t *testing.T, v any) {
method := func(d *decoder) (bool, error) {
rv := reflect.ValueOf(v)
return d.asFixedSlice(rv.Elem(), 1)
}

testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{},
Error: io.EOF,
MethodAsCustom: method,
},
{
Name: "ok",
Data: []byte{def.PositiveFixIntMin + 9},
Expected: true,
ReadCount: 1,
MethodAsCustom: method,
},
}
for _, tc := range testcases {
tc.Run(t)
}
}

v1 := new([]uint64)
run(t, v1)
tu.EqualSlice(t, *v1, []uint64{9})
}

func Test_asFixedSlice_Float32(t *testing.T) {
run := func(t *testing.T, v any) {
method := func(d *decoder) (bool, error) {
rv := reflect.ValueOf(v)
return d.asFixedSlice(rv.Elem(), 1)
}

testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{},
Error: io.EOF,
MethodAsCustom: method,
},
{
Name: "ok",
Data: []byte{def.Float32, 63, 128, 0, 0},
Expected: true,
ReadCount: 2,
MethodAsCustom: method,
},
}
for _, tc := range testcases {
tc.Run(t)
}
}

v1 := new([]float32)
run(t, v1)
tu.EqualSlice(t, *v1, []float32{1})
}

func Test_asFixedSlice_Float64(t *testing.T) {
run := func(t *testing.T, v any) {
method := func(d *decoder) (bool, error) {
rv := reflect.ValueOf(v)
return d.asFixedSlice(rv.Elem(), 1)
}

testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{},
Error: io.EOF,
MethodAsCustom: method,
},
{
Name: "ok",
Data: []byte{def.Float64, 63, 240, 0, 0, 0, 0, 0, 0},
Expected: true,
ReadCount: 2,
MethodAsCustom: method,
},
}
for _, tc := range testcases {
tc.Run(t)
}
}

v1 := new([]float64)
run(t, v1)
tu.EqualSlice(t, *v1, []float64{1})
}

func Test_asFixedSlice_String(t *testing.T) {
run := func(t *testing.T, v any) {
method := func(d *decoder) (bool, error) {
rv := reflect.ValueOf(v)
return d.asFixedSlice(rv.Elem(), 2)
}

testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{},
Error: io.EOF,
MethodAsCustom: method,
},
{
Name: "ok",
Data: []byte{def.FixStr + 1, 'a', def.FixStr + 1, 'b'},
Expected: true,
ReadCount: 4,
MethodAsCustom: method,
},
}
for _, tc := range testcases {
tc.Run(t)
}
}

v1 := new([]string)
run(t, v1)
tu.EqualSlice(t, *v1, []string{"a", "b"})
}

func Test_asFixedSlice_Bool(t *testing.T) {
run := func(t *testing.T, v any) {
method := func(d *decoder) (bool, error) {
rv := reflect.ValueOf(v)
return d.asFixedSlice(rv.Elem(), 1)
}

testcases := AsXXXTestCases[bool]{
{
Name: "error",
Data: []byte{},
Error: io.EOF,
MethodAsCustom: method,
},
{
Name: "ok",
Data: []byte{def.True},
Expected: true,
ReadCount: 1,
MethodAsCustom: method,
},
}
for _, tc := range testcases {
tc.Run(t)
}
}

v1 := new([]bool)
run(t, v1)
tu.EqualSlice(t, *v1, []bool{true})
}

0 comments on commit 62005b8

Please sign in to comment.