Skip to content

Commit

Permalink
fix boolean tests
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Dec 24, 2024
1 parent 6a27aa7 commit be3b863
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 73 deletions.
41 changes: 0 additions & 41 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,6 @@ var marshalTests = []struct {
MarshalError error
UnmarshalError error
}{
{
NativeType{proto: 2, typ: TypeBoolean},
[]byte("\x00"),
false,
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeBoolean},
[]byte("\x01"),
true,
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeDecimal},
[]byte("\x00\x00\x00\x00\x00"),
Expand Down Expand Up @@ -303,33 +289,6 @@ var marshalTests = []struct {
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeBoolean},
[]byte("\x00"),
func() *bool {
b := false
return &b
}(),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeBoolean},
[]byte("\x01"),
func() *bool {
b := true
return &b
}(),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeBoolean},
[]byte(nil),
(*bool)(nil),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeInet},
[]byte("\x7F\x00\x00\x01"),
Expand Down
54 changes: 54 additions & 0 deletions tests/serialization/marshal_1_boolean_corrupt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//go:build all || unit
// +build all unit

package serialization_test

import (
"github.com/gocql/gocql/serialization/boolean"
"testing"

"github.com/gocql/gocql"
"github.com/gocql/gocql/internal/tests/serialization"
"github.com/gocql/gocql/internal/tests/serialization/mod"
)

func TestMarshalBooleanCorrupt(t *testing.T) {
tType := gocql.NewNativeType(4, gocql.TypeBoolean, "")

type testSuite struct {
name string
marshal func(interface{}) ([]byte, error)
unmarshal func(bytes []byte, i interface{}) error
}

testSuites := [2]testSuite{
{
name: "serialization.boolean",
marshal: boolean.Marshal,
unmarshal: boolean.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)
},
},
}

for _, tSuite := range testSuites {
unmarshal := tSuite.unmarshal

t.Run(tSuite.name, func(t *testing.T) {

serialization.NegativeUnmarshalSet{
Data: []byte("\x00\x00"),
Values: mod.Values{
false,
}.AddVariants(mod.All...),
}.Run("big_data", t, unmarshal)
})
}
}
91 changes: 59 additions & 32 deletions tests/serialization/marshal_1_boolean_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,70 @@ import (
"github.com/gocql/gocql"
"github.com/gocql/gocql/internal/tests/serialization"
"github.com/gocql/gocql/internal/tests/serialization/mod"
"github.com/gocql/gocql/serialization/boolean"
)

func TestMarshalBoolean(t *testing.T) {
tType := gocql.NewNativeType(4, gocql.TypeBoolean, "")

marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) }
unmarshal := func(bytes []byte, i interface{}) error {
return gocql.Unmarshal(tType, bytes, i)
type testSuite struct {
name string
marshal func(interface{}) ([]byte, error)
unmarshal func(bytes []byte, i interface{}) error
}

serialization.PositiveSet{
Data: nil,
Values: mod.Values{(*bool)(nil)}.AddVariants(mod.CustomType),
}.Run("[nil]nullable", t, marshal, unmarshal)

serialization.PositiveSet{
Data: nil,
Values: mod.Values{false}.AddVariants(mod.CustomType),
}.Run("[nil]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: make([]byte, 0),
Values: mod.Values{false}.AddVariants(mod.All...),
}.Run("[]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: []byte("\x00"),
Values: mod.Values{false}.AddVariants(mod.All...),
}.Run("zeros", t, marshal, unmarshal)

serialization.PositiveSet{
Data: []byte("\x01"),
Values: mod.Values{true}.AddVariants(mod.All...),
}.Run("[ff]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: []byte("\xff"),
Values: mod.Values{true}.AddVariants(mod.All...),
}.Run("[01]", t, nil, unmarshal)
testSuites := [2]testSuite{
{
name: "serialization.boolean",
marshal: boolean.Marshal,
unmarshal: boolean.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)
},
},
}

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{(*bool)(nil)}.AddVariants(mod.CustomType),
}.Run("[nil]nullable", t, marshal, unmarshal)

serialization.PositiveSet{
Data: nil,
Values: mod.Values{false}.AddVariants(mod.CustomType),
}.Run("[nil]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: make([]byte, 0),
Values: mod.Values{false}.AddVariants(mod.All...),
}.Run("[]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: []byte("\x00"),
Values: mod.Values{false}.AddVariants(mod.All...),
}.Run("zeros", t, marshal, unmarshal)

serialization.PositiveSet{
Data: []byte("\x01"),
Values: mod.Values{true}.AddVariants(mod.All...),
}.Run("[1]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: []byte("\xff"),
Values: mod.Values{true}.AddVariants(mod.All...),
}.Run("[255]", t, nil, unmarshal)
})
}
}

0 comments on commit be3b863

Please sign in to comment.