/////////////////////////////////////////////////////////////// // Copyright 2012 John Maddock. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_ // #ifndef BOOST_LIGHTWEIGHT_TEST_MACROS_HPP #define BOOST_LIGHTWEIGHT_TEST_MACROS_HPP #include #include #include #include #include #include #include #include #include #include namespace boost{ namespace lightweight_test{ namespace detail{ template inline typename boost::disable_if_c::value || boost::multiprecision::is_unsigned_number::value, T>::type abs(const T& a) { return a < 0 ? -a : a; } template inline typename boost::enable_if_c::value || boost::multiprecision::is_unsigned_number::value, T>::type abs(const T& a) { return a; } } template typename boost::enable_if_c::value == boost::multiprecision::number_kind_integer, T>::type relative_error(T a, T b) { return a > b ? a - b : b - a; } template typename boost::disable_if_c<(boost::multiprecision::number_category::value == boost::multiprecision::number_kind_integer) || boost::multiprecision::is_interval_number::value, T>::type relative_error(T a, T b) { using std::abs; using detail::abs; T min_val = (std::numeric_limits::min)(); T max_val = (std::numeric_limits::max)(); if((a != 0) && (b != 0)) { if(a == b) return 0; // TODO: use isfinite: if(abs(b) >= max_val) { if(abs(a) >= max_val) return 0; // one infinity is as good as another! } // If the result is denormalised, treat all denorms as equivalent: if((a < min_val) && (a > 0)) a = min_val; else if((a > -min_val) && (a < 0)) a = -min_val; if((b < min_val) && (b > 0)) b = min_val; else if((b > -min_val) && (b < 0)) b = -min_val; return (std::max)(abs(T((a-b)/a)), abs(T((a-b)/b))) / std::numeric_limits::epsilon(); } // Handle special case where one or both are zero: if(min_val == 0) return abs(T(a-b)); if(abs(a) < min_val) a = min_val; if(abs(b) < min_val) b = min_val; return (std::max)(abs(T((a-b)/a)), abs(T((a-b)/b))) / std::numeric_limits::epsilon(); } template typename boost::mpl::if_c::value, U, T>::type relative_error(T a, U b) { typedef typename boost::mpl::if_c::value, U, T>::type cast_type; return relative_error(static_cast(a), static_cast(b)); } template typename boost::enable_if_c::value, T>::type relative_error(T a, T b) { typename boost::multiprecision::component_type::type am = median(a); typename boost::multiprecision::component_type::type bm = median(b); return relative_error::type>(am, bm); } enum { warn_on_fail, error_on_fail, abort_on_fail }; template inline typename enable_if_c::is_specialized, T>::type epsilon_of(const T&) { return std::numeric_limits::is_integer ? 1 : std::numeric_limits::epsilon(); } template inline typename disable_if_c::is_specialized, T>::type epsilon_of(const T&) { using std::ldexp; return ldexp(T(1), 1 - boost::math::tools::digits()); } template inline int digits_of(const T&) { return std::numeric_limits::is_specialized ? std::numeric_limits::digits10 + 2 : std::numeric_limits::digits10 + 2; } inline std::ostream& report_where(const char* file, int line, const char* function) { if(function) BOOST_LIGHTWEIGHT_TEST_OSTREAM << "In function: "<< function << std::endl; BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << ":" << line; return BOOST_LIGHTWEIGHT_TEST_OSTREAM; } #define BOOST_MP_REPORT_WHERE boost::lightweight_test::report_where(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION) inline void report_severity(int severity) { if(severity == error_on_fail) ++boost::detail::test_errors(); else if(severity == abort_on_fail) { ++boost::detail::test_errors(); abort(); } } #define BOOST_MP_REPORT_SEVERITY(severity) boost::lightweight_test::report_severity(severity) template void report_unexpected_exception(const E& e, int severity, const char* file, int line, const char* function) { report_where(file, line, function) << " Unexpected exception of type " << typeid(e).name() << std::endl; BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Errot message was: " << e.what() << std::endl; BOOST_MP_REPORT_SEVERITY(severity); } #define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity) \ catch(const std::exception& e) \ { boost::lightweight_test::report_unexpected_exception(e, severity, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); }\ catch(...)\ { std::cout << "Exception of unknown type was thrown" << std::endl; boost::lightweight_test::report_severity(severity); } #define BOOST_CHECK_IMP(x, severity)\ do{\ try{ if(x){}else{\ BOOST_MP_REPORT_WHERE << " Failed predicate: " << BOOST_STRINGIZE(x) << std::endl;\ BOOST_MP_REPORT_SEVERITY(severity);\ }\ }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)\ }while(0) #define BOOST_CHECK(x) BOOST_CHECK_IMP(x, boost::lightweight_test::error_on_fail) #define BOOST_WARN(x) BOOST_CHECK_IMP(x, boost::lightweight_test::warn_on_fail) #define BOOST_REQUIRE(x) BOOST_CHECK_IMP(x, boost::lightweight_test::abort_on_fail) template void check_close_imp(T x, T y, T tol, int severity) { try { if(boost::lightweight_test::relative_error(x, y) > tol) { BOOST_MP_REPORT_WHERE << " Failed check for closeness: \n" << std::setprecision(boost::lightweight_test::digits_of(x)) << std::scientific << "Value of LHS was: " << x << "\n" << "Value of RHS was: " << y << "\n" << std::setprecision(5) << std::fixed << "Relative error was: " << boost::lightweight_test::relative_error(x, y) << "eps\n" << "Tolerance was: " << tol << "eps" << std::endl; BOOST_MP_REPORT_SEVERITY(severity); } } BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity) } template void check_close_forwarder(T x, U y, V tol, int severity) { typedef typename mpl::if_c< is_convertible::value && (sizeof(T) >= sizeof(U)), T, typename mpl::if_c< is_convertible::value && (sizeof(U) >= sizeof(T)), U, typename mpl::if_c< is_convertible::value, T, U >::type >::type >::type arg_type; arg_type t = tol / (100 * boost::lightweight_test::epsilon_of(x)); check_close_imp(static_cast(x), static_cast(y), static_cast(t), severity); } template void check_close_fraction_forwarder(T x, U y, V tol, int severity) { typedef typename mpl::if_c< is_convertible::value && (sizeof(T) >= sizeof(U)), T, typename mpl::if_c< is_convertible::value && (sizeof(U) >= sizeof(T)), U, typename mpl::if_c< is_convertible::value, T, U >::type >::type >::type arg_type; arg_type t = tol / boost::lightweight_test::epsilon_of(x); check_close_imp(static_cast(x), static_cast(y), static_cast(t), severity); } #define BOOST_EQUAL_IMP(x, y, severity)\ do{\ try{ if(!((x) == (y))){\ BOOST_MP_REPORT_WHERE << " Failed check for equality: \n" \ << std::setprecision(boost::lightweight_test::digits_of(x)) << std::scientific\ << "Value of LHS was: " << (x) << "\n"\ << "Value of RHS was: " << (y) << "\n"\ << std::setprecision(3) << std::endl;\ BOOST_MP_REPORT_SEVERITY(severity);\ }\ }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)\ }while(0) #define BOOST_NE_IMP(x, y, severity)\ do{\ try{ if(!(x != y)){\ BOOST_MP_REPORT_WHERE << " Failed check for non-equality: \n" \ << std::setprecision(boost::lightweight_test::digits_of(x)) << std::scientific\ << "Value of LHS was: " << x << "\n"\ << "Value of RHS was: " << y << "\n"\ << std::setprecision(3) << std::endl;\ BOOST_MP_REPORT_SEVERITY(severity);\ }\ }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)\ }while(0) #define BOOST_LT_IMP(x, y, severity)\ do{\ try{ if(!(x < y)){\ BOOST_MP_REPORT_WHERE << " Failed check for less than: \n" \ << std::setprecision(boost::lightweight_test::digits_of(x)) << std::scientific\ << "Value of LHS was: " << x << "\n"\ << "Value of RHS was: " << y << "\n"\ << std::setprecision(3) << std::endl;\ BOOST_MP_REPORT_SEVERITY(severity);\ }\ }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)\ }while(0) #define BOOST_GT_IMP(x, y, severity)\ do{\ try{ if(!(x > y)){\ BOOST_MP_REPORT_WHERE << " Failed check for greater than: \n" \ << std::setprecision(boost::lightweight_test::digits_of(x)) << std::scientific\ << "Value of LHS was: " << x << "\n"\ << "Value of RHS was: " << y << "\n"\ << std::setprecision(3) << std::endl;\ BOOST_MP_REPORT_SEVERITY(severity);\ }\ }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)\ }while(0) #define BOOST_LE_IMP(x, y, severity)\ do{\ try{ if(!(x <= y)){\ BOOST_MP_REPORT_WHERE << " Failed check for less-than-equal-to: \n" \ << std::setprecision(boost::lightweight_test::digits_of(x)) << std::scientific\ << "Value of LHS was: " << x << "\n"\ << "Value of RHS was: " << y << "\n"\ << std::setprecision(3) << std::endl;\ BOOST_MP_REPORT_SEVERITY(severity);\ }\ }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)\ }while(0) #define BOOST_GE_IMP(x, y, severity)\ do{\ try{ if(!(x >= y)){\ BOOST_MP_REPORT_WHERE << " Failed check for greater-than-equal-to \n" \ << std::setprecision(boost::lightweight_test::digits_of(x)) << std::scientific\ << "Value of LHS was: " << x << "\n"\ << "Value of RHS was: " << y << "\n"\ << std::setprecision(3) << std::endl;\ BOOST_MP_REPORT_SEVERITY(severity);\ }\ }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)\ }while(0) #define BOOST_MT_CHECK_THROW_IMP(x, E, severity)\ do{\ try{ \ x;\ BOOST_MP_REPORT_WHERE << " Expected exception not thrown in expression " << BOOST_STRINGIZE(x) << std::endl;\ BOOST_MP_REPORT_SEVERITY(severity);\ }\ catch(const E&){}\ BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)\ }while(0) #define BOOST_MT_CHECK_NO_THROW_IMP(x, severity)\ do{\ try{\ x;\ }\ BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)\ }while(0) #define BOOST_MP_CHECK_SMALL(val, tolerance, severity)\ do{\ try{\ using std::abs; using boost::lightweight_test::detail::abs;\ if(abs(val) > abs(tolerance))\ {\ BOOST_MP_REPORT_WHERE << " Failed check for small value \n" \ << std::setprecision(boost::lightweight_test::digits_of(val)) << std::scientific\ << "Value of argument was: " << val << "\n"\ << "Value of tolerance was: " << tolerance << "\n"\ << std::setprecision(3) << std::endl;\ BOOST_MP_REPORT_SEVERITY(severity);\ }\ }\ BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)\ }while(0) #ifndef BOOST_CHECK_NO_THROW #define BOOST_CHECK_NO_THROW(x) BOOST_MT_CHECK_NO_THROW_IMP(x, boost::lightweight_test::error_on_fail) #define BOOST_WARN_NO_THROW(x) BOOST_MT_CHECK_NO_THROW_IMP(x, boost::lightweight_test::warn_on_fail) #define BOOST_REQUIRE_NO_THROW(x) BOOST_MT_CHECK_NO_THROW_IMP(x, boost::lightweight_test::abort_on_fail) #define BOOST_CHECK_CLOSE(x, y, tol) BOOST_CHECK_NO_THROW(check_close_forwarder(x, y, tol, boost::lightweight_test::error_on_fail)) #define BOOST_WARN_CLOSE(x, y, tol) BOOST_WARN_NO_THROW(check_close_forwarder(x, y, tol, boost::lightweight_test::warn_on_fail)) #define BOOST_REQUIRE_CLOSE(x, y, tol) BOOST_REQUIRE_NO_THROW(check_close_forwarder(x, y, tol, boost::lightweight_test::abort_on_fail)) #define BOOST_CHECK_CLOSE_FRACTION(x, y, tol) BOOST_CHECK_NO_THROW(check_close_fraction_forwarder(x, y, tol, boost::lightweight_test::error_on_fail)) #define BOOST_WARN_CLOSE_FRACTION(x, y, tol) BOOST_WARN_NO_THROW(check_close_fraction_forwarder(x, y, tol, boost::lightweight_test::warn_on_fail)) #define BOOST_REQUIRE_CLOSE_FRACTION(x, y, tol) BOOST_REQUIRE_NO_THROW(check_close_fraction_forwarder(x, y, tol, boost::lightweight_test::abort_on_fail)) #define BOOST_CHECK_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, boost::lightweight_test::error_on_fail) #define BOOST_WARN_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, boost::lightweight_test::warn_on_fail) #define BOOST_REQUIRE_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, boost::lightweight_test::abort_on_fail) #define BOOST_CHECK_NE(x, y) BOOST_NE_IMP(x, y, boost::lightweight_test::error_on_fail) #define BOOST_WARN_NE(x, y) BOOST_NE_IMP(x, y, boost::lightweight_test::warn_on_fail) #define BOOST_REQUIRE_NE(x, y) BOOST_NE_IMP(x, y, boost::lightweight_test::abort_on_fail) #define BOOST_CHECK_LT(x, y) BOOST_LT_IMP(x, y, boost::lightweight_test::error_on_fail) #define BOOST_WARN_LT(x, y) BOOST_LT_IMP(x, y, boost::lightweight_test::warn_on_fail) #define BOOST_REQUIRE_LT(x, y) BOOST_LT_IMP(x, y, boost::lightweight_test::abort_on_fail) #define BOOST_CHECK_GT(x, y) BOOST_GT_IMP(x, y, boost::lightweight_test::error_on_fail) #define BOOST_WARN_GT(x, y) BOOST_GT_IMP(x, y, boost::lightweight_test::warn_on_fail) #define BOOST_REQUIRE_GT(x, y) BOOST_GT_IMP(x, y, boost::lightweight_test::abort_on_fail) #define BOOST_CHECK_LE(x, y) BOOST_LE_IMP(x, y, boost::lightweight_test::error_on_fail) #define BOOST_WARN_LE(x, y) BOOST_LE_IMP(x, y, boost::lightweight_test::warn_on_fail) #define BOOST_REQUIRE_LE(x, y) BOOST_LE_IMP(x, y, boost::lightweight_test::abort_on_fail) #define BOOST_CHECK_GE(x, y) BOOST_GE_IMP(x, y, boost::lightweight_test::error_on_fail) #define BOOST_WARN_GE(x, y) BOOST_GE_IMP(x, y, boost::lightweight_test::warn_on_fail) #define BOOST_REQUIRE_GE(x, y) BOOST_GE_IMP(x, y, boost::lightweight_test::abort_on_fail) #define BOOST_CHECK_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, boost::lightweight_test::error_on_fail) #define BOOST_WARN_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, boost::lightweight_test::warn_on_fail) #define BOOST_REQUIRE_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, boost::lightweight_test::abort_on_fail) #define BOOST_CHECK_SMALL(x, tol) BOOST_MP_CHECK_SMALL(x, tol, boost::lightweight_test::error_on_fail) #define BOOST_WARN_SMALL(x, tol) BOOST_MP_CHECK_SMALL(x, tol, boost::lightweight_test::warn_on_fail) #define BOOST_REQUIRE_SMALL(x, tol) BOOST_MP_CHECK_SMALL(x, tol, boost::lightweight_test::abort_on_fail) #define BOOST_TEST_MESSAGE(x) BOOST_LIGHTWEIGHT_TEST_OSTREAM << x << std::endl; #undef BOOST_ERROR #define BOOST_ERROR(x) BOOST_LIGHTWEIGHT_TEST_OSTREAM << x << std::endl; #endif }} // end of namespaces #endif