test_checked_cpp_int.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2012 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. //
  6. // Compare arithmetic results using fixed_int to GMP results.
  7. //
  8. #ifdef _MSC_VER
  9. #define _SCL_SECURE_NO_WARNINGS
  10. #endif
  11. #include <boost/multiprecision/cpp_int.hpp>
  12. #include "test.hpp"
  13. #include <boost/random/mersenne_twister.hpp>
  14. #include <boost/random/uniform_int.hpp>
  15. template <class T>
  16. T generate_random(unsigned bits_wanted)
  17. {
  18. static boost::random::mt19937 gen;
  19. typedef boost::random::mt19937::result_type random_type;
  20. T max_val;
  21. unsigned digits;
  22. if (std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
  23. {
  24. max_val = (std::numeric_limits<T>::max)();
  25. digits = std::numeric_limits<T>::digits;
  26. }
  27. else
  28. {
  29. max_val = T(1) << bits_wanted;
  30. digits = bits_wanted;
  31. }
  32. unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
  33. while ((random_type(1) << bits_per_r_val) > (gen.max)())
  34. --bits_per_r_val;
  35. unsigned terms_needed = digits / bits_per_r_val + 1;
  36. T val = 0;
  37. for (unsigned i = 0; i < terms_needed; ++i)
  38. {
  39. val *= (gen.max)();
  40. val += gen();
  41. }
  42. val %= max_val;
  43. return val;
  44. }
  45. template <class Number>
  46. void test_signed_overflow(Number a, Number b, const boost::mpl::true_&)
  47. {
  48. a = -a;
  49. BOOST_CHECK_THROW(Number(a * b), std::overflow_error);
  50. ++a;
  51. BOOST_CHECK(Number(a * b) >= (std::numeric_limits<Number>::min)());
  52. }
  53. template <class Number>
  54. void test_signed_overflow(Number a, Number b, const boost::mpl::false_&)
  55. {
  56. }
  57. template <class Number>
  58. void test()
  59. {
  60. using namespace boost::multiprecision;
  61. typedef Number test_type;
  62. if (std::numeric_limits<test_type>::is_bounded)
  63. {
  64. test_type val = (std::numeric_limits<test_type>::max)();
  65. #ifndef BOOST_NO_EXCEPTIONS
  66. BOOST_CHECK_THROW(++val, std::overflow_error);
  67. val = (std::numeric_limits<test_type>::max)();
  68. BOOST_CHECK_THROW(test_type(1 + val), std::overflow_error);
  69. BOOST_CHECK_THROW(test_type(val + 1), std::overflow_error);
  70. BOOST_CHECK_THROW(test_type(2 * val), std::overflow_error);
  71. val /= 2;
  72. val += 1;
  73. BOOST_CHECK_THROW(test_type(2 * val), std::overflow_error);
  74. if (std::numeric_limits<test_type>::is_signed)
  75. {
  76. val = (std::numeric_limits<test_type>::min)();
  77. BOOST_CHECK_THROW(--val, std::overflow_error);
  78. val = (std::numeric_limits<test_type>::min)();
  79. BOOST_CHECK_THROW(test_type(val - 1), std::overflow_error);
  80. BOOST_CHECK_THROW(test_type(2 * val), std::overflow_error);
  81. val /= 2;
  82. val -= 1;
  83. BOOST_CHECK_THROW(test_type(2 * val), std::overflow_error);
  84. }
  85. else
  86. {
  87. val = (std::numeric_limits<test_type>::min)();
  88. BOOST_CHECK_THROW(--val, std::range_error);
  89. val = (std::numeric_limits<test_type>::min)();
  90. BOOST_CHECK_THROW(test_type(val - 1), std::range_error);
  91. }
  92. #endif
  93. //
  94. // Test overflow in random values:
  95. //
  96. for (unsigned bits = 30; bits < std::numeric_limits<test_type>::digits; bits += 30)
  97. {
  98. for (unsigned i = 0; i < 100; ++i)
  99. {
  100. val = static_cast<test_type>(generate_random<cpp_int>(bits));
  101. test_type val2 = 1 + (std::numeric_limits<test_type>::max)() / val;
  102. BOOST_CHECK_THROW(test_type(val2 * val), std::overflow_error);
  103. test_signed_overflow(val2, val, boost::mpl::bool_<std::numeric_limits<test_type>::is_signed>());
  104. --val2;
  105. BOOST_CHECK(cpp_int(val2) * cpp_int(val) <= cpp_int((std::numeric_limits<test_type>::max)()));
  106. BOOST_CHECK(val2 * val <= (std::numeric_limits<test_type>::max)());
  107. val2 = (std::numeric_limits<test_type>::max)() - val;
  108. ++val2;
  109. BOOST_CHECK_THROW(test_type(val2 + val), std::overflow_error);
  110. BOOST_CHECK((val2 - 1) + val == (std::numeric_limits<test_type>::max)());
  111. if (std::numeric_limits<test_type>::is_signed)
  112. {
  113. val2 = (std::numeric_limits<test_type>::min)() + val;
  114. --val2;
  115. BOOST_CHECK_THROW(test_type(val2 - val), std::overflow_error);
  116. ++val2;
  117. BOOST_CHECK(val2 - val == (std::numeric_limits<test_type>::min)());
  118. }
  119. unsigned shift = std::numeric_limits<test_type>::digits - msb(val);
  120. BOOST_CHECK_THROW((val << shift) > 0, std::overflow_error);
  121. }
  122. }
  123. }
  124. #ifndef BOOST_NO_EXCEPTIONS
  125. if (std::numeric_limits<test_type>::is_signed)
  126. {
  127. test_type a = -1;
  128. test_type b = 1;
  129. BOOST_CHECK_THROW(test_type(a | b), std::range_error);
  130. BOOST_CHECK_THROW(test_type(a & b), std::range_error);
  131. BOOST_CHECK_THROW(test_type(a ^ b), std::range_error);
  132. }
  133. else
  134. {
  135. // Constructing from a negative value is not allowed:
  136. BOOST_CHECK_THROW(test_type(-2), std::range_error);
  137. BOOST_CHECK_THROW(test_type("-2"), std::range_error);
  138. }
  139. if (std::numeric_limits<test_type>::digits < std::numeric_limits<long long>::digits)
  140. {
  141. long long llm = (std::numeric_limits<long long>::max)();
  142. test_type t;
  143. BOOST_CHECK_THROW(t = llm, std::range_error);
  144. BOOST_CHECK_THROW(t = static_cast<test_type>(llm), std::range_error);
  145. unsigned long long ullm = (std::numeric_limits<unsigned long long>::max)();
  146. BOOST_CHECK_THROW(t = ullm, std::range_error);
  147. BOOST_CHECK_THROW(t = static_cast<test_type>(ullm), std::range_error);
  148. static const checked_uint512_t big = (std::numeric_limits<checked_uint512_t>::max)();
  149. BOOST_CHECK_THROW(t = static_cast<test_type>(big), std::range_error);
  150. }
  151. //
  152. // String errors:
  153. //
  154. BOOST_CHECK_THROW(test_type("12A"), std::runtime_error);
  155. BOOST_CHECK_THROW(test_type("0658"), std::runtime_error);
  156. if (std::numeric_limits<test_type>::is_signed)
  157. {
  158. BOOST_CHECK_THROW(test_type(-2).str(0, std::ios_base::hex), std::runtime_error);
  159. BOOST_CHECK_THROW(test_type(-2).str(0, std::ios_base::oct), std::runtime_error);
  160. }
  161. #endif
  162. }
  163. int main()
  164. {
  165. using namespace boost::multiprecision;
  166. test<number<cpp_int_backend<0, 0, signed_magnitude, checked> > >();
  167. test<checked_int512_t>();
  168. test<checked_uint512_t>();
  169. test<number<cpp_int_backend<32, 32, signed_magnitude, checked, void> > >();
  170. test<number<cpp_int_backend<32, 32, unsigned_magnitude, checked, void> > >();
  171. //
  172. // We also need to test type with "odd" bit counts in order to ensure full code coverage:
  173. //
  174. test<number<cpp_int_backend<528, 528, signed_magnitude, checked, void> > >();
  175. test<number<cpp_int_backend<528, 528, unsigned_magnitude, checked, void> > >();
  176. test<number<cpp_int_backend<48, 48, signed_magnitude, checked, void> > >();
  177. test<number<cpp_int_backend<48, 48, unsigned_magnitude, checked, void> > >();
  178. return boost::report_errors();
  179. }