Skip to content

Commit

Permalink
fix bugs and add decoding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shamaton committed Aug 15, 2024
1 parent 5ac2f33 commit 7155d37
Show file tree
Hide file tree
Showing 20 changed files with 3,352 additions and 32 deletions.
4 changes: 3 additions & 1 deletion def/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ var (
ErrCanNotSetSliceAsMapKey = errors.New("can not set slice as map key")
ErrCanNotSetMapAsMapKey = errors.New("can not set map as map key")

ErrTooShortBytes = errors.New("too short bytes")
ErrTooShortBytes = errors.New("too short bytes")
ErrLackDataLengthToSlice = errors.New("data length lacks to create slice")
ErrLackDataLengthToMap = errors.New("data length lacks to create map")
)
2 changes: 1 addition & 1 deletion internal/decoding/bin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func Test_asBin(t *testing.T) {
},
{
Name: "Bin32.error.data",
Data: []byte{def.Bin32, 0, 1},
Data: []byte{def.Bin32, 0, 0, 0, 1},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
Expand Down
6 changes: 4 additions & 2 deletions internal/decoding/bool.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
)

func (d *decoder) asBool(offset int, k reflect.Kind) (bool, int, error) {
code := d.data[offset]
offset++
code, offset, err := d.readSize1(offset)
if err != nil {
return false, 0, err
}

switch code {
case def.True:
Expand Down
40 changes: 40 additions & 0 deletions internal/decoding/bool_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package decoding

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

func Test_asBool(t *testing.T) {
method := func(d *decoder) func(int, reflect.Kind) (bool, int, error) {
return d.asBool
}
testcases := AsXXXTestCases[bool]{
{
Name: "error.code",
Data: []byte{},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Bool.false",
Data: []byte{def.False},
Expected: false,
MethodAs: method,
},
{
Name: "Bool.true",
Data: []byte{def.True},
Expected: true,
MethodAs: method,
},
{
Name: "Unexpected",
Data: []byte{def.Nil},
Error: def.ErrCanNotDecode,
MethodAs: method,
},
}
testcases.Run(t)
}
148 changes: 148 additions & 0 deletions internal/decoding/complex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package decoding

import (
"reflect"
"testing"

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

func Test_asComplex64(t *testing.T) {
method := func(d *decoder) func(int, reflect.Kind) (complex64, int, error) {
return d.asComplex64
}
testcases := AsXXXTestCases[complex64]{
{
Name: "error.code",
Data: []byte{},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext8.error.type",
Data: []byte{def.Fixext8},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext8.error.r",
Data: []byte{def.Fixext8, byte(def.ComplexTypeCode())},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext8.error.i",
Data: []byte{def.Fixext8, byte(def.ComplexTypeCode()), 0, 0, 0, 1},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext8.ok",
Data: []byte{def.Fixext8, byte(def.ComplexTypeCode()), 63, 128, 0, 0, 63, 128, 0, 0},
Expected: complex(1, 1),
MethodAs: method,
},
{
Name: "Fixext16.error.type",
Data: []byte{def.Fixext16},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext16.error.r",
Data: []byte{def.Fixext16, byte(def.ComplexTypeCode())},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext16.error.i",
Data: []byte{def.Fixext16, byte(def.ComplexTypeCode()), 0, 0, 0, 1},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext16.ok",
Data: []byte{def.Fixext16, byte(def.ComplexTypeCode()),
63, 240, 0, 0, 0, 0, 0, 0, 63, 240, 0, 0, 0, 0, 0, 0},
Expected: complex(1, 1),
MethodAs: method,
},
{
Name: "Unexpected",
Data: []byte{def.Nil},
Error: def.ErrCanNotDecode,
MethodAs: method,
},
}
testcases.Run(t)
}

func Test_asComplex128(t *testing.T) {
method := func(d *decoder) func(int, reflect.Kind) (complex128, int, error) {
return d.asComplex128
}
testcases := AsXXXTestCases[complex128]{
{
Name: "error.code",
Data: []byte{},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext8.error.type",
Data: []byte{def.Fixext8},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext8.error.r",
Data: []byte{def.Fixext8, byte(def.ComplexTypeCode())},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext8.error.i",
Data: []byte{def.Fixext8, byte(def.ComplexTypeCode()), 0, 0, 0, 1},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext8.ok",
Data: []byte{def.Fixext8, byte(def.ComplexTypeCode()), 63, 128, 0, 0, 63, 128, 0, 0},
Expected: complex(1, 1),
MethodAs: method,
},
{
Name: "Fixext16.error.type",
Data: []byte{def.Fixext16},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext16.error.r",
Data: []byte{def.Fixext16, byte(def.ComplexTypeCode())},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext16.error.i",
Data: []byte{def.Fixext16, byte(def.ComplexTypeCode()), 0, 0, 0, 1},
Error: def.ErrTooShortBytes,
MethodAs: method,
},
{
Name: "Fixext16.ok",
Data: []byte{def.Fixext16, byte(def.ComplexTypeCode()),
63, 240, 0, 0, 0, 0, 0, 0, 63, 240, 0, 0, 0, 0, 0, 0},
Expected: complex(1, 1),
MethodAs: method,
},
{
Name: "Unexpected",
Data: []byte{def.Nil},
Error: def.ErrCanNotDecode,
MethodAs: method,
},
}
testcases.Run(t)
}
6 changes: 3 additions & 3 deletions internal/decoding/decoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (d *decoder) decode(rv reflect.Value, offset int) (int, error) {
return 0, err
}
if len(bs) > rv.Len() {
return 0, fmt.Errorf("%v len is %d, but msgpack has %d elements", rv.Type(), rv.Len(), len(bs))
return 0, fmt.Errorf("%v len is %d, but msgpack has %d elements, %w", rv.Type(), rv.Len(), len(bs), def.ErrNotMatchArrayElement)
}
for i, b := range bs {
rv.Index(i).SetUint(uint64(b))
Expand All @@ -206,7 +206,7 @@ func (d *decoder) decode(rv reflect.Value, offset int) (int, error) {
return 0, err
}
if l > rv.Len() {
return 0, fmt.Errorf("%v len is %d, but msgpack has %d elements", rv.Type(), rv.Len(), l)
return 0, fmt.Errorf("%v len is %d, but msgpack has %d elements, %w", rv.Type(), rv.Len(), l, def.ErrNotMatchArrayElement)
}
bs, offset, err := d.asStringByteByLength(offset, l, k)
if err != nil {
Expand All @@ -225,7 +225,7 @@ func (d *decoder) decode(rv reflect.Value, offset int) (int, error) {
}

if l > rv.Len() {
return 0, fmt.Errorf("%v len is %d, but msgpack has %d elements", rv.Type(), rv.Len(), l)
return 0, fmt.Errorf("%v len is %d, but msgpack has %d elements, %w", rv.Type(), rv.Len(), l, def.ErrNotMatchArrayElement)
}

if err = d.hasRequiredLeastSliceSize(o, l); err != nil {
Expand Down
Loading

0 comments on commit 7155d37

Please sign in to comment.