PageRenderTime 23ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_error_handling.cpp

https://bitbucket.org/boostorg/math
C++ | 492 lines | 263 code | 34 blank | 195 comment | 27 complexity | 9b007923a58061f34e5760ee86d5cf46 MD5 | raw file
  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. const char* msg2 = "Error message goes here...";
  261. static const typename T::value_type data[] = { 1, 2, 3 };
  262. static const T val(data, 2);
  263. // Check that exception is thrown, catch and show the message, for example:
  264. // Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
  265. #ifndef BOOST_NO_EXCEPTIONS
  266. 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 }"));
  267. 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 }"));
  268. 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 }"));
  269. 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 }"));
  270. //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..."));
  271. //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"));
  272. //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..."));
  273. //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"));
  274. //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..."));
  275. //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"));
  276. 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 }"));
  277. 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 }"));
  278. 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 }"));
  279. 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 }"));
  280. //
  281. // Now try user error handlers: these should all throw user_error():
  282. // - because by design these are undefined and must be defined by the user ;-)
  283. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_domain_error(func, msg1, T(0.0), user_policy), user_defined_error);
  284. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_pole_error(func, msg1, T(0.0), user_policy), user_defined_error);
  285. //BOOST_MATH_CHECK_THROW(boost::math::policies::raise_overflow_error<T>(func, msg2, user_policy), user_defined_error);
  286. //BOOST_MATH_CHECK_THROW(boost::math::policies::raise_underflow_error<T>(func, msg2, user_policy), user_defined_error);
  287. //BOOST_MATH_CHECK_THROW(boost::math::policies::raise_denorm_error<T>(func, msg2, T(0), user_policy), user_defined_error);
  288. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_evaluation_error(func, msg1, T(0.0), user_policy), user_defined_error);
  289. BOOST_MATH_CHECK_THROW(boost::math::policies::raise_indeterminate_result_error(func, msg1, T(0.0), T(0.0), user_policy), user_defined_error);
  290. #endif
  291. // Test with ignore_error
  292. 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);
  293. 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);
  294. //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>());
  295. //BOOST_CHECK_EQUAL(boost::math::policies::raise_underflow_error<T>(func, msg2, ignore_policy), T(0));
  296. //BOOST_CHECK_EQUAL(boost::math::policies::raise_denorm_error<T>(func, msg2, T(1.25), ignore_policy), T(1.25));
  297. BOOST_CHECK_EQUAL(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), ignore_policy), T(1.25));
  298. BOOST_CHECK_EQUAL(boost::math::policies::raise_indeterminate_result_error(func, 0, T(0.0), T(12.34), ignore_policy), T(12.34));
  299. // Test with errno_on_error
  300. errno = 0;
  301. 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);
  302. BOOST_CHECK(errno == EDOM);
  303. errno = 0;
  304. 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);
  305. BOOST_CHECK(errno == EDOM);
  306. errno = 0;
  307. //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>());
  308. //BOOST_CHECK_EQUAL(errno, ERANGE);
  309. //errno = 0;
  310. //BOOST_CHECK_EQUAL(boost::math::policies::raise_underflow_error<T>(func, msg2, errno_policy), T(0));
  311. //BOOST_CHECK_EQUAL(errno, ERANGE);
  312. //errno = 0;
  313. //BOOST_CHECK_EQUAL(boost::math::policies::raise_denorm_error<T>(func, msg2, T(1.25), errno_policy), T(1.25));
  314. //BOOST_CHECK_EQUAL(errno, ERANGE);
  315. //errno = 0;
  316. BOOST_CHECK_EQUAL(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), errno_policy), T(1.25));
  317. BOOST_CHECK(errno == EDOM);
  318. errno = 0;
  319. BOOST_CHECK(boost::math::policies::raise_indeterminate_result_error(func, 0, T(0.0), T(12.34), errno_policy) == T(12.34));
  320. BOOST_CHECK_EQUAL(errno, EDOM);
  321. }
  322. BOOST_AUTO_TEST_CASE( test_main )
  323. {
  324. // Test error handling.
  325. // (Parameter value, arbitrarily zero, only communicates the floating point type FPT).
  326. test_error(0.0F); // Test float.
  327. test_error(0.0); // Test double.
  328. test_error(0.0L); // Test long double.
  329. test_error(boost::math::concepts::real_concept(0.0L)); // Test concepts.
  330. // try complex numbers too:
  331. test_complex_error(std::complex<float>(0));
  332. test_complex_error(std::complex<double>(0));
  333. test_complex_error(std::complex<long double>(0));
  334. test_polynomial_error(boost::math::tools::polynomial<float>());
  335. } // BOOST_AUTO_TEST_CASE( test_main )
  336. /*
  337. Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_error_handling.exe"
  338. Running 1 test case...
  339. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
  340. Error in function boost::math::test_function<float>(float, float, float): Domain Error evaluating function at 0
  341. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
  342. Error in function boost::math::test_function<float>(float, float, float): Evaluation of function at pole 0
  343. Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
  344. Error in function boost::math::test_function<float>(float, float, float): Overflow Error
  345. Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
  346. Error in function boost::math::test_function<float>(float, float, float): Underflow Error
  347. Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
  348. Error in function boost::math::test_function<float>(float, float, float): Denorm Error
  349. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 1.25
  350. Error in function boost::math::test_function<float>(float, float, float): Internal Evaluation Error, best value so far was 1.25
  351. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
  352. Error in function boost::math::test_function<double>(double, double, double): Domain Error evaluating function at 0
  353. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
  354. Error in function boost::math::test_function<double>(double, double, double): Evaluation of function at pole 0
  355. Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
  356. Error in function boost::math::test_function<double>(double, double, double): Overflow Error
  357. Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
  358. Error in function boost::math::test_function<double>(double, double, double): Underflow Error
  359. Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
  360. Error in function boost::math::test_function<double>(double, double, double): Denorm Error
  361. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 1.25
  362. Error in function boost::math::test_function<double>(double, double, double): Internal Evaluation Error, best value so far was 1.25
  363. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
  364. Error in function boost::math::test_function<long double>(long double, long double, long double): Domain Error evaluating function at 0
  365. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
  366. Error in function boost::math::test_function<long double>(long double, long double, long double): Evaluation of function at pole 0
  367. Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
  368. Error in function boost::math::test_function<long double>(long double, long double, long double): Overflow Error
  369. Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
  370. Error in function boost::math::test_function<long double>(long double, long double, long double): Underflow Error
  371. Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
  372. Error in function boost::math::test_function<long double>(long double, long double, long double): Denorm Error
  373. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 1.25
  374. 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
  375. 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
  376. 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
  377. 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
  378. 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
  379. 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...
  380. 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
  381. 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...
  382. 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
  383. 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...
  384. 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
  385. 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
  386. 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
  387. *** No errors detected
  388. VS 2010
  389. ------ Rebuild All started: Project: test_error_handling, Configuration: Release Win32 ------
  390. test_error_handling.cpp
  391. Generating code
  392. Finished generating code
  393. test_error_handling.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\test_error_handling.exe
  394. Running 1 test case...
  395. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
  396. Error in function boost::math::test_function<float>(float, float, float): Domain Error evaluating function at 0
  397. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
  398. Error in function boost::math::test_function<float>(float, float, float): Evaluation of function at pole 0
  399. Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
  400. Error in function boost::math::test_function<float>(float, float, float): Overflow Error
  401. Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
  402. Error in function boost::math::test_function<float>(float, float, float): Underflow Error
  403. Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
  404. Error in function boost::math::test_function<float>(float, float, float): Denorm Error
  405. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 1.25
  406. Error in function boost::math::test_function<float>(float, float, float): Internal Evaluation Error, best value so far was 1.25
  407. Error in function boost::math::test_function<float>(float, float, float): Error while handling value 1.25
  408. Error in function boost::math::test_function<float>(float, float, float): Indeterminate result with value 1.25
  409. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
  410. Error in function boost::math::test_function<double>(double, double, double): Domain Error evaluating function at 0
  411. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
  412. Error in function boost::math::test_function<double>(double, double, double): Evaluation of function at pole 0
  413. Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
  414. Error in function boost::math::test_function<double>(double, double, double): Overflow Error
  415. Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
  416. Error in function boost::math::test_function<double>(double, double, double): Underflow Error
  417. Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
  418. Error in function boost::math::test_function<double>(double, double, double): Denorm Error
  419. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 1.25
  420. Error in function boost::math::test_function<double>(double, double, double): Internal Evaluation Error, best value so far was 1.25
  421. Error in function boost::math::test_function<double>(double, double, double): Error while handling value 1.25
  422. Error in function boost::math::test_function<double>(double, double, double): Indeterminate result with value 1.25
  423. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
  424. Error in function boost::math::test_function<long double>(long double, long double, long double): Domain Error evaluating function at 0
  425. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
  426. Error in function boost::math::test_function<long double>(long double, long double, long double): Evaluation of function at pole 0
  427. Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
  428. Error in function boost::math::test_function<long double>(long double, long double, long double): Overflow Error
  429. Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
  430. Error in function boost::math::test_function<long double>(long double, long double, long double): Underflow Error
  431. Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
  432. Error in function boost::math::test_function<long double>(long double, long double, long double): Denorm Error
  433. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 1.25
  434. 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
  435. Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 1.25
  436. Error in function boost::math::test_function<long double>(long double, long double, long double): Indeterminate result with value 1.25
  437. 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
  438. 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
  439. 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
  440. 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
  441. 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...
  442. 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
  443. 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...
  444. 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
  445. 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...
  446. 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
  447. 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
  448. 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
  449. 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
  450. *** No errors detected
  451. 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
  452. ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
  453. */