github.com/jonasnick/go-ethereum@v0.7.12-0.20150216215225-22176f05d387/accounts/abi/numbers_test.go (about) 1 package abi 2 3 import ( 4 "bytes" 5 "math/big" 6 "reflect" 7 "testing" 8 ) 9 10 func TestNumberTypes(t *testing.T) { 11 ubytes := make([]byte, 32) 12 ubytes[31] = 1 13 sbytesmin := []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} 14 15 unsigned := U256(big.NewInt(1)) 16 if !bytes.Equal(unsigned, ubytes) { 17 t.Error("expected %x got %x", ubytes, unsigned) 18 } 19 20 signed := S256(big.NewInt(1)) 21 if !bytes.Equal(signed, ubytes) { 22 t.Error("expected %x got %x", ubytes, unsigned) 23 } 24 25 signed = S256(big.NewInt(-1)) 26 if !bytes.Equal(signed, sbytesmin) { 27 t.Error("expected %x got %x", ubytes, unsigned) 28 } 29 } 30 31 func TestPackNumber(t *testing.T) { 32 ubytes := make([]byte, 32) 33 ubytes[31] = 1 34 sbytesmin := []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} 35 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} 36 37 packed := packNum(reflect.ValueOf(1), IntTy) 38 if !bytes.Equal(packed, ubytes) { 39 t.Errorf("expected %x got %x", ubytes, packed) 40 } 41 packed = packNum(reflect.ValueOf(-1), IntTy) 42 if !bytes.Equal(packed, sbytesmin) { 43 t.Errorf("expected %x got %x", ubytes, packed) 44 } 45 packed = packNum(reflect.ValueOf(1), UintTy) 46 if !bytes.Equal(packed, ubytes) { 47 t.Errorf("expected %x got %x", ubytes, packed) 48 } 49 packed = packNum(reflect.ValueOf(-1), UintTy) 50 if !bytes.Equal(packed, maxunsigned) { 51 t.Errorf("expected %x got %x", maxunsigned, packed) 52 } 53 54 packed = packNum(reflect.ValueOf("string"), UintTy) 55 if packed != nil { 56 t.Errorf("expected 'string' to pack to nil. got %x instead", packed) 57 } 58 } 59 60 func TestSigned(t *testing.T) { 61 if isSigned(reflect.ValueOf(uint(10))) { 62 t.Error() 63 } 64 65 if !isSigned(reflect.ValueOf(int(10))) { 66 t.Error() 67 } 68 69 if !isSigned(reflect.ValueOf(big.NewInt(10))) { 70 t.Error() 71 } 72 }