From db6b1c15c44e9aa1df0cc193adc57cf957fc19f3 Mon Sep 17 00:00:00 2001 From: Sammy Rosso <15244892+saolyn@users.noreply.github.com> Date: Tue, 17 Jan 2023 18:54:43 +0100 Subject: [PATCH] Add additional tests to bytesutil (#11877) * Add missing tests from bytes.go and integers.go * Fix failing test Co-authored-by: Raul Jordan --- encoding/bytesutil/bytes_test.go | 35 +++++++++++++++++++++ encoding/bytesutil/integers_test.go | 47 +++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/encoding/bytesutil/bytes_test.go b/encoding/bytesutil/bytes_test.go index bdbde02a29..f266ff2b08 100644 --- a/encoding/bytesutil/bytes_test.go +++ b/encoding/bytesutil/bytes_test.go @@ -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) + } +} diff --git a/encoding/bytesutil/integers_test.go b/encoding/bytesutil/integers_test.go index 2dbc2a8709..38f40e9492 100644 --- a/encoding/bytesutil/integers_test.go +++ b/encoding/bytesutil/integers_test.go @@ -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) + } + }) + } +}