utils.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /**
  2. * \file util.hpp
  3. *
  4. * \brief Utility macros/functions for testing and debugging purpose.
  5. *
  6. * Basic usage:
  7. * <pre>
  8. * BOOST_UBLAS_TEST_DEF( test_case_1 )
  9. * {
  10. * // do your test stuff
  11. * }
  12. *
  13. * BOOST_UBLAS_TEST_DEF( test_case_2 )
  14. * {
  15. * // do your test stuff
  16. * }
  17. *
  18. * // ...
  19. *
  20. * BOOST_UBLAS_TEST_DEF( test_case_n )
  21. * {
  22. * // do your test stuff
  23. * }
  24. *
  25. * int main()
  26. * {
  27. * BOOST_UBLAS_TEST_SUITE( "My Test Suite" ); // optional
  28. *
  29. * BOOST_UBLAS_TEST_BEGIN();
  30. * BOOST_UBLAS_TEST_DO( test_case_1 );
  31. * BOOST_UBLAS_TEST_DO( test_case_2 );
  32. * // ...
  33. * BOOST_UBLAS_TEST_DO( test_case_n );
  34. * BOOST_UBLAS_TEST_END();
  35. * }
  36. * </pre>
  37. * Inside each <em>test_case_<code>k</code></em> you can use the various
  38. * \c BOOST_UBLAS_TEST_CHECK* macros.
  39. *
  40. * <hr/>
  41. *
  42. * Copyright (c) 2009-2012, Marco Guazzone
  43. *
  44. * Distributed under the Boost Software License, Version 1.0. (See
  45. * accompanying file LICENSE_1_0.txt or copy at
  46. * http://www.boost.org/LICENSE_1_0.txt)
  47. *
  48. * \author Marco Guazzone, marco.guazzone@gmail.com
  49. */
  50. #ifndef BOOST_NUMERIC_UBLAS_TEST_UTILS_HPP
  51. #define BOOST_NUMERIC_UBLAS_TEST_UTILS_HPP
  52. #include <boost/numeric/ublas/detail/config.hpp>
  53. #include <boost/numeric/ublas/traits.hpp>
  54. #include <boost/math/special_functions/fpclassify.hpp> // isnan, isinf
  55. #include <cmath>
  56. #include <complex>
  57. #include <cstddef>
  58. #include <iostream>
  59. #include <limits>
  60. #include <stdexcept>
  61. #define BOOST_UBLAS_NOT_USED(x) (void)(x)
  62. namespace boost { namespace numeric { namespace ublas { namespace test { namespace detail { namespace /*<unnamed>*/ {
  63. using ::std::abs;
  64. using ::std::max;
  65. /// Check if the given complex number is a NaN.
  66. // read the comments in fpclassify as well
  67. template <typename T>
  68. BOOST_UBLAS_INLINE
  69. bool (isnan)(::std::complex<T> const& z)
  70. {
  71. // According to IEEE, NaN is different even by itself
  72. return (z != z) || (boost::math::isnan)(z.real()) || (boost::math::isnan)(z.imag());
  73. }
  74. /// Check if two (real) numbers are close each other (wrt a given tolerance).
  75. template <typename T1, typename T2, typename T3>
  76. BOOST_UBLAS_INLINE
  77. bool close_to(T1 x, T2 y, T3 tol)
  78. {
  79. typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type,
  80. T3>::promote_type real_type;
  81. if ((boost::math::isnan)(x) || (boost::math::isnan)(y))
  82. {
  83. // According to IEEE, NaN is different even by itself
  84. return false;
  85. }
  86. return abs(x-y) <= (max(static_cast<real_type>(abs(x)), static_cast<real_type>(abs(y)))*tol);
  87. }
  88. /// Check if two complex numbers are close each other (wrt a given tolerance).
  89. template <typename T1, typename T2, typename T3>
  90. BOOST_UBLAS_INLINE
  91. bool close_to(::std::complex<T1> const& x, ::std::complex<T2> const& y, T3 tol)
  92. {
  93. typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type,
  94. T3>::promote_type real_type;
  95. if ((isnan)(x) || (isnan)(y))
  96. {
  97. // According to IEEE, NaN is different even by itself
  98. return false;
  99. }
  100. ::std::complex<real_type> xx(x);
  101. ::std::complex<real_type> yy(y);
  102. return abs(xx-yy) <= (max(abs(xx), abs(yy))*tol);
  103. }
  104. /// Check if two (real) numbers are close each other (wrt a given tolerance).
  105. template <typename T1, typename T2, typename T3>
  106. BOOST_UBLAS_INLINE
  107. bool rel_close_to(T1 x, T2 y, T3 tol)
  108. {
  109. //typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type,
  110. // T3>::promote_type real_type;
  111. if ((boost::math::isnan)(x) || (boost::math::isnan)(y))
  112. {
  113. // According to IEEE, NaN is different even by itself
  114. return false;
  115. }
  116. return abs(x-y)/abs(y) <= tol;
  117. }
  118. /// Check if two complex numbers are close each other (wrt a given tolerance).
  119. template <typename T1, typename T2, typename T3>
  120. BOOST_UBLAS_INLINE
  121. bool rel_close_to(::std::complex<T1> const& x, ::std::complex<T2> const& y, T3 tol)
  122. {
  123. typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type,
  124. T3>::promote_type real_type;
  125. if ((isnan)(x) || (isnan)(y))
  126. {
  127. // According to IEEE, NaN is different even by itself
  128. return false;
  129. }
  130. ::std::complex<real_type> xx(x);
  131. ::std::complex<real_type> yy(y);
  132. return abs(xx-yy)/abs(yy) <= tol;
  133. }
  134. }}}}}} // Namespace boost::numeric::ublas::test::detail::<unnamed>
  135. /// Expand its argument \a x.
  136. #define BOOST_UBLAS_TEST_EXPAND_(x) x
  137. /// Expand its argument \a x inside parenthesis.
  138. #define BOOST_UBLAS_TEST_EXPANDP_(x) (x)
  139. /// Transform its argument \a x into a string.
  140. #define BOOST_UBLAS_TEST_STRINGIFY_(x) #x
  141. /// Concatenate its two \e string arguments \a x and \a y.
  142. #define BOOST_UBLAS_TEST_JOIN_(x,y) x ## y
  143. /// Output the message \a x if in debug-mode; otherwise output nothing.
  144. /// Note: we don't use macro expansion inside parenthesis to let \a m be an
  145. /// expression of the form <code>a &lt;&lt; b</code>.
  146. #ifndef NDEBUG
  147. # define BOOST_UBLAS_DEBUG_TRACE(x) ::std::cerr << "[Debug>> " << BOOST_UBLAS_TEST_EXPAND_(x) << ::std::endl
  148. #else
  149. # define BOOST_UBLAS_DEBUG_TRACE(x) /**/
  150. #endif // NDEBUG
  151. /// Define the name \a m of the entire test suite.
  152. #define BOOST_UBLAS_TEST_SUITE(m) ::std::cerr << "--- Test Suite: " << BOOST_UBLAS_TEST_EXPAND_(m) << " ---" << ::std::endl;
  153. /// Define the beginning of a test suite.
  154. #define BOOST_UBLAS_TEST_BEGIN() /* [BOOST_UBLAS_TEST_BEGIN] */ \
  155. { \
  156. /* Begin of Test Suite */ \
  157. ::std::size_t test_fails__(0) \
  158. /* [/BOOST_UBLAS_TEST_BEGIN] */
  159. /// Define a test case \a x inside the current test suite.
  160. #define BOOST_UBLAS_TEST_DEF(x) static void BOOST_UBLAS_TEST_EXPAND_(x)(::std::size_t& test_fails__)
  161. /// Call the test case \a x.
  162. #define BOOST_UBLAS_TEST_DO(x) /* [BOOST_UBLAS_TEST_DO] */ \
  163. try \
  164. { \
  165. BOOST_UBLAS_TEST_EXPAND_(x)(test_fails__); \
  166. } \
  167. catch (::std::exception& e) \
  168. { \
  169. ++test_fails__; \
  170. BOOST_UBLAS_TEST_ERROR( e.what() ); \
  171. } \
  172. catch (...) \
  173. { \
  174. ++test_fails__; \
  175. } \
  176. /* [/BOOST_UBLAS_TEST_DO] */
  177. /// Define the end of a test suite and return non-zero value if any test failed.
  178. #define BOOST_UBLAS_TEST_END() /* [BOOST_UBLAS_TEST_END] */ \
  179. if (test_fails__ > 0) \
  180. { \
  181. ::std::cerr << "Number of failed tests: " << test_fails__ << ::std::endl; \
  182. return EXIT_FAILURE; \
  183. } \
  184. else \
  185. { \
  186. ::std::cerr << "No failed test" << ::std::endl; \
  187. return EXIT_SUCCESS; \
  188. } \
  189. } /* End of test suite */ \
  190. /* [/BOOST_UBLAS_TEST_END] */
  191. /// Output the message \a m.
  192. /// Note: we don't use macro expansion inside parenthesis to let \a m be an
  193. /// expression of the form <code>a &lt;&lt; b</code>.
  194. #define BOOST_UBLAS_TEST_TRACE(m) ::std::cerr << "[Info>> " << BOOST_UBLAS_TEST_EXPAND_(m) << ::std::endl
  195. /// Check the truth of assertion \a x.
  196. #define BOOST_UBLAS_TEST_CHECK(x) /* [BOOST_UBLAS_TEST_CHECK] */ \
  197. if (!BOOST_UBLAS_TEST_EXPANDP_(x)) \
  198. { \
  199. BOOST_UBLAS_TEST_ERROR( "Failed assertion: " << BOOST_UBLAS_TEST_STRINGIFY_(x) ); \
  200. ++test_fails__; \
  201. } \
  202. /* [/BOOST_UBLAS_TEST_CHECK] */
  203. /// Check for the equality of \a x against \a y.
  204. #define BOOST_UBLAS_TEST_CHECK_EQ(x,y) /* [BOOST_UBLAS_TEST_CHECK_EQUAL] */ \
  205. if (!(BOOST_UBLAS_TEST_EXPANDP_(x) == BOOST_UBLAS_TEST_EXPANDP_(y))) \
  206. { \
  207. BOOST_UBLAS_TEST_ERROR( "Failed assertion: (" << BOOST_UBLAS_TEST_STRINGIFY_(x) << " == " << BOOST_UBLAS_TEST_STRINGIFY_(y) << ")" ); \
  208. ++test_fails__; \
  209. } \
  210. /* [/BOOST_UBLAS_TEST_CHECK_EQUAL] */
  211. /// Alias for macro \c BOOST_UBLAS_TEST_CHECK_EQ (for backward compatibility).
  212. #define BOOST_UBLAS_TEST_CHECK_EQUAL(x,y) BOOST_UBLAS_TEST_CHECK_EQ(x,y)
  213. /// Check that \a x and \a y are close with respect to a given precision \a e.
  214. #define BOOST_UBLAS_TEST_CHECK_CLOSE(x,y,e) /* [BOOST_UBLAS_TEST_CHECK_CLOSE] */ \
  215. if (!::boost::numeric::ublas::test::detail::close_to(BOOST_UBLAS_TEST_EXPAND_(x), BOOST_UBLAS_TEST_EXPAND_(y), BOOST_UBLAS_TEST_EXPAND_(e))) \
  216. { \
  217. BOOST_UBLAS_TEST_ERROR( "Failed assertion: abs(" << BOOST_UBLAS_TEST_STRINGIFY_(x) << "-" << BOOST_UBLAS_TEST_STRINGIFY_(y) << ") <= " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " [with " << BOOST_UBLAS_TEST_STRINGIFY_(x) << " == " << BOOST_UBLAS_TEST_EXPANDP_(x) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y) << " == " << BOOST_UBLAS_TEST_EXPANDP_(y) << " and " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " == " << BOOST_UBLAS_TEST_EXPANDP_(e) << "]" ); \
  218. ++test_fails__; \
  219. } \
  220. /* [/BOOST_UBLAS_TEST_CHECK_CLOSE] */
  221. /// Alias for macro \c BOOST_UBLAS_TEST_CHECK_CLOSE (for backward compatibility),
  222. #define BOOST_UBLAS_TEST_CHECK_PRECISION(x,y,e) BOOST_UBLAS_TEST_CHECK_CLOSE(x,y,e)
  223. /// Check that \a x is close to \a y with respect to a given relative precision \a e.
  224. #define BOOST_UBLAS_TEST_CHECK_REL_CLOSE(x,y,e) /* [BOOST_UBLAS_TEST_CHECK_REL_CLOSE] */ \
  225. if (!::boost::numeric::ublas::test::detail::rel_close_to(BOOST_UBLAS_TEST_EXPAND_(x), BOOST_UBLAS_TEST_EXPAND_(y), BOOST_UBLAS_TEST_EXPAND_(e))) \
  226. { \
  227. BOOST_UBLAS_TEST_ERROR( "Failed assertion: abs((" << BOOST_UBLAS_TEST_STRINGIFY_(x) << "-" << BOOST_UBLAS_TEST_STRINGIFY_(y) << ")/" << BOOST_UBLAS_TEST_STRINGIFY_(y) << ") <= " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " [with " << BOOST_UBLAS_TEST_STRINGIFY_(x) << " == " << BOOST_UBLAS_TEST_EXPANDP_(x) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y) << " == " << BOOST_UBLAS_TEST_EXPANDP_(y) << " and " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " == " << BOOST_UBLAS_TEST_EXPANDP_(e) << "]" ); \
  228. ++test_fails__; \
  229. } \
  230. /* [/BOOST_UBLAS_TEST_CHECK_REL_CLOSE] */
  231. /// Alias for macro \c BOOST_UBLAS_TEST_CHECK_REL_CLOSE (for backward compatibility),
  232. #define BOOST_UBLAS_TEST_CHECK_REL_PRECISION(x,y,e) BOOST_UBLAS_TEST_CHECK_REL_CLOSE(x,y,e)
  233. /// Check that elements of \a x and \a y are equal.
  234. #define BOOST_UBLAS_TEST_CHECK_VECTOR_EQ(x,y,n) /* [BOOST_UBLAS_TEST_CHECK_VECTOR_EQ] */ \
  235. if (BOOST_UBLAS_TEST_EXPANDP_(n) > 0) \
  236. { \
  237. ::std::size_t n__ = BOOST_UBLAS_TEST_EXPAND_(n); \
  238. for (::std::size_t i__ = n__; i__ > 0; --i__) \
  239. { \
  240. if (!(BOOST_UBLAS_TEST_EXPANDP_(x)[n__-i__]==BOOST_UBLAS_TEST_EXPANDP_(y)[n__-i__])) \
  241. { \
  242. BOOST_UBLAS_TEST_ERROR( "Failed assertion: (" << BOOST_UBLAS_TEST_STRINGIFY_(x[i__]) << "==" << BOOST_UBLAS_TEST_STRINGIFY_(y[i__]) << ")" << " [with " << BOOST_UBLAS_TEST_STRINGIFY_(x[i__]) << " == " << BOOST_UBLAS_TEST_EXPANDP_(x)[n__-i__] << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y[i__]) << " == " << BOOST_UBLAS_TEST_EXPANDP_(y)[n__-i__] << ", " << BOOST_UBLAS_TEST_STRINGIFY_(i__) << " == " << i__ << " and " << BOOST_UBLAS_TEST_STRINGIFY_(n) << " == " << n__ << "]" ); \
  243. ++test_fails__; \
  244. } \
  245. } \
  246. } \
  247. /* [/BOOST_UBLAS_TEST_CHECK_VECTOR_EQ] */
  248. /// Check that elements of \a x and \a y are close with respect to a given precision \a e.
  249. #define BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE(x,y,n,e) /* [BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE] */ \
  250. if (BOOST_UBLAS_TEST_EXPANDP_(n) > 0) \
  251. { \
  252. ::std::size_t n__ = BOOST_UBLAS_TEST_EXPAND_(n); \
  253. for (::std::size_t i__ = n__; i__ > 0; --i__) \
  254. { \
  255. if (!::boost::numeric::ublas::test::detail::close_to(BOOST_UBLAS_TEST_EXPANDP_(x)[n__-i__], BOOST_UBLAS_TEST_EXPANDP_(y)[n__-i__], BOOST_UBLAS_TEST_EXPANDP_(e))) \
  256. { \
  257. BOOST_UBLAS_TEST_ERROR( "Failed assertion: abs((" << BOOST_UBLAS_TEST_STRINGIFY_(x[i__]) << "-" << BOOST_UBLAS_TEST_STRINGIFY_(y[i__]) << ") <= " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " [with " << BOOST_UBLAS_TEST_STRINGIFY_(x[i__]) << " == " << BOOST_UBLAS_TEST_EXPANDP_(x)[n__-i__] << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y[i__]) << " == " << BOOST_UBLAS_TEST_EXPANDP_(y)[n__-i__] << ", " << BOOST_UBLAS_TEST_STRINGIFY_(i__) << " == " << i__ << " and " << BOOST_UBLAS_TEST_STRINGIFY_(n) << " == " << n__ << "]" ); \
  258. ++test_fails__; \
  259. } \
  260. } \
  261. } \
  262. /* [/BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE] */
  263. /// Check that elements of \a x and \a y are close with respect to a given relative precision \a e.
  264. #define BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE(x,y,n,e) /* [BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE] */ \
  265. if (BOOST_UBLAS_TEST_EXPANDP_(n) > 0) \
  266. { \
  267. ::std::size_t n__ = BOOST_UBLAS_TEST_EXPAND_(n); \
  268. for (::std::size_t i__ = n__; i__ > 0; --i__) \
  269. { \
  270. if (!::boost::numeric::ublas::test::detail::rel_close_to(BOOST_UBLAS_TEST_EXPANDP_(x)[n__-i__], BOOST_UBLAS_TEST_EXPANDP_(y)[n__-i__], BOOST_UBLAS_TEST_EXPANDP_(e))) \
  271. { \
  272. BOOST_UBLAS_TEST_ERROR( "Failed assertion: abs((" << BOOST_UBLAS_TEST_STRINGIFY_(x[i__]) << "-" << BOOST_UBLAS_TEST_STRINGIFY_(y[i__]) << ") <= " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " [with " << BOOST_UBLAS_TEST_STRINGIFY_(x[i__]) << " == " << BOOST_UBLAS_TEST_EXPANDP_(x)[n__-i__] << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y[i__]) << " == " << BOOST_UBLAS_TEST_EXPANDP_(y)[n__-i__] << ", " << BOOST_UBLAS_TEST_STRINGIFY_(i__) << " == " << i__ << " and " << BOOST_UBLAS_TEST_STRINGIFY_(n) << " == " << n__ << "]" ); \
  273. ++test_fails__; \
  274. } \
  275. } \
  276. } \
  277. /* [/BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE] */
  278. /// Check that elements of matrices \a x and \a y are equal.
  279. #define BOOST_UBLAS_TEST_CHECK_MATRIX_EQ(x,y,nr,nc) /* [BOOST_UBLAS_TEST_CHECK_MATRIX_EQ] */ \
  280. for (::std::size_t i__ = 0; i__ < BOOST_UBLAS_TEST_EXPANDP_(nr); ++i__) \
  281. { \
  282. for (::std::size_t j__ = 0; j__ < BOOST_UBLAS_TEST_EXPANDP_(nc); ++j__) \
  283. { \
  284. if (!(BOOST_UBLAS_TEST_EXPANDP_(x)(i__,j__)==BOOST_UBLAS_TEST_EXPANDP_(y)(i__,j__))) \
  285. { \
  286. BOOST_UBLAS_TEST_ERROR( "Failed assertion: (" << BOOST_UBLAS_TEST_STRINGIFY_(x(i__,j__)) << " == " << BOOST_UBLAS_TEST_STRINGIFY_(y(i__,j__)) << ") [with " << BOOST_UBLAS_TEST_STRINGIFY_(x(i__,j__)) << " == " << BOOST_UBLAS_TEST_EXPANDP_(x)(i__,j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y(i__,j__)) << " == " << BOOST_UBLAS_TEST_EXPANDP_(y)(i__,j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(i__) << " == " << i__ << ", " << BOOST_UBLAS_TEST_STRINGIFY_(j__) << " == " << BOOST_UBLAS_TEST_EXPANDP_(j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(nr) << " == " << BOOST_UBLAS_TEST_EXPANDP_(nr) << " and " << BOOST_UBLAS_TEST_STRINGIFY_(nc) << " == " << BOOST_UBLAS_TEST_EXPANDP_(nc) << "]" ); \
  287. ++test_fails__; \
  288. } \
  289. } \
  290. } \
  291. /* [/BOOST_UBLAS_TEST_CHECK_MATRIX_EQ] */
  292. /// Check that elements of matrices \a x and \a y are close with respect to a given precision \a e.
  293. #define BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE(x,y,nr,nc,e) /* [BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE] */ \
  294. for (::std::size_t i__ = 0; i__ < BOOST_UBLAS_TEST_EXPANDP_(nr); ++i__) \
  295. { \
  296. for (::std::size_t j__ = 0; j__ < BOOST_UBLAS_TEST_EXPANDP_(nc); ++j__) \
  297. { \
  298. if (!::boost::numeric::ublas::test::detail::close_to(BOOST_UBLAS_TEST_EXPANDP_(x)(i__,j__), BOOST_UBLAS_TEST_EXPANDP_(y)(i__,j__), BOOST_UBLAS_TEST_EXPANDP_(e))) \
  299. { \
  300. BOOST_UBLAS_TEST_ERROR( "Failed assertion: abs((" << BOOST_UBLAS_TEST_STRINGIFY_(x(i__,j__)) << "-" << BOOST_UBLAS_TEST_STRINGIFY_(y(i__,j__)) << ") <= " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " [with " << BOOST_UBLAS_TEST_STRINGIFY_(x(i__,j__)) << " == " << BOOST_UBLAS_TEST_EXPANDP_(x)(i__,j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y(i__,j__)) << " == " << BOOST_UBLAS_TEST_EXPANDP_(y)(i__,j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(i__) << " == " << i__ << ", " << BOOST_UBLAS_TEST_STRINGIFY_(j__) << " == " << BOOST_UBLAS_TEST_EXPANDP_(j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(nr) << " == " << BOOST_UBLAS_TEST_EXPANDP_(nr) << " and " << BOOST_UBLAS_TEST_STRINGIFY_(nc) << " == " << BOOST_UBLAS_TEST_EXPANDP_(nc) << "]" ); \
  301. ++test_fails__; \
  302. } \
  303. } \
  304. } \
  305. /* [/BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE] */
  306. /// Check that elements of matrices \a x and \a y are close with respect to a given relative precision \a e.
  307. #define BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE(x,y,nr,nc,e) /* [BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE] */ \
  308. for (::std::size_t i__ = 0; i__ < BOOST_UBLAS_TEST_EXPANDP_(nr); ++i__) \
  309. { \
  310. for (::std::size_t j__ = 0; j__ < BOOST_UBLAS_TEST_EXPANDP_(nc); ++j__) \
  311. { \
  312. if (!::boost::numeric::ublas::test::detail::rel_close_to(BOOST_UBLAS_TEST_EXPANDP_(x)(i__,j__), BOOST_UBLAS_TEST_EXPANDP_(y)(i__,j__), BOOST_UBLAS_TEST_EXPANDP_(e))) \
  313. { \
  314. BOOST_UBLAS_TEST_ERROR( "Failed assertion: abs((" << BOOST_UBLAS_TEST_STRINGIFY_(x(i__,j__)) << "-" << BOOST_UBLAS_TEST_STRINGIFY_(y(i__,j__)) << ") <= " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " [with " << BOOST_UBLAS_TEST_STRINGIFY_(x(i__,j__)) << " == " << BOOST_UBLAS_TEST_EXPANDP_(x)(i__,j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y(i__,j__)) << " == " << BOOST_UBLAS_TEST_EXPANDP_(y)(i__,j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(i__) << " == " << i__ << ", " << BOOST_UBLAS_TEST_STRINGIFY_(j__) << " == " << BOOST_UBLAS_TEST_EXPANDP_(j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(nr) << " == " << BOOST_UBLAS_TEST_EXPANDP_(nr) << " and " << BOOST_UBLAS_TEST_STRINGIFY_(nc) << " == " << BOOST_UBLAS_TEST_EXPANDP_(nc) << "]" ); \
  315. ++test_fails__; \
  316. } \
  317. } \
  318. } \
  319. /* [/BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE] */
  320. ///< Output the error message \a x.
  321. #ifdef _MSC_VER
  322. # define BOOST_UBLAS_TEST_ERROR(x) ::std::cerr << "[Error (" << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << ")>> " << BOOST_UBLAS_TEST_EXPAND_(x) << ::std::endl
  323. #else
  324. # define BOOST_UBLAS_TEST_ERROR(x) ::std::cerr << "[Error (" << __FILE__ << ":" << __func__ << ":" << __LINE__ << ")>> " << BOOST_UBLAS_TEST_EXPAND_(x) << ::std::endl
  325. #endif
  326. #endif // BOOST_NUMERIC_UBLAS_TEST_UTILS_HPP