diff -Naur /old/libs/numeric/ublas/test/utils.hpp /new/libs/numeric/ublas/test/utils.hpp --- /old/libs/numeric/ublas/test/utils.hpp 2012-08-28 16:56:06.000000000 +0200 +++ /new/libs/numeric/ublas/test/utils.hpp 2012-08-28 19:14:30.000000000 +0200 @@ -1,7 +1,43 @@ /** - * \file util.hpp + * \file util.hpp * - * \brief Utility macros/functions for testing and debugging purpose. + * \brief Utility macros/functions for testing and debugging purpose. + * + * Basic usage: + *
+ * BOOST_UBLAS_TEST_DEF( test_case_1 )
+ * {
+ *   // do your test stuff
+ * }
+ *
+ * BOOST_UBLAS_TEST_DEF( test_case_2 )
+ * {
+ *   // do your test stuff
+ * }
+ *
+ * // ...
+ *
+ * BOOST_UBLAS_TEST_DEF( test_case_n )
+ * {
+ *   // do your test stuff
+ * }
+ *
+ * int main()
+ * {
+ *   BOOST_UBLAS_TEST_SUITE( "My Test Suite" ); // optional
+ *
+ *   BOOST_UBLAS_TEST_BEGIN();
+ *     BOOST_UBLAS_TEST_DO( test_case_1 );
+ *     BOOST_UBLAS_TEST_DO( test_case_2 );
+ *     // ...
+ *     BOOST_UBLAS_TEST_DO( test_case_n );
+ *   BOOST_UBLAS_TEST_END();
+ * }
+ * 
+ * Inside each test_case_k you can use the various + * \c BOOST_UBLAS_TEST_CHECK* macros. + * + *
* * Copyright (c) 2009-2012, Marco Guazzone * @@ -16,7 +52,10 @@ #define BOOST_NUMERIC_UBLAS_TEST_UTILS_HPP +#include +#include #include +#include #include #include #include @@ -24,16 +63,81 @@ namespace boost { namespace numeric { namespace ublas { namespace test { namespace detail { namespace /**/ { +/// Check if the given complex number is a NaN. +template +BOOST_UBLAS_INLINE +bool isnan(::std::complex const& z) +{ + // According to IEEE, NaN is different even by itself + return (z != z) || ::std::isnan(z.real()) || ::std::isnan(z.imag()); +} + +/// Check if two (real) numbers are close each other (wrt a given tolerance). template -inline +BOOST_UBLAS_INLINE bool close_to(T1 x, T2 y, T3 tol) { + typedef typename promote_traits::promote_type, + T3>::promote_type real_type; + + if (::std::isnan(x) || ::std::isnan(y)) + { + // According to IEEE, NaN is different even by itself + return false; + } + return ::std::abs(x-y) <= (::std::max(static_cast(::std::abs(x)), static_cast(::std::abs(y)))*tol); +} + +/// Check if two complex numbers are close each other (wrt a given tolerance). +template +BOOST_UBLAS_INLINE +bool close_to(::std::complex const& x, ::std::complex const& y, T3 tol) +{ + typedef typename promote_traits::promote_type, + T3>::promote_type real_type; + + if (isnan(x) || isnan(y)) + { + // According to IEEE, NaN is different even by itself + return false; + } + ::std::complex xx(x); + ::std::complex yy(y); + return ::std::abs(xx-yy) <= (::std::max(::std::abs(xx), ::std::abs(yy))*tol); +} + +/// Check if two (real) numbers are close each other (wrt a given tolerance). +template +BOOST_UBLAS_INLINE +bool rel_close_to(T1 x, T2 y, T3 tol) +{ + typedef typename promote_traits::promote_type, + T3>::promote_type real_type; + if (::std::isnan(x) || ::std::isnan(y)) { - // According to IEEE, NaN are different event by itself + // According to IEEE, NaN is different even by itself + return false; + } + return ::std::abs(x-y)/::std::abs(y) <= tol; +} + +/// Check if two complex numbers are close each other (wrt a given tolerance). +template +BOOST_UBLAS_INLINE +bool rel_close_to(::std::complex const& x, ::std::complex const& y, T3 tol) +{ + typedef typename promote_traits::promote_type, + T3>::promote_type real_type; + + if (isnan(x) || isnan(y)) + { + // According to IEEE, NaN is different even by itself return false; } - return ::std::abs(x-y) <= (::std::max(::std::abs(x), ::std::abs(y))*tol); + ::std::complex xx(x); + ::std::complex yy(y); + return ::std::abs(xx-yy)/::std::abs(yy) <= tol; } }}}}}} // Namespace boost::numeric::ublas::test::detail:: @@ -60,7 +164,7 @@ /// Define the name of the entire test suite. -#define DCS_TEST_SUITE(m) ::std::cerr << "--- Test Suite: " << m << " ---" << ::std::endl; +#define BOOST_UBLAS_TEST_SUITE(m) ::std::cerr << "--- Test Suite: " << m << " ---" << ::std::endl; /// Define the beginning of a test suite. @@ -72,7 +176,7 @@ /// Define a test case \a x inside the current test suite. -#define BOOST_UBLAS_TEST_DEF(x) void BOOST_UBLAS_TEST_EXPAND_(x)(unsigned int& test_fails__) +#define BOOST_UBLAS_TEST_DEF(x) static void BOOST_UBLAS_TEST_EXPAND_(x)(unsigned int& test_fails__) /// Call the test case \a x. @@ -107,6 +211,10 @@ /* [/BOOST_UBLAS_TEST_END] */ +/// Output the message \a m. +#define BOOST_UBLAS_TEST_TRACE(m) ::std::cerr << "[Info>> " << BOOST_UBLAS_TEST_EXPAND_(m) << ::std::endl + + /// Check the truth of assertion \a x. #define BOOST_UBLAS_TEST_CHECK(x) /* [BOOST_UBLAS_TEST_CHECK] */ \ if (!(x)) \ @@ -119,7 +227,7 @@ /// Check for the equality of \a x against \a y. #define BOOST_UBLAS_TEST_CHECK_EQ(x,y) /* [BOOST_UBLAS_TEST_CHECK_EQUAL] */ \ - if (!(BOOST_UBLAS_TEST_PARAM_EXPAND_(x) == BOOST_UBLAS_TEST_PARAM_EXPAND_(y))) \ + if (!(BOOST_UBLAS_TEST_EXPAND_(x) == BOOST_UBLAS_TEST_EXPAND_(y))) \ { \ BOOST_UBLAS_TEST_ERROR( "Failed assertion: (" << BOOST_UBLAS_TEST_STRINGIFY_(x) << " == " << BOOST_UBLAS_TEST_STRINGIFY_(y) << ")" ); \ ++test_fails__; \ @@ -146,13 +254,17 @@ /// Check that \a x is close to \a y with respect to a given relative precision. -#define BOOST_UBLAS_TEST_CHECK_REL_PRECISION(x,y,e) /* [BOOST_UBLAS_TEST_CHECK_REL_PRECISION] */ \ - if (!::boost::numeric::ublas::test::detail::close_to(BOOST_UBLAS_TEST_EXPAND_(x)/BOOST_UBLAS_TEST_EXPAND_(y), 1.0, BOOST_UBLAS_TEST_EXPAND_(e))) \ - { \ - 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_EXPAND_(x) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y) << " == " << BOOST_UBLAS_TEST_EXPAND_(y) << " and " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " == " << BOOST_UBLAS_TEST_EXPAND_(e) << "]" ); \ - ++test_fails__; \ - } \ - /* [/BOOST_UBLAS_TEST_CHECK_REL_PRECISION] */ +#define BOOST_UBLAS_TEST_CHECK_REL_CLOSE(x,y,e) /* [BOOST_UBLAS_TEST_CHECK_REL_PRECISION] */ \ + if (!::boost::numeric::ublas::test::detail::rel_close_to(BOOST_UBLAS_TEST_EXPAND_(x), BOOST_UBLAS_TEST_EXPAND_(y), BOOST_UBLAS_TEST_EXPAND_(e))) \ + { \ + 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_EXPAND_(x) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y) << " == " << BOOST_UBLAS_TEST_EXPAND_(y) << " and " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " == " << BOOST_UBLAS_TEST_EXPAND_(e) << "]" ); \ + ++test_fails__; \ + } \ + /* [/BOOST_UBLAS_TEST_CHECK_REL_PRECISION] */ + + +/// Alias for macro \c BOOST_UBLAS_TEST_CHECK_REL_CLOSE (for backward compatibility), +#define BOOST_UBLAS_TEST_CHECK_REL_PRECISION(x,y,e) BOOST_UBLAS_TEST_CHECK_REL_CLOSE(x,y,e) /// Check that elements of \a x and \a y are equal.