4 * \brief Utility macros/functions for testing and debugging purpose.
8 * BOOST_UBLAS_TEST_DEF( test_case_1 )
10 * // do your test stuff
13 * BOOST_UBLAS_TEST_DEF( test_case_2 )
15 * // do your test stuff
20 * BOOST_UBLAS_TEST_DEF( test_case_n )
22 * // do your test stuff
27 * BOOST_UBLAS_TEST_SUITE( "My Test Suite" ); // optional
29 * BOOST_UBLAS_TEST_BEGIN();
30 * BOOST_UBLAS_TEST_DO( test_case_1 );
31 * BOOST_UBLAS_TEST_DO( test_case_2 );
33 * BOOST_UBLAS_TEST_DO( test_case_n );
34 * BOOST_UBLAS_TEST_END();
37 * Inside each <em>test_case_<code>k</code></em> you can use the various
38 * \c BOOST_UBLAS_TEST_CHECK* macros.
42 * Copyright (c) 2009-2012, Marco Guazzone
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)
48 * \author Marco Guazzone, marco.guazzone@gmail.com
51 #ifndef BOOST_NUMERIC_UBLAS_TEST_UTILS_HPP
52 #define BOOST_NUMERIC_UBLAS_TEST_UTILS_HPP
55 #include <boost/numeric/ublas/detail/config.hpp>
56 #include <boost/numeric/ublas/traits.hpp>
58 #include <boost/math/special_functions/fpclassify.hpp> // isnan, isinf
67 #define BOOST_UBLAS_NOT_USED(x) (void)(x)
69 namespace boost { namespace numeric { namespace ublas { namespace test { namespace detail { namespace /*<unnamed>*/ {
74 /// Check if the given complex number is a NaN.
75 // read the comments in fpclassify as well
78 bool (isnan)(::std::complex<T> const& z)
80 // According to IEEE, NaN is different even by itself
81 return (z != z) || (boost::math::isnan)(z.real()) || (boost::math::isnan)(z.imag());
84 /// Check if two (real) numbers are close each other (wrt a given tolerance).
85 template <typename T1, typename T2, typename T3>
87 bool close_to(T1 x, T2 y, T3 tol)
89 typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type,
90 T3>::promote_type real_type;
92 if ((boost::math::isnan)(x) || (boost::math::isnan)(y))
94 // According to IEEE, NaN is different even by itself
97 return abs(x-y) <= (max(static_cast<real_type>(abs(x)), static_cast<real_type>(abs(y)))*tol);
100 /// Check if two complex numbers are close each other (wrt a given tolerance).
101 template <typename T1, typename T2, typename T3>
103 bool close_to(::std::complex<T1> const& x, ::std::complex<T2> const& y, T3 tol)
105 typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type,
106 T3>::promote_type real_type;
108 if ((isnan)(x) || (isnan)(y))
110 // According to IEEE, NaN is different even by itself
113 ::std::complex<real_type> xx(x);
114 ::std::complex<real_type> yy(y);
116 return abs(xx-yy) <= (max(abs(xx), abs(yy))*tol);
119 /// Check if two (real) numbers are close each other (wrt a given tolerance).
120 template <typename T1, typename T2, typename T3>
122 bool rel_close_to(T1 x, T2 y, T3 tol)
124 //typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type,
125 // T3>::promote_type real_type;
127 if ((boost::math::isnan)(x) || (boost::math::isnan)(y))
129 // According to IEEE, NaN is different even by itself
132 return abs(x-y)/abs(y) <= tol;
135 /// Check if two complex numbers are close each other (wrt a given tolerance).
136 template <typename T1, typename T2, typename T3>
138 bool rel_close_to(::std::complex<T1> const& x, ::std::complex<T2> const& y, T3 tol)
140 typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type,
141 T3>::promote_type real_type;
143 if ((isnan)(x) || (isnan)(y))
145 // According to IEEE, NaN is different even by itself
148 ::std::complex<real_type> xx(x);
149 ::std::complex<real_type> yy(y);
151 return abs(xx-yy)/abs(yy) <= tol;
154 }}}}}} // Namespace boost::numeric::ublas::test::detail::<unnamed>
157 /// Expand its argument \a x.
158 #define BOOST_UBLAS_TEST_EXPAND_(x) x
161 /// Expand its argument \a x inside parenthesis.
162 #define BOOST_UBLAS_TEST_EXPANDP_(x) (x)
165 /// Transform its argument \a x into a string.
166 #define BOOST_UBLAS_TEST_STRINGIFY_(x) #x
169 /// Concatenate its two \e string arguments \a x and \a y.
170 #define BOOST_UBLAS_TEST_JOIN_(x,y) x ## y
173 /// Output the message \a x if in debug-mode; otherwise output nothing.
174 /// Note: we don't use macro expansion inside parenthesis to let \a m be an
175 /// expression of the form <code>a << b</code>.
177 # define BOOST_UBLAS_DEBUG_TRACE(x) ::std::cerr << "[Debug>> " << BOOST_UBLAS_TEST_EXPAND_(x) << ::std::endl
179 # define BOOST_UBLAS_DEBUG_TRACE(x) /**/
183 /// Define the name \a m of the entire test suite.
184 #define BOOST_UBLAS_TEST_SUITE(m) ::std::cerr << "--- Test Suite: " << BOOST_UBLAS_TEST_EXPAND_(m) << " ---" << ::std::endl;
187 /// Define the beginning of a test suite.
188 #define BOOST_UBLAS_TEST_BEGIN() /* [BOOST_UBLAS_TEST_BEGIN] */ \
190 /* Begin of Test Suite */ \
191 ::std::size_t test_fails__(0) \
192 /* [/BOOST_UBLAS_TEST_BEGIN] */
195 /// Define a test case \a x inside the current test suite.
196 #define BOOST_UBLAS_TEST_DEF(x) static void BOOST_UBLAS_TEST_EXPAND_(x)(::std::size_t& test_fails__)
199 /// Call the test case \a x.
200 #define BOOST_UBLAS_TEST_DO(x) /* [BOOST_UBLAS_TEST_DO] */ \
203 BOOST_UBLAS_TEST_EXPAND_(x)(test_fails__); \
205 catch (::std::exception& e) \
208 BOOST_UBLAS_TEST_ERROR( e.what() ); \
214 /* [/BOOST_UBLAS_TEST_DO] */
217 /// Define the end of a test suite and return non-zero value if any test failed.
218 #define BOOST_UBLAS_TEST_END() /* [BOOST_UBLAS_TEST_END] */ \
219 if (test_fails__ > 0) \
221 ::std::cerr << "Number of failed tests: " << test_fails__ << ::std::endl; \
226 ::std::cerr << "No failed test" << ::std::endl; \
229 } /* End of test suite */ \
230 /* [/BOOST_UBLAS_TEST_END] */
233 /// Output the message \a m.
234 /// Note: we don't use macro expansion inside parenthesis to let \a m be an
235 /// expression of the form <code>a << b</code>.
236 #define BOOST_UBLAS_TEST_TRACE(m) ::std::cerr << "[Info>> " << BOOST_UBLAS_TEST_EXPAND_(m) << ::std::endl
239 /// Check the truth of assertion \a x.
240 #define BOOST_UBLAS_TEST_CHECK(x) /* [BOOST_UBLAS_TEST_CHECK] */ \
241 if (!BOOST_UBLAS_TEST_EXPANDP_(x)) \
243 BOOST_UBLAS_TEST_ERROR( "Failed assertion: " << BOOST_UBLAS_TEST_STRINGIFY_(x) ); \
246 /* [/BOOST_UBLAS_TEST_CHECK] */
249 /// Check for the equality of \a x against \a y.
250 #define BOOST_UBLAS_TEST_CHECK_EQ(x,y) /* [BOOST_UBLAS_TEST_CHECK_EQUAL] */ \
251 if (!(BOOST_UBLAS_TEST_EXPANDP_(x) == BOOST_UBLAS_TEST_EXPANDP_(y))) \
253 BOOST_UBLAS_TEST_ERROR( "Failed assertion: (" << BOOST_UBLAS_TEST_STRINGIFY_(x) << " == " << BOOST_UBLAS_TEST_STRINGIFY_(y) << ")" ); \
256 /* [/BOOST_UBLAS_TEST_CHECK_EQUAL] */
259 /// Alias for macro \c BOOST_UBLAS_TEST_CHECK_EQ (for backward compatibility).
260 #define BOOST_UBLAS_TEST_CHECK_EQUAL(x,y) BOOST_UBLAS_TEST_CHECK_EQ(x,y)
263 /// Check that \a x and \a y are close with respect to a given precision \a e.
264 #define BOOST_UBLAS_TEST_CHECK_CLOSE(x,y,e) /* [BOOST_UBLAS_TEST_CHECK_CLOSE] */ \
265 if (!::boost::numeric::ublas::test::detail::close_to(BOOST_UBLAS_TEST_EXPAND_(x), BOOST_UBLAS_TEST_EXPAND_(y), BOOST_UBLAS_TEST_EXPAND_(e))) \
267 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) << "]" ); \
270 /* [/BOOST_UBLAS_TEST_CHECK_CLOSE] */
273 /// Alias for macro \c BOOST_UBLAS_TEST_CHECK_CLOSE (for backward compatibility),
274 #define BOOST_UBLAS_TEST_CHECK_PRECISION(x,y,e) BOOST_UBLAS_TEST_CHECK_CLOSE(x,y,e)
277 /// Check that \a x is close to \a y with respect to a given relative precision \a e.
278 #define BOOST_UBLAS_TEST_CHECK_REL_CLOSE(x,y,e) /* [BOOST_UBLAS_TEST_CHECK_REL_CLOSE] */ \
279 if (!::boost::numeric::ublas::test::detail::rel_close_to(BOOST_UBLAS_TEST_EXPAND_(x), BOOST_UBLAS_TEST_EXPAND_(y), BOOST_UBLAS_TEST_EXPAND_(e))) \
281 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) << "]" ); \
284 /* [/BOOST_UBLAS_TEST_CHECK_REL_CLOSE] */
287 /// Alias for macro \c BOOST_UBLAS_TEST_CHECK_REL_CLOSE (for backward compatibility),
288 #define BOOST_UBLAS_TEST_CHECK_REL_PRECISION(x,y,e) BOOST_UBLAS_TEST_CHECK_REL_CLOSE(x,y,e)
291 /// Check that elements of \a x and \a y are equal.
292 #define BOOST_UBLAS_TEST_CHECK_VECTOR_EQ(x,y,n) /* [BOOST_UBLAS_TEST_CHECK_VECTOR_EQ] */ \
293 if (BOOST_UBLAS_TEST_EXPANDP_(n) > 0) \
295 ::std::size_t n__ = BOOST_UBLAS_TEST_EXPAND_(n); \
296 for (::std::size_t i__ = n__; i__ > 0; --i__) \
298 if (!(BOOST_UBLAS_TEST_EXPANDP_(x)[n__-i__]==BOOST_UBLAS_TEST_EXPANDP_(y)[n__-i__])) \
300 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__ << "]" ); \
305 /* [/BOOST_UBLAS_TEST_CHECK_VECTOR_EQ] */
308 /// Check that elements of \a x and \a y are close with respect to a given precision \a e.
309 #define BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE(x,y,n,e) /* [BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE] */ \
310 if (BOOST_UBLAS_TEST_EXPANDP_(n) > 0) \
312 ::std::size_t n__ = BOOST_UBLAS_TEST_EXPAND_(n); \
313 for (::std::size_t i__ = n__; i__ > 0; --i__) \
315 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))) \
317 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__ << "]" ); \
322 /* [/BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE] */
325 /// Check that elements of \a x and \a y are close with respect to a given relative precision \a e.
326 #define BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE(x,y,n,e) /* [BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE] */ \
327 if (BOOST_UBLAS_TEST_EXPANDP_(n) > 0) \
329 ::std::size_t n__ = BOOST_UBLAS_TEST_EXPAND_(n); \
330 for (::std::size_t i__ = n__; i__ > 0; --i__) \
332 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))) \
334 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__ << "]" ); \
339 /* [/BOOST_UBLAS_TEST_CHECK_VECTOR_REL_CLOSE] */
342 /// Check that elements of matrices \a x and \a y are equal.
343 #define BOOST_UBLAS_TEST_CHECK_MATRIX_EQ(x,y,nr,nc) /* [BOOST_UBLAS_TEST_CHECK_MATRIX_EQ] */ \
344 for (::std::size_t i__ = 0; i__ < BOOST_UBLAS_TEST_EXPANDP_(nr); ++i__) \
346 for (::std::size_t j__ = 0; j__ < BOOST_UBLAS_TEST_EXPANDP_(nc); ++j__) \
348 if (!(BOOST_UBLAS_TEST_EXPANDP_(x)(i__,j__)==BOOST_UBLAS_TEST_EXPANDP_(y)(i__,j__))) \
350 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) << "]" ); \
355 /* [/BOOST_UBLAS_TEST_CHECK_MATRIX_EQ] */
358 /// Check that elements of matrices \a x and \a y are close with respect to a given precision \a e.
359 #define BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE(x,y,nr,nc,e) /* [BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE] */ \
360 for (::std::size_t i__ = 0; i__ < BOOST_UBLAS_TEST_EXPANDP_(nr); ++i__) \
362 for (::std::size_t j__ = 0; j__ < BOOST_UBLAS_TEST_EXPANDP_(nc); ++j__) \
364 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))) \
366 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) << "]" ); \
371 /* [/BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE] */
374 /// Check that elements of matrices \a x and \a y are close with respect to a given relative precision \a e.
375 #define BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE(x,y,nr,nc,e) /* [BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE] */ \
376 for (::std::size_t i__ = 0; i__ < BOOST_UBLAS_TEST_EXPANDP_(nr); ++i__) \
378 for (::std::size_t j__ = 0; j__ < BOOST_UBLAS_TEST_EXPANDP_(nc); ++j__) \
380 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))) \
382 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) << "]" ); \
387 /* [/BOOST_UBLAS_TEST_CHECK_MATRIX_REL_CLOSE] */
389 ///< Output the error message \a x.
391 # define BOOST_UBLAS_TEST_ERROR(x) ::std::cerr << "[Error (" << __FILE__ << ":" << __FUNCTION__ << ":" << __LINE__ << ")>> " << BOOST_UBLAS_TEST_EXPAND_(x) << ::std::endl
393 # define BOOST_UBLAS_TEST_ERROR(x) ::std::cerr << "[Error (" << __FILE__ << ":" << __func__ << ":" << __LINE__ << ")>> " << BOOST_UBLAS_TEST_EXPAND_(x) << ::std::endl
396 #endif // BOOST_NUMERIC_UBLAS_TEST_UTILS_HPP