8 "github.com/holiman/uint256"
11 func TestBoolBytes(t *testing.T) {
12 got := BoolBytes(true)
14 if !bytes.Equal(got, want) {
15 t.Errorf("BoolBytes(t) = %x want %x", got, want)
18 got = BoolBytes(false)
20 if !bytes.Equal(got, want) {
21 t.Errorf("BoolBytes(f) = %x want %x", got, want)
25 func TestAsBool(t *testing.T) {
30 {[]byte{0, 0, 0, 0}, false},
34 {[]byte{1, 1, 1, 1}, true},
35 {[]byte{0, 0, 0, 1}, true},
36 {[]byte{1, 0, 0, 0}, true},
40 for _, c := range cases {
44 t.Errorf("AsBool(%x) = %v want %v", c.data, got, c.want)
49 func TestInt64(t *testing.T) {
57 {256, []byte{0x00, 0x01}},
58 {1 << 16, []byte{0x00, 0x00, 0x01}},
59 {-1, []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
60 {-2, []byte{0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}},
63 for _, c := range cases {
64 gotData := Int64Bytes(c.num)
66 if !bytes.Equal(gotData, c.data) {
67 t.Errorf("Int64Bytes(%d) = %x want %x", c.num, gotData, c.data)
70 gotNum, _ := AsInt64(c.data)
73 t.Errorf("AsInt64(%x) = %d want %d", c.data, gotNum, c.num)
77 data := []byte{1, 1, 1, 1, 1, 1, 1, 1, 1}
78 _, err := AsInt64(data)
81 t.Errorf("AsInt64(%x) = %v want %v", data, err, want)
85 func TestBigIntBytes(t *testing.T) {
90 {num: new(big.Int), input: []byte{}},
91 {num: new(big.Int).SetInt64(0), input: []byte{}},
92 {num: new(big.Int).SetInt64(1), input: []byte{0x01}},
93 {num: new(big.Int).SetInt64(255), input: []byte{0xff}},
94 {num: new(big.Int).SetInt64(256), input: []byte{0x00, 0x01}},
95 {num: new(big.Int).SetInt64(46657), input: []byte{0x41, 0xb6}},
96 {num: new(big.Int).SetInt64(1 << 32), input: []byte{0x00, 0x00, 0x00, 0x00, 0x01}},
98 num: new(big.Int).Exp(new(big.Int).SetInt64(10), new(big.Int).SetInt64(32), nil),
99 input: []byte{0x00, 0x00, 0x00, 0x00, 0x81, 0xef, 0xac, 0x85, 0x5b, 0x41, 0x6d, 0x2d, 0xee, 0x04},
101 {num: new(big.Int).SetInt64(-1), input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
102 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
103 0xff, 0xff, 0xff, 0xff}},
104 {num: new(big.Int).SetInt64(-256), input: []byte{0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
105 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
106 0xff, 0xff, 0xff, 0xff}},
108 for _, test := range tests {
109 fromBig, b := uint256.FromBig(test.num)
111 t.Errorf("FromBig overflow")
114 gotData := BigIntBytes(fromBig)
115 if !bytes.Equal(gotData, test.input) {
116 t.Errorf("BigIntBytes(%s) = %x want %x", test.num.String(), gotData, test.input)
121 func TestAsBigInt(t *testing.T) {
127 {num: new(big.Int), input: []byte{}},
128 {num: new(big.Int), input: []byte{0x00}},
129 {num: new(big.Int).SetInt64(0), input: []byte{0x00}},
130 {num: new(big.Int).SetInt64(1), input: []byte{0x01}},
131 {num: new(big.Int).SetInt64(255), input: []byte{0xff}},
132 {num: new(big.Int).SetInt64(256), input: []byte{0x00, 0x01}},
133 {num: new(big.Int).SetInt64(46657), input: []byte{0x41, 0xb6}},
134 {num: new(big.Int).SetInt64(1 << 32), input: []byte{0x00, 0x00, 0x00, 0x00, 0x01}},
136 num: new(big.Int).Exp(new(big.Int).SetInt64(10), new(big.Int).SetInt64(32), nil),
137 input: []byte{0x00, 0x00, 0x00, 0x00, 0x81, 0xef, 0xac, 0x85, 0x5b, 0x41, 0x6d, 0x2d, 0xee, 0x04},
139 {num: new(big.Int).SetInt64(-1), input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, wantError: true},
140 {num: new(big.Int).SetInt64(-256), input: []byte{0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00}, wantError: true},
141 {input: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, wantError: true},
143 for _, test := range tests {
144 data, err := AsBigInt(test.input)
149 t.Errorf("AsBigInt(%q) --> err %s", test.input, err.Error())
152 fromBig, b := uint256.FromBig(test.num)
154 t.Errorf("FromBig overflow")
157 if data != nil && !data.Eq(fromBig) {
158 t.Errorf("AsBigInt(%s) = %x want %x", test.num.String(), data, test.input)