@@ -18,6 +18,7 @@ package abi
18
18
19
19
import (
20
20
"bytes"
21
+ "math"
21
22
"math/big"
22
23
"reflect"
23
24
"testing"
@@ -34,21 +35,38 @@ func TestNumberTypes(t *testing.T) {
34
35
}
35
36
36
37
func TestPackNumber (t * testing.T ) {
37
- ubytes := make ([]byte , 32 )
38
- ubytes [31 ] = 1
39
- 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 }
38
+ tests := []struct {
39
+ value reflect.Value
40
+ packed []byte
41
+ }{
42
+ // Protocol limits
43
+ {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 }},
44
+ {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 }},
45
+ {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 }},
46
+
47
+ // Type corner cases
48
+ {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 }},
49
+ {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 }},
50
+ {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 }},
51
+ {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 }},
40
52
41
- packed := packNum (reflect .ValueOf (1 ))
42
- if ! bytes .Equal (packed , ubytes ) {
43
- t .Errorf ("expected %x got %x" , ubytes , packed )
53
+ {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 }},
54
+ {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 }},
55
+ {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 }},
56
+ {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 }},
57
+
58
+ {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 }},
59
+ {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 }},
60
+ {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 }},
61
+ {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 }},
44
62
}
45
- packed = packNum (reflect .ValueOf (- 1 ))
46
- if ! bytes .Equal (packed , maxunsigned ) {
47
- t .Errorf ("expected %x got %x" , maxunsigned , packed )
63
+ for i , tt := range tests {
64
+ packed := packNum (tt .value )
65
+ if ! bytes .Equal (packed , tt .packed ) {
66
+ t .Errorf ("test %d: pack mismatch: have %x, want %x" , i , packed , tt .packed )
67
+ }
48
68
}
49
-
50
- packed = packNum (reflect .ValueOf ("string" ))
51
- if packed != nil {
69
+ if packed := packNum (reflect .ValueOf ("string" )); packed != nil {
52
70
t .Errorf ("expected 'string' to pack to nil. got %x instead" , packed )
53
71
}
54
72
}
0 commit comments