Add additional tests to bytesutil (#11877)

* Add missing tests from bytes.go and integers.go

* Fix failing test

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
This commit is contained in:
Sammy Rosso
2023-01-17 18:54:43 +01:00
committed by GitHub
parent 7c9bff489e
commit db6b1c15c4
2 changed files with 82 additions and 0 deletions

View File

@@ -213,3 +213,38 @@ func BenchmarkToBytes32(b *testing.B) {
bytesutil.ToBytes32(x)
}
}
func TestFromBytes48Array(t *testing.T) {
tests := []struct {
a [][]byte
b [][48]byte
}{
{[][]byte{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[][48]byte{{0}}},
{[][]byte{{253, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[][48]byte{{253}}},
{[][]byte{{254, 255, 255, 255, 255, 255, 255, 127, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
[][48]byte{{254, 255, 255, 255, 255, 255, 255, 127}}},
{[][]byte{{255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255}},
[][48]byte{{255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255}},
},
}
for _, tt := range tests {
a := bytesutil.FromBytes48Array(tt.b)
assert.DeepEqual(t, tt.a, a)
}
}

View File

@@ -143,6 +143,30 @@ func TestBytes8(t *testing.T) {
}
}
func TestBytes32(t *testing.T) {
tests := []struct {
a uint64
b []byte
}{
{0,
[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{16777216,
[]byte{0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{4294967296,
[]byte{0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{4294967297,
[]byte{1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{9223372036854775806,
[]byte{254, 255, 255, 255, 255, 255, 255, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
{9223372036854775807,
[]byte{255, 255, 255, 255, 255, 255, 255, 127, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
}
for _, tt := range tests {
b := bytesutil.Bytes32(tt.a)
assert.DeepEqual(t, tt.b, b)
}
}
func TestFromBool(t *testing.T) {
tests := []byte{
0,
@@ -253,3 +277,26 @@ func TestBigIntToLittleEndianBytes(t *testing.T) {
converted := bytesutil.BigIntToLittleEndianBytes(bigInt)
assert.DeepEqual(t, expected, converted)
}
func TestUint64ToBytesLittleEndian(t *testing.T) {
tests := []struct {
value uint64
want [8]byte
}{
{
value: 0x01000000,
want: [8]byte{0, 0, 0, 1, 0, 0, 0, 0},
},
{
value: 0x00000001,
want: [8]byte{1, 0, 0, 0, 0, 0, 0, 0},
},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("0x%08x", tt.value), func(t *testing.T) {
if got := bytesutil.Uint64ToBytesLittleEndian(tt.value); !bytes.Equal(got, tt.want[:]) {
t.Errorf("Uint64ToBytesLittleEndian() = got %v, want %v", got, tt.want)
}
})
}
}