test_cpp_rat_serial.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/binary_oarchive.hpp>
  23. #include <boost/archive/xml_iarchive.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. if (!val)
  55. val = 1;
  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. boost::archive::binary_oarchive ob(ss);
  78. ob << static_cast<const T&>(val);
  79. boost::archive::binary_iarchive ib(ss);
  80. ib >> val2;
  81. BOOST_CHECK_EQUAL(val, val2);
  82. }
  83. {
  84. std::stringstream ss;
  85. {
  86. boost::archive::xml_oarchive ob(ss);
  87. ob << boost::serialization::make_nvp("value", static_cast<const T&>(val));
  88. }
  89. boost::archive::xml_iarchive ib(ss);
  90. ib >> boost::serialization::make_nvp("value", 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<typename component_type<T>::type>(d(gen)), generate_random<typename component_type<T>::type>(d(gen)));
  119. #ifndef BOOST_NO_EXCEPTIONS
  120. try
  121. {
  122. #endif
  123. std::stringstream ss;
  124. boost::archive::text_oarchive oa(ss);
  125. oa << static_cast<const T&>(val);
  126. boost::archive::text_iarchive ia(ss);
  127. T val2;
  128. ia >> val2;
  129. BOOST_CHECK_EQUAL(val, val2);
  130. ss.clear();
  131. boost::archive::binary_oarchive ob(ss);
  132. ob << static_cast<const T&>(val);
  133. boost::archive::binary_iarchive ib(ss);
  134. ib >> val2;
  135. BOOST_CHECK_EQUAL(val, val2);
  136. #ifndef BOOST_NO_EXCEPTIONS
  137. }
  138. catch (const boost::exception& e)
  139. {
  140. std::cout << "Caught boost::exception with:\n";
  141. std::cout << diagnostic_information(e);
  142. }
  143. catch (const std::exception& e)
  144. {
  145. std::cout << "Caught std::exception with:\n";
  146. std::cout << e.what() << std::endl;
  147. }
  148. #endif
  149. test_neg(val, boost::mpl::bool_<std::numeric_limits<T>::is_signed>());
  150. //
  151. // Check to see if test is taking too long.
  152. // Tests run on the compiler farm time out after 300 seconds,
  153. // so don't get too close to that:
  154. //
  155. #ifndef CI_SUPPRESS_KNOWN_ISSUES
  156. if (tim.elapsed() > 150)
  157. #else
  158. if (tim.elapsed() > 25)
  159. #endif
  160. {
  161. std::cout << "Timeout reached, aborting tests now....\n";
  162. break;
  163. }
  164. }
  165. }
  166. int main()
  167. {
  168. using namespace boost::multiprecision;
  169. test<cpp_rational>();
  170. return boost::report_errors();
  171. }