test_macros.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 http://www.boost.org/LICENSE_1_
  5. //
  6. #ifndef BOOST_MULTIPRECISION_TEST_HPP
  7. #define BOOST_MULTIPRECISION_TEST_HPP
  8. #include <limits>
  9. #include <cmath>
  10. #include <typeinfo>
  11. #include <iostream>
  12. #include <iomanip>
  13. #include <stdlib.h>
  14. #include <boost/core/lightweight_test.hpp>
  15. #include <boost/current_function.hpp>
  16. #include <boost/static_assert.hpp>
  17. #include <boost/utility/enable_if.hpp>
  18. enum
  19. {
  20. warn_on_fail,
  21. error_on_fail,
  22. abort_on_fail
  23. };
  24. template <class T>
  25. inline int digits_of(const T&)
  26. {
  27. return std::numeric_limits<T>::is_specialized ? std::numeric_limits<T>::digits : 18;
  28. }
  29. inline std::ostream& report_where(const char* file, int line, const char* function)
  30. {
  31. if(function)
  32. BOOST_LIGHTWEIGHT_TEST_OSTREAM << "In function: "<< function << std::endl;
  33. BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << ":" << line;
  34. return BOOST_LIGHTWEIGHT_TEST_OSTREAM;
  35. }
  36. #define BOOST_MP_REPORT_WHERE report_where(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
  37. inline void report_severity(int severity)
  38. {
  39. if(severity == error_on_fail)
  40. ++boost::detail::test_errors();
  41. else if(severity == abort_on_fail)
  42. {
  43. ++boost::detail::test_errors();
  44. abort();
  45. }
  46. }
  47. #define BOOST_MP_REPORT_SEVERITY(severity) report_severity(severity)
  48. template <class E>
  49. void report_unexpected_exception(const E& e, int severity, const char* file, int line, const char* function)
  50. {
  51. report_where(file, line, function) << " Unexpected exception of type " << typeid(e).name() << std::endl;
  52. BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Errot message was: " << e.what() << std::endl;
  53. BOOST_MP_REPORT_SEVERITY(severity);
  54. }
  55. #define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity) \
  56. catch(const std::exception& __e) \
  57. { report_unexpected_exception(__e, severity, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); }\
  58. catch(...)\
  59. { BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Exception of unknown type was thrown" << std::endl; report_severity(severity); }
  60. #define BOOST_CHECK_IMP(x, severity)\
  61. try{ if(x){}else{\
  62. BOOST_MP_REPORT_WHERE << " Failed predicate: " << BOOST_STRINGIZE(x) << std::endl;\
  63. BOOST_MP_REPORT_SEVERITY(severity);\
  64. }\
  65. }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  66. #define BOOST_CHECK(x) BOOST_CHECK_IMP(x, error_on_fail)
  67. #define BOOST_WARN(x) BOOST_CHECK_IMP(x, warn_on_fail)
  68. #define BOOST_REQUIRE(x) BOOST_CHECK_IMP(x, abort_on_fail)
  69. #define BOOST_EQUAL_IMP(x, y, severity)\
  70. try{ if(!((x) == (y))){\
  71. BOOST_MP_REPORT_WHERE << " Failed check for equality: \n" \
  72. << std::setprecision(digits_of(x)) << std::scientific\
  73. << "Value of LHS was: " << (x) << "\n"\
  74. << "Value of RHS was: " << (y) << "\n"\
  75. << std::setprecision(3) << std::endl;\
  76. BOOST_MP_REPORT_SEVERITY(severity);\
  77. }\
  78. }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  79. #define BOOST_NE_IMP(x, y, severity)\
  80. try{ if(!(x != y)){\
  81. BOOST_MP_REPORT_WHERE << " Failed check for non-equality: \n" \
  82. << std::setprecision(digits_of(x)) << std::scientific\
  83. << "Value of LHS was: " << x << "\n"\
  84. << "Value of RHS was: " << y << "\n"\
  85. << std::setprecision(3) << std::endl;\
  86. BOOST_MP_REPORT_SEVERITY(severity);\
  87. }\
  88. }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  89. #define BOOST_LT_IMP(x, y, severity)\
  90. try{ if(!(x < y)){\
  91. BOOST_MP_REPORT_WHERE << " Failed check for less than: \n" \
  92. << std::setprecision(digits_of(x)) << std::scientific\
  93. << "Value of LHS was: " << x << "\n"\
  94. << "Value of RHS was: " << y << "\n"\
  95. << std::setprecision(3) << std::endl;\
  96. BOOST_MP_REPORT_SEVERITY(severity);\
  97. }\
  98. }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  99. #define BOOST_GT_IMP(x, y, severity)\
  100. try{ if(!(x > y)){\
  101. BOOST_MP_REPORT_WHERE << " Failed check for greater than: \n" \
  102. << std::setprecision(digits_of(x)) << std::scientific\
  103. << "Value of LHS was: " << x << "\n"\
  104. << "Value of RHS was: " << y << "\n"\
  105. << std::setprecision(3) << std::endl;\
  106. BOOST_MP_REPORT_SEVERITY(severity);\
  107. }\
  108. }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  109. #define BOOST_LE_IMP(x, y, severity)\
  110. try{ if(!(x <= y)){\
  111. BOOST_MP_REPORT_WHERE << " Failed check for less-than-equal-to: \n" \
  112. << std::setprecision(digits_of(x)) << std::scientific\
  113. << "Value of LHS was: " << x << "\n"\
  114. << "Value of RHS was: " << y << "\n"\
  115. << std::setprecision(3) << std::endl;\
  116. BOOST_MP_REPORT_SEVERITY(severity);\
  117. }\
  118. }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  119. #define BOOST_GE_IMP(x, y, severity)\
  120. try{ if(!(x >= y)){\
  121. BOOST_MP_REPORT_WHERE << " Failed check for greater-than-equal-to \n" \
  122. << std::setprecision(digits_of(x)) << std::scientific\
  123. << "Value of LHS was: " << x << "\n"\
  124. << "Value of RHS was: " << y << "\n"\
  125. << std::setprecision(3) << std::endl;\
  126. BOOST_MP_REPORT_SEVERITY(severity);\
  127. }\
  128. }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  129. #define BOOST_MT_CHECK_THROW_IMP(x, E, severity)\
  130. try{ \
  131. x;\
  132. BOOST_MP_REPORT_WHERE << " Expected exception not thrown in expression " << BOOST_STRINGIZE(x) << std::endl;\
  133. BOOST_MP_REPORT_SEVERITY(severity);\
  134. }\
  135. catch(const E&){}\
  136. BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  137. template <class I, class J>
  138. bool check_equal_collections(I a, I b, J x, J y)
  139. {
  140. int i = 0;
  141. while(a != b)
  142. {
  143. if(x == y)
  144. {
  145. BOOST_LIGHTWEIGHT_TEST_OSTREAM << " Unexpected end of second sequence" << std::endl;
  146. return false;
  147. }
  148. if(*a != *x)
  149. {
  150. BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Error occured in position " << i << " of the collection." << std::endl;
  151. BOOST_LIGHTWEIGHT_TEST_OSTREAM << "First value was " << std::setprecision(digits_of(x)) << std::scientific << *a << std::endl;
  152. BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Second value was " << std::setprecision(digits_of(x)) << std::scientific << *x << std::endl;
  153. return false;
  154. }
  155. ++a;
  156. ++x;
  157. }
  158. return true;
  159. }
  160. #define BOOST_MT_CHECK_EQ_COLLECTIONS(a, b, x, y, severity)\
  161. try{ \
  162. if(!check_equal_collections(a, b, x, y))\
  163. {\
  164. BOOST_MP_REPORT_WHERE << " Collections were not equal" << std::endl;\
  165. BOOST_MP_REPORT_SEVERITY(severity);\
  166. }\
  167. }\
  168. BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
  169. #define BOOST_CHECK_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, error_on_fail)
  170. #define BOOST_WARN_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, warn_on_fail)
  171. #define BOOST_REQUIRE_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, abort_on_fail)
  172. #define BOOST_CHECK_NE(x, y) BOOST_NE_IMP(x, y, error_on_fail)
  173. #define BOOST_WARN_NE(x, y) BOOST_NE_IMP(x, y, warn_on_fail)
  174. #define BOOST_REQUIRE_NE(x, y) BOOST_NE_IMP(x, y, abort_on_fail)
  175. #define BOOST_CHECK_LT(x, y) BOOST_LT_IMP(x, y, error_on_fail)
  176. #define BOOST_WARN_LT(x, y) BOOST_LT_IMP(x, y, warn_on_fail)
  177. #define BOOST_REQUIRE_LT(x, y) BOOST_LT_IMP(x, y, abort_on_fail)
  178. #define BOOST_CHECK_GT(x, y) BOOST_GT_IMP(x, y, error_on_fail)
  179. #define BOOST_WARN_GT(x, y) BOOST_GT_IMP(x, y, warn_on_fail)
  180. #define BOOST_REQUIRE_GT(x, y) BOOST_GT_IMP(x, y, abort_on_fail)
  181. #define BOOST_CHECK_LE(x, y) BOOST_LE_IMP(x, y, error_on_fail)
  182. #define BOOST_WARN_LE(x, y) BOOST_LE_IMP(x, y, warn_on_fail)
  183. #define BOOST_REQUIRE_LE(x, y) BOOST_LE_IMP(x, y, abort_on_fail)
  184. #define BOOST_CHECK_GE(x, y) BOOST_GE_IMP(x, y, error_on_fail)
  185. #define BOOST_WARN_GE(x, y) BOOST_GE_IMP(x, y, warn_on_fail)
  186. #define BOOST_REQUIRE_GE(x, y) BOOST_GE_IMP(x, y, abort_on_fail)
  187. #define BOOST_CHECK_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, error_on_fail)
  188. #define BOOST_WARN_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, warn_on_fail)
  189. #define BOOST_REQUIRE_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, abort_on_fail)
  190. #define BOOST_CHECK_EQUAL_COLLECTIONS(a, b, x, y) BOOST_MT_CHECK_EQ_COLLECTIONS(a, b, x, y, error_on_fail)
  191. #define BOOST_WARN_EQUAL_COLLECTIONS(a, b, x, y) BOOST_MT_CHECK_EQ_COLLECTIONS(a, b, x, y, warn_on_fail)
  192. #define BOOST_REQUIRE_EQUAL_COLLECTIONS(a, b, x, y) BOOST_MT_CHECK_EQ_COLLECTIONS(a, b, x, y, abort_on_fail)
  193. #endif