Implement [][] to [][48] helper (#11606)

This commit is contained in:
terencechain 2022-11-02 11:04:40 -07:00 committed by GitHub
parent a0c5669511
commit ead329e610
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 0 deletions

View File

@ -117,6 +117,16 @@ func ToBytes48(x []byte) [48]byte {
return y
}
// ToBytes48Array is a convenience method for converting an array of
// byte slices to an array of fixed-sized byte arrays.
func ToBytes48Array(x [][]byte) [][48]byte {
y := make([][48]byte, len(x))
for i := range x {
y[i] = ToBytes48(x[i])
}
return y
}
// ToBytes64 is a convenience method for converting a byte slice to a fix
// sized 64 byte array. This method will truncate the input if it is larger
// than 64 bytes.

View File

@ -606,3 +606,31 @@ func TestUint32ToBytes4(t *testing.T) {
})
}
}
func TestToBytes48Array(t *testing.T) {
tests := []struct {
a [][]byte
b [][48]byte
}{
{[][]byte{{0}}, [][48]byte{{0}}},
{[][]byte{{253}}, [][48]byte{{253}}},
{[][]byte{{254, 255, 255, 255, 255, 255, 255, 127}},
[][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, 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 {
b := bytesutil.ToBytes48Array(tt.a)
assert.DeepEqual(t, tt.b, b)
}
}