File: test_error_handling.cpp

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (193 lines) | stat: -rw-r--r-- 12,769 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright Paul A. Bristow 2006-7.
// Copyright John Maddock 2006-7.

// Use, modification and distribution are subject to the
// Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt
// or copy at http://www.boost.org/LICENSE_1_0.txt)

// Test error handling mechanism produces the expected error messages.
// for example Error in function boost::math::test_function<float>(float, float, float): Domain Error evaluating function at 0

// Define some custom dummy error handlers that do nothing but throw,
// in order to check that they are otherwise undefined.
// The user MUST define them before they can be used.
//
struct user_defined_error{};

namespace boost{ namespace math{ namespace policies{

template <class T>
T user_domain_error(const char* , const char* , const T& )
{
   throw user_defined_error();
}

template <class T>
T user_pole_error(const char* , const char* , const T& )
{
   throw user_defined_error();
}

template <class T>
T user_overflow_error(const char* , const char* , const T& )
{
   throw user_defined_error();
}

template <class T>
T user_underflow_error(const char* , const char* , const T& )
{
   throw user_defined_error();
}

template <class T>
T user_denorm_error(const char* , const char* , const T& )
{
   throw user_defined_error();
}

template <class T>
T user_evaluation_error(const char* , const char* , const T& )
{
   throw user_defined_error();
}

}}} // namespaces

#include <boost/math/policies/policy.hpp>
#include <boost/math/policies/error_handling.hpp>
#include <boost/math/concepts/real_concept.hpp>
#include <boost/test/included/test_exec_monitor.hpp> // for test_main
//
// Define some policies:
//
using namespace boost::math::policies;
policy<
   domain_error<throw_on_error>,
   pole_error<throw_on_error>,
   overflow_error<throw_on_error>,
   underflow_error<throw_on_error>,
   denorm_error<throw_on_error>,
   evaluation_error<throw_on_error> > throw_policy;
policy<
   domain_error<errno_on_error>,
   pole_error<errno_on_error>,
   overflow_error<errno_on_error>,
   underflow_error<errno_on_error>,
   denorm_error<errno_on_error>,
   evaluation_error<errno_on_error> > errno_policy;
policy<
   domain_error<user_error>,
   pole_error<user_error>,
   overflow_error<user_error>,
   underflow_error<user_error>,
   denorm_error<user_error>,
   evaluation_error<user_error> > user_policy;
policy<> default_policy;

#define TEST_EXCEPTION(expression, exception)\
   BOOST_CHECK_THROW(expression, exception);\
   try{ expression; }catch(const exception& e){ std::cout << e.what() << std::endl; }

template <class T>
void test_error(T)
{
   const char* func = "boost::math::test_function<%1%>(%1%, %1%, %1%)";
   const char* msg1 = "Error while handling value %1%";
   const char* msg2 = "Error message goes here...";

   // Check that exception is thrown, catch and show the message, for example:
   // Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0

   TEST_EXCEPTION(boost::math::policies::raise_domain_error(func, msg1, T(0.0), throw_policy), std::domain_error);
   TEST_EXCEPTION(boost::math::policies::raise_domain_error(func, 0, T(0.0), throw_policy), std::domain_error);
   TEST_EXCEPTION(boost::math::policies::raise_pole_error(func, msg1, T(0.0), throw_policy), std::domain_error);
   TEST_EXCEPTION(boost::math::policies::raise_pole_error(func, 0, T(0.0), throw_policy), std::domain_error);
   TEST_EXCEPTION(boost::math::policies::raise_overflow_error<T>(func, msg2, throw_policy), std::overflow_error);
   TEST_EXCEPTION(boost::math::policies::raise_overflow_error<T>(func, 0, throw_policy), std::overflow_error);
   TEST_EXCEPTION(boost::math::policies::raise_underflow_error<T>(func, msg2, throw_policy), std::underflow_error);
   TEST_EXCEPTION(boost::math::policies::raise_underflow_error<T>(func, 0, throw_policy), std::underflow_error);
   TEST_EXCEPTION(boost::math::policies::raise_denorm_error<T>(func, msg2, T(0), throw_policy), std::underflow_error);
   TEST_EXCEPTION(boost::math::policies::raise_denorm_error<T>(func, 0, T(0), throw_policy), std::underflow_error);
   TEST_EXCEPTION(boost::math::policies::raise_evaluation_error(func, msg1, T(1.25), throw_policy), boost::math::evaluation_error);
   TEST_EXCEPTION(boost::math::policies::raise_evaluation_error(func, 0, T(1.25), throw_policy), boost::math::evaluation_error);
   //
   // Now try user error handlers: these should all throw user_error():
   // - because by design these are undefined and must be defined by the user ;-)
   BOOST_CHECK_THROW(boost::math::policies::raise_domain_error(func, msg1, T(0.0), user_policy), user_defined_error);
   BOOST_CHECK_THROW(boost::math::policies::raise_pole_error(func, msg1, T(0.0), user_policy), user_defined_error);
   BOOST_CHECK_THROW(boost::math::policies::raise_overflow_error<T>(func, msg2, user_policy), user_defined_error);
   BOOST_CHECK_THROW(boost::math::policies::raise_underflow_error<T>(func, msg2, user_policy), user_defined_error);
   BOOST_CHECK_THROW(boost::math::policies::raise_denorm_error<T>(func, msg2, T(0), user_policy), user_defined_error);
   BOOST_CHECK_THROW(boost::math::policies::raise_evaluation_error(func, msg1, T(0.0), user_policy), user_defined_error);

}

int test_main(int, char* [])
{
   // Test error handling.
   // (Parameter value, arbitrarily zero, only communicates the floating point type FPT).
   test_error(0.0F); // Test float.
   test_error(0.0); // Test double.
   test_error(0.0L); // Test long double.
  test_error(boost::math::concepts::real_concept(0.0L)); // Test concepts.
   return 0;
} // int test_main(int, char* [])

/*

Autorun "i:\boost-06-05-03-1300\libs\math\test\Math_test\debug\test_error_handling.exe"
Running 1 test case...
Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
Error in function boost::math::test_function<float>(float, float, float): Domain Error evaluating function at 0
Error in function boost::math::test_function<float>(float, float, float): Error while handling value 0
Error in function boost::math::test_function<float>(float, float, float): Evaluation of function at pole 0
Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
Error in function boost::math::test_function<float>(float, float, float): Overflow Error
Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
Error in function boost::math::test_function<float>(float, float, float): Underflow Error
Error in function boost::math::test_function<float>(float, float, float): Error message goes here...
Error in function boost::math::test_function<float>(float, float, float): Denorm Error
Error in function boost::math::test_function<float>(float, float, float): Error while handling value 1.25
Error in function boost::math::test_function<float>(float, float, float): Internal Evaluation Error, best value so far was 1.25
Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
Error in function boost::math::test_function<double>(double, double, double): Domain Error evaluating function at 0
Error in function boost::math::test_function<double>(double, double, double): Error while handling value 0
Error in function boost::math::test_function<double>(double, double, double): Evaluation of function at pole 0
Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
Error in function boost::math::test_function<double>(double, double, double): Overflow Error
Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
Error in function boost::math::test_function<double>(double, double, double): Underflow Error
Error in function boost::math::test_function<double>(double, double, double): Error message goes here...
Error in function boost::math::test_function<double>(double, double, double): Denorm Error
Error in function boost::math::test_function<double>(double, double, double): Error while handling value 1.25
Error in function boost::math::test_function<double>(double, double, double): Internal Evaluation Error, best value so far was 1.25
Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
Error in function boost::math::test_function<long double>(long double, long double, long double): Domain Error evaluating function at 0
Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 0
Error in function boost::math::test_function<long double>(long double, long double, long double): Evaluation of function at pole 0
Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
Error in function boost::math::test_function<long double>(long double, long double, long double): Overflow Error
Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
Error in function boost::math::test_function<long double>(long double, long double, long double): Underflow Error
Error in function boost::math::test_function<long double>(long double, long double, long double): Error message goes here...
Error in function boost::math::test_function<long double>(long double, long double, long double): Denorm Error
Error in function boost::math::test_function<long double>(long double, long double, long double): Error while handling value 1.25
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
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
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
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
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
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...
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
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...
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
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...
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
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
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
*** No errors detected

*/