// 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; // 2 argument version now removed, so these two commented out. // 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* []) /* Output: ------ Build started: Project: test_error_handling, Configuration: Debug Win32 ------ Compiling... test_error_handling.cpp Linking... Embedding manifest... Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_error_handling.exe" Running 1 test case... Current function is void __cdecl test_error(float) Message from thrown exception was: Error in function void __cdecl test_error(float): Domain Error on value 3.12456799 Message from thrown exception was: Error in function void __cdecl test_error(float): Out of range argument 3.12456799 in test invocation Message from thrown exception was: Error in function void __cdecl test_error(float): Internal logic error, computed value was 3.12456799 Message from thrown exception was: Error in function void __cdecl test_error(float): Internal error, computed result was 3.12456799, but should be in the range [0,1] Message from thrown exception was: Error in function double __cdecl boost::math::detail::chisqr_imp(double,double): degrees of freedom argument is -1, but must be > 0 ! Message from thrown exception was: Error in function float __cdecl boost::math::detail::chisqr_imp(float,float): chisqr argument is -1, but must be > 0 ! Message from thrown exception was: Error in function float __cdecl boost::math::detail::chisqr_imp(float,float): degrees of freedom argument is 0, but must be > 0 ! Message from thrown exception was: Error in function double __cdecl boost::math::detail::gamma_imp(double,const struct boost::math::lanczos::lanczos6 &): Evaluation of tgamma at a negative integer 0. Message from thrown exception was: Error in function double __cdecl boost::math::detail::gamma_imp(double,const struct boost::math::lanczos::lanczos6 &): Evaluation of tgamma at a negative integer -10. Message from thrown exception was: Error in function double __cdecl boost::math::detail::gamma_imp(double,const struct boost::math::lanczos::lanczos6 &): Evaluation of tgamma at a negative integer -10123458117632. Current function is void __cdecl test_error(double) Message from thrown exception was: Error in function void __cdecl test_error(double): Domain Error on value 3.1245678901234566 Message from thrown exception was: Error in function void __cdecl test_error(double): Out of range argument 3.1245678901234566 in test invocation Message from thrown exception was: Error in function void __cdecl test_error(double): Internal logic error, computed value was 3.1245678901234566 Message from thrown exception was: Error in function void __cdecl test_error(double): Internal error, computed result was 3.1245678901234566, but should be in the range [0,1] Message from thrown exception was: Error in function double __cdecl boost::math::detail::chisqr_imp(double,double): degrees of freedom argument is -1, but must be > 0 ! Message from thrown exception was: Error in function double __cdecl boost::math::detail::chisqr_imp(double,double): chisqr argument is -1, but must be > 0 ! Message from thrown exception was: Error in function double __cdecl boost::math::detail::chisqr_imp(double,double): degrees of freedom argument is 0, but must be > 0 ! Message from thrown exception was: Error in function double __cdecl boost::math::detail::gamma_imp(double,const struct boost::math::lanczos::lanczos13m53 &): Evaluation of tgamma at a negative integer 0. Message from thrown exception was: Error in function double __cdecl boost::math::detail::gamma_imp(double,const struct boost::math::lanczos::lanczos13m53 &): Evaluation of tgamma at a negative integer -10. Message from thrown exception was: Error in function double __cdecl boost::math::detail::gamma_imp(double,const struct boost::math::lanczos::lanczos13m53 &): Evaluation of tgamma at a negative integer -10123457772243. Current function is void __cdecl test_error(long double) Message from thrown exception was: Error in function void __cdecl test_error(long double): Domain Error on value 3.1245678901234566 Message from thrown exception was: Error in function void __cdecl test_error(long double): Out of range argument 3.1245678901234566 in test invocation Message from thrown exception was: Error in function void __cdecl test_error(long double): Internal logic error, computed value was 3.1245678901234566 Message from thrown exception was: Error in function void __cdecl test_error(long double): Internal error, computed result was 3.1245678901234566, but should be in the range [0,1] Message from thrown exception was: Error in function long double __cdecl boost::math::detail::chisqr_imp(long double,long double): degrees of freedom argument is -1, but must be > 0 ! Message from thrown exception was: Error in function long double __cdecl boost::math::detail::chisqr_imp(long double,long double): chisqr argument is -1, but must be > 0 ! Message from thrown exception was: Error in function long double __cdecl boost::math::detail::chisqr_imp(long double,long double): degrees of freedom argument is 0, but must be > 0 ! Message from thrown exception was: Error in function long double __cdecl boost::math::detail::gamma_imp(long double,const struct boost::math::lanczos::lanczos13m53 &): Evaluation of tgamma at a negative integer 0. Message from thrown exception was: Error in function long double __cdecl boost::math::detail::gamma_imp(long double,const struct boost::math::lanczos::lanczos13m53 &): Evaluation of tgamma at a negative integer -10. Message from thrown exception was: Error in function long double __cdecl boost::math::detail::gamma_imp(long double,const struct boost::math::lanczos::lanczos13m53 &): Evaluation of tgamma at a negative integer -10123457772243. Current function is void __cdecl test_error(class boost::math::concepts::real_concept) Message from thrown exception was: Error in function void __cdecl test_error(class boost::math::concepts::real_concept): Domain Error on value 3.1245678901234566 Message from thrown exception was: Error in function void __cdecl test_error(class boost::math::concepts::real_concept): Out of range argument 3.1245678901234566 in test invocation Message from thrown exception was: Error in function void __cdecl test_error(class boost::math::concepts::real_concept): Internal logic error, computed value was 3.1245678901234566 Message from thrown exception was: Error in function void __cdecl test_error(class boost::math::concepts::real_concept): Internal error, computed result was 3.1245678901234566, but should be in the range [0,1] Message from thrown exception was: Error in function class boost::math::concepts::real_concept __cdecl boost::math::detail::chisqr_imp(class boost::math::concepts::real_concept,class boost::math::concepts::real_concept): degrees of freedom argument is -1, but must be > 0 ! Message from thrown exception was: Error in function class boost::math::concepts::real_concept __cdecl boost::math::detail::chisqr_imp(class boost::math::concepts::real_concept,class boost::math::concepts::real_concept): chisqr argument is -1, but must be > 0 ! Message from thrown exception was: Error in function class boost::math::concepts::real_concept __cdecl boost::math::detail::chisqr_imp(class boost::math::concepts::real_concept,class boost::math::concepts::real_concept): degrees of freedom argument is 0, but must be > 0 ! Message from thrown exception was: Error in function class boost::math::concepts::real_concept __cdecl boost::math::detail::gamma_imp(class boost::math::concepts::real_concept,const struct boost::math::lanczos::undefined_lanczos &): Evaluation of tgamma at a negative integer 0. Message from thrown exception was: Error in function class boost::math::concepts::real_concept __cdecl boost::math::detail::gamma_imp(class boost::math::concepts::real_concept,const struct boost::math::lanczos::undefined_lanczos &): Evaluation of tgamma at a negative integer -10. Message from thrown exception was: Error in function class boost::math::concepts::real_concept __cdecl boost::math::detail::gamma_imp(class boost::math::concepts::real_concept,const struct boost::math::lanczos::undefined_lanczos &): Evaluation of tgamma at a negative integer -10123457772243. *** No errors detected Build Time 0:07 Build log was saved at "file://i:\boost-06-05-03-1300\libs\math\test\Math_test\test_error_handling\Debug\BuildLog.htm" test_error_handling - 0 error(s), 0 warning(s) ========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ========== */