You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scriptnum_tests.cpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Copyright (c) 2012-2014 The Bitcoin Core developers
  2. // Distributed under the MIT software license, see the accompanying
  3. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4. #include "bignum.h"
  5. #include "script/script.h"
  6. #include "test/test_bitcoin.h"
  7. #include <boost/test/unit_test.hpp>
  8. #include <limits.h>
  9. #include <stdint.h>
  10. BOOST_FIXTURE_TEST_SUITE(scriptnum_tests, BasicTestingSetup)
  11. static const int64_t values[] = \
  12. { 0, 1, CHAR_MIN, CHAR_MAX, UCHAR_MAX, SHRT_MIN, USHRT_MAX, INT_MIN, INT_MAX, UINT_MAX, LONG_MIN, LONG_MAX };
  13. static const int64_t offsets[] = { 1, 0x79, 0x80, 0x81, 0xFF, 0x7FFF, 0x8000, 0xFFFF, 0x10000};
  14. static bool verify(const CBigNum& bignum, const CScriptNum& scriptnum)
  15. {
  16. return bignum.getvch() == scriptnum.getvch() && bignum.getint() == scriptnum.getint();
  17. }
  18. static void CheckCreateVch(const int64_t& num)
  19. {
  20. CBigNum bignum(num);
  21. CScriptNum scriptnum(num);
  22. BOOST_CHECK(verify(bignum, scriptnum));
  23. CBigNum bignum2(bignum.getvch());
  24. CScriptNum scriptnum2(scriptnum.getvch(), false);
  25. BOOST_CHECK(verify(bignum2, scriptnum2));
  26. CBigNum bignum3(scriptnum2.getvch());
  27. CScriptNum scriptnum3(bignum2.getvch(), false);
  28. BOOST_CHECK(verify(bignum3, scriptnum3));
  29. }
  30. static void CheckCreateInt(const int64_t& num)
  31. {
  32. CBigNum bignum(num);
  33. CScriptNum scriptnum(num);
  34. BOOST_CHECK(verify(bignum, scriptnum));
  35. BOOST_CHECK(verify(bignum.getint(), CScriptNum(scriptnum.getint())));
  36. BOOST_CHECK(verify(scriptnum.getint(), CScriptNum(bignum.getint())));
  37. BOOST_CHECK(verify(CBigNum(scriptnum.getint()).getint(), CScriptNum(CScriptNum(bignum.getint()).getint())));
  38. }
  39. static void CheckAdd(const int64_t& num1, const int64_t& num2)
  40. {
  41. const CBigNum bignum1(num1);
  42. const CBigNum bignum2(num2);
  43. const CScriptNum scriptnum1(num1);
  44. const CScriptNum scriptnum2(num2);
  45. CBigNum bignum3(num1);
  46. CBigNum bignum4(num1);
  47. CScriptNum scriptnum3(num1);
  48. CScriptNum scriptnum4(num1);
  49. // int64_t overflow is undefined.
  50. bool invalid = (((num2 > 0) && (num1 > (std::numeric_limits<int64_t>::max() - num2))) ||
  51. ((num2 < 0) && (num1 < (std::numeric_limits<int64_t>::min() - num2))));
  52. if (!invalid)
  53. {
  54. BOOST_CHECK(verify(bignum1 + bignum2, scriptnum1 + scriptnum2));
  55. BOOST_CHECK(verify(bignum1 + bignum2, scriptnum1 + num2));
  56. BOOST_CHECK(verify(bignum1 + bignum2, scriptnum2 + num1));
  57. }
  58. }
  59. static void CheckNegate(const int64_t& num)
  60. {
  61. const CBigNum bignum(num);
  62. const CScriptNum scriptnum(num);
  63. // -INT64_MIN is undefined
  64. if (num != std::numeric_limits<int64_t>::min())
  65. BOOST_CHECK(verify(-bignum, -scriptnum));
  66. }
  67. static void CheckSubtract(const int64_t& num1, const int64_t& num2)
  68. {
  69. const CBigNum bignum1(num1);
  70. const CBigNum bignum2(num2);
  71. const CScriptNum scriptnum1(num1);
  72. const CScriptNum scriptnum2(num2);
  73. bool invalid = false;
  74. // int64_t overflow is undefined.
  75. invalid = ((num2 > 0 && num1 < std::numeric_limits<int64_t>::min() + num2) ||
  76. (num2 < 0 && num1 > std::numeric_limits<int64_t>::max() + num2));
  77. if (!invalid)
  78. {
  79. BOOST_CHECK(verify(bignum1 - bignum2, scriptnum1 - scriptnum2));
  80. BOOST_CHECK(verify(bignum1 - bignum2, scriptnum1 - num2));
  81. }
  82. invalid = ((num1 > 0 && num2 < std::numeric_limits<int64_t>::min() + num1) ||
  83. (num1 < 0 && num2 > std::numeric_limits<int64_t>::max() + num1));
  84. if (!invalid)
  85. {
  86. BOOST_CHECK(verify(bignum2 - bignum1, scriptnum2 - scriptnum1));
  87. BOOST_CHECK(verify(bignum2 - bignum1, scriptnum2 - num1));
  88. }
  89. }
  90. static void CheckCompare(const int64_t& num1, const int64_t& num2)
  91. {
  92. const CBigNum bignum1(num1);
  93. const CBigNum bignum2(num2);
  94. const CScriptNum scriptnum1(num1);
  95. const CScriptNum scriptnum2(num2);
  96. BOOST_CHECK((bignum1 == bignum1) == (scriptnum1 == scriptnum1));
  97. BOOST_CHECK((bignum1 != bignum1) == (scriptnum1 != scriptnum1));
  98. BOOST_CHECK((bignum1 < bignum1) == (scriptnum1 < scriptnum1));
  99. BOOST_CHECK((bignum1 > bignum1) == (scriptnum1 > scriptnum1));
  100. BOOST_CHECK((bignum1 >= bignum1) == (scriptnum1 >= scriptnum1));
  101. BOOST_CHECK((bignum1 <= bignum1) == (scriptnum1 <= scriptnum1));
  102. BOOST_CHECK((bignum1 == bignum1) == (scriptnum1 == num1));
  103. BOOST_CHECK((bignum1 != bignum1) == (scriptnum1 != num1));
  104. BOOST_CHECK((bignum1 < bignum1) == (scriptnum1 < num1));
  105. BOOST_CHECK((bignum1 > bignum1) == (scriptnum1 > num1));
  106. BOOST_CHECK((bignum1 >= bignum1) == (scriptnum1 >= num1));
  107. BOOST_CHECK((bignum1 <= bignum1) == (scriptnum1 <= num1));
  108. BOOST_CHECK((bignum1 == bignum2) == (scriptnum1 == scriptnum2));
  109. BOOST_CHECK((bignum1 != bignum2) == (scriptnum1 != scriptnum2));
  110. BOOST_CHECK((bignum1 < bignum2) == (scriptnum1 < scriptnum2));
  111. BOOST_CHECK((bignum1 > bignum2) == (scriptnum1 > scriptnum2));
  112. BOOST_CHECK((bignum1 >= bignum2) == (scriptnum1 >= scriptnum2));
  113. BOOST_CHECK((bignum1 <= bignum2) == (scriptnum1 <= scriptnum2));
  114. BOOST_CHECK((bignum1 == bignum2) == (scriptnum1 == num2));
  115. BOOST_CHECK((bignum1 != bignum2) == (scriptnum1 != num2));
  116. BOOST_CHECK((bignum1 < bignum2) == (scriptnum1 < num2));
  117. BOOST_CHECK((bignum1 > bignum2) == (scriptnum1 > num2));
  118. BOOST_CHECK((bignum1 >= bignum2) == (scriptnum1 >= num2));
  119. BOOST_CHECK((bignum1 <= bignum2) == (scriptnum1 <= num2));
  120. }
  121. static void RunCreate(const int64_t& num)
  122. {
  123. CheckCreateInt(num);
  124. CScriptNum scriptnum(num);
  125. if (scriptnum.getvch().size() <= CScriptNum::nMaxNumSize)
  126. CheckCreateVch(num);
  127. else
  128. {
  129. BOOST_CHECK_THROW (CheckCreateVch(num), scriptnum_error);
  130. }
  131. }
  132. static void RunOperators(const int64_t& num1, const int64_t& num2)
  133. {
  134. CheckAdd(num1, num2);
  135. CheckSubtract(num1, num2);
  136. CheckNegate(num1);
  137. CheckCompare(num1, num2);
  138. }
  139. BOOST_AUTO_TEST_CASE(creation)
  140. {
  141. for(size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i)
  142. {
  143. for(size_t j = 0; j < sizeof(offsets) / sizeof(offsets[0]); ++j)
  144. {
  145. RunCreate(values[i]);
  146. RunCreate(values[i] + offsets[j]);
  147. RunCreate(values[i] - offsets[j]);
  148. }
  149. }
  150. }
  151. BOOST_AUTO_TEST_CASE(operators)
  152. {
  153. for(size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i)
  154. {
  155. for(size_t j = 0; j < sizeof(offsets) / sizeof(offsets[0]); ++j)
  156. {
  157. RunOperators(values[i], values[i]);
  158. RunOperators(values[i], -values[i]);
  159. RunOperators(values[i], values[j]);
  160. RunOperators(values[i], -values[j]);
  161. RunOperators(values[i] + values[j], values[j]);
  162. RunOperators(values[i] + values[j], -values[j]);
  163. RunOperators(values[i] - values[j], values[j]);
  164. RunOperators(values[i] - values[j], -values[j]);
  165. RunOperators(values[i] + values[j], values[i] + values[j]);
  166. RunOperators(values[i] + values[j], values[i] - values[j]);
  167. RunOperators(values[i] - values[j], values[i] + values[j]);
  168. RunOperators(values[i] - values[j], values[i] - values[j]);
  169. }
  170. }
  171. }
  172. BOOST_AUTO_TEST_SUITE_END()