aboutsummaryrefslogtreecommitdiffstats
path: root/accounts/abi/numbers_test.go
diff options
context:
space:
mode:
authorPéter Szilágyi <peterke@gmail.com>2016-06-10 15:35:10 +0800
committerPéter Szilágyi <peterke@gmail.com>2016-06-10 17:32:08 +0800
commit0f9539e1e3e77bb181d67591cfbb77f6a17e5537 (patch)
treecb957cfbcf678ebc5919727109e8f0015ee4a055 /accounts/abi/numbers_test.go
parent63d1d145e2b2c8db4106d87767842019492e0aea (diff)
downloadgo-tangerine-0f9539e1e3e77bb181d67591cfbb77f6a17e5537.tar
go-tangerine-0f9539e1e3e77bb181d67591cfbb77f6a17e5537.tar.gz
go-tangerine-0f9539e1e3e77bb181d67591cfbb77f6a17e5537.tar.bz2
go-tangerine-0f9539e1e3e77bb181d67591cfbb77f6a17e5537.tar.lz
go-tangerine-0f9539e1e3e77bb181d67591cfbb77f6a17e5537.tar.xz
go-tangerine-0f9539e1e3e77bb181d67591cfbb77f6a17e5537.tar.zst
go-tangerine-0f9539e1e3e77bb181d67591cfbb77f6a17e5537.zip
accounts/abi: fix uint64 upper range encoding.
Diffstat (limited to 'accounts/abi/numbers_test.go')
-rw-r--r--accounts/abi/numbers_test.go42
1 files changed, 30 insertions, 12 deletions
diff --git a/accounts/abi/numbers_test.go b/accounts/abi/numbers_test.go
index f409aa60f..44afe8647 100644
--- a/accounts/abi/numbers_test.go
+++ b/accounts/abi/numbers_test.go
@@ -18,6 +18,7 @@ package abi
import (
"bytes"
+ "math"
"math/big"
"reflect"
"testing"
@@ -34,21 +35,38 @@ func TestNumberTypes(t *testing.T) {
}
func TestPackNumber(t *testing.T) {
- ubytes := make([]byte, 32)
- ubytes[31] = 1
- maxunsigned := []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}
+ tests := []struct {
+ value reflect.Value
+ packed []byte
+ }{
+ // Protocol limits
+ {reflect.ValueOf(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}},
+ {reflect.ValueOf(1), []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, 1}},
+ {reflect.ValueOf(-1), []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}},
+
+ // Type corner cases
+ {reflect.ValueOf(uint8(math.MaxUint8)), []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, 255}},
+ {reflect.ValueOf(uint16(math.MaxUint16)), []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, 255, 255}},
+ {reflect.ValueOf(uint32(math.MaxUint32)), []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, 255, 255, 255, 255}},
+ {reflect.ValueOf(uint64(math.MaxUint64)), []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, 255, 255, 255, 255, 255, 255, 255, 255}},
- packed := packNum(reflect.ValueOf(1))
- if !bytes.Equal(packed, ubytes) {
- t.Errorf("expected %x got %x", ubytes, packed)
+ {reflect.ValueOf(int8(math.MaxInt8)), []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, 127}},
+ {reflect.ValueOf(int16(math.MaxInt16)), []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, 127, 255}},
+ {reflect.ValueOf(int32(math.MaxInt32)), []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, 127, 255, 255, 255}},
+ {reflect.ValueOf(int64(math.MaxInt64)), []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, 127, 255, 255, 255, 255, 255, 255, 255}},
+
+ {reflect.ValueOf(int8(math.MinInt8)), []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, 128}},
+ {reflect.ValueOf(int16(math.MinInt16)), []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, 128, 0}},
+ {reflect.ValueOf(int32(math.MinInt32)), []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, 128, 0, 0, 0}},
+ {reflect.ValueOf(int64(math.MinInt64)), []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, 128, 0, 0, 0, 0, 0, 0, 0}},
}
- packed = packNum(reflect.ValueOf(-1))
- if !bytes.Equal(packed, maxunsigned) {
- t.Errorf("expected %x got %x", maxunsigned, packed)
+ for i, tt := range tests {
+ packed := packNum(tt.value)
+ if !bytes.Equal(packed, tt.packed) {
+ t.Errorf("test %d: pack mismatch: have %x, want %x", i, packed, tt.packed)
+ }
}
-
- packed = packNum(reflect.ValueOf("string"))
- if packed != nil {
+ if packed := packNum(reflect.ValueOf("string")); packed != nil {
t.Errorf("expected 'string' to pack to nil. got %x instead", packed)
}
}