mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 23:18:15 -05:00
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:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user