Skip to content

Commit

Permalink
add encoding bool, int tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shamaton committed Aug 22, 2024
1 parent d79d112 commit 8c48060
Show file tree
Hide file tree
Showing 4 changed files with 286 additions and 2 deletions.
14 changes: 12 additions & 2 deletions internal/common/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,21 @@ func Equal[T any](t *testing.T, actual, expected T) {
func EqualSlice[T comparable](t *testing.T, actual, expected []T) {
t.Helper()
if len(actual) != len(expected) {
t.Fatalf("diffrent length. actual: %v, expected: %v", actual, expected)
switch any(actual).(type) {
case []byte:
t.Fatalf("diffrent length. actual: [% 02x], expected: [% 02x]", actual, expected)
default:
t.Fatalf("diffrent length. actual: %v, expected: %v", actual, expected)
}
}
for i := range actual {
if !reflect.DeepEqual(actual[i], expected[i]) {
t.Fatalf("not equal. actual: %v, expected: %v", actual, expected)
switch any(actual).(type) {
case []byte:
t.Fatalf("not equal. actual: [% 02x], expected: [% 02x]", actual, expected)
default:
t.Fatalf("not equal. actual: %v, expected: %v", actual, expected)
}
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions internal/stream/encoding/bool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package encoding

import (
"testing"

"github.com/shamaton/msgpack/v2/def"
)

func Test_asBool(t *testing.T) {
method := func(e *encoder) func(bool) error {
return e.writeBool
}
testcases := AsXXXTestCases[bool]{
{
Name: "True.error",
Value: true,
BufferSize: 1,
PreWriteSize: 1,
Method: method,
},
{
Name: "True.ok",
Value: true,
Expected: []byte{def.True},
BufferSize: 1,
Method: method,
},
}
testcases.Run(t)
}
111 changes: 111 additions & 0 deletions internal/stream/encoding/encoding_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package encoding

import (
"bytes"
"errors"
"io"
"reflect"
"testing"

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

const dummyByte = 0xc1

type TestWriter struct {
WrittenBytes []byte
}

var _ io.Writer = (*TestWriter)(nil)

var ErrTestWriter = errors.New("expected written error")

func (w *TestWriter) Write(p []byte) (n int, err error) {
w.WrittenBytes = append(w.WrittenBytes, p...)
if bytes.Contains(p, []byte{dummyByte}) {
return 0, ErrTestWriter
}
return len(p), nil
}

func NewTestWriter() *TestWriter {
return &TestWriter{}
}

type AsXXXTestCase[T any] struct {
Name string
Value T
Expected []byte
Contains []byte
BufferSize int
PreWriteSize int
Method func(*encoder) func(T) error
MethodForFixed func(*encoder) func(reflect.Value) (bool, error)
MethodForStruct func(*encoder) func(reflect.Value) error
}

type AsXXXTestCases[T any] []AsXXXTestCase[T]

func (tcs AsXXXTestCases[T]) Run(t *testing.T) {
for _, tc := range tcs {
tc.Run(t)
}
}

func (tc *AsXXXTestCase[T]) Run(t *testing.T) {
t.Helper()

if tc.Method == nil && tc.MethodForFixed == nil && tc.MethodForStruct == nil {
t.Fatal("must set either Method or MethodForFixed or MethodForStruct")
}

method := func(e *encoder) error {
if tc.Method != nil {
return tc.Method(e)(tc.Value)
}
if tc.MethodForFixed != nil {
_, err := tc.MethodForFixed(e)(reflect.ValueOf(tc.Value))
return err
}
if tc.MethodForStruct != nil {
return tc.MethodForStruct(e)(reflect.ValueOf(tc.Value))
}
panic("unreachable")
}

t.Run(tc.Name, func(t *testing.T) {
w := NewTestWriter()
e := encoder{
w: w,
buf: common.GetBuffer(),
Common: common.Common{},
}

if tc.BufferSize < tc.PreWriteSize {
t.Fatal("buffer size must be greater than pre write size")
}

e.buf.Data = make([]byte, tc.BufferSize)
if tc.PreWriteSize > 0 {
for i := 0; i < tc.PreWriteSize; i++ {
_ = e.buf.Write(e.w, dummyByte)
}
}

err := method(&e)
_ = e.buf.Flush(w)
common.PutBuffer(e.buf)

if tc.PreWriteSize > 0 {
tu.IsError(t, err, ErrTestWriter)
if !bytes.Contains(w.WrittenBytes, tc.Contains) {
t.Fatalf("[% 02x] does not contain in [% 02x]", tc.Contains, w.WrittenBytes)
}
return
}

tu.NoError(t, err)
tu.EqualSlice(t, w.WrittenBytes, tc.Expected)
})
}
133 changes: 133 additions & 0 deletions internal/stream/encoding/int_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
package encoding

import (
"math"
"testing"

"github.com/shamaton/msgpack/v2/def"
)

func Test_asInt(t *testing.T) {
method := func(e *encoder) func(int64) error {
return e.writeInt
}
testcases := AsXXXTestCases[int64]{
{
Name: "Uint.error",
Value: math.MaxInt32,
BufferSize: 1,
PreWriteSize: 1,
Method: method,
},
{
Name: "Uint.ok",
Value: math.MaxInt32,
Expected: []byte{def.Uint32, 0x7f, 0xff, 0xff, 0xff},
BufferSize: 1,
Method: method,
},
{
Name: "NegativeFix.error",
Value: -1,
BufferSize: 1,
PreWriteSize: 1,
Method: method,
},
{
Name: "NegativeFix.ok",
Value: -1,
Expected: []byte{0xff},
BufferSize: 1,
Method: method,
},
{
Name: "Int8.error.def",
Value: math.MinInt8,
BufferSize: 1,
PreWriteSize: 1,
Method: method,
},
{
Name: "Int8.error.value",
Value: math.MinInt8,
BufferSize: 2,
PreWriteSize: 1,
Contains: []byte{def.Int8},
Method: method,
},
{
Name: "Int8.ok",
Value: math.MinInt8,
Expected: []byte{def.Int8, 0x80},
BufferSize: 1,
Method: method,
},
{
Name: "Int16.error.def",
Value: math.MinInt16,
BufferSize: 1,
PreWriteSize: 1,
Method: method,
},
{
Name: "Int16.error.value",
Value: math.MinInt16,
BufferSize: 3,
PreWriteSize: 1,
Contains: []byte{def.Int16},
Method: method,
},
{
Name: "Int16.ok",
Value: math.MinInt16,
Expected: []byte{def.Int16, 0x80, 0x00},
BufferSize: 1,
Method: method,
},
{
Name: "Int32.error.def",
Value: math.MinInt32,
BufferSize: 1,
PreWriteSize: 1,
Method: method,
},
{
Name: "Int32.error.value",
Value: math.MinInt32,
BufferSize: 5,
PreWriteSize: 1,
Contains: []byte{def.Int32},
Method: method,
},
{
Name: "Int32.ok",
Value: math.MinInt32,
Expected: []byte{def.Int32, 0x80, 0x00, 0x00, 0x00},
BufferSize: 1,
Method: method,
},
{
Name: "Int64.error.def",
Value: math.MinInt64,
BufferSize: 1,
PreWriteSize: 1,
Method: method,
},
{
Name: "Int64.error.value",
Value: math.MinInt64,
BufferSize: 9,
PreWriteSize: 1,
Contains: []byte{def.Int64},
Method: method,
},
{
Name: "Int64.ok",
Value: math.MinInt64,
Expected: []byte{def.Int64, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
BufferSize: 1,
Method: method,
},
}
testcases.Run(t)
}

0 comments on commit 8c48060

Please sign in to comment.