github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/common/math/big_test.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 19:16:34</date> 10 //</624450073891180544> 11 12 13 package math 14 15 import ( 16 "bytes" 17 "encoding/hex" 18 "math/big" 19 "testing" 20 21 "github.com/ethereum/go-ethereum/common" 22 ) 23 24 func TestHexOrDecimal256(t *testing.T) { 25 tests := []struct { 26 input string 27 num *big.Int 28 ok bool 29 }{ 30 {"", big.NewInt(0), true}, 31 {"0", big.NewInt(0), true}, 32 {"0x0", big.NewInt(0), true}, 33 {"12345678", big.NewInt(12345678), true}, 34 {"0x12345678", big.NewInt(0x12345678), true}, 35 {"0X12345678", big.NewInt(0x12345678), true}, 36 //超前零行为测试: 37 {"0123456789", big.NewInt(123456789), true}, //注:不是八进制 38 {"00", big.NewInt(0), true}, 39 {"0x00", big.NewInt(0), true}, 40 {"0x012345678abc", big.NewInt(0x12345678abc), true}, 41 //无效语法: 42 {"abcdef", nil, false}, 43 {"0xgg", nil, false}, 44 //大于256位: 45 {"115792089237316195423570985008687907853269984665640564039457584007913129639936", nil, false}, 46 } 47 for _, test := range tests { 48 var num HexOrDecimal256 49 err := num.UnmarshalText([]byte(test.input)) 50 if (err == nil) != test.ok { 51 t.Errorf("ParseBig(%q) -> (err == nil) == %t, want %t", test.input, err == nil, test.ok) 52 continue 53 } 54 if test.num != nil && (*big.Int)(&num).Cmp(test.num) != 0 { 55 t.Errorf("ParseBig(%q) -> %d, want %d", test.input, (*big.Int)(&num), test.num) 56 } 57 } 58 } 59 60 func TestMustParseBig256(t *testing.T) { 61 defer func() { 62 if recover() == nil { 63 t.Error("MustParseBig should've panicked") 64 } 65 }() 66 MustParseBig256("ggg") 67 } 68 69 func TestBigMax(t *testing.T) { 70 a := big.NewInt(10) 71 b := big.NewInt(5) 72 73 max1 := BigMax(a, b) 74 if max1 != a { 75 t.Errorf("Expected %d got %d", a, max1) 76 } 77 78 max2 := BigMax(b, a) 79 if max2 != a { 80 t.Errorf("Expected %d got %d", a, max2) 81 } 82 } 83 84 func TestBigMin(t *testing.T) { 85 a := big.NewInt(10) 86 b := big.NewInt(5) 87 88 min1 := BigMin(a, b) 89 if min1 != b { 90 t.Errorf("Expected %d got %d", b, min1) 91 } 92 93 min2 := BigMin(b, a) 94 if min2 != b { 95 t.Errorf("Expected %d got %d", b, min2) 96 } 97 } 98 99 func TestFirstBigSet(t *testing.T) { 100 tests := []struct { 101 num *big.Int 102 ix int 103 }{ 104 {big.NewInt(0), 0}, 105 {big.NewInt(1), 0}, 106 {big.NewInt(2), 1}, 107 {big.NewInt(0x100), 8}, 108 } 109 for _, test := range tests { 110 if ix := FirstBitSet(test.num); ix != test.ix { 111 t.Errorf("FirstBitSet(b%b) = %d, want %d", test.num, ix, test.ix) 112 } 113 } 114 } 115 116 func TestPaddedBigBytes(t *testing.T) { 117 tests := []struct { 118 num *big.Int 119 n int 120 result []byte 121 }{ 122 {num: big.NewInt(0), n: 4, result: []byte{0, 0, 0, 0}}, 123 {num: big.NewInt(1), n: 4, result: []byte{0, 0, 0, 1}}, 124 {num: big.NewInt(512), n: 4, result: []byte{0, 0, 2, 0}}, 125 {num: BigPow(2, 32), n: 4, result: []byte{1, 0, 0, 0, 0}}, 126 } 127 for _, test := range tests { 128 if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) { 129 t.Errorf("PaddedBigBytes(%d, %d) = %v, want %v", test.num, test.n, result, test.result) 130 } 131 } 132 } 133 134 func BenchmarkPaddedBigBytesLargePadding(b *testing.B) { 135 bigint := MustParseBig256("123456789123456789123456789123456789") 136 for i := 0; i < b.N; i++ { 137 PaddedBigBytes(bigint, 200) 138 } 139 } 140 141 func BenchmarkPaddedBigBytesSmallPadding(b *testing.B) { 142 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 143 for i := 0; i < b.N; i++ { 144 PaddedBigBytes(bigint, 5) 145 } 146 } 147 148 func BenchmarkPaddedBigBytesSmallOnePadding(b *testing.B) { 149 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 150 for i := 0; i < b.N; i++ { 151 PaddedBigBytes(bigint, 32) 152 } 153 } 154 155 func BenchmarkByteAtBrandNew(b *testing.B) { 156 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 157 for i := 0; i < b.N; i++ { 158 bigEndianByteAt(bigint, 15) 159 } 160 } 161 162 func BenchmarkByteAt(b *testing.B) { 163 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 164 for i := 0; i < b.N; i++ { 165 bigEndianByteAt(bigint, 15) 166 } 167 } 168 169 func BenchmarkByteAtOld(b *testing.B) { 170 171 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 172 for i := 0; i < b.N; i++ { 173 PaddedBigBytes(bigint, 32) 174 } 175 } 176 177 func TestReadBits(t *testing.T) { 178 check := func(input string) { 179 want, _ := hex.DecodeString(input) 180 int, _ := new(big.Int).SetString(input, 16) 181 buf := make([]byte, len(want)) 182 ReadBits(int, buf) 183 if !bytes.Equal(buf, want) { 184 t.Errorf("have: %x\nwant: %x", buf, want) 185 } 186 } 187 check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0") 188 check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0") 189 check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0") 190 } 191 192 func TestU256(t *testing.T) { 193 tests := []struct{ x, y *big.Int }{ 194 {x: big.NewInt(0), y: big.NewInt(0)}, 195 {x: big.NewInt(1), y: big.NewInt(1)}, 196 {x: BigPow(2, 255), y: BigPow(2, 255)}, 197 {x: BigPow(2, 256), y: big.NewInt(0)}, 198 {x: new(big.Int).Add(BigPow(2, 256), big.NewInt(1)), y: big.NewInt(1)}, 199 // 200 {x: big.NewInt(-1), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1))}, 201 {x: big.NewInt(-2), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2))}, 202 {x: BigPow(2, -255), y: big.NewInt(1)}, 203 } 204 for _, test := range tests { 205 if y := U256(new(big.Int).Set(test.x)); y.Cmp(test.y) != 0 { 206 t.Errorf("U256(%x) = %x, want %x", test.x, y, test.y) 207 } 208 } 209 } 210 211 func TestBigEndianByteAt(t *testing.T) { 212 tests := []struct { 213 x string 214 y int 215 exp byte 216 }{ 217 {"00", 0, 0x00}, 218 {"01", 1, 0x00}, 219 {"00", 1, 0x00}, 220 {"01", 0, 0x01}, 221 {"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x30}, 222 {"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x20}, 223 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0xAB}, 224 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00}, 225 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 500, 0x00}, 226 } 227 for _, test := range tests { 228 v := new(big.Int).SetBytes(common.Hex2Bytes(test.x)) 229 actual := bigEndianByteAt(v, test.y) 230 if actual != test.exp { 231 t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual) 232 } 233 234 } 235 } 236 func TestLittleEndianByteAt(t *testing.T) { 237 tests := []struct { 238 x string 239 y int 240 exp byte 241 }{ 242 {"00", 0, 0x00}, 243 {"01", 1, 0x00}, 244 {"00", 1, 0x00}, 245 {"01", 0, 0x00}, 246 {"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x00}, 247 {"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x00}, 248 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0x00}, 249 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00}, 250 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, 0xAB}, 251 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, 0xCD}, 252 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, 0x00}, 253 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, 0xCD}, 254 {"0000000000000000000000000000000000000000000000000000000000102030", 31, 0x30}, 255 {"0000000000000000000000000000000000000000000000000000000000102030", 30, 0x20}, 256 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, 0x0}, 257 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 31, 0xFF}, 258 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFF, 0x0}, 259 } 260 for _, test := range tests { 261 v := new(big.Int).SetBytes(common.Hex2Bytes(test.x)) 262 actual := Byte(v, 32, test.y) 263 if actual != test.exp { 264 t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual) 265 } 266 267 } 268 } 269 270 func TestS256(t *testing.T) { 271 tests := []struct{ x, y *big.Int }{ 272 {x: big.NewInt(0), y: big.NewInt(0)}, 273 {x: big.NewInt(1), y: big.NewInt(1)}, 274 {x: big.NewInt(2), y: big.NewInt(2)}, 275 { 276 x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)), 277 y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)), 278 }, 279 { 280 x: BigPow(2, 255), 281 y: new(big.Int).Neg(BigPow(2, 255)), 282 }, 283 { 284 x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)), 285 y: big.NewInt(-1), 286 }, 287 { 288 x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)), 289 y: big.NewInt(-2), 290 }, 291 } 292 for _, test := range tests { 293 if y := S256(test.x); y.Cmp(test.y) != 0 { 294 t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y) 295 } 296 } 297 } 298 299 func TestExp(t *testing.T) { 300 tests := []struct{ base, exponent, result *big.Int }{ 301 {base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)}, 302 {base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)}, 303 {base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)}, 304 {base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)}, 305 {base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")}, 306 {base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")}, 307 } 308 for _, test := range tests { 309 if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 { 310 t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result) 311 } 312 } 313 } 314