test.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. #ifndef BOOST_MULTIPRECISION_TEST_HPP
  7. #define BOOST_MULTIPRECISION_TEST_HPP
  8. #include <limits>
  9. #include <cmath>
  10. #include <typeinfo>
  11. #include <boost/detail/lightweight_test.hpp>
  12. #include <boost/current_function.hpp>
  13. #include <boost/static_assert.hpp>
  14. #include <boost/utility/enable_if.hpp>
  15. #include <boost/type_traits/is_unsigned.hpp>
  16. #include <boost/multiprecision/number.hpp>
  17. namespace detail {
  18. template <class T>
  19. inline typename boost::disable_if_c<boost::is_unsigned<T>::value || boost::multiprecision::is_unsigned_number<T>::value, T>::type
  20. abs(const T& a)
  21. {
  22. return a < 0 ? -a : a;
  23. }
  24. template <class T>
  25. inline typename boost::enable_if_c<boost::is_unsigned<T>::value || boost::multiprecision::is_unsigned_number<T>::value, T>::type
  26. abs(const T& a)
  27. {
  28. return a;
  29. }
  30. } // namespace detail
  31. template <class T>
  32. typename boost::enable_if_c<boost::multiprecision::number_category<T>::value == boost::multiprecision::number_kind_integer, T>::type relative_error(T a, T b)
  33. {
  34. return a > b ? a - b : b - a;
  35. }
  36. template <class T>
  37. typename boost::disable_if_c<(boost::multiprecision::number_category<T>::value == boost::multiprecision::number_kind_integer) || boost::multiprecision::is_interval_number<T>::value, T>::type relative_error(T a, T b)
  38. {
  39. using ::detail::abs;
  40. using std::abs;
  41. T min_val = (std::numeric_limits<T>::min)();
  42. T max_val = (std::numeric_limits<T>::max)();
  43. if ((a != 0) && (b != 0))
  44. {
  45. if (a == b)
  46. return 0;
  47. // TODO: use isfinite:
  48. if (abs(b) >= max_val)
  49. {
  50. if (abs(a) >= max_val)
  51. return 0; // one infinity is as good as another!
  52. }
  53. // If the result is denormalised, treat all denorms as equivalent:
  54. if ((a < min_val) && (a > 0))
  55. a = min_val;
  56. else if ((a > -min_val) && (a < 0))
  57. a = -min_val;
  58. if ((b < min_val) && (b > 0))
  59. b = min_val;
  60. else if ((b > -min_val) && (b < 0))
  61. b = -min_val;
  62. return (std::max)(abs(T((a - b) / a)), abs(T((a - b) / b))) / std::numeric_limits<T>::epsilon();
  63. }
  64. // Handle special case where one or both are zero:
  65. if (min_val == 0)
  66. return abs(T(a - b));
  67. if (abs(a) < min_val)
  68. a = min_val;
  69. if (abs(b) < min_val)
  70. b = min_val;
  71. return (std::max)(abs(T((a - b) / a)), abs(T((a - b) / b))) / std::numeric_limits<T>::epsilon();
  72. }
  73. template <class T, class U>
  74. typename boost::mpl::if_c<boost::is_convertible<T, U>::value, U, T>::type
  75. relative_error(T a, U b)
  76. {
  77. typedef typename boost::mpl::if_c<boost::is_convertible<T, U>::value, U, T>::type cast_type;
  78. return relative_error<cast_type>(static_cast<cast_type>(a), static_cast<cast_type>(b));
  79. }
  80. template <class T>
  81. typename boost::enable_if_c<boost::multiprecision::is_interval_number<T>::value, T>::type relative_error(T a, T b)
  82. {
  83. typename boost::multiprecision::component_type<T>::type am = median(a);
  84. typename boost::multiprecision::component_type<T>::type bm = median(b);
  85. return relative_error<typename boost::multiprecision::component_type<T>::type>(am, bm);
  86. }
  87. enum
  88. {
  89. warn_on_fail,
  90. error_on_fail,
  91. abort_on_fail
  92. };
  93. template <class T>
  94. inline T epsilon_of(const T&)
  95. {
  96. BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_specialized);
  97. return std::numeric_limits<T>::is_integer ? static_cast<T>(1) : std::numeric_limits<T>::epsilon();
  98. }
  99. template <class T>
  100. inline int digits_of(const T&)
  101. {
  102. return std::numeric_limits<T>::is_specialized ? std::numeric_limits<T>::digits10 + 3 : std::numeric_limits<long double>::digits10 + 3;
  103. }
  104. inline std::ostream& report_where(const char* file, int line, const char* function)
  105. {
  106. if (function)
  107. BOOST_LIGHTWEIGHT_TEST_OSTREAM << "In function: " << function << std::endl;
  108. BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << ":" << line;
  109. return BOOST_LIGHTWEIGHT_TEST_OSTREAM;
  110. }
  111. #define BOOST_MP_REPORT_WHERE report_where(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
  112. inline void report_severity(int severity)
  113. {
  114. if (severity == error_on_fail)
  115. ++boost::detail::test_errors();
  116. else if (severity == abort_on_fail)
  117. {
  118. ++boost::detail::test_errors();
  119. abort();
  120. }
  121. }
  122. #define BOOST_MP_REPORT_SEVERITY(severity) report_severity(severity)
  123. template <class E>
  124. void report_unexpected_exception(const E& e, int severity, const char* file, int line, const char* function)
  125. {
  126. report_where(file, line, function) << " Unexpected exception of type " << typeid(e).name() << std::endl;
  127. BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Errot message was: " << e.what() << std::endl;
  128. BOOST_MP_REPORT_SEVERITY(severity);
  129. }
  130. #ifndef BOOST_NO_EXCEPTIONS
  131. #define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity) \
  132. catch (const std::exception& e) \
  133. { \
  134. report_unexpected_exception(e, severity, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
  135. } \
  136. catch (...) \
  137. { \
  138. std::cout << "Exception of unknown type was thrown" << std::endl; \
  139. report_severity(severity); \
  140. }
  141. #define BOOST_MP_TRY try
  142. #else
  143. #define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  144. #define BOOST_MP_TRY
  145. #endif
  146. #define BOOST_CHECK_IMP(x, severity) \
  147. BOOST_MP_TRY \
  148. { \
  149. if (x) \
  150. { \
  151. } \
  152. else \
  153. { \
  154. BOOST_MP_REPORT_WHERE << " Failed predicate: " << BOOST_STRINGIZE(x) << std::endl; \
  155. BOOST_MP_REPORT_SEVERITY(severity); \
  156. } \
  157. } \
  158. BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  159. #define BOOST_CHECK(x) BOOST_CHECK_IMP(x, error_on_fail)
  160. #define BOOST_WARN(x) BOOST_CHECK_IMP(x, warn_on_fail)
  161. #define BOOST_REQUIRE(x) BOOST_CHECK_IMP(x, abort_on_fail)
  162. #define BOOST_CLOSE_IMP(x, y, tol, severity) \
  163. BOOST_MP_TRY \
  164. { \
  165. if (relative_error(x, y) > tol) \
  166. { \
  167. BOOST_MP_REPORT_WHERE << " Failed check for closeness: \n" \
  168. << std::setprecision(digits_of(x)) << std::scientific \
  169. << "Value of LHS was: " << x << "\n" \
  170. << "Value of RHS was: " << y << "\n" \
  171. << std::setprecision(5) << std::fixed \
  172. << "Relative error was: " << relative_error(x, y) << "eps\n" \
  173. << "Tolerance was: " << tol << "eps" << std::endl; \
  174. BOOST_MP_REPORT_SEVERITY(severity); \
  175. } \
  176. } \
  177. BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  178. #define BOOST_EQUAL_IMP(x, y, severity) \
  179. BOOST_MP_TRY \
  180. { \
  181. if (!((x) == (y))) \
  182. { \
  183. BOOST_MP_REPORT_WHERE << " Failed check for equality: \n" \
  184. << std::setprecision(digits_of(x)) << std::scientific \
  185. << "Value of LHS was: " << (x) << "\n" \
  186. << "Value of RHS was: " << (y) << "\n" \
  187. << std::setprecision(3) << std::endl; \
  188. BOOST_MP_REPORT_SEVERITY(severity); \
  189. } \
  190. } \
  191. BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  192. #define BOOST_NE_IMP(x, y, severity) \
  193. BOOST_MP_TRY \
  194. { \
  195. if (!(x != y)) \
  196. { \
  197. BOOST_MP_REPORT_WHERE << " Failed check for non-equality: \n" \
  198. << std::setprecision(digits_of(x)) << std::scientific \
  199. << "Value of LHS was: " << x << "\n" \
  200. << "Value of RHS was: " << y << "\n" \
  201. << std::setprecision(3) << std::endl; \
  202. BOOST_MP_REPORT_SEVERITY(severity); \
  203. } \
  204. } \
  205. BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  206. #define BOOST_LT_IMP(x, y, severity) \
  207. BOOST_MP_TRY \
  208. { \
  209. if (!(x < y)) \
  210. { \
  211. BOOST_MP_REPORT_WHERE << " Failed check for less than: \n" \
  212. << std::setprecision(digits_of(x)) << std::scientific \
  213. << "Value of LHS was: " << x << "\n" \
  214. << "Value of RHS was: " << y << "\n" \
  215. << std::setprecision(3) << std::endl; \
  216. BOOST_MP_REPORT_SEVERITY(severity); \
  217. } \
  218. } \
  219. BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  220. #define BOOST_GT_IMP(x, y, severity) \
  221. BOOST_MP_TRY \
  222. { \
  223. if (!(x > y)) \
  224. { \
  225. BOOST_MP_REPORT_WHERE << " Failed check for greater than: \n" \
  226. << std::setprecision(digits_of(x)) << std::scientific \
  227. << "Value of LHS was: " << x << "\n" \
  228. << "Value of RHS was: " << y << "\n" \
  229. << std::setprecision(3) << std::endl; \
  230. BOOST_MP_REPORT_SEVERITY(severity); \
  231. } \
  232. } \
  233. BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  234. #define BOOST_LE_IMP(x, y, severity) \
  235. BOOST_MP_TRY \
  236. { \
  237. if (!(x <= y)) \
  238. { \
  239. BOOST_MP_REPORT_WHERE << " Failed check for less-than-equal-to: \n" \
  240. << std::setprecision(digits_of(x)) << std::scientific \
  241. << "Value of LHS was: " << x << "\n" \
  242. << "Value of RHS was: " << y << "\n" \
  243. << std::setprecision(3) << std::endl; \
  244. BOOST_MP_REPORT_SEVERITY(severity); \
  245. } \
  246. } \
  247. BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  248. #define BOOST_GE_IMP(x, y, severity) \
  249. BOOST_MP_TRY \
  250. { \
  251. if (!(x >= y)) \
  252. { \
  253. BOOST_MP_REPORT_WHERE << " Failed check for greater-than-equal-to \n" \
  254. << std::setprecision(digits_of(x)) << std::scientific \
  255. << "Value of LHS was: " << x << "\n" \
  256. << "Value of RHS was: " << y << "\n" \
  257. << std::setprecision(3) << std::endl; \
  258. BOOST_MP_REPORT_SEVERITY(severity); \
  259. } \
  260. } \
  261. BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  262. #ifndef BOOST_NO_EXCEPTIONS
  263. #define BOOST_MT_CHECK_THROW_IMP(x, E, severity) \
  264. BOOST_MP_TRY \
  265. { \
  266. x; \
  267. BOOST_MP_REPORT_WHERE << " Expected exception not thrown in expression " << BOOST_STRINGIZE(x) << std::endl; \
  268. BOOST_MP_REPORT_SEVERITY(severity); \
  269. } \
  270. catch (const E&) {} \
  271. BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  272. #else
  273. #define BOOST_MT_CHECK_THROW_IMP(x, E, severity)
  274. #endif
  275. #define BOOST_CHECK_CLOSE(x, y, tol) BOOST_CLOSE_IMP(x, y, ((tol / (100 * epsilon_of(x)))), error_on_fail)
  276. #define BOOST_WARN_CLOSE(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (100 * epsilon_of(x))), warn_on_fail)
  277. #define BOOST_REQUIRE_CLOSE(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (100 * epsilon_of(x))), abort_on_fail)
  278. #define BOOST_CHECK_CLOSE_FRACTION(x, y, tol) BOOST_CLOSE_IMP(x, y, ((tol / (epsilon_of(x)))), error_on_fail)
  279. #define BOOST_WARN_CLOSE_FRACTION(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (epsilon_of(x))), warn_on_fail)
  280. #define BOOST_REQUIRE_CLOSE_FRACTION(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (epsilon_of(x))), abort_on_fail)
  281. #define BOOST_CHECK_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, error_on_fail)
  282. #define BOOST_WARN_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, warn_on_fail)
  283. #define BOOST_REQUIRE_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, abort_on_fail)
  284. #define BOOST_CHECK_NE(x, y) BOOST_NE_IMP(x, y, error_on_fail)
  285. #define BOOST_WARN_NE(x, y) BOOST_NE_IMP(x, y, warn_on_fail)
  286. #define BOOST_REQUIRE_NE(x, y) BOOST_NE_IMP(x, y, abort_on_fail)
  287. #define BOOST_CHECK_LT(x, y) BOOST_LT_IMP(x, y, error_on_fail)
  288. #define BOOST_WARN_LT(x, y) BOOST_LT_IMP(x, y, warn_on_fail)
  289. #define BOOST_REQUIRE_LT(x, y) BOOST_LT_IMP(x, y, abort_on_fail)
  290. #define BOOST_CHECK_GT(x, y) BOOST_GT_IMP(x, y, error_on_fail)
  291. #define BOOST_WARN_GT(x, y) BOOST_GT_IMP(x, y, warn_on_fail)
  292. #define BOOST_REQUIRE_GT(x, y) BOOST_GT_IMP(x, y, abort_on_fail)
  293. #define BOOST_CHECK_LE(x, y) BOOST_LE_IMP(x, y, error_on_fail)
  294. #define BOOST_WARN_LE(x, y) BOOST_LE_IMP(x, y, warn_on_fail)
  295. #define BOOST_REQUIRE_LE(x, y) BOOST_LE_IMP(x, y, abort_on_fail)
  296. #define BOOST_CHECK_GE(x, y) BOOST_GE_IMP(x, y, error_on_fail)
  297. #define BOOST_WARN_GE(x, y) BOOST_GE_IMP(x, y, warn_on_fail)
  298. #define BOOST_REQUIRE_GE(x, y) BOOST_GE_IMP(x, y, abort_on_fail)
  299. #define BOOST_CHECK_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, error_on_fail)
  300. #define BOOST_WARN_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, warn_on_fail)
  301. #define BOOST_REQUIRE_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, abort_on_fail)
  302. #endif