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