big_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package math
  17. import (
  18. "bytes"
  19. "encoding/hex"
  20. "math/big"
  21. "testing"
  22. )
  23. func TestParseBig256(t *testing.T) {
  24. tests := []struct {
  25. input string
  26. num *big.Int
  27. ok bool
  28. }{
  29. {"", big.NewInt(0), true},
  30. {"0", big.NewInt(0), true},
  31. {"0x0", big.NewInt(0), true},
  32. {"12345678", big.NewInt(12345678), true},
  33. {"0x12345678", big.NewInt(0x12345678), true},
  34. {"0X12345678", big.NewInt(0x12345678), true},
  35. // Tests for leading zero behaviour:
  36. {"0123456789", big.NewInt(123456789), true}, // note: not octal
  37. {"00", big.NewInt(0), true},
  38. {"0x00", big.NewInt(0), true},
  39. {"0x012345678abc", big.NewInt(0x12345678abc), true},
  40. // Invalid syntax:
  41. {"abcdef", nil, false},
  42. {"0xgg", nil, false},
  43. // Larger than 256 bits:
  44. {"115792089237316195423570985008687907853269984665640564039457584007913129639936", nil, false},
  45. }
  46. for _, test := range tests {
  47. num, ok := ParseBig256(test.input)
  48. if ok != test.ok {
  49. t.Errorf("ParseBig(%q) -> ok = %t, want %t", test.input, ok, test.ok)
  50. continue
  51. }
  52. if num != nil && test.num != nil && num.Cmp(test.num) != 0 {
  53. t.Errorf("ParseBig(%q) -> %d, want %d", test.input, num, test.num)
  54. }
  55. }
  56. }
  57. func TestMustParseBig256(t *testing.T) {
  58. defer func() {
  59. if recover() == nil {
  60. t.Error("MustParseBig should've panicked")
  61. }
  62. }()
  63. MustParseBig256("ggg")
  64. }
  65. func TestBigMax(t *testing.T) {
  66. a := big.NewInt(10)
  67. b := big.NewInt(5)
  68. max1 := BigMax(a, b)
  69. if max1 != a {
  70. t.Errorf("Expected %d got %d", a, max1)
  71. }
  72. max2 := BigMax(b, a)
  73. if max2 != a {
  74. t.Errorf("Expected %d got %d", a, max2)
  75. }
  76. }
  77. func TestBigMin(t *testing.T) {
  78. a := big.NewInt(10)
  79. b := big.NewInt(5)
  80. min1 := BigMin(a, b)
  81. if min1 != b {
  82. t.Errorf("Expected %d got %d", b, min1)
  83. }
  84. min2 := BigMin(b, a)
  85. if min2 != b {
  86. t.Errorf("Expected %d got %d", b, min2)
  87. }
  88. }
  89. func TestFirstBigSet(t *testing.T) {
  90. tests := []struct {
  91. num *big.Int
  92. ix int
  93. }{
  94. {big.NewInt(0), 0},
  95. {big.NewInt(1), 0},
  96. {big.NewInt(2), 1},
  97. {big.NewInt(0x100), 8},
  98. }
  99. for _, test := range tests {
  100. if ix := FirstBitSet(test.num); ix != test.ix {
  101. t.Errorf("FirstBitSet(b%b) = %d, want %d", test.num, ix, test.ix)
  102. }
  103. }
  104. }
  105. func TestPaddedBigBytes(t *testing.T) {
  106. tests := []struct {
  107. num *big.Int
  108. n int
  109. result []byte
  110. }{
  111. {num: big.NewInt(0), n: 4, result: []byte{0, 0, 0, 0}},
  112. {num: big.NewInt(1), n: 4, result: []byte{0, 0, 0, 1}},
  113. {num: big.NewInt(512), n: 4, result: []byte{0, 0, 2, 0}},
  114. {num: BigPow(2, 32), n: 4, result: []byte{1, 0, 0, 0, 0}},
  115. }
  116. for _, test := range tests {
  117. if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) {
  118. t.Errorf("PaddedBigBytes(%d, %d) = %v, want %v", test.num, test.n, result, test.result)
  119. }
  120. }
  121. }
  122. func BenchmarkPaddedBigBytes(b *testing.B) {
  123. bigint := MustParseBig256("123456789123456789123456789123456789")
  124. for i := 0; i < b.N; i++ {
  125. PaddedBigBytes(bigint, 32)
  126. }
  127. }
  128. func TestReadBits(t *testing.T) {
  129. check := func(input string) {
  130. want, _ := hex.DecodeString(input)
  131. int, _ := new(big.Int).SetString(input, 16)
  132. buf := make([]byte, len(want))
  133. ReadBits(int, buf)
  134. if !bytes.Equal(buf, want) {
  135. t.Errorf("have: %x\nwant: %x", buf, want)
  136. }
  137. }
  138. check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0")
  139. check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0")
  140. check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0")
  141. }
  142. func TestU256(t *testing.T) {
  143. tests := []struct{ x, y *big.Int }{
  144. {x: big.NewInt(0), y: big.NewInt(0)},
  145. {x: big.NewInt(1), y: big.NewInt(1)},
  146. {x: BigPow(2, 255), y: BigPow(2, 255)},
  147. {x: BigPow(2, 256), y: big.NewInt(0)},
  148. {x: new(big.Int).Add(BigPow(2, 256), big.NewInt(1)), y: big.NewInt(1)},
  149. // negative values
  150. {x: big.NewInt(-1), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1))},
  151. {x: big.NewInt(-2), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2))},
  152. {x: BigPow(2, -255), y: big.NewInt(1)},
  153. }
  154. for _, test := range tests {
  155. if y := U256(new(big.Int).Set(test.x)); y.Cmp(test.y) != 0 {
  156. t.Errorf("U256(%x) = %x, want %x", test.x, y, test.y)
  157. }
  158. }
  159. }
  160. func TestS256(t *testing.T) {
  161. tests := []struct{ x, y *big.Int }{
  162. {x: big.NewInt(0), y: big.NewInt(0)},
  163. {x: big.NewInt(1), y: big.NewInt(1)},
  164. {x: big.NewInt(2), y: big.NewInt(2)},
  165. {
  166. x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
  167. y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
  168. },
  169. {
  170. x: BigPow(2, 255),
  171. y: new(big.Int).Neg(BigPow(2, 255)),
  172. },
  173. {
  174. x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)),
  175. y: big.NewInt(-1),
  176. },
  177. {
  178. x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)),
  179. y: big.NewInt(-2),
  180. },
  181. }
  182. for _, test := range tests {
  183. if y := S256(test.x); y.Cmp(test.y) != 0 {
  184. t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y)
  185. }
  186. }
  187. }
  188. func TestExp(t *testing.T) {
  189. tests := []struct{ base, exponent, result *big.Int }{
  190. {base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)},
  191. {base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)},
  192. {base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)},
  193. {base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)},
  194. {base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")},
  195. {base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")},
  196. }
  197. for _, test := range tests {
  198. if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 {
  199. t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result)
  200. }
  201. }
  202. }