From 37aa65ebe7cd7a28a21707ef22ff1f2856c2310e Mon Sep 17 00:00:00 2001 From: illia-li Date: Mon, 30 Sep 2024 14:51:11 -0400 Subject: [PATCH] fix `smallint` tests --- marshal_3_smallint_corrupt_test.go | 165 +++++++++++++------------ marshal_3_smallint_test.go | 187 +++++++++++++++-------------- marshal_test.go | 115 ------------------ 3 files changed, 184 insertions(+), 283 deletions(-) diff --git a/marshal_3_smallint_corrupt_test.go b/marshal_3_smallint_corrupt_test.go index 262188cf1..9028ef9c3 100644 --- a/marshal_3_smallint_corrupt_test.go +++ b/marshal_3_smallint_corrupt_test.go @@ -7,86 +7,101 @@ import ( "github.com/gocql/gocql" "github.com/gocql/gocql/internal/tests/serialization" "github.com/gocql/gocql/internal/tests/serialization/mod" + "github.com/gocql/gocql/marshal/smallint" ) func TestMarshalSmallintCorrupt(t *testing.T) { + type testSuite struct { + name string + marshal func(interface{}) ([]byte, error) + unmarshal func(bytes []byte, i interface{}) error + } + tType := gocql.NewNativeType(4, gocql.TypeSmallInt, "") - marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } - unmarshal := func(bytes []byte, i interface{}) error { - return gocql.Unmarshal(tType, bytes, i) + testSuites := [2]testSuite{ + { + name: "serialization.smallint", + marshal: smallint.Marshal, + unmarshal: smallint.Unmarshal, + }, + { + name: "glob", + marshal: func(i interface{}) ([]byte, error) { + return gocql.Marshal(tType, i) + }, + unmarshal: func(bytes []byte, i interface{}) error { + return gocql.Unmarshal(tType, bytes, i) + }, + }, } - // unmarshal function does not return an error in cases where the length of the data is different from 0 or 2 - brokenUnmarshalTypes := serialization.GetTypes( - mod.Values{ - int8(0), int16(0), int32(0), int64(0), int(0), - uint8(0), uint16(0), uint32(0), uint64(0), uint(0), - *big.NewInt(0), - }.AddVariants(mod.All...)...) - brokenUnmarshalTypes = append(brokenUnmarshalTypes, serialization.GetTypes("", (*string)(nil))...) - - serialization.NegativeMarshalSet{ - Values: mod.Values{ - int32(32768), int64(32768), int(32768), - "32768", *big.NewInt(32768), - int32(-32769), int64(-32769), int(-32769), - "-32769", *big.NewInt(-32769), - uint32(65536), uint64(65536), uint(65536), - }.AddVariants(mod.All...), - }.Run("big_vals", t, marshal) - - serialization.NegativeMarshalSet{ - Values: mod.Values{"1s2", "1s", "-1s", ".1", ",1", "0.1", "0,1"}.AddVariants(mod.All...), - }.Run("corrupt_vals", t, marshal) - - serialization.NegativeUnmarshalSet{ - Data: []byte("\x80\x00\x00"), - Values: mod.Values{ - int8(0), int16(0), int32(0), int64(0), int(0), - uint8(0), uint16(0), uint32(0), uint64(0), uint(0), - "", *big.NewInt(0), - }.AddVariants(mod.All...), - BrokenTypes: brokenUnmarshalTypes, - }.Run("big_data", t, unmarshal) - - serialization.NegativeUnmarshalSet{ - Data: []byte("\x80"), - Values: mod.Values{ - int8(0), int16(0), int32(0), int64(0), int(0), - uint8(0), uint16(0), uint32(0), uint64(0), uint(0), - "", *big.NewInt(0), - }.AddVariants(mod.All...), - BrokenTypes: brokenUnmarshalTypes, - }.Run("small_data", t, unmarshal) - - serialization.NegativeUnmarshalSet{ - Data: []byte("\x00\x80"), - Values: mod.Values{int8(0)}.AddVariants(mod.All...), - }.Run("small_type_int8_128", t, unmarshal) - - serialization.NegativeUnmarshalSet{ - Data: []byte("\x7f\xff"), - Values: mod.Values{int8(0)}.AddVariants(mod.All...), - }.Run("small_type_int8_32767", t, unmarshal) - - serialization.NegativeUnmarshalSet{ - Data: []byte("\xff\x7f"), - Values: mod.Values{int8(0)}.AddVariants(mod.All...), - }.Run("small_type_int8_-129", t, unmarshal) - - serialization.NegativeUnmarshalSet{ - Data: []byte("\x7f\xff"), - Values: mod.Values{int8(0)}.AddVariants(mod.All...), - }.Run("small_type_int8_-32768", t, unmarshal) - - serialization.NegativeUnmarshalSet{ - Data: []byte("\x01\x00"), - Values: mod.Values{int8(0)}.AddVariants(mod.All...), - }.Run("small_type_uint_256", t, unmarshal) - - serialization.NegativeUnmarshalSet{ - Data: []byte("\xff\xff"), - Values: mod.Values{uint8(0)}.AddVariants(mod.All...), - }.Run("small_type_uint_65535", t, unmarshal) + for _, tSuite := range testSuites { + marshal := tSuite.marshal + unmarshal := tSuite.unmarshal + + t.Run(tSuite.name, func(t *testing.T) { + serialization.NegativeMarshalSet{ + Values: mod.Values{ + int32(32768), int64(32768), int(32768), + "32768", *big.NewInt(32768), + int32(-32769), int64(-32769), int(-32769), + "-32769", *big.NewInt(-32769), + uint32(65536), uint64(65536), uint(65536), + }.AddVariants(mod.All...), + }.Run("big_vals", t, marshal) + + serialization.NegativeMarshalSet{ + Values: mod.Values{"1s2", "1s", "-1s", ".1", ",1", "0.1", "0,1"}.AddVariants(mod.All...), + }.Run("corrupt_vals", t, marshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x80\x00\x00"), + Values: mod.Values{ + int8(0), int16(0), int32(0), int64(0), int(0), + uint8(0), uint16(0), uint32(0), uint64(0), uint(0), + "", *big.NewInt(0), + }.AddVariants(mod.All...), + }.Run("big_data", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x80"), + Values: mod.Values{ + int8(0), int16(0), int32(0), int64(0), int(0), + uint8(0), uint16(0), uint32(0), uint64(0), uint(0), + "", *big.NewInt(0), + }.AddVariants(mod.All...), + }.Run("small_data", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x00\x80"), + Values: mod.Values{int8(0)}.AddVariants(mod.All...), + }.Run("small_type_int8_128", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x7f\xff"), + Values: mod.Values{int8(0)}.AddVariants(mod.All...), + }.Run("small_type_int8_32767", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\xff\x7f"), + Values: mod.Values{int8(0)}.AddVariants(mod.All...), + }.Run("small_type_int8_-129", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x7f\xff"), + Values: mod.Values{int8(0)}.AddVariants(mod.All...), + }.Run("small_type_int8_-32768", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\x01\x00"), + Values: mod.Values{int8(0)}.AddVariants(mod.All...), + }.Run("small_type_uint_256", t, unmarshal) + + serialization.NegativeUnmarshalSet{ + Data: []byte("\xff\xff"), + Values: mod.Values{uint8(0)}.AddVariants(mod.All...), + }.Run("small_type_uint_65535", t, unmarshal) + }) + } } diff --git a/marshal_3_smallint_test.go b/marshal_3_smallint_test.go index 2a86e06da..723e15cfa 100644 --- a/marshal_3_smallint_test.go +++ b/marshal_3_smallint_test.go @@ -7,104 +7,105 @@ import ( "github.com/gocql/gocql" "github.com/gocql/gocql/internal/tests/serialization" "github.com/gocql/gocql/internal/tests/serialization/mod" + "github.com/gocql/gocql/marshal/smallint" ) func TestMarshalSmallint(t *testing.T) { + type testSuite struct { + name string + marshal func(interface{}) ([]byte, error) + unmarshal func(bytes []byte, i interface{}) error + } + tType := gocql.NewNativeType(4, gocql.TypeSmallInt, "") - marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) } - unmarshal := func(bytes []byte, i interface{}) error { - return gocql.Unmarshal(tType, bytes, i) + testSuites := [2]testSuite{ + { + name: "serialization.smallint", + marshal: smallint.Marshal, + unmarshal: smallint.Unmarshal, + }, + { + name: "glob", + marshal: func(i interface{}) ([]byte, error) { + return gocql.Marshal(tType, i) + }, + unmarshal: func(bytes []byte, i interface{}) error { + return gocql.Unmarshal(tType, bytes, i) + }, + }, } - // unmarshal `custom string` unsupported - brokenCustomStrings := serialization.GetTypes(mod.String(""), (*mod.String)(nil)) - - // marshal "" (empty string) unsupported - // unmarshal nil value into (string)("0") - brokenEmptyStrings := serialization.GetTypes(string(""), mod.String("")) - - // marshal `custom string` unsupported - // marshal `big.Int` unsupported - brokenMarshalTypes := append(brokenCustomStrings, serialization.GetTypes(big.Int{}, &big.Int{})...) - - serialization.PositiveSet{ - Data: nil, - Values: mod.Values{ - (*int8)(nil), (*int16)(nil), (*int32)(nil), (*int64)(nil), (*int)(nil), - (*uint8)(nil), (*uint16)(nil), (*uint32)(nil), (*uint64)(nil), (*uint)(nil), - (*string)(nil), (*big.Int)(nil), "", - }.AddVariants(mod.CustomType), - BrokenMarshalTypes: brokenEmptyStrings, - BrokenUnmarshalTypes: brokenEmptyStrings, - }.Run("[nil]nullable", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: nil, - Values: mod.Values{ - int8(0), int16(0), int32(0), int64(0), int(0), - uint8(0), uint16(0), uint32(0), uint64(0), uint(0), - "0", big.Int{}, - }.AddVariants(mod.CustomType), - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("[nil]unmarshal", t, nil, unmarshal) - - serialization.PositiveSet{ - Data: make([]byte, 0), - Values: mod.Values{ - int8(0), int16(0), int32(0), int64(0), int(0), - uint8(0), uint16(0), uint32(0), uint64(0), uint(0), - "0", *big.NewInt(0), - }.AddVariants(mod.All...), - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("[]unmarshal", t, nil, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x00\x00"), - Values: mod.Values{ - int8(0), int16(0), int32(0), int64(0), int(0), - uint8(0), uint16(0), uint32(0), uint64(0), uint(0), - "0", *big.NewInt(0), - }.AddVariants(mod.All...), - BrokenMarshalTypes: brokenMarshalTypes, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("zeros", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x00\x7f"), - Values: mod.Values{int8(127), int16(127), int32(127), int64(127), int(127), "127", *big.NewInt(127)}.AddVariants(mod.All...), - BrokenMarshalTypes: brokenMarshalTypes, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("127", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\xff\x80"), - Values: mod.Values{int8(-128), int16(-128), int32(-128), int64(-128), int(-128), "-128", *big.NewInt(-128)}.AddVariants(mod.All...), - BrokenMarshalTypes: brokenMarshalTypes, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("-128", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x7f\xff"), - Values: mod.Values{int16(32767), int32(32767), int64(32767), int(32767), "32767", *big.NewInt(32767)}.AddVariants(mod.All...), - BrokenMarshalTypes: brokenMarshalTypes, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("32767", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x80\x00"), - Values: mod.Values{int16(-32768), int32(-32768), int64(-32768), int(-32768), "-32768", *big.NewInt(-32768)}.AddVariants(mod.All...), - BrokenMarshalTypes: brokenMarshalTypes, - BrokenUnmarshalTypes: brokenCustomStrings, - }.Run("-32768", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\x00\xff"), - Values: mod.Values{uint8(255), uint16(255), uint32(255), uint64(255), uint(255)}.AddVariants(mod.All...), - }.Run("255", t, marshal, unmarshal) - - serialization.PositiveSet{ - Data: []byte("\xff\xff"), - Values: mod.Values{uint16(65535), uint32(65535), uint64(65535), uint(65535)}.AddVariants(mod.All...), - }.Run("65535", t, marshal, unmarshal) + for _, tSuite := range testSuites { + marshal := tSuite.marshal + unmarshal := tSuite.unmarshal + + t.Run(tSuite.name, func(t *testing.T) { + serialization.PositiveSet{ + Data: nil, + Values: mod.Values{ + (*int8)(nil), (*int16)(nil), (*int32)(nil), (*int64)(nil), (*int)(nil), + (*uint8)(nil), (*uint16)(nil), (*uint32)(nil), (*uint64)(nil), (*uint)(nil), + (*string)(nil), (*big.Int)(nil), "", + }.AddVariants(mod.CustomType), + }.Run("[nil]nullable", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: nil, + Values: mod.Values{ + int8(0), int16(0), int32(0), int64(0), int(0), + uint8(0), uint16(0), uint32(0), uint64(0), uint(0), + "", big.Int{}, + }.AddVariants(mod.CustomType), + }.Run("[nil]unmarshal", t, nil, unmarshal) + + serialization.PositiveSet{ + Data: make([]byte, 0), + Values: mod.Values{ + int8(0), int16(0), int32(0), int64(0), int(0), + uint8(0), uint16(0), uint32(0), uint64(0), uint(0), + "0", *big.NewInt(0), + }.AddVariants(mod.All...), + }.Run("[]unmarshal", t, nil, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x00"), + Values: mod.Values{ + int8(0), int16(0), int32(0), int64(0), int(0), + uint8(0), uint16(0), uint32(0), uint64(0), uint(0), + "0", *big.NewInt(0), + }.AddVariants(mod.All...), + }.Run("zeros", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\x7f"), + Values: mod.Values{int8(127), int16(127), int32(127), int64(127), int(127), "127", *big.NewInt(127)}.AddVariants(mod.All...), + }.Run("127", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\xff\x80"), + Values: mod.Values{int8(-128), int16(-128), int32(-128), int64(-128), int(-128), "-128", *big.NewInt(-128)}.AddVariants(mod.All...), + }.Run("-128", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x7f\xff"), + Values: mod.Values{int16(32767), int32(32767), int64(32767), int(32767), "32767", *big.NewInt(32767)}.AddVariants(mod.All...), + }.Run("32767", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x80\x00"), + Values: mod.Values{int16(-32768), int32(-32768), int64(-32768), int(-32768), "-32768", *big.NewInt(-32768)}.AddVariants(mod.All...), + }.Run("-32768", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\x00\xff"), + Values: mod.Values{uint8(255), uint16(255), uint32(255), uint64(255), uint(255)}.AddVariants(mod.All...), + }.Run("255", t, marshal, unmarshal) + + serialization.PositiveSet{ + Data: []byte("\xff\xff"), + Values: mod.Values{uint16(65535), uint32(65535), uint64(65535), uint(65535)}.AddVariants(mod.All...), + }.Run("65535", t, marshal, unmarshal) + }) + } } diff --git a/marshal_test.go b/marshal_test.go index 901aca5fb..fb20c40ca 100644 --- a/marshal_test.go +++ b/marshal_test.go @@ -814,97 +814,6 @@ var marshalTests = []struct { nil, nil, }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\x7f\xff"), - 32767, // math.MaxInt16 - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\x7f\xff"), - "32767", // math.MaxInt16 - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\x00\x01"), - int16(1), - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\xff\xff"), - int16(-1), - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\x00\xff"), - uint8(255), - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\xff\xff"), - uint16(65535), - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\xff\xff"), - uint32(65535), - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\xff\xff"), - uint64(65535), - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\x00\xff"), - AliasUint8(255), - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\xff\xff"), - AliasUint16(65535), - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\xff\xff"), - AliasUint32(65535), - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\xff\xff"), - AliasUint64(65535), - nil, - nil, - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\xff\xff"), - AliasUint(65535), - nil, - nil, - }, { NativeType{proto: 2, typ: TypeTinyInt}, []byte("\x7f"), @@ -1077,18 +986,6 @@ var unmarshalTests = []struct { Value interface{} UnmarshalError error }{ - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\xff\xff"), - uint8(0), - unmarshalErrorf("unmarshal int: value -1 out of range for uint8"), - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\x01\x00"), - uint8(0), - unmarshalErrorf("unmarshal int: value 256 out of range for uint8"), - }, { NativeType{proto: 2, typ: TypeInt}, []byte("\xff\xff\xff\xff"), @@ -1161,18 +1058,6 @@ var unmarshalTests = []struct { uint32(0), unmarshalErrorf("unmarshal int: value 4294967296 out of range for uint32"), }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\xff\xff"), - AliasUint8(0), - unmarshalErrorf("unmarshal int: value -1 out of range for gocql.AliasUint8"), - }, - { - NativeType{proto: 2, typ: TypeSmallInt}, - []byte("\x01\x00"), - AliasUint8(0), - unmarshalErrorf("unmarshal int: value 256 out of range for gocql.AliasUint8"), - }, { NativeType{proto: 2, typ: TypeInt}, []byte("\xff\xff\xff\xff"),