// test_error_handling.cpp // Test error handling. // Copyright Paul A. Bristow 2006. // Copyright John Maddock 2006. // Use, modification and distribution are subject to the // Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt // or copy at http://www.boost.org/LICENSE_1_0.txt) #define BOOST_MATH_THROW_ON_DOMAIN_ERROR #ifdef _MSC_VER # pragma warning(disable: 4127) // conditional expression is constant. # pragma warning(disable: 4512) // assignment operator could not be generated. # pragma warning(disable: 4996) // std::char_traits::copy' was declared deprecated. #endif // Boost #include // for domain_error. using ::boost::math::tools::domain_error; using ::boost::math::tools::pole_error; using ::boost::math::tools::overflow_error; using ::boost::math::tools::underflow_error; using ::boost::math::tools::denorm_error; using ::boost::math::tools::logic_error; #include // chisqr using ::boost::math::chisqr; #include // tgamma using ::boost::math::tgamma; #include using boost::math::isnan; // std #include using std::cout; using std::endl; #include using std::numeric_limits; #include using std::exception; #include // for test_main #include // for BOOST_CHECK_CLOSE #include // for real_concept using ::boost::math::concepts::real_concept; // // The macro BOOST_CHECK_THROW_MSG is the same as BOOST_CHECK_THROW // which is to say it verifies that the exception we expect is // thrown from the function under test, but it also prints the message // contained in the thrown exception so we can manually inspect the // quality of the message. // // The expanded code is: // // try // { // code; // } // catch(const std::exception& e) // { // std::cout << // "Message from thrown exception was:\n " << e.what() << std::endl; // } // BOOST_CHECK_THROW(code, t); // #define BOOST_CHECK_THROW_MSG(code,t)\ try{ code; } catch(const std::exception& e){\ std::cout << "Message from thrown exception was:\n " << e.what() << std::endl; }\ BOOST_CHECK_THROW(code, t); template // Any floating-point type FPT. void test_error(FPT) { cout << "Current function is " << BOOST_CURRENT_FUNCTION << endl; BOOST_CHECK_THROW_MSG(domain_error(BOOST_CURRENT_FUNCTION, 0), std::domain_error); BOOST_CHECK_THROW_MSG(domain_error(BOOST_CURRENT_FUNCTION, "Out of range argument"), std::domain_error); BOOST_CHECK_THROW_MSG(domain_error(BOOST_CURRENT_FUNCTION, 0, static_cast(3.124567890123456789012345678901L)), std::domain_error); BOOST_CHECK_THROW_MSG(domain_error(BOOST_CURRENT_FUNCTION, "Out of range argument %1% in test invocation", static_cast(3.124567890123456789012345678901L)), std::domain_error); BOOST_CHECK_THROW_MSG(logic_error(BOOST_CURRENT_FUNCTION, 0), std::logic_error); BOOST_CHECK_THROW_MSG(logic_error(BOOST_CURRENT_FUNCTION, "Internal error"), std::logic_error); BOOST_CHECK_THROW_MSG(logic_error(BOOST_CURRENT_FUNCTION, 0, static_cast(3.124567890123456789012345678901L)), std::logic_error); BOOST_CHECK_THROW_MSG(logic_error(BOOST_CURRENT_FUNCTION, "Internal error, computed result was %1%, but should be in the range [0,1]", static_cast(3.124567890123456789012345678901L)), std::logic_error); BOOST_CHECK_THROW_MSG(chisqr(-1, static_cast(1)), std::domain_error); BOOST_CHECK_THROW_MSG(chisqr(static_cast(1), static_cast(-1)), std::domain_error); BOOST_CHECK_THROW_MSG(chisqr(static_cast(0), static_cast(1)), std::domain_error); BOOST_CHECK_THROW_MSG(tgamma(static_cast(0)), std::domain_error); BOOST_CHECK_THROW_MSG(tgamma(static_cast(-10)), std::domain_error); BOOST_CHECK_THROW_MSG(tgamma(static_cast(-10123457772243.0)), std::domain_error); cout << endl; } // template void test_error(FPT) int test_main(int, char* []) { // Test error handling. // (Parameter value, arbitrarily zero, only communicates the floating point type FPT). test_error(0.0F); // Test float. test_error(0.0); // Test double. test_error(0.0L); // Test long double. test_error(real_concept(0.0L)); // Test concepts. return 0; } // int test_main(int, char* [])