test_adapt_serial.cpp 5.5 KB

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