Skip to content

Commit

Permalink
add decoding slice tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shamaton committed Aug 8, 2024
1 parent 57e5210 commit 5dc8e94
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 16 deletions.
4 changes: 3 additions & 1 deletion internal/common/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ func EqualSlice[T comparable](t *testing.T, actual, expected []T) {
t.Fatalf("diffrent length. actual: %v, expected: %v", actual, expected)
}
for i := range actual {
Equal[T](t, actual[i], expected[i])
if !reflect.DeepEqual(actual[i], expected[i]) {
t.Fatalf("not equal. actual: %v, expected: %v", actual, expected)
}
}
}

Expand Down
16 changes: 1 addition & 15 deletions internal/stream/decoding/map_test.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package decoding

import (
"bytes"
"fmt"
"io"
"math"
"reflect"
"testing"

tu "github.com/shamaton/msgpack/v2/internal/common/testutil"
"github.com/shamaton/msgpack/v2/internal/stream/encoding"

"github.com/shamaton/msgpack/v2/def"
tu "github.com/shamaton/msgpack/v2/internal/common/testutil"
)

func Test_mapLength(t *testing.T) {
Expand Down Expand Up @@ -70,17 +67,6 @@ func Test_mapLength(t *testing.T) {
}
}

func TestHoge(t *testing.T) {
buf := new(bytes.Buffer)
err := encoding.Encode(buf, map[string]int{"a": 1}, false)
tu.NoError(t, err)

v := map[string]int{}
err = Decode(buf, &v, false)
tu.NoError(t, err)
t.Log(v)
}

func Test_asFixedMap_StringInt(t *testing.T) {
run := func(t *testing.T, v any, dv byte) {
method := func(d *decoder) (bool, error) {
Expand Down
131 changes: 131 additions & 0 deletions internal/stream/decoding/slice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package decoding

import (
"io"
"math"
"reflect"
"testing"

"github.com/shamaton/msgpack/v2/def"
tu "github.com/shamaton/msgpack/v2/internal/common/testutil"
)

func Test_sliceLength(t *testing.T) {
method := func(d *decoder) func(byte, reflect.Kind) (int, error) {
return d.sliceLength
}
testcases := AsXXXTestCases[int]{
{
Name: "FixArray",
Code: def.FixArray + 3,
Expected: 3,
MethodAsWithCode: method,
},
{
Name: "Array16.error",
Code: def.Array16,
Data: []byte{},
ReadCount: 0,
Error: io.EOF,
MethodAsWithCode: method,
},
{
Name: "Array16.ok",
Code: def.Array16,
Data: []byte{0xff, 0xff},
Expected: math.MaxUint16,
ReadCount: 1,
MethodAsWithCode: method,
},
{
Name: "Array32.error",
Code: def.Array32,
Data: []byte{},
ReadCount: 0,
Error: io.EOF,
MethodAsWithCode: method,
},
{
Name: "Array32.ok",
Code: def.Array32,
Data: []byte{0xff, 0xff, 0xff, 0xff},
Expected: math.MaxUint32,
ReadCount: 1,
MethodAsWithCode: method,
},
{
Name: "Unexpected",
Code: def.Nil,
Error: ErrCanNotDecode,
MethodAsWithCode: method,
},
}

for _, tc := range testcases {
tc.Run(t)
}
}

func Test_asFixedSlice_Int(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 + 3},
Expected: true,
ReadCount: 1,
MethodAsCustom: method,
},
}
for _, tc := range testcases {
tc.Run(t)
}
}

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

func Test_asFixedSlice_Uint(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([]uint)
run(t, v1)
tu.EqualSlice(t, *v1, []uint{5})
}

0 comments on commit 5dc8e94

Please sign in to comment.