Skip to content

Commit

Permalink
test(util): add random test for var int (#1533)
Browse files Browse the repository at this point in the history
Co-authored-by: Javad Rajabzadeh <ja7ad@live.com>
  • Loading branch information
b00f and Ja7ad authored Oct 9, 2024
1 parent 20d101d commit 55cf023
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions util/encoding/encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import (
"github.com/pactus-project/pactus/crypto/hash"
"github.com/pactus-project/pactus/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/rand"
)

// TestElementEncoding tests encode and decode for various element types. This
Expand Down Expand Up @@ -355,6 +357,12 @@ func TestVarIntError(t *testing.T) {
in []byte // Value to decode
readErr error
}{
{
[]byte{0x98, 0}, ErrNonCanonical,
},
{
[]byte{0xFF}, io.EOF,
},
{
[]byte{0x80, 0x00}, ErrNonCanonical,
},
Expand All @@ -378,16 +386,17 @@ func TestVarIntError(t *testing.T) {
}
}

func TestVarIntOverflow(t *testing.T) {
var buf bytes.Buffer
buf.Write([]byte{0xff, 0x00})
_, err := ReadVarInt(&buf)
assert.Error(t, err)

buf.Reset()
buf.Write([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
_, err = ReadVarInt(&buf)
assert.Error(t, err)
func TestVarIntRandom(t *testing.T) {
randInt1 := uint64(rand.Int63())
var wBuf bytes.Buffer
err := WriteVarInt(&wBuf, randInt1)
require.NoError(t, err)

rBuf := bytes.NewReader(wBuf.Bytes())
randInt2, err := ReadVarInt(rBuf)
require.NoError(t, err)

assert.Equal(t, randInt1, randInt2)
}

func TestWriteElements(t *testing.T) {
Expand Down

0 comments on commit 55cf023

Please sign in to comment.