Skip to content

Commit

Permalink
fix and add varint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Nov 25, 2024
1 parent 8446db5 commit d7394ef
Show file tree
Hide file tree
Showing 6 changed files with 771 additions and 526 deletions.
4 changes: 2 additions & 2 deletions cassandra_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1900,8 +1900,8 @@ func TestVarint(t *testing.T) {
}

err := session.Query("SELECT test FROM varint_test").Scan(&result64)
if err == nil || strings.Index(err.Error(), "out of range") == -1 {
t.Errorf("expected out of range error since value is too big for int64")
if err == nil || strings.Index(err.Error(), "the data value should be in the int64 range") == -1 {
t.Errorf("expected out of range error since value is too big for int64, result:%d", result64)
}

// value not set in cassandra, leave bind variable empty
Expand Down
192 changes: 0 additions & 192 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,55 +275,6 @@ var marshalTests = []struct {
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte("\x00"),
0,
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte("\x37\xE2\x3C\xEC"),
int32(937573612),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte("\x37\xE2\x3C\xEC"),
big.NewInt(937573612),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte("\x03\x9EV \x15\f\x03\x9DK\x18\xCDI\\$?\a["),
bigintize("1231312312331283012830129382342342412123"), // From the iconara/cql-rb test suite
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte("\xC9v\x8D:\x86"),
big.NewInt(-234234234234), // From the iconara/cql-rb test suite
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte("f\x1e\xfd\xf2\xe3\xb1\x9f|\x04_\x15"),
bigintize("123456789123456789123456789"), // From the datastax/python-driver test suite
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarint},
[]byte(nil),
nil,
nil,
unmarshalErrorf("can not unmarshal into non-pointer <nil>"),
},
{
NativeType{proto: 2, typ: TypeInet},
[]byte("\x7F\x00\x00\x01"),
Expand Down Expand Up @@ -751,149 +702,6 @@ func TestMarshal_Decode(t *testing.T) {
}
}

func TestMarshalVarint(t *testing.T) {
varintTests := []struct {
Value interface{}
Marshaled []byte
Unmarshaled *big.Int
}{
{
Value: int8(0),
Marshaled: []byte("\x00"),
Unmarshaled: big.NewInt(0),
},
{
Value: uint8(255),
Marshaled: []byte("\x00\xFF"),
Unmarshaled: big.NewInt(255),
},
{
Value: int8(-1),
Marshaled: []byte("\xFF"),
Unmarshaled: big.NewInt(-1),
},
{
Value: big.NewInt(math.MaxInt32),
Marshaled: []byte("\x7F\xFF\xFF\xFF"),
Unmarshaled: big.NewInt(math.MaxInt32),
},
{
Value: big.NewInt(int64(math.MaxInt32) + 1),
Marshaled: []byte("\x00\x80\x00\x00\x00"),
Unmarshaled: big.NewInt(int64(math.MaxInt32) + 1),
},
{
Value: big.NewInt(math.MinInt32),
Marshaled: []byte("\x80\x00\x00\x00"),
Unmarshaled: big.NewInt(math.MinInt32),
},
{
Value: big.NewInt(int64(math.MinInt32) - 1),
Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF"),
Unmarshaled: big.NewInt(int64(math.MinInt32) - 1),
},
{
Value: math.MinInt64,
Marshaled: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"),
Unmarshaled: big.NewInt(math.MinInt64),
},
{
Value: uint64(math.MaxInt64) + 1,
Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
Unmarshaled: bigintize("9223372036854775808"),
},
{
Value: bigintize("2361183241434822606848"), // 2**71
Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00\x00"),
Unmarshaled: bigintize("2361183241434822606848"),
},
{
Value: bigintize("-9223372036854775809"), // -2**63 - 1
Marshaled: []byte("\xFF\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
Unmarshaled: bigintize("-9223372036854775809"),
},
}

for i, test := range varintTests {
data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
if err != nil {
t.Errorf("error marshaling varint: %v (test #%d)", err, i)
}

if !bytes.Equal(test.Marshaled, data) {
t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
}

binder := new(big.Int)
err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, binder)
if err != nil {
t.Errorf("error unmarshaling varint: %v (test #%d)", err, i)
}

if test.Unmarshaled.Cmp(binder) != 0 {
t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
}
}

varintUint64Tests := []struct {
Value interface{}
Marshaled []byte
Unmarshaled uint64
}{
{
Value: int8(0),
Marshaled: []byte("\x00"),
Unmarshaled: 0,
},
{
Value: uint8(255),
Marshaled: []byte("\x00\xFF"),
Unmarshaled: 255,
},
{
Value: big.NewInt(math.MaxInt32),
Marshaled: []byte("\x7F\xFF\xFF\xFF"),
Unmarshaled: uint64(math.MaxInt32),
},
{
Value: big.NewInt(int64(math.MaxInt32) + 1),
Marshaled: []byte("\x00\x80\x00\x00\x00"),
Unmarshaled: uint64(int64(math.MaxInt32) + 1),
},
{
Value: uint64(math.MaxInt64) + 1,
Marshaled: []byte("\x00\x80\x00\x00\x00\x00\x00\x00\x00"),
Unmarshaled: 9223372036854775808,
},
{
Value: uint64(math.MaxUint64),
Marshaled: []byte("\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"),
Unmarshaled: uint64(math.MaxUint64),
},
}

for i, test := range varintUint64Tests {
data, err := Marshal(NativeType{proto: 2, typ: TypeVarint}, test.Value)
if err != nil {
t.Errorf("error marshaling varint: %v (test #%d)", err, i)
}

if !bytes.Equal(test.Marshaled, data) {
t.Errorf("marshaled varint mismatch: expected %v, got %v (test #%d)", test.Marshaled, data, i)
}

var binder uint64
err = Unmarshal(NativeType{proto: 2, typ: TypeVarint}, test.Marshaled, &binder)
if err != nil {
t.Errorf("error unmarshaling varint to uint64: %v (test #%d)", err, i)
}

if test.Unmarshaled != binder {
t.Errorf("unmarshaled varint mismatch: expected %v, got %v (test #%d)", test.Unmarshaled, binder, i)
}
}
}

func equalStringPointerSlice(leftList, rightList []*string) bool {
if len(leftList) != len(rightList) {
return false
Expand Down
73 changes: 73 additions & 0 deletions serialization/varint/marshal_bigint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package varint

import (
"bytes"
"math"
"math/big"
"testing"
)

func TestEnc2BigInt(t *testing.T) {
t.Parallel()

genData := func(v int64) []byte {
data := []byte{byte(v >> 56), byte(v >> 48), byte(v >> 40), byte(v >> 32), byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)}
out := make([]byte, 0)
add := false

for i, b := range data {
if !add {
if v < 0 {
if b != 255 || b == 255 && data[i+1] < 128 {
add = true
} else {
continue
}
} else {
if b != 0 || b == 0 && data[i+1] > 127 {
add = true
} else {
continue
}
}
}
out = append(out, b)
}

return out
}

t.Run("positive", func(t *testing.T) {
t.Parallel()
for i := int64(math.MaxInt16); i < 1<<24; i++ {
expected := genData(i)

received := EncBigIntRS(big.NewInt(i))
if !bytes.Equal(expected, received) {
t.Fatalf("%d\nexpected:%x\nreceived:%x", i, expected, received)
}

received = encInt64(i)
if !bytes.Equal(expected, received) {
t.Fatalf("%d\nexpected:%x\nreceived:%x", i, expected, received)
}
}
})

t.Run("negative", func(t *testing.T) {
t.Parallel()
for i := int64(math.MinInt16); i > -1<<24; i-- {
expected := genData(i)

received := EncBigIntRS(big.NewInt(i))
if !bytes.Equal(expected, received) {
t.Fatalf("%d\nexpected:%x\nreceived:%x", i, expected, received)
}

received = encInt64(i)
if !bytes.Equal(expected, received) {
t.Fatalf("%d\nexpected:%x\nreceived:%x", i, expected, received)
}
}
})
}
73 changes: 73 additions & 0 deletions serialization/varint/unmarshal_bigint_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package varint

import (
"math"
"math/big"
"testing"
)

func TestDec2BigInt(t *testing.T) {
t.Parallel()

genData := func(v int64) []byte {
data := []byte{byte(v >> 56), byte(v >> 48), byte(v >> 40), byte(v >> 32), byte(v >> 24), byte(v >> 16), byte(v >> 8), byte(v)}
out := make([]byte, 0)
add := false
for i, b := range data {
if !add {
if v < 0 {
if b != 255 || b == 255 && data[i+1] < 128 {
add = true
} else {
continue
}
} else {
if b != 0 || b == 0 && data[i+1] > 127 {
add = true
} else {
continue
}
}
}
out = append(out, b)
}

return out
}

t.Run("positive", func(t *testing.T) {
t.Parallel()
for i := int64(math.MaxInt16); i < 1<<23; i++ {
data := genData(i)
expected := big.NewInt(i)

received := Dec2BigInt(data)
if expected.Cmp(received) != 0 {
t.Fatalf("%d\nexpected:%s\nreceived:%s", i, expected, received)
}

_ = DecBigInt(data, received)
if expected.Cmp(received) != 0 {
t.Fatalf("%d\nexpected:%s\nreceived:%s", i, expected, received)
}
}
})

t.Run("negative", func(t *testing.T) {
t.Parallel()
for i := int64(math.MinInt16); i > -1<<23; i-- {
data := genData(i)
expected := big.NewInt(i)

received := Dec2BigInt(data)
if expected.Cmp(received) != 0 {
t.Fatalf("%d\nexpected:%s\nreceived:%s", i, expected, received)
}

_ = DecBigInt(data, received)
if expected.Cmp(received) != 0 {
t.Fatalf("%d\nexpected:%s\nreceived:%s", i, expected, received)
}
}
})
}
Loading

0 comments on commit d7394ef

Please sign in to comment.