github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/common/math/big_test.go (about) 1 // Copyright 2018 The go-ethereum Authors 2 // Copyright 2019 The go-aigar Authors 3 // This file is part of the go-aigar library. 4 // 5 // The go-aigar library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-aigar library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>. 17 18 package math 19 20 import ( 21 "bytes" 22 "encoding/hex" 23 "math/big" 24 "testing" 25 26 "github.com/AigarNetwork/aigar/common" 27 ) 28 29 func TestHexOrDecimal256(t *testing.T) { 30 tests := []struct { 31 input string 32 num *big.Int 33 ok bool 34 }{ 35 {"", big.NewInt(0), true}, 36 {"0", big.NewInt(0), true}, 37 {"0x0", big.NewInt(0), true}, 38 {"12345678", big.NewInt(12345678), true}, 39 {"0x12345678", big.NewInt(0x12345678), true}, 40 {"0X12345678", big.NewInt(0x12345678), true}, 41 // Tests for leading zero behaviour: 42 {"0123456789", big.NewInt(123456789), true}, // note: not octal 43 {"00", big.NewInt(0), true}, 44 {"0x00", big.NewInt(0), true}, 45 {"0x012345678abc", big.NewInt(0x12345678abc), true}, 46 // Invalid syntax: 47 {"abcdef", nil, false}, 48 {"0xgg", nil, false}, 49 // Larger than 256 bits: 50 {"115792089237316195423570985008687907853269984665640564039457584007913129639936", nil, false}, 51 } 52 for _, test := range tests { 53 var num HexOrDecimal256 54 err := num.UnmarshalText([]byte(test.input)) 55 if (err == nil) != test.ok { 56 t.Errorf("ParseBig(%q) -> (err == nil) == %t, want %t", test.input, err == nil, test.ok) 57 continue 58 } 59 if test.num != nil && (*big.Int)(&num).Cmp(test.num) != 0 { 60 t.Errorf("ParseBig(%q) -> %d, want %d", test.input, (*big.Int)(&num), test.num) 61 } 62 } 63 } 64 65 func TestMustParseBig256(t *testing.T) { 66 defer func() { 67 if recover() == nil { 68 t.Error("MustParseBig should've panicked") 69 } 70 }() 71 MustParseBig256("ggg") 72 } 73 74 func TestBigMax(t *testing.T) { 75 a := big.NewInt(10) 76 b := big.NewInt(5) 77 78 max1 := BigMax(a, b) 79 if max1 != a { 80 t.Errorf("Expected %d got %d", a, max1) 81 } 82 83 max2 := BigMax(b, a) 84 if max2 != a { 85 t.Errorf("Expected %d got %d", a, max2) 86 } 87 } 88 89 func TestBigMin(t *testing.T) { 90 a := big.NewInt(10) 91 b := big.NewInt(5) 92 93 min1 := BigMin(a, b) 94 if min1 != b { 95 t.Errorf("Expected %d got %d", b, min1) 96 } 97 98 min2 := BigMin(b, a) 99 if min2 != b { 100 t.Errorf("Expected %d got %d", b, min2) 101 } 102 } 103 104 func TestFirstBigSet(t *testing.T) { 105 tests := []struct { 106 num *big.Int 107 ix int 108 }{ 109 {big.NewInt(0), 0}, 110 {big.NewInt(1), 0}, 111 {big.NewInt(2), 1}, 112 {big.NewInt(0x100), 8}, 113 } 114 for _, test := range tests { 115 if ix := FirstBitSet(test.num); ix != test.ix { 116 t.Errorf("FirstBitSet(b%b) = %d, want %d", test.num, ix, test.ix) 117 } 118 } 119 } 120 121 func TestPaddedBigBytes(t *testing.T) { 122 tests := []struct { 123 num *big.Int 124 n int 125 result []byte 126 }{ 127 {num: big.NewInt(0), n: 4, result: []byte{0, 0, 0, 0}}, 128 {num: big.NewInt(1), n: 4, result: []byte{0, 0, 0, 1}}, 129 {num: big.NewInt(512), n: 4, result: []byte{0, 0, 2, 0}}, 130 {num: BigPow(2, 32), n: 4, result: []byte{1, 0, 0, 0, 0}}, 131 } 132 for _, test := range tests { 133 if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) { 134 t.Errorf("PaddedBigBytes(%d, %d) = %v, want %v", test.num, test.n, result, test.result) 135 } 136 } 137 } 138 139 func BenchmarkPaddedBigBytesLargePadding(b *testing.B) { 140 bigint := MustParseBig256("123456789123456789123456789123456789") 141 for i := 0; i < b.N; i++ { 142 PaddedBigBytes(bigint, 200) 143 } 144 } 145 146 func BenchmarkPaddedBigBytesSmallPadding(b *testing.B) { 147 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 148 for i := 0; i < b.N; i++ { 149 PaddedBigBytes(bigint, 5) 150 } 151 } 152 153 func BenchmarkPaddedBigBytesSmallOnePadding(b *testing.B) { 154 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 155 for i := 0; i < b.N; i++ { 156 PaddedBigBytes(bigint, 32) 157 } 158 } 159 160 func BenchmarkByteAtBrandNew(b *testing.B) { 161 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 162 for i := 0; i < b.N; i++ { 163 bigEndianByteAt(bigint, 15) 164 } 165 } 166 167 func BenchmarkByteAt(b *testing.B) { 168 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 169 for i := 0; i < b.N; i++ { 170 bigEndianByteAt(bigint, 15) 171 } 172 } 173 174 func BenchmarkByteAtOld(b *testing.B) { 175 176 bigint := MustParseBig256("0x18F8F8F1000111000110011100222004330052300000000000000000FEFCF3CC") 177 for i := 0; i < b.N; i++ { 178 PaddedBigBytes(bigint, 32) 179 } 180 } 181 182 func TestReadBits(t *testing.T) { 183 check := func(input string) { 184 want, _ := hex.DecodeString(input) 185 int, _ := new(big.Int).SetString(input, 16) 186 buf := make([]byte, len(want)) 187 ReadBits(int, buf) 188 if !bytes.Equal(buf, want) { 189 t.Errorf("have: %x\nwant: %x", buf, want) 190 } 191 } 192 check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0") 193 check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0") 194 check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0") 195 } 196 197 func TestU256(t *testing.T) { 198 tests := []struct{ x, y *big.Int }{ 199 {x: big.NewInt(0), y: big.NewInt(0)}, 200 {x: big.NewInt(1), y: big.NewInt(1)}, 201 {x: BigPow(2, 255), y: BigPow(2, 255)}, 202 {x: BigPow(2, 256), y: big.NewInt(0)}, 203 {x: new(big.Int).Add(BigPow(2, 256), big.NewInt(1)), y: big.NewInt(1)}, 204 // negative values 205 {x: big.NewInt(-1), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1))}, 206 {x: big.NewInt(-2), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2))}, 207 {x: BigPow(2, -255), y: big.NewInt(1)}, 208 } 209 for _, test := range tests { 210 if y := U256(new(big.Int).Set(test.x)); y.Cmp(test.y) != 0 { 211 t.Errorf("U256(%x) = %x, want %x", test.x, y, test.y) 212 } 213 } 214 } 215 216 func TestBigEndianByteAt(t *testing.T) { 217 tests := []struct { 218 x string 219 y int 220 exp byte 221 }{ 222 {"00", 0, 0x00}, 223 {"01", 1, 0x00}, 224 {"00", 1, 0x00}, 225 {"01", 0, 0x01}, 226 {"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x30}, 227 {"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x20}, 228 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0xAB}, 229 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00}, 230 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 500, 0x00}, 231 } 232 for _, test := range tests { 233 v := new(big.Int).SetBytes(common.Hex2Bytes(test.x)) 234 actual := bigEndianByteAt(v, test.y) 235 if actual != test.exp { 236 t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual) 237 } 238 239 } 240 } 241 func TestLittleEndianByteAt(t *testing.T) { 242 tests := []struct { 243 x string 244 y int 245 exp byte 246 }{ 247 {"00", 0, 0x00}, 248 {"01", 1, 0x00}, 249 {"00", 1, 0x00}, 250 {"01", 0, 0x00}, 251 {"0000000000000000000000000000000000000000000000000000000000102030", 0, 0x00}, 252 {"0000000000000000000000000000000000000000000000000000000000102030", 1, 0x00}, 253 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 31, 0x00}, 254 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 32, 0x00}, 255 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 0, 0xAB}, 256 {"ABCDEF0908070605040302010000000000000000000000000000000000000000", 1, 0xCD}, 257 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 0, 0x00}, 258 {"00CDEF090807060504030201ffffffffffffffffffffffffffffffffffffffff", 1, 0xCD}, 259 {"0000000000000000000000000000000000000000000000000000000000102030", 31, 0x30}, 260 {"0000000000000000000000000000000000000000000000000000000000102030", 30, 0x20}, 261 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 32, 0x0}, 262 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 31, 0xFF}, 263 {"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", 0xFFFF, 0x0}, 264 } 265 for _, test := range tests { 266 v := new(big.Int).SetBytes(common.Hex2Bytes(test.x)) 267 actual := Byte(v, 32, test.y) 268 if actual != test.exp { 269 t.Fatalf("Expected [%v] %v:th byte to be %v, was %v.", test.x, test.y, test.exp, actual) 270 } 271 272 } 273 } 274 275 func TestS256(t *testing.T) { 276 tests := []struct{ x, y *big.Int }{ 277 {x: big.NewInt(0), y: big.NewInt(0)}, 278 {x: big.NewInt(1), y: big.NewInt(1)}, 279 {x: big.NewInt(2), y: big.NewInt(2)}, 280 { 281 x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)), 282 y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)), 283 }, 284 { 285 x: BigPow(2, 255), 286 y: new(big.Int).Neg(BigPow(2, 255)), 287 }, 288 { 289 x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)), 290 y: big.NewInt(-1), 291 }, 292 { 293 x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)), 294 y: big.NewInt(-2), 295 }, 296 } 297 for _, test := range tests { 298 if y := S256(test.x); y.Cmp(test.y) != 0 { 299 t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y) 300 } 301 } 302 } 303 304 func TestExp(t *testing.T) { 305 tests := []struct{ base, exponent, result *big.Int }{ 306 {base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)}, 307 {base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)}, 308 {base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)}, 309 {base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)}, 310 {base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")}, 311 {base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")}, 312 } 313 for _, test := range tests { 314 if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 { 315 t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result) 316 } 317 } 318 }