big_test.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. // Copyright 2017 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 TestHexOrDecimal256(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. var num HexOrDecimal256
  48. err := num.UnmarshalText([]byte(test.input))
  49. if (err == nil) != test.ok {
  50. t.Errorf("ParseBig(%q) -> (err == nil) == %t, want %t", test.input, err == nil, test.ok)
  51. continue
  52. }
  53. if test.num != nil && (*big.Int)(&num).Cmp(test.num) != 0 {
  54. t.Errorf("ParseBig(%q) -> %d, want %d", test.input, (*big.Int)(&num), test.num)
  55. }
  56. }
  57. }
  58. func TestMustParseBig256(t *testing.T) {
  59. defer func() {
  60. if recover() == nil {
  61. t.Error("MustParseBig should've panicked")
  62. }
  63. }()
  64. MustParseBig256("ggg")
  65. }
  66. func TestBigMax(t *testing.T) {
  67. a := big.NewInt(10)
  68. b := big.NewInt(5)
  69. max1 := BigMax(a, b)
  70. if max1 != a {
  71. t.Errorf("Expected %d got %d", a, max1)
  72. }
  73. max2 := BigMax(b, a)
  74. if max2 != a {
  75. t.Errorf("Expected %d got %d", a, max2)
  76. }
  77. }
  78. func TestBigMin(t *testing.T) {
  79. a := big.NewInt(10)
  80. b := big.NewInt(5)
  81. min1 := BigMin(a, b)
  82. if min1 != b {
  83. t.Errorf("Expected %d got %d", b, min1)
  84. }
  85. min2 := BigMin(b, a)
  86. if min2 != b {
  87. t.Errorf("Expected %d got %d", b, min2)
  88. }
  89. }
  90. func TestFirstBigSet(t *testing.T) {
  91. tests := []struct {
  92. num *big.Int
  93. ix int
  94. }{
  95. {big.NewInt(0), 0},
  96. {big.NewInt(1), 0},
  97. {big.NewInt(2), 1},
  98. {big.NewInt(0x100), 8},
  99. }
  100. for _, test := range tests {
  101. if ix := FirstBitSet(test.num); ix != test.ix {
  102. t.Errorf("FirstBitSet(b%b) = %d, want %d", test.num, ix, test.ix)
  103. }
  104. }
  105. }
  106. func TestPaddedBigBytes(t *testing.T) {
  107. tests := []struct {
  108. num *big.Int
  109. n int
  110. result []byte
  111. }{
  112. {num: big.NewInt(0), n: 4, result: []byte{0, 0, 0, 0}},
  113. {num: big.NewInt(1), n: 4, result: []byte{0, 0, 0, 1}},
  114. {num: big.NewInt(512), n: 4, result: []byte{0, 0, 2, 0}},
  115. {num: BigPow(2, 32), n: 4, result: []byte{1, 0, 0, 0, 0}},
  116. }
  117. for _, test := range tests {
  118. if result := PaddedBigBytes(test.num, test.n); !bytes.Equal(result, test.result) {
  119. t.Errorf("PaddedBigBytes(%d, %d) = %v, want %v", test.num, test.n, result, test.result)
  120. }
  121. }
  122. }
  123. func BenchmarkPaddedBigBytes(b *testing.B) {
  124. bigint := MustParseBig256("123456789123456789123456789123456789")
  125. for i := 0; i < b.N; i++ {
  126. PaddedBigBytes(bigint, 32)
  127. }
  128. }
  129. func TestReadBits(t *testing.T) {
  130. check := func(input string) {
  131. want, _ := hex.DecodeString(input)
  132. int, _ := new(big.Int).SetString(input, 16)
  133. buf := make([]byte, len(want))
  134. ReadBits(int, buf)
  135. if !bytes.Equal(buf, want) {
  136. t.Errorf("have: %x\nwant: %x", buf, want)
  137. }
  138. }
  139. check("000000000000000000000000000000000000000000000000000000FEFCF3F8F0")
  140. check("0000000000012345000000000000000000000000000000000000FEFCF3F8F0")
  141. check("18F8F8F1000111000110011100222004330052300000000000000000FEFCF3F8F0")
  142. }
  143. func TestU256(t *testing.T) {
  144. tests := []struct{ x, y *big.Int }{
  145. {x: big.NewInt(0), y: big.NewInt(0)},
  146. {x: big.NewInt(1), y: big.NewInt(1)},
  147. {x: BigPow(2, 255), y: BigPow(2, 255)},
  148. {x: BigPow(2, 256), y: big.NewInt(0)},
  149. {x: new(big.Int).Add(BigPow(2, 256), big.NewInt(1)), y: big.NewInt(1)},
  150. // negative values
  151. {x: big.NewInt(-1), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1))},
  152. {x: big.NewInt(-2), y: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2))},
  153. {x: BigPow(2, -255), y: big.NewInt(1)},
  154. }
  155. for _, test := range tests {
  156. if y := U256(new(big.Int).Set(test.x)); y.Cmp(test.y) != 0 {
  157. t.Errorf("U256(%x) = %x, want %x", test.x, y, test.y)
  158. }
  159. }
  160. }
  161. func TestS256(t *testing.T) {
  162. tests := []struct{ x, y *big.Int }{
  163. {x: big.NewInt(0), y: big.NewInt(0)},
  164. {x: big.NewInt(1), y: big.NewInt(1)},
  165. {x: big.NewInt(2), y: big.NewInt(2)},
  166. {
  167. x: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
  168. y: new(big.Int).Sub(BigPow(2, 255), big.NewInt(1)),
  169. },
  170. {
  171. x: BigPow(2, 255),
  172. y: new(big.Int).Neg(BigPow(2, 255)),
  173. },
  174. {
  175. x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(1)),
  176. y: big.NewInt(-1),
  177. },
  178. {
  179. x: new(big.Int).Sub(BigPow(2, 256), big.NewInt(2)),
  180. y: big.NewInt(-2),
  181. },
  182. }
  183. for _, test := range tests {
  184. if y := S256(test.x); y.Cmp(test.y) != 0 {
  185. t.Errorf("S256(%x) = %x, want %x", test.x, y, test.y)
  186. }
  187. }
  188. }
  189. func TestExp(t *testing.T) {
  190. tests := []struct{ base, exponent, result *big.Int }{
  191. {base: big.NewInt(0), exponent: big.NewInt(0), result: big.NewInt(1)},
  192. {base: big.NewInt(1), exponent: big.NewInt(0), result: big.NewInt(1)},
  193. {base: big.NewInt(1), exponent: big.NewInt(1), result: big.NewInt(1)},
  194. {base: big.NewInt(1), exponent: big.NewInt(2), result: big.NewInt(1)},
  195. {base: big.NewInt(3), exponent: big.NewInt(144), result: MustParseBig256("507528786056415600719754159741696356908742250191663887263627442114881")},
  196. {base: big.NewInt(2), exponent: big.NewInt(255), result: MustParseBig256("57896044618658097711785492504343953926634992332820282019728792003956564819968")},
  197. }
  198. for _, test := range tests {
  199. if result := Exp(test.base, test.exponent); result.Cmp(test.result) != 0 {
  200. t.Errorf("Exp(%d, %d) = %d, want %d", test.base, test.exponent, result, test.result)
  201. }
  202. }
  203. }