test_error_handling.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. // Copyright Paul A. Bristow 2006-7.
  2. // Copyright John Maddock 2006-7.
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt
  6. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. // Test error handling mechanism produces the expected error messages.
  8. // for example Error in function boost::math::test_function<float>(float, float, float): Domain Error evaluating function at 0
  9. // Define some custom dummy error handlers that do nothing but throw,
  10. // in order to check that they are otherwise undefined.
  11. // The user MUST define them before they can be used.
  12. //
  13. struct user_defined_error{};
  14. namespace boost{ namespace math{ namespace policies{
  15. #ifndef BOOST_NO_EXCEPTIONS
  16. template <class T>
  17. T user_domain_error(const char* , const char* , const T& )
  18. {
  19. throw user_defined_error();
  20. }
  21. template <class T>
  22. T user_pole_error(const char* , const char* , const T& )
  23. {
  24. throw user_defined_error();
  25. }
  26. template <class T>
  27. T user_overflow_error(const char* , const char* , const T& )
  28. {
  29. throw user_defined_error();
  30. }
  31. template <class T>
  32. T user_underflow_error(const char* , const char* , const T& )
  33. {
  34. throw user_defined_error();
  35. }
  36. template <class T>
  37. T user_denorm_error(const char* , const char* , const T& )
  38. {
  39. throw user_defined_error();
  40. }
  41. template <class T>
  42. T user_evaluation_error(const char* , const char* , const T& )
  43. {
  44. throw user_defined_error();
  45. }
  46. template <class T>
  47. T user_indeterminate_result_error(const char* , const char* , const T& )
  48. {
  49. throw user_defined_error();
  50. }
  51. #endif
  52. }}} // namespaces
  53. #include <boost/math/tools/test.hpp>
  54. #include <boost/math/concepts/real_concept.hpp>
  55. #include <boost/math/policies/policy.hpp>
  56. #include <boost/math/policies/error_handling.hpp>
  57. #include <boost/math/tools/polynomial.hpp>
  58. #define BOOST_TEST_MAIN
  59. #include <boost/test/unit_test.hpp> // for test_main
  60. #include <cerrno> // for errno
  61. #include <iostream>
  62. #include <iomanip>
  63. //
  64. // Define some policies:
  65. //
  66. using namespace boost::math::policies;
  67. policy<
  68. domain_error<throw_on_error>,
  69. pole_error<throw_on_error>,
  70. overflow_error<throw_on_error>,
  71. underflow_error<throw_on_error>,
  72. denorm_error<throw_on_error>,
  73. evaluation_error<throw_on_error>,
  74. indeterminate_result_error<throw_on_error> > throw_policy;
  75. policy<
  76. domain_error<errno_on_error>,
  77. pole_error<errno_on_error>,
  78. overflow_error<errno_on_error>,
  79. underflow_error<errno_on_error>,
  80. denorm_error<errno_on_error>,
  81. evaluation_error<errno_on_error>,
  82. indeterminate_result_error<errno_on_error> > errno_policy;
  83. policy<
  84. domain_error<ignore_error>,
  85. pole_error<ignore_error>,
  86. overflow_error<ignore_error>,
  87. underflow_error<ignore_error>,
  88. denorm_error<ignore_error>,
  89. evaluation_error<ignore_error>,
  90. indeterminate_result_error<ignore_error> > ignore_policy;
  91. policy<
  92. domain_error<user_error>,
  93. pole_error<user_error>,
  94. overflow_error<user_error>,
  95. underflow_error<user_error>,
  96. denorm_error<user_error>,
  97. evaluation_error<user_error>,
  98. indeterminate_result_error<user_error> > user_policy;
  99. policy<> default_policy;
  100. #define TEST_EXCEPTION(expression, exception, msg)\
  101. BOOST_MATH_CHECK_THROW(expression, exception);\
  102. try{ expression; }catch(const exception& e){ std::cout << e.what() << std::endl; BOOST_CHECK_EQUAL(std::string(e.what()), std::string(msg)); }
  103. template <class T>
  104. std::string format_message_string(const char* str)
  105. {
  106. std::string type_name = boost::math::policies::detail::name_of<T>();
  107. std::string result(str);
  108. if(type_name != "float")
  109. {
  110. std::string::size_type pos = 0;
  111. while((pos = result.find("float", pos)) != std::string::npos)
  112. {
  113. result.replace(pos, 5, type_name);
  114. pos += type_name.size();
  115. }
  116. }
  117. return result;
  118. }
  119. template <class T>
  120. void test_error(T)
  121. {
  122. const char* func = "boost::math::test_function<%1%>(%1%, %1%, %1%)";
  123. const char* msg1 = "Error while handling value %1%";
  124. const char* msg2 = "Error message goes here...";
  125. // Check that exception is thrown, catch and show the message, for example:
  126. // Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
  127. #ifndef BOOST_NO_EXCEPTIONS
  128. TEST_EXCEPTION(boost::math::policies::raise_domain_error(func, msg1, T(0.0), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0"));
  129. TEST_EXCEPTION(boost::math::policies::raise_domain_error(func, 0, T(0.0), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Domain Error evaluating function at 0"));
  130. TEST_EXCEPTION(boost::math::policies::raise_pole_error(func, msg1, T(0.0), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0"));
  131. TEST_EXCEPTION(boost::math::policies::raise_pole_error(func, 0, T(0.0), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Evaluation of function at pole 0"));
  132. TEST_EXCEPTION(boost::math::policies::raise_overflow_error<T>(func, msg2, throw_policy), std::overflow_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error message goes here..."));
  133. TEST_EXCEPTION(boost::math::policies::raise_overflow_error<T>(func, 0, throw_policy), std::overflow_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Overflow Error"));
  134. TEST_EXCEPTION(boost::math::policies::raise_underflow_error<T>(func, msg2, throw_policy), std::underflow_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error message goes here..."));
  135. TEST_EXCEPTION(boost::math::policies::raise_underflow_error<T>(func, 0, throw_policy), std::underflow_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Underflow Error"));
  136. TEST_EXCEPTION(boost::math::policies::raise_denorm_error<T>(func, msg2, T(0), throw_policy), std::underflow_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error message goes here..."));
  137. TEST_EXCEPTION(boost::math::policies::raise_denorm_error<T>(func, 0, T(0), throw_policy), std::underflow_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Denorm Error"));
  138. TEST_EXCEPTION(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), throw_policy), boost::math::evaluation_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error while handling value 1.25"));
  139. TEST_EXCEPTION(boost::math::policies::raise_evaluation_error(func, 0, T(1.25), throw_policy), boost::math::evaluation_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Internal Evaluation Error, best value so far was 1.25"));
  140. TEST_EXCEPTION(boost::math::policies::raise_indeterminate_result_error(func, msg1, T(1.25), T(12.34), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error while handling value 1.25"));
  141. TEST_EXCEPTION(boost::math::policies::raise_indeterminate_result_error(func, 0, T(1.25), T(12.34), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Indeterminate result with value 1.25"));
  142. //
  143. // Now try user error handlers: these should all throw user_error():
  144. // - because by design these are undefined and must be defined by the user ;-)
  145. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_domain_error(func, msg1, T(0.0), user_policy), user_defined_error);
  146. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_pole_error(func, msg1, T(0.0), user_policy), user_defined_error);
  147. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_overflow_error<T>(func, msg2, user_policy), user_defined_error);
  148. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_underflow_error<T>(func, msg2, user_policy), user_defined_error);
  149. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_denorm_error<T>(func, msg2, T(0), user_policy), user_defined_error);
  150. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_evaluation_error(func, msg1, T(0.0), user_policy), user_defined_error);
  151. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_indeterminate_result_error(func, msg1, T(0.0), T(0.0), user_policy), user_defined_error);
  152. #endif
  153. // Test with ignore_error
  154. BOOST_CHECK((boost::math::isnan)(boost::math::policies::raise_domain_error(func, msg1, T(0.0), ignore_policy)) || !std::numeric_limits<T>::has_quiet_NaN);
  155. BOOST_CHECK((boost::math::isnan)(boost::math::policies::raise_pole_error(func, msg1, T(0.0), ignore_policy)) || !std::numeric_limits<T>::has_quiet_NaN);
  156. BOOST_CHECK_EQUAL(boost::math::policies::raise_overflow_error<T>(func, msg2, ignore_policy), std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity() : boost::math::tools::max_value<T>());
  157. BOOST_CHECK_EQUAL(boost::math::policies::raise_underflow_error<T>(func, msg2, ignore_policy), T(0));
  158. BOOST_CHECK_EQUAL(boost::math::policies::raise_denorm_error<T>(func, msg2, T(1.25), ignore_policy), T(1.25));
  159. BOOST_CHECK_EQUAL(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), ignore_policy), T(1.25));
  160. BOOST_CHECK_EQUAL(boost::math::policies::raise_indeterminate_result_error(func, 0, T(0.0), T(12.34), ignore_policy), T(12.34));
  161. // Test with errno_on_error
  162. errno = 0;
  163. BOOST_CHECK((boost::math::isnan)(boost::math::policies::raise_domain_error(func, msg1, T(0.0), errno_policy)) || !std::numeric_limits<T>::has_quiet_NaN);
  164. BOOST_CHECK(errno == EDOM);
  165. errno = 0;
  166. BOOST_CHECK((boost::math::isnan)(boost::math::policies::raise_pole_error(func, msg1, T(0.0), errno_policy)) || !std::numeric_limits<T>::has_quiet_NaN);
  167. BOOST_CHECK(errno == EDOM);
  168. errno = 0;
  169. BOOST_CHECK_EQUAL(boost::math::policies::raise_overflow_error<T>(func, msg2, errno_policy), std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity() : boost::math::tools::max_value<T>());
  170. BOOST_CHECK_EQUAL(errno, ERANGE);
  171. errno = 0;
  172. BOOST_CHECK_EQUAL(boost::math::policies::raise_underflow_error<T>(func, msg2, errno_policy), T(0));
  173. BOOST_CHECK_EQUAL(errno, ERANGE);
  174. errno = 0;
  175. BOOST_CHECK_EQUAL(boost::math::policies::raise_denorm_error<T>(func, msg2, T(1.25), errno_policy), T(1.25));
  176. BOOST_CHECK_EQUAL(errno, ERANGE);
  177. errno = 0;
  178. BOOST_CHECK_EQUAL(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), errno_policy), T(1.25));
  179. BOOST_CHECK(errno == EDOM);
  180. errno = 0;
  181. BOOST_CHECK(boost::math::policies::raise_indeterminate_result_error(func, 0, T(0.0), T(12.34), errno_policy) == T(12.34));
  182. BOOST_CHECK_EQUAL(errno, EDOM);
  183. }
  184. template <class T>
  185. void test_complex_error(T)
  186. {
  187. //
  188. // Error handling that can be applied to non-scalar types such as std::complex
  189. //
  190. const char* func = "boost::math::test_function<%1%>(%1%, %1%, %1%)";
  191. const char* msg1 = "Error while handling value %1%";
  192. const char* msg2 = "Error message goes here...";
  193. // Check that exception is thrown, catch and show the message, for example:
  194. // Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
  195. #ifndef BOOST_NO_EXCEPTIONS
  196. TEST_EXCEPTION(boost::math::policies::raise_domain_error(func, msg1, T(0.0), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error while handling value (0,0)"));
  197. TEST_EXCEPTION(boost::math::policies::raise_domain_error(func, 0, T(0.0), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Domain Error evaluating function at (0,0)"));
  198. TEST_EXCEPTION(boost::math::policies::raise_pole_error(func, msg1, T(0.0), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error while handling value (0,0)"));
  199. TEST_EXCEPTION(boost::math::policies::raise_pole_error(func, 0, T(0.0), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Evaluation of function at pole (0,0)"));
  200. //TEST_EXCEPTION(boost::math::policies::raise_overflow_error<T>(func, msg2, throw_policy), std::overflow_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error message goes here..."));
  201. //TEST_EXCEPTION(boost::math::policies::raise_overflow_error<T>(func, 0, throw_policy), std::overflow_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Overflow Error"));
  202. TEST_EXCEPTION(boost::math::policies::raise_underflow_error<T>(func, msg2, throw_policy), std::underflow_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error message goes here..."));
  203. TEST_EXCEPTION(boost::math::policies::raise_underflow_error<T>(func, 0, throw_policy), std::underflow_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Underflow Error"));
  204. TEST_EXCEPTION(boost::math::policies::raise_denorm_error<T>(func, msg2, T(0), throw_policy), std::underflow_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error message goes here..."));
  205. TEST_EXCEPTION(boost::math::policies::raise_denorm_error<T>(func, 0, T(0), throw_policy), std::underflow_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Denorm Error"));
  206. TEST_EXCEPTION(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), throw_policy), boost::math::evaluation_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error while handling value (1.25,0)"));
  207. TEST_EXCEPTION(boost::math::policies::raise_evaluation_error(func, 0, T(1.25), throw_policy), boost::math::evaluation_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Internal Evaluation Error, best value so far was (1.25,0)"));
  208. TEST_EXCEPTION(boost::math::policies::raise_indeterminate_result_error(func, msg1, T(1.25), T(12.34), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error while handling value (1.25,0)"));
  209. TEST_EXCEPTION(boost::math::policies::raise_indeterminate_result_error(func, 0, T(1.25), T(12.34), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Indeterminate result with value (1.25,0)"));
  210. //
  211. // Now try user error handlers: these should all throw user_error():
  212. // - because by design these are undefined and must be defined by the user ;-)
  213. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_domain_error(func, msg1, T(0.0), user_policy), user_defined_error);
  214. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_pole_error(func, msg1, T(0.0), user_policy), user_defined_error);
  215. //BOOST_MATH_CHECK_THROW(boost::math::policies::raise_overflow_error<T>(func, msg2, user_policy), user_defined_error);
  216. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_underflow_error<T>(func, msg2, user_policy), user_defined_error);
  217. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_denorm_error<T>(func, msg2, T(0), user_policy), user_defined_error);
  218. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_evaluation_error(func, msg1, T(0.0), user_policy), user_defined_error);
  219. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_indeterminate_result_error(func, msg1, T(0.0), T(0.0), user_policy), user_defined_error);
  220. #endif
  221. // Test with ignore_error
  222. BOOST_CHECK((boost::math::isnan)(boost::math::policies::raise_domain_error(func, msg1, T(0.0), ignore_policy)) || !std::numeric_limits<T>::has_quiet_NaN);
  223. BOOST_CHECK((boost::math::isnan)(boost::math::policies::raise_pole_error(func, msg1, T(0.0), ignore_policy)) || !std::numeric_limits<T>::has_quiet_NaN);
  224. //BOOST_CHECK_EQUAL(boost::math::policies::raise_overflow_error<T>(func, msg2, ignore_policy), std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity() : boost::math::tools::max_value<T>());
  225. BOOST_CHECK_EQUAL(boost::math::policies::raise_underflow_error<T>(func, msg2, ignore_policy), T(0));
  226. BOOST_CHECK_EQUAL(boost::math::policies::raise_denorm_error<T>(func, msg2, T(1.25), ignore_policy), T(1.25));
  227. BOOST_CHECK_EQUAL(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), ignore_policy), T(1.25));
  228. BOOST_CHECK_EQUAL(boost::math::policies::raise_indeterminate_result_error(func, 0, T(0.0), T(12.34), ignore_policy), T(12.34));
  229. // Test with errno_on_error
  230. errno = 0;
  231. BOOST_CHECK((boost::math::isnan)(boost::math::policies::raise_domain_error(func, msg1, T(0.0), errno_policy)) || !std::numeric_limits<T>::has_quiet_NaN);
  232. BOOST_CHECK(errno == EDOM);
  233. errno = 0;
  234. BOOST_CHECK((boost::math::isnan)(boost::math::policies::raise_pole_error(func, msg1, T(0.0), errno_policy)) || !std::numeric_limits<T>::has_quiet_NaN);
  235. BOOST_CHECK(errno == EDOM);
  236. errno = 0;
  237. //BOOST_CHECK_EQUAL(boost::math::policies::raise_overflow_error<T>(func, msg2, errno_policy), std::numeric_limits<T>::has_infinity ? std::numeric_limits<T>::infinity() : boost::math::tools::max_value<T>());
  238. //BOOST_CHECK_EQUAL(errno, ERANGE);
  239. errno = 0;
  240. BOOST_CHECK_EQUAL(boost::math::policies::raise_underflow_error<T>(func, msg2, errno_policy), T(0));
  241. BOOST_CHECK_EQUAL(errno, ERANGE);
  242. errno = 0;
  243. BOOST_CHECK_EQUAL(boost::math::policies::raise_denorm_error<T>(func, msg2, T(1.25), errno_policy), T(1.25));
  244. BOOST_CHECK_EQUAL(errno, ERANGE);
  245. errno = 0;
  246. BOOST_CHECK_EQUAL(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), errno_policy), T(1.25));
  247. BOOST_CHECK(errno == EDOM);
  248. errno = 0;
  249. BOOST_CHECK(boost::math::policies::raise_indeterminate_result_error(func, 0, T(0.0), T(12.34), errno_policy) == T(12.34));
  250. BOOST_CHECK_EQUAL(errno, EDOM);
  251. }
  252. template <class T>
  253. void test_polynomial_error(T)
  254. {
  255. //
  256. // Error handling that can be applied to non-scalar types such as std::complex
  257. //
  258. const char* func = "boost::math::test_function<%1%>(%1%, %1%, %1%)";
  259. const char* msg1 = "Error while handling value %1%";
  260. static const typename T::value_type data[] = { 1, 2, 3 };
  261. static const T val(data, 2);
  262. // Check that exception is thrown, catch and show the message, for example:
  263. // Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
  264. #ifndef BOOST_NO_EXCEPTIONS
  265. TEST_EXCEPTION(boost::math::policies::raise_domain_error(func, msg1, val, throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error while handling value { 1, 2, 3 }"));
  266. TEST_EXCEPTION(boost::math::policies::raise_domain_error(func, 0, val, throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Domain Error evaluating function at { 1, 2, 3 }"));
  267. TEST_EXCEPTION(boost::math::policies::raise_pole_error(func, msg1, val, throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error while handling value { 1, 2, 3 }"));
  268. TEST_EXCEPTION(boost::math::policies::raise_pole_error(func, 0, val, throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Evaluation of function at pole { 1, 2, 3 }"));
  269. TEST_EXCEPTION(boost::math::policies::raise_evaluation_error(func, msg1, val, throw_policy), boost::math::evaluation_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error while handling value { 1, 2, 3 }"));
  270. TEST_EXCEPTION(boost::math::policies::raise_evaluation_error(func, 0, val, throw_policy), boost::math::evaluation_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Internal Evaluation Error, best value so far was { 1, 2, 3 }"));
  271. TEST_EXCEPTION(boost::math::policies::raise_indeterminate_result_error(func, msg1, val, T(12.34), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Error while handling value { 1, 2, 3 }"));
  272. TEST_EXCEPTION(boost::math::policies::raise_indeterminate_result_error(func, 0, val, T(12.34), throw_policy), std::domain_error, format_message_string<T>("Error in function boost::math::test_function<float>(float, float, float): Indeterminate result with value { 1, 2, 3 }"));
  273. //
  274. // Now try user error handlers: these should all throw user_error():
  275. // - because by design these are undefined and must be defined by the user ;-)
  276. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_domain_error(func, msg1, T(0.0), user_policy), user_defined_error);
  277. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_pole_error(func, msg1, T(0.0), user_policy), user_defined_error);
  278. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_evaluation_error(func, msg1, T(0.0), user_policy), user_defined_error);
  279. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_indeterminate_result_error(func, msg1, T(0.0), T(0.0), user_policy), user_defined_error);
  280. #endif
  281. // Test with ignore_error
  282. BOOST_CHECK((boost::math::isnan)(boost::math::policies::raise_domain_error(func, msg1, T(0.0), ignore_policy)) || !std::numeric_limits<T>::has_quiet_NaN);
  283. BOOST_CHECK((boost::math::isnan)(boost::math::policies::raise_pole_error(func, msg1, T(0.0), ignore_policy)) || !std::numeric_limits<T>::has_quiet_NaN);
  284. BOOST_CHECK_EQUAL(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), ignore_policy), T(1.25));
  285. BOOST_CHECK_EQUAL(boost::math::policies::raise_indeterminate_result_error(func, 0, T(0.0), T(12.34), ignore_policy), T(12.34));
  286. // Test with errno_on_error
  287. errno = 0;
  288. BOOST_CHECK((boost::math::isnan)(boost::math::policies::raise_domain_error(func, msg1, T(0.0), errno_policy)) || !std::numeric_limits<T>::has_quiet_NaN);
  289. BOOST_CHECK(errno == EDOM);
  290. errno = 0;
  291. BOOST_CHECK((boost::math::isnan)(boost::math::policies::raise_pole_error(func, msg1, T(0.0), errno_policy)) || !std::numeric_limits<T>::has_quiet_NaN);
  292. BOOST_CHECK(errno == EDOM);
  293. errno = 0;
  294. BOOST_CHECK_EQUAL(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), errno_policy), T(1.25));
  295. BOOST_CHECK(errno == EDOM);
  296. errno = 0;
  297. BOOST_CHECK(boost::math::policies::raise_indeterminate_result_error(func, 0, T(0.0), T(12.34), errno_policy) == T(12.34));
  298. BOOST_CHECK_EQUAL(errno, EDOM);
  299. }
  300. BOOST_AUTO_TEST_CASE( test_main )
  301. {
  302. // Test error handling.
  303. // (Parameter value, arbitrarily zero, only communicates the floating point type FPT).
  304. test_error(0.0F); // Test float.
  305. test_error(0.0); // Test double.
  306. test_error(0.0L); // Test long double.
  307. test_error(boost::math::concepts::real_concept(0.0L)); // Test concepts.
  308. // try complex numbers too:
  309. test_complex_error(std::complex<float>(0));
  310. test_complex_error(std::complex<double>(0));
  311. test_complex_error(std::complex<long double>(0));
  312. test_polynomial_error(boost::math::tools::polynomial<float>());
  313. } // BOOST_AUTO_TEST_CASE( test_main )
  314. /*
  315. Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_error_handling.exe"
  316. Running 1 test case...
  317. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
  318. Error in function boost::math::test_function<float>(float, float, float): Domain Error evaluating function at 0
  319. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
  320. Error in function boost::math::test_function<float>(float, float, float): Evaluation of function at pole 0
  321. Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
  322. Error in function boost::math::test_function<float>(float, float, float): Overflow Error
  323. Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
  324. Error in function boost::math::test_function<float>(float, float, float): Underflow Error
  325. Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
  326. Error in function boost::math::test_function<float>(float, float, float): Denorm Error
  327. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 1.25
  328. Error in function boost::math::test_function<float>(float, float, float): Internal Evaluation Error, best value so far was 1.25
  329. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
  330. Error in function boost::math::test_function<double>(double, double, double): Domain Error evaluating function at 0
  331. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
  332. Error in function boost::math::test_function<double>(double, double, double): Evaluation of function at pole 0
  333. Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
  334. Error in function boost::math::test_function<double>(double, double, double): Overflow Error
  335. Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
  336. Error in function boost::math::test_function<double>(double, double, double): Underflow Error
  337. Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
  338. Error in function boost::math::test_function<double>(double, double, double): Denorm Error
  339. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 1.25
  340. Error in function boost::math::test_function<double>(double, double, double): Internal Evaluation Error, best value so far was 1.25
  341. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
  342. Error in function boost::math::test_function<long double>(long double, long double, long double): Domain Error evaluating function at 0
  343. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
  344. Error in function boost::math::test_function<long double>(long double, long double, long double): Evaluation of function at pole 0
  345. Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
  346. Error in function boost::math::test_function<long double>(long double, long double, long double): Overflow Error
  347. Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
  348. Error in function boost::math::test_function<long double>(long double, long double, long double): Underflow Error
  349. Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
  350. Error in function boost::math::test_function<long double>(long double, long double, long double): Denorm Error
  351. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 1.25
  352. Error in function boost::math::test_function<long double>(long double, long double, long double): Internal Evaluation Error, best value so far was 1.25
  353. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Error while handling value 0
  354. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Domain Error evaluating function at 0
  355. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Error while handling value 0
  356. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Evaluation of function at pole 0
  357. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Error message goes here...
  358. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Overflow Error
  359. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Error message goes here...
  360. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Underflow Error
  361. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Error message goes here...
  362. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Denorm Error
  363. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Error while handling value 1.25
  364. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Internal Evaluation Error, best value so far was 1.25
  365. *** No errors detected
  366. VS 2010
  367. ------ Rebuild All started: Project: test_error_handling, Configuration: Release Win32 ------
  368. test_error_handling.cpp
  369. Generating code
  370. Finished generating code
  371. test_error_handling.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\test_error_handling.exe
  372. Running 1 test case...
  373. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
  374. Error in function boost::math::test_function<float>(float, float, float): Domain Error evaluating function at 0
  375. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
  376. Error in function boost::math::test_function<float>(float, float, float): Evaluation of function at pole 0
  377. Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
  378. Error in function boost::math::test_function<float>(float, float, float): Overflow Error
  379. Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
  380. Error in function boost::math::test_function<float>(float, float, float): Underflow Error
  381. Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
  382. Error in function boost::math::test_function<float>(float, float, float): Denorm Error
  383. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 1.25
  384. Error in function boost::math::test_function<float>(float, float, float): Internal Evaluation Error, best value so far was 1.25
  385. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 1.25
  386. Error in function boost::math::test_function<float>(float, float, float): Indeterminate result with value 1.25
  387. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
  388. Error in function boost::math::test_function<double>(double, double, double): Domain Error evaluating function at 0
  389. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
  390. Error in function boost::math::test_function<double>(double, double, double): Evaluation of function at pole 0
  391. Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
  392. Error in function boost::math::test_function<double>(double, double, double): Overflow Error
  393. Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
  394. Error in function boost::math::test_function<double>(double, double, double): Underflow Error
  395. Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
  396. Error in function boost::math::test_function<double>(double, double, double): Denorm Error
  397. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 1.25
  398. Error in function boost::math::test_function<double>(double, double, double): Internal Evaluation Error, best value so far was 1.25
  399. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 1.25
  400. Error in function boost::math::test_function<double>(double, double, double): Indeterminate result with value 1.25
  401. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
  402. Error in function boost::math::test_function<long double>(long double, long double, long double): Domain Error evaluating function at 0
  403. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
  404. Error in function boost::math::test_function<long double>(long double, long double, long double): Evaluation of function at pole 0
  405. Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
  406. Error in function boost::math::test_function<long double>(long double, long double, long double): Overflow Error
  407. Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
  408. Error in function boost::math::test_function<long double>(long double, long double, long double): Underflow Error
  409. Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
  410. Error in function boost::math::test_function<long double>(long double, long double, long double): Denorm Error
  411. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 1.25
  412. Error in function boost::math::test_function<long double>(long double, long double, long double): Internal Evaluation Error, best value so far was 1.25
  413. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 1.25
  414. Error in function boost::math::test_function<long double>(long double, long double, long double): Indeterminate result with value 1.25
  415. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Error while handling value 0
  416. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Domain Error evaluating function at 0
  417. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Error while handling value 0
  418. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Evaluation of function at pole 0
  419. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Error message goes here...
  420. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Overflow Error
  421. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Error message goes here...
  422. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Underflow Error
  423. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Error message goes here...
  424. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Denorm Error
  425. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Error while handling value 1.25
  426. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Internal Evaluation Error, best value so far was 1.25
  427. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Error while handling value 1.25
  428. *** No errors detected
  429. Error in function boost::math::test_function<class boost::math::concepts::real_concept>(class boost::math::concepts::real_concept, class boost::math::concepts::real_concept, class boost::math::concepts::real_concept): Indeterminate result with value 1.25
  430. ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
  431. */