]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/math/test/test_error_handling.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / math / test / test_error_handling.cpp
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 //
16 struct user_defined_error{};
17
18 namespace boost{ namespace math{ namespace policies{
19
20 #ifndef BOOST_NO_EXCEPTIONS
21
22 template <class T>
23 T user_domain_error(const char* , const char* , const T& )
24 {
25 throw user_defined_error();
26 }
27
28 template <class T>
29 T user_pole_error(const char* , const char* , const T& )
30 {
31 throw user_defined_error();
32 }
33
34 template <class T>
35 T user_overflow_error(const char* , const char* , const T& )
36 {
37 throw user_defined_error();
38 }
39
40 template <class T>
41 T user_underflow_error(const char* , const char* , const T& )
42 {
43 throw user_defined_error();
44 }
45
46 template <class T>
47 T user_denorm_error(const char* , const char* , const T& )
48 {
49 throw user_defined_error();
50 }
51
52 template <class T>
53 T user_evaluation_error(const char* , const char* , const T& )
54 {
55 throw user_defined_error();
56 }
57
58 template <class T>
59 T 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 //
79 using namespace boost::math::policies;
80 policy<
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;
88 policy<
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;
96 policy<
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;
104 policy<
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;
112 policy<> 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
118 template <class T>
119 std::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
135 template <class T>
136 void 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
204 template <class T>
205 void 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
276 template <class T>
277 void 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
351 BOOST_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
371 Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\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<double>(double, double, double): Error while handling value 0
386 Error in function boost::math::test_function<double>(double, double, double): Domain Error evaluating function at 0
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): Evaluation of function at pole 0
389 Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
390 Error in function boost::math::test_function<double>(double, double, double): Overflow Error
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): Underflow 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): Denorm Error
395 Error in function boost::math::test_function<double>(double, double, double): Error while handling value 1.25
396 Error in function boost::math::test_function<double>(double, double, double): Internal Evaluation Error, best value so far was 1.25
397 Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
398 Error in function boost::math::test_function<long double>(long double, long double, long double): Domain Error evaluating function at 0
399 Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
400 Error in function boost::math::test_function<long double>(long double, long double, long double): Evaluation of function at pole 0
401 Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
402 Error in function boost::math::test_function<long double>(long double, long double, long double): Overflow Error
403 Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
404 Error in function boost::math::test_function<long double>(long double, long double, long double): Underflow Error
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): Denorm Error
407 Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 1.25
408 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
409 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
410 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
411 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
412 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
413 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...
414 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
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 message goes here...
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): Underflow Error
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 message goes here...
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): Denorm Error
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 while handling value 1.25
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): Internal Evaluation Error, best value so far was 1.25
421 *** No errors detected
422
423 VS 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