Skip to content

Commit

Permalink
add decoding float tests, struct tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shamaton committed Aug 7, 2024
1 parent a201ea4 commit 2226b13
Show file tree
Hide file tree
Showing 6 changed files with 948 additions and 6 deletions.
11 changes: 11 additions & 0 deletions internal/common/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,14 @@ func EqualMap[K comparable, V comparable](t *testing.T, actual, expected map[K]V
}
}
}

type Equaler[T any] interface {
Equal(other T) bool
}

func EqualEqualer[T Equaler[T]](t *testing.T, actual, expected T) {
t.Helper()
if !actual.Equal(expected) {
t.Fatalf("not equal. actual: %v, expected: %v", actual, expected)
}
}
5 changes: 4 additions & 1 deletion internal/stream/decoding/decoding.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package decoding

import (
"errors"
"fmt"
"io"
"reflect"
Expand Down Expand Up @@ -319,6 +320,8 @@ func (d *decoder) decodeWithCode(code byte, rv reflect.Value) error {
return nil
}

var ErrCanNotDecode = errors.New("msgpack : invalid code")

func (d *decoder) errorTemplate(code byte, k reflect.Kind) error {
return fmt.Errorf("msgpack : invalid code %x decoding %v", code, k)
return fmt.Errorf("msgpack : invalid code %x decoding %v, %w", code, k, ErrCanNotDecode)
}
8 changes: 4 additions & 4 deletions internal/stream/decoding/float.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ func (d *decoder) asFloat32WithCode(code byte, k reflect.Kind) (float32, error)
case d.isPositiveFixNum(code), code == def.Uint8, code == def.Uint16, code == def.Uint32, code == def.Uint64:
v, err := d.asUintWithCode(code, k)
if err != nil {
break
return 0, err
}
return float32(v), nil

case d.isNegativeFixNum(code), code == def.Int8, code == def.Int16, code == def.Int32, code == def.Int64:
v, err := d.asIntWithCode(code, k)
if err != nil {
break
return 0, err
}
return float32(v), nil

Expand Down Expand Up @@ -75,14 +75,14 @@ func (d *decoder) asFloat64WithCode(code byte, k reflect.Kind) (float64, error)
case d.isPositiveFixNum(code), code == def.Uint8, code == def.Uint16, code == def.Uint32, code == def.Uint64:
v, err := d.asUintWithCode(code, k)
if err != nil {
break
return 0, err
}
return float64(v), nil

case d.isNegativeFixNum(code), code == def.Int8, code == def.Int16, code == def.Int32, code == def.Int64:
v, err := d.asIntWithCode(code, k)
if err != nil {
break
return 0, err
}
return float64(v), nil

Expand Down
204 changes: 204 additions & 0 deletions internal/stream/decoding/float_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
package decoding

import (
"github.com/shamaton/msgpack/v2/def"
"io"
"reflect"
"testing"
)

func Test_asFloat32(t *testing.T) {
method := func(d *decoder) func(reflect.Kind) (float32, error) {
return d.asFloat32
}
testcases := AsXXXTestCases[float32]{
{
Name: "error",
Error: io.EOF,
MethodAs: method,
},
{
Name: "ok",
Data: []byte{def.Float32, 63, 128, 0, 0},
Expected: float32(1),
ReadCount: 2,
MethodAs: method,
},
}

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

func Test_asFloat32WithCode(t *testing.T) {
method := func(d *decoder) func(byte, reflect.Kind) (float32, error) {
return d.asFloat32WithCode
}
testcases := AsXXXTestCases[float32]{
{
Name: "Float32.error",
Code: def.Float32,
Error: io.EOF,
MethodAsWithCode: method,
},
{
Name: "Float32.ok",
Code: def.Float32,
Data: []byte{63, 128, 0, 0},
Expected: float32(1),
ReadCount: 1,
MethodAsWithCode: method,
},
{
Name: "Uint8.error",
Code: def.Uint8,
Error: io.EOF,
MethodAsWithCode: method,
},
{
Name: "Uint8.ok",
Code: def.Uint8,
Data: []byte{1},
Expected: float32(1),
ReadCount: 1,
MethodAsWithCode: method,
},
{
Name: "Int8.error",
Code: def.Int8,
Error: io.EOF,
MethodAsWithCode: method,
},
{
Name: "Int8.ok",
Code: def.Int8,
Data: []byte{0xff},
Expected: float32(-1),
ReadCount: 1,
MethodAsWithCode: method,
},
{
Name: "Nil.ok",
Code: def.Nil,
Expected: float32(0),
ReadCount: 0,
MethodAsWithCode: method,
},
{
Name: "Unexpected",
Code: def.Str8,
IsTemplateError: true,
MethodAsWithCode: method,
},
}

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

func Test_asFloat64(t *testing.T) {
method := func(d *decoder) func(reflect.Kind) (float64, error) {
return d.asFloat64
}
testcases := AsXXXTestCases[float64]{
{
Name: "error",
Error: io.EOF,
MethodAs: method,
},
{
Name: "ok",
Data: []byte{def.Float64, 63, 240, 0, 0, 0, 0, 0, 0},
Expected: float64(1),
ReadCount: 2,
MethodAs: method,
},
}

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

func Test_asFloat64WithCode(t *testing.T) {
method := func(d *decoder) func(byte, reflect.Kind) (float64, error) {
return d.asFloat64WithCode
}
testcases := AsXXXTestCases[float64]{
{
Name: "Float64.error",
Code: def.Float64,
Error: io.EOF,
MethodAsWithCode: method,
},
{
Name: "Float64.ok",
Code: def.Float64,
Data: []byte{63, 240, 0, 0, 0, 0, 0, 0},
Expected: float64(1),
ReadCount: 1,
MethodAsWithCode: method,
},
{
Name: "Float32.error",
Code: def.Float32,
Error: io.EOF,
MethodAsWithCode: method,
},
{
Name: "Float32.ok",
Code: def.Float32,
Data: []byte{63, 128, 0, 0},
Expected: float64(1),
ReadCount: 1,
MethodAsWithCode: method,
},
{
Name: "Uint8.error",
Code: def.Uint8,
Error: io.EOF,
MethodAsWithCode: method,
},
{
Name: "Uint8.ok",
Code: def.Uint8,
Data: []byte{1},
Expected: float64(1),
ReadCount: 1,
MethodAsWithCode: method,
},
{
Name: "Int8.error",
Code: def.Int8,
Error: io.EOF,
MethodAsWithCode: method,
},
{
Name: "Int8.ok",
Code: def.Int8,
Data: []byte{0xff},
Expected: float64(-1),
ReadCount: 1,
MethodAsWithCode: method,
},
{
Name: "Nil.ok",
Code: def.Nil,
Expected: float64(0),
ReadCount: 0,
MethodAsWithCode: method,
},
{
Name: "Unexpected",
Code: def.Str8,
IsTemplateError: true,
MethodAsWithCode: method,
},
}

for _, tc := range testcases {
tc.Run(t)
}
}
2 changes: 1 addition & 1 deletion internal/stream/decoding/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (d *decoder) jumpOffset() error {
case code == def.Fixext16:
_, err = d.readSizeN(def.Byte1 + def.Byte16)
return err

case code == def.Ext8:
b, err := d.readSize1()
if err != nil {
Expand Down
Loading

0 comments on commit 2226b13

Please sign in to comment.