Files
evm/utils/bytes_test.go
T
Ceyhun OnurandGitHub 19a0e5bf99 move predicate package and merge with predicate utils (#907)
* add get predicate result bytes helpers

* move predicate package and merge with predicate utils

* update readme

* refactor imports & utils

* typed errs

* add comment

* fix import name

* fix stutter & same var names as pkg name

* rename functions

* fix reviews

* more rename
2023-10-02 16:55:55 -04:00

67 lines
1.4 KiB
Go

// (c) 2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package utils
import (
"bytes"
"testing"
"github.com/ava-labs/avalanchego/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIncrOne(t *testing.T) {
type test struct {
input []byte
expected []byte
}
for name, test := range map[string]test{
"increment no overflow no carry": {
input: []byte{0, 0},
expected: []byte{0, 1},
},
"increment overflow": {
input: []byte{255, 255},
expected: []byte{0, 0},
},
"increment carry": {
input: []byte{0, 255},
expected: []byte{1, 0},
},
} {
t.Run(name, func(t *testing.T) {
output := common.CopyBytes(test.input)
IncrOne(output)
assert.Equal(t, output, test.expected)
})
}
}
func testBytesToHashSlice(t testing.TB, b []byte) {
hashSlice := BytesToHashSlice(b)
copiedBytes := HashSliceToBytes(hashSlice)
if len(b)%32 == 0 {
require.Equal(t, b, copiedBytes)
} else {
require.Equal(t, b, copiedBytes[:len(b)])
// Require that any additional padding is all zeroes
padding := copiedBytes[len(b):]
require.Equal(t, bytes.Repeat([]byte{0x00}, len(padding)), padding)
}
}
func FuzzHashSliceToBytes(f *testing.F) {
for i := 0; i < 100; i++ {
f.Add(utils.RandomBytes(i))
}
f.Fuzz(func(t *testing.T, b []byte) {
testBytesToHashSlice(t, b)
})
}