| 4 | | * \brief Utility macros/functions for testing and debugging purpose. |
| | 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/> |
| | 80 | typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type, |
| | 81 | T3>::promote_type real_type; |
| | 82 | |
| | 83 | if (::std::isnan(x) || ::std::isnan(y)) |
| | 84 | { |
| | 85 | // According to IEEE, NaN is different even by itself |
| | 86 | return false; |
| | 87 | } |
| | 88 | return ::std::abs(x-y) <= (::std::max(static_cast<real_type>(::std::abs(x)), static_cast<real_type>(::std::abs(y)))*tol); |
| | 89 | } |
| | 90 | |
| | 91 | /// Check if two complex numbers are close each other (wrt a given tolerance). |
| | 92 | template <typename T1, typename T2, typename T3> |
| | 93 | BOOST_UBLAS_INLINE |
| | 94 | bool close_to(::std::complex<T1> const& x, ::std::complex<T2> const& y, T3 tol) |
| | 95 | { |
| | 96 | typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type, |
| | 97 | T3>::promote_type real_type; |
| | 98 | |
| | 99 | if (isnan(x) || isnan(y)) |
| | 100 | { |
| | 101 | // According to IEEE, NaN is different even by itself |
| | 102 | return false; |
| | 103 | } |
| | 104 | ::std::complex<real_type> xx(x); |
| | 105 | ::std::complex<real_type> yy(y); |
| | 106 | return ::std::abs(xx-yy) <= (::std::max(::std::abs(xx), ::std::abs(yy))*tol); |
| | 107 | } |
| | 108 | |
| | 109 | /// Check if two (real) numbers are close each other (wrt a given tolerance). |
| | 110 | template <typename T1, typename T2, typename T3> |
| | 111 | BOOST_UBLAS_INLINE |
| | 112 | bool rel_close_to(T1 x, T2 y, T3 tol) |
| | 113 | { |
| | 114 | typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type, |
| | 115 | T3>::promote_type real_type; |
| | 116 | |
| 33 | | // According to IEEE, NaN are different event by itself |
| | 119 | // According to IEEE, NaN is different even by itself |
| | 120 | return false; |
| | 121 | } |
| | 122 | return ::std::abs(x-y)/::std::abs(y) <= tol; |
| | 123 | } |
| | 124 | |
| | 125 | /// Check if two complex numbers are close each other (wrt a given tolerance). |
| | 126 | template <typename T1, typename T2, typename T3> |
| | 127 | BOOST_UBLAS_INLINE |
| | 128 | bool rel_close_to(::std::complex<T1> const& x, ::std::complex<T2> const& y, T3 tol) |
| | 129 | { |
| | 130 | typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type, |
| | 131 | T3>::promote_type real_type; |
| | 132 | |
| | 133 | if (isnan(x) || isnan(y)) |
| | 134 | { |
| | 135 | // According to IEEE, NaN is different even by itself |
| 149 | | #define BOOST_UBLAS_TEST_CHECK_REL_PRECISION(x,y,e) /* [BOOST_UBLAS_TEST_CHECK_REL_PRECISION] */ \ |
| 150 | | 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))) \ |
| 151 | | { \ |
| 152 | | 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) << "]" ); \ |
| 153 | | ++test_fails__; \ |
| 154 | | } \ |
| 155 | | /* [/BOOST_UBLAS_TEST_CHECK_REL_PRECISION] */ |
| | 257 | #define BOOST_UBLAS_TEST_CHECK_REL_CLOSE(x,y,e) /* [BOOST_UBLAS_TEST_CHECK_REL_PRECISION] */ \ |
| | 258 | if (!::boost::numeric::ublas::test::detail::rel_close_to(BOOST_UBLAS_TEST_EXPAND_(x), BOOST_UBLAS_TEST_EXPAND_(y), BOOST_UBLAS_TEST_EXPAND_(e))) \ |
| | 259 | { \ |
| | 260 | 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) << "]" ); \ |
| | 261 | ++test_fails__; \ |
| | 262 | } \ |
| | 263 | /* [/BOOST_UBLAS_TEST_CHECK_REL_PRECISION] */ |
| | 264 | |
| | 265 | |
| | 266 | /// Alias for macro \c BOOST_UBLAS_TEST_CHECK_REL_CLOSE (for backward compatibility), |
| | 267 | #define BOOST_UBLAS_TEST_CHECK_REL_PRECISION(x,y,e) BOOST_UBLAS_TEST_CHECK_REL_CLOSE(x,y,e) |