test_cpp_int_serial.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <boost/random/mersenne_twister.hpp>
  13. #include <boost/random/uniform_int.hpp>
  14. #include <boost/timer.hpp>
  15. #include "test.hpp"
  16. #include <iostream>
  17. #include <iomanip>
  18. #include <sstream>
  19. #include <boost/archive/text_iarchive.hpp>
  20. #include <boost/archive/text_oarchive.hpp>
  21. #include <boost/archive/binary_iarchive.hpp>
  22. #include <boost/archive/xml_iarchive.hpp>
  23. #include <boost/archive/binary_oarchive.hpp>
  24. #include <boost/archive/xml_oarchive.hpp>
  25. #include <boost/exception/all.hpp>
  26. template <class T>
  27. T generate_random(unsigned bits_wanted)
  28. {
  29. static boost::random::mt19937 gen;
  30. typedef boost::random::mt19937::result_type random_type;
  31. T max_val;
  32. unsigned digits;
  33. if (std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
  34. {
  35. max_val = (std::numeric_limits<T>::max)();
  36. digits = std::numeric_limits<T>::digits;
  37. }
  38. else
  39. {
  40. max_val = T(1) << bits_wanted;
  41. digits = bits_wanted;
  42. }
  43. unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
  44. while ((random_type(1) << bits_per_r_val) > (gen.max)())
  45. --bits_per_r_val;
  46. unsigned terms_needed = digits / bits_per_r_val + 1;
  47. T val = 0;
  48. for (unsigned i = 0; i < terms_needed; ++i)
  49. {
  50. val *= (gen.max)();
  51. val += gen();
  52. }
  53. val %= max_val;
  54. return val;
  55. }
  56. template <class T>
  57. void test_neg(const T& x, const boost::mpl::true_&)
  58. {
  59. T val = -x;
  60. #ifndef BOOST_NO_EXCEPTIONS
  61. try
  62. {
  63. #endif
  64. std::stringstream ss;
  65. boost::archive::text_oarchive oa(ss);
  66. oa << static_cast<const T&>(val);
  67. boost::archive::text_iarchive ia(ss);
  68. T val2;
  69. ia >> val2;
  70. BOOST_CHECK_EQUAL(val, val2);
  71. ss.clear();
  72. boost::archive::binary_oarchive ob(ss);
  73. ob << static_cast<const T&>(val);
  74. boost::archive::binary_iarchive ib(ss);
  75. ib >> val2;
  76. BOOST_CHECK_EQUAL(val, val2);
  77. #ifndef BOOST_NO_EXCEPTIONS
  78. }
  79. catch (const boost::exception& e)
  80. {
  81. std::cout << "Caught boost::exception with:\n";
  82. std::cout << diagnostic_information(e);
  83. }
  84. catch (const std::exception& e)
  85. {
  86. std::cout << "Caught std::exception with:\n";
  87. std::cout << e.what() << std::endl;
  88. }
  89. #endif
  90. }
  91. template <class T>
  92. void test_neg(const T&, const boost::mpl::false_&) {}
  93. template <class T>
  94. void test()
  95. {
  96. using namespace boost::multiprecision;
  97. boost::random::mt19937 gen;
  98. boost::uniform_int<> d(3, std::numeric_limits<T>::is_bounded ? std::numeric_limits<T>::digits : 3000);
  99. boost::timer tim;
  100. while (true)
  101. {
  102. T val = generate_random<T>(d(gen));
  103. #ifndef BOOST_NO_EXCEPTIONS
  104. try
  105. {
  106. #endif
  107. T val2;
  108. {
  109. std::stringstream ss;
  110. boost::archive::text_oarchive oa(ss);
  111. oa << static_cast<const T&>(val);
  112. boost::archive::text_iarchive ia(ss);
  113. ia >> val2;
  114. BOOST_CHECK_EQUAL(val, val2);
  115. }
  116. {
  117. std::stringstream ss;
  118. boost::archive::binary_oarchive ob(ss);
  119. ob << static_cast<const T&>(val);
  120. boost::archive::binary_iarchive ib(ss);
  121. ib >> val2;
  122. BOOST_CHECK_EQUAL(val, val2);
  123. }
  124. {
  125. std::stringstream ss;
  126. {
  127. boost::archive::xml_oarchive oc(ss);
  128. oc << boost::serialization::make_nvp("value", static_cast<const T&>(val));
  129. }
  130. boost::archive::xml_iarchive ic(ss);
  131. ic >> boost::serialization::make_nvp("value", val2);
  132. BOOST_CHECK_EQUAL(val, val2);
  133. }
  134. #ifndef BOOST_NO_EXCEPTIONS
  135. }
  136. catch (const boost::exception& e)
  137. {
  138. std::cout << "Caught boost::exception with:\n";
  139. std::cout << diagnostic_information(e);
  140. }
  141. catch (const std::exception& e)
  142. {
  143. std::cout << "Caught std::exception with:\n";
  144. std::cout << e.what() << std::endl;
  145. }
  146. #endif
  147. test_neg(val, boost::mpl::bool_<std::numeric_limits<T>::is_signed>());
  148. //
  149. // Check to see if test is taking too long.
  150. // Tests run on the compiler farm time out after 300 seconds,
  151. // so don't get too close to that:
  152. //
  153. #ifndef CI_SUPPRESS_KNOWN_ISSUES
  154. if (tim.elapsed() > 150)
  155. #else
  156. if (tim.elapsed() > 25)
  157. #endif
  158. {
  159. std::cout << "Timeout reached, aborting tests now....\n";
  160. break;
  161. }
  162. }
  163. }
  164. #if !defined(TEST1) && !defined(TEST2) && !defined(TEST3) && !defined(TEST4)
  165. #define TEST1
  166. #define TEST2
  167. #define TEST3
  168. #define TEST4
  169. #endif
  170. int main()
  171. {
  172. using namespace boost::multiprecision;
  173. #ifdef TEST1
  174. test<cpp_int>();
  175. #endif
  176. #ifdef TEST2
  177. test<number<cpp_int_backend<61, 61, unsigned_magnitude, unchecked, void> > >();
  178. #endif
  179. #ifdef TEST3
  180. test<number<cpp_int_backend<120, 120, signed_magnitude, unchecked, void> > >();
  181. #endif
  182. #ifdef TEST4
  183. test<int1024_t>();
  184. #endif
  185. return boost::report_errors();
  186. }