Imported Upstream version 1.57.0
[platform/upstream/boost.git] / 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 template <class T>
21 T user_domain_error(const char* , const char* , const T& )
22 {
23    throw user_defined_error();
24 }
25
26 template <class T>
27 T user_pole_error(const char* , const char* , const T& )
28 {
29    throw user_defined_error();
30 }
31
32 template <class T>
33 T user_overflow_error(const char* , const char* , const T& )
34 {
35    throw user_defined_error();
36 }
37
38 template <class T>
39 T user_underflow_error(const char* , const char* , const T& )
40 {
41    throw user_defined_error();
42 }
43
44 template <class T>
45 T user_denorm_error(const char* , const char* , const T& )
46 {
47    throw user_defined_error();
48 }
49
50 template <class T>
51 T user_evaluation_error(const char* , const char* , const T& )
52 {
53    throw user_defined_error();
54 }
55
56 template <class T>
57 T user_indeterminate_result_error(const char* , const char* , const T& )
58 {
59    throw user_defined_error();
60 }
61
62 }}} // namespaces
63
64 #include <boost/math/concepts/real_concept.hpp>
65 #include <boost/math/policies/policy.hpp>
66 #include <boost/math/policies/error_handling.hpp>
67 #define BOOST_TEST_MAIN
68 #include <boost/test/unit_test.hpp> // for test_main
69 #include <cerrno> // for errno
70 #include <iostream>
71 #include <iomanip>
72 //
73 // Define some policies:
74 //
75 using namespace boost::math::policies;
76 policy<
77    domain_error<throw_on_error>,
78    pole_error<throw_on_error>,
79    overflow_error<throw_on_error>,
80    underflow_error<throw_on_error>,
81    denorm_error<throw_on_error>,
82    evaluation_error<throw_on_error>,
83    indeterminate_result_error<throw_on_error> > throw_policy;
84 policy<
85    domain_error<errno_on_error>,
86    pole_error<errno_on_error>,
87    overflow_error<errno_on_error>,
88    underflow_error<errno_on_error>,
89    denorm_error<errno_on_error>,
90    evaluation_error<errno_on_error>,
91    indeterminate_result_error<errno_on_error> > errno_policy;
92 policy<
93    domain_error<ignore_error>,
94    pole_error<ignore_error>,
95    overflow_error<ignore_error>,
96    underflow_error<ignore_error>,
97    denorm_error<ignore_error>,
98    evaluation_error<ignore_error>,
99    indeterminate_result_error<ignore_error> > ignore_policy;
100 policy<
101    domain_error<user_error>,
102    pole_error<user_error>,
103    overflow_error<user_error>,
104    underflow_error<user_error>,
105    denorm_error<user_error>,
106    evaluation_error<user_error>,
107    indeterminate_result_error<user_error> > user_policy;
108 policy<> default_policy;
109
110 #define TEST_EXCEPTION(expression, exception)\
111    BOOST_CHECK_THROW(expression, exception);\
112    try{ expression; }catch(const exception& e){ std::cout << e.what() << std::endl; }
113
114 template <class T>
115 void test_error(T)
116 {
117    const char* func = "boost::math::test_function<%1%>(%1%, %1%, %1%)";
118    const char* msg1 = "Error while handling value %1%";
119    const char* msg2 = "Error message goes here...";
120
121    // Check that exception is thrown, catch and show the message, for example:
122    // Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
123
124    TEST_EXCEPTION(boost::math::policies::raise_domain_error(func, msg1, T(0.0), throw_policy), std::domain_error);
125    TEST_EXCEPTION(boost::math::policies::raise_domain_error(func, 0, T(0.0), throw_policy), std::domain_error);
126    TEST_EXCEPTION(boost::math::policies::raise_pole_error(func, msg1, T(0.0), throw_policy), std::domain_error);
127    TEST_EXCEPTION(boost::math::policies::raise_pole_error(func, 0, T(0.0), throw_policy), std::domain_error);
128    TEST_EXCEPTION(boost::math::policies::raise_overflow_error<T>(func, msg2, throw_policy), std::overflow_error);
129    TEST_EXCEPTION(boost::math::policies::raise_overflow_error<T>(func, 0, throw_policy), std::overflow_error);
130    TEST_EXCEPTION(boost::math::policies::raise_underflow_error<T>(func, msg2, throw_policy), std::underflow_error);
131    TEST_EXCEPTION(boost::math::policies::raise_underflow_error<T>(func, 0, throw_policy), std::underflow_error);
132    TEST_EXCEPTION(boost::math::policies::raise_denorm_error<T>(func, msg2, T(0), throw_policy), std::underflow_error);
133    TEST_EXCEPTION(boost::math::policies::raise_denorm_error<T>(func, 0, T(0), throw_policy), std::underflow_error);
134    TEST_EXCEPTION(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), throw_policy), boost::math::evaluation_error);
135    TEST_EXCEPTION(boost::math::policies::raise_evaluation_error(func, 0, T(1.25), throw_policy), boost::math::evaluation_error);
136    TEST_EXCEPTION(boost::math::policies::raise_indeterminate_result_error(func, msg1, T(1.25), T(12.34), throw_policy), std::domain_error);
137    TEST_EXCEPTION(boost::math::policies::raise_indeterminate_result_error(func, 0, T(1.25), T(12.34), throw_policy), std::domain_error);
138    //
139    // Now try user error handlers: these should all throw user_error():
140    // - because by design these are undefined and must be defined by the user ;-)
141    BOOST_CHECK_THROW(boost::math::policies::raise_domain_error(func, msg1, T(0.0), user_policy), user_defined_error);
142    BOOST_CHECK_THROW(boost::math::policies::raise_pole_error(func, msg1, T(0.0), user_policy), user_defined_error);
143    BOOST_CHECK_THROW(boost::math::policies::raise_overflow_error<T>(func, msg2, user_policy), user_defined_error);
144    BOOST_CHECK_THROW(boost::math::policies::raise_underflow_error<T>(func, msg2, user_policy), user_defined_error);
145    BOOST_CHECK_THROW(boost::math::policies::raise_denorm_error<T>(func, msg2, T(0), user_policy), user_defined_error);
146    BOOST_CHECK_THROW(boost::math::policies::raise_evaluation_error(func, msg1, T(0.0), user_policy), user_defined_error);
147    BOOST_CHECK_THROW(boost::math::policies::raise_indeterminate_result_error(func, msg1, T(0.0), T(0.0), user_policy), user_defined_error);
148
149    // Test with ignore_error
150    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);
151    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);
152    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>());
153    BOOST_CHECK_EQUAL(boost::math::policies::raise_underflow_error<T>(func, msg2, ignore_policy), T(0));
154    BOOST_CHECK_EQUAL(boost::math::policies::raise_denorm_error<T>(func, msg2, T(1.25), ignore_policy), T(1.25));
155    BOOST_CHECK_EQUAL(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), ignore_policy), T(1.25));
156    BOOST_CHECK_EQUAL(boost::math::policies::raise_indeterminate_result_error(func, 0, T(0.0), T(12.34), ignore_policy), T(12.34));
157
158    // Test with errno_on_error
159    errno = 0;
160    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);
161    BOOST_CHECK(errno == EDOM);
162    errno = 0;
163    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);
164    BOOST_CHECK(errno == EDOM);
165    errno = 0;
166    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>());
167    BOOST_CHECK_EQUAL(errno, ERANGE);
168    errno = 0;
169    BOOST_CHECK_EQUAL(boost::math::policies::raise_underflow_error<T>(func, msg2, errno_policy), T(0));
170    BOOST_CHECK_EQUAL(errno, ERANGE);
171    errno = 0;
172    BOOST_CHECK_EQUAL(boost::math::policies::raise_denorm_error<T>(func, msg2, T(1.25), errno_policy), T(1.25));
173    BOOST_CHECK_EQUAL(errno, ERANGE);
174    errno = 0;
175    BOOST_CHECK_EQUAL(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), errno_policy), T(1.25));
176    BOOST_CHECK(errno == EDOM);
177    errno = 0;
178    BOOST_CHECK(boost::math::policies::raise_indeterminate_result_error(func, 0, T(0.0), T(12.34), errno_policy) == T(12.34));
179    BOOST_CHECK_EQUAL(errno, EDOM);
180 }
181
182 BOOST_AUTO_TEST_CASE( test_main )
183 {
184    // Test error handling.
185    // (Parameter value, arbitrarily zero, only communicates the floating point type FPT).
186    test_error(0.0F); // Test float.
187    test_error(0.0); // Test double.
188    test_error(0.0L); // Test long double.
189    test_error(boost::math::concepts::real_concept(0.0L)); // Test concepts.
190 } // BOOST_AUTO_TEST_CASE( test_main )
191
192 /*
193
194 Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_error_handling.exe"
195 Running 1 test case...
196 Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
197 Error in function boost::math::test_function<float>(float, float, float): Domain Error evaluating function at 0
198 Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
199 Error in function boost::math::test_function<float>(float, float, float): Evaluation of function at pole 0
200 Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
201 Error in function boost::math::test_function<float>(float, float, float): Overflow Error
202 Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
203 Error in function boost::math::test_function<float>(float, float, float): Underflow Error
204 Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
205 Error in function boost::math::test_function<float>(float, float, float): Denorm Error
206 Error in function boost::math::test_function<float>(float, float, float): Error while handling value 1.25
207 Error in function boost::math::test_function<float>(float, float, float): Internal Evaluation Error, best value so far was 1.25
208 Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
209 Error in function boost::math::test_function<double>(double, double, double): Domain Error evaluating function at 0
210 Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
211 Error in function boost::math::test_function<double>(double, double, double): Evaluation of function at pole 0
212 Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
213 Error in function boost::math::test_function<double>(double, double, double): Overflow Error
214 Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
215 Error in function boost::math::test_function<double>(double, double, double): Underflow Error
216 Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
217 Error in function boost::math::test_function<double>(double, double, double): Denorm Error
218 Error in function boost::math::test_function<double>(double, double, double): Error while handling value 1.25
219 Error in function boost::math::test_function<double>(double, double, double): Internal Evaluation Error, best value so far was 1.25
220 Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
221 Error in function boost::math::test_function<long double>(long double, long double, long double): Domain Error evaluating function at 0
222 Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
223 Error in function boost::math::test_function<long double>(long double, long double, long double): Evaluation of function at pole 0
224 Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
225 Error in function boost::math::test_function<long double>(long double, long double, long double): Overflow Error
226 Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
227 Error in function boost::math::test_function<long double>(long double, long double, long double): Underflow Error
228 Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
229 Error in function boost::math::test_function<long double>(long double, long double, long double): Denorm Error
230 Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 1.25
231 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
232 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
233 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
234 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
235 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
236 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...
237 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
238 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...
239 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
240 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...
241 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
242 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
243 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
244 *** No errors detected
245
246 VS 2010
247 ------ Rebuild All started: Project: test_error_handling, Configuration: Release Win32 ------
248   test_error_handling.cpp
249   Generating code
250   Finished generating code
251   test_error_handling.vcxproj -> J:\Cpp\MathToolkit\test\Math_test\Release\test_error_handling.exe
252   Running 1 test case...
253   Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
254   Error in function boost::math::test_function<float>(float, float, float): Domain Error evaluating function at 0
255   Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
256   Error in function boost::math::test_function<float>(float, float, float): Evaluation of function at pole 0
257   Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
258   Error in function boost::math::test_function<float>(float, float, float): Overflow Error
259   Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
260   Error in function boost::math::test_function<float>(float, float, float): Underflow Error
261   Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
262   Error in function boost::math::test_function<float>(float, float, float): Denorm Error
263   Error in function boost::math::test_function<float>(float, float, float): Error while handling value 1.25
264   Error in function boost::math::test_function<float>(float, float, float): Internal Evaluation Error, best value so far was 1.25
265   Error in function boost::math::test_function<float>(float, float, float): Error while handling value 1.25
266   Error in function boost::math::test_function<float>(float, float, float): Indeterminate result with value 1.25
267   Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
268   Error in function boost::math::test_function<double>(double, double, double): Domain Error evaluating function at 0
269   Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
270   Error in function boost::math::test_function<double>(double, double, double): Evaluation of function at pole 0
271   Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
272   Error in function boost::math::test_function<double>(double, double, double): Overflow Error
273   Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
274   Error in function boost::math::test_function<double>(double, double, double): Underflow Error
275   Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
276   Error in function boost::math::test_function<double>(double, double, double): Denorm Error
277   Error in function boost::math::test_function<double>(double, double, double): Error while handling value 1.25
278   Error in function boost::math::test_function<double>(double, double, double): Internal Evaluation Error, best value so far was 1.25
279   Error in function boost::math::test_function<double>(double, double, double): Error while handling value 1.25
280   Error in function boost::math::test_function<double>(double, double, double): Indeterminate result with value 1.25
281   Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
282   Error in function boost::math::test_function<long double>(long double, long double, long double): Domain Error evaluating function at 0
283   Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
284   Error in function boost::math::test_function<long double>(long double, long double, long double): Evaluation of function at pole 0
285   Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
286   Error in function boost::math::test_function<long double>(long double, long double, long double): Overflow Error
287   Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
288   Error in function boost::math::test_function<long double>(long double, long double, long double): Underflow Error
289   Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
290   Error in function boost::math::test_function<long double>(long double, long double, long double): Denorm Error
291   Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 1.25
292   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
293   Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 1.25
294   Error in function boost::math::test_function<long double>(long double, long double, long double): Indeterminate result with value 1.25
295   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
296   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
297   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
298   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
299   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...
300   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
301   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...
302   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
303   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...
304   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
305   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
306   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
307   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
308   
309   *** No errors detected
310   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
311 ========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
312
313
314 */
315