mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-23 20:07:17 +00:00
c632b96454
* Starting * Update logging for service * Update logging for assignment * Update logging for attest * Update logging for propose * Update logging for balance update * Final touchup * Fixed test * Fixed test * Feedback * Fix * Fix all the tests
208 lines
4.3 KiB
Go
208 lines
4.3 KiB
Go
package bytesutil
|
|
|
|
import (
|
|
"bytes"
|
|
"testing"
|
|
)
|
|
|
|
func TestToBytes(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0}},
|
|
{1, []byte{1}},
|
|
{2, []byte{2}},
|
|
{253, []byte{253}},
|
|
{254, []byte{254}},
|
|
{255, []byte{255}},
|
|
{0, []byte{0, 0}},
|
|
{1, []byte{1, 0}},
|
|
{255, []byte{255, 0}},
|
|
{256, []byte{0, 1}},
|
|
{65534, []byte{254, 255}},
|
|
{65535, []byte{255, 255}},
|
|
{0, []byte{0, 0, 0}},
|
|
{255, []byte{255, 0, 0}},
|
|
{256, []byte{0, 1, 0}},
|
|
{65535, []byte{255, 255, 0}},
|
|
{65536, []byte{0, 0, 1}},
|
|
{16777215, []byte{255, 255, 255}},
|
|
{0, []byte{0, 0, 0, 0}},
|
|
{256, []byte{0, 1, 0, 0}},
|
|
{65536, []byte{0, 0, 1, 0}},
|
|
{16777216, []byte{0, 0, 0, 1}},
|
|
{16777217, []byte{1, 0, 0, 1}},
|
|
{4294967295, []byte{255, 255, 255, 255}},
|
|
{0, []byte{0, 0, 0, 0, 0, 0, 0, 0}},
|
|
{16777216, []byte{0, 0, 0, 1, 0, 0, 0, 0}},
|
|
{4294967296, []byte{0, 0, 0, 0, 1, 0, 0, 0}},
|
|
{4294967297, []byte{1, 0, 0, 0, 1, 0, 0, 0}},
|
|
{9223372036854775806, []byte{254, 255, 255, 255, 255, 255, 255, 127}},
|
|
{9223372036854775807, []byte{255, 255, 255, 255, 255, 255, 255, 127}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := ToBytes(tt.a, len(tt.b))
|
|
if !bytes.Equal(b, tt.b) {
|
|
t.Errorf("Bytes1(%d) = %v, want = %d", tt.a, b, tt.b)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBytes1(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0}},
|
|
{1, []byte{1}},
|
|
{2, []byte{2}},
|
|
{253, []byte{253}},
|
|
{254, []byte{254}},
|
|
{255, []byte{255}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := Bytes1(tt.a)
|
|
if !bytes.Equal(b, tt.b) {
|
|
t.Errorf("Bytes1(%d) = %v, want = %d", tt.a, b, tt.b)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBytes2(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0, 0}},
|
|
{1, []byte{1, 0}},
|
|
{255, []byte{255, 0}},
|
|
{256, []byte{0, 1}},
|
|
{65534, []byte{254, 255}},
|
|
{65535, []byte{255, 255}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := Bytes2(tt.a)
|
|
if !bytes.Equal(b, tt.b) {
|
|
t.Errorf("Bytes2(%d) = %v, want = %d", tt.a, b, tt.b)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBytes3(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0, 0, 0}},
|
|
{255, []byte{255, 0, 0}},
|
|
{256, []byte{0, 1, 0}},
|
|
{65535, []byte{255, 255, 0}},
|
|
{65536, []byte{0, 0, 1}},
|
|
{16777215, []byte{255, 255, 255}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := Bytes3(tt.a)
|
|
if !bytes.Equal(b, tt.b) {
|
|
t.Errorf("Bytes3(%d) = %v, want = %d", tt.a, b, tt.b)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBytes4(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0, 0, 0, 0}},
|
|
{256, []byte{0, 1, 0, 0}},
|
|
{65536, []byte{0, 0, 1, 0}},
|
|
{16777216, []byte{0, 0, 0, 1}},
|
|
{16777217, []byte{1, 0, 0, 1}},
|
|
{4294967295, []byte{255, 255, 255, 255}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := Bytes4(tt.a)
|
|
if !bytes.Equal(b, tt.b) {
|
|
t.Errorf("Bytes4(%d) = %v, want = %d", tt.a, b, tt.b)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestBytes8(t *testing.T) {
|
|
tests := []struct {
|
|
a uint64
|
|
b []byte
|
|
}{
|
|
{0, []byte{0, 0, 0, 0, 0, 0, 0, 0}},
|
|
{16777216, []byte{0, 0, 0, 1, 0, 0, 0, 0}},
|
|
{4294967296, []byte{0, 0, 0, 0, 1, 0, 0, 0}},
|
|
{4294967297, []byte{1, 0, 0, 0, 1, 0, 0, 0}},
|
|
{9223372036854775806, []byte{254, 255, 255, 255, 255, 255, 255, 127}},
|
|
{9223372036854775807, []byte{255, 255, 255, 255, 255, 255, 255, 127}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := Bytes8(tt.a)
|
|
if !bytes.Equal(b, tt.b) {
|
|
t.Errorf("Bytes8(%d) = %v, want = %d", tt.a, b, tt.b)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFromBytes4(t *testing.T) {
|
|
tests := []uint64{
|
|
0,
|
|
1776,
|
|
96726,
|
|
4290997,
|
|
4294967295, //2^32 - 1
|
|
4294967200,
|
|
3894948296,
|
|
}
|
|
for _, tt := range tests {
|
|
b := ToBytes(tt, 4)
|
|
c := FromBytes4(b)
|
|
if c != tt {
|
|
t.Errorf("Wanted %d but got %d", tt, c)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestFromBytes8(t *testing.T) {
|
|
tests := []uint64{
|
|
0,
|
|
1776,
|
|
96726,
|
|
4290997,
|
|
922376854775806,
|
|
42893720984775807,
|
|
18446744073709551615,
|
|
}
|
|
for _, tt := range tests {
|
|
b := ToBytes(tt, 8)
|
|
c := FromBytes8(b)
|
|
if c != tt {
|
|
t.Errorf("Wanted %d but got %d", tt, c)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTruncate(t *testing.T) {
|
|
tests := []struct {
|
|
a []byte
|
|
b []byte
|
|
}{
|
|
{[]byte{'A', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O'},
|
|
[]byte{'A', 'C', 'D', 'E', 'F', 'G'}},
|
|
{[]byte{'A', 'C', 'D', 'E', 'F'},
|
|
[]byte{'A', 'C', 'D', 'E', 'F'}},
|
|
{[]byte{}, []byte{}},
|
|
}
|
|
for _, tt := range tests {
|
|
b := Trunc(tt.a)
|
|
if !bytes.Equal(b, tt.b) {
|
|
t.Errorf("Trunc(%d) = %v, want = %d", tt.a, b, tt.b)
|
|
}
|
|
}
|
|
}
|