1 // (C) Copyright John Maddock 2006.
2 // Use, modification and distribution are subject to the
3 // Boost Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 #ifndef BOOST_MATH_TOOLS_TEST_HPP
7 #define BOOST_MATH_TOOLS_TEST_HPP
13 #include <boost/math/tools/config.hpp>
14 #include <boost/math/tools/stats.hpp>
15 #include <boost/math/special_functions/fpclassify.hpp>
16 #include <boost/math/special_functions/relative_difference.hpp>
17 #include <boost/math/policies/error_handling.hpp>
18 #include <boost/test/test_tools.hpp>
23 namespace boost{ namespace math{ namespace tools{
29 boost::math::tools::stats<T> stat; // Statistics for the test.
30 unsigned worst_case; // Index of the worst case test.
32 test_result() { worst_case = 0; }
33 void set_worst(int i){ worst_case = i; }
34 void add(const T& point){ stat.add(point); }
36 unsigned worst()const{ return worst_case; }
37 T min BOOST_PREVENT_MACRO_SUBSTITUTION()const{ return (stat.min)(); }
38 T max BOOST_PREVENT_MACRO_SUBSTITUTION()const{ return (stat.max)(); }
39 T total()const{ return stat.total(); }
40 T mean()const{ return stat.mean(); }
41 std::uintmax_t count()const{ return stat.count(); }
42 T variance()const{ return stat.variance(); }
43 T variance1()const{ return stat.variance1(); }
44 T rms()const{ return stat.rms(); }
46 test_result& operator+=(const test_result& t)
48 if((t.stat.max)() > (stat.max)())
49 worst_case = t.worst_case;
56 struct calculate_result_type
58 typedef typename T::value_type row_type;
59 typedef typename row_type::value_type value_type;
63 T relative_error(T a, T b)
65 return boost::math::relative_difference(a, b);
70 void set_output_precision(T, std::ostream& os)
74 #pragma warning(disable:4127)
76 if(std::numeric_limits<T>::digits10)
78 os << std::setprecision(std::numeric_limits<T>::digits10 + 2);
81 os << std::setprecision(22); // and hope for the best!
89 void print_row(const Seq& row, std::ostream& os = std::cout)
92 set_output_precision(row[0], os);
93 for (unsigned i = 0; i < row.size(); ++i)
101 catch (const std::exception&) {}
105 // Function test accepts an matrix of input values (probably a 2D std::array)
106 // and calls two functors for each row in the array - one calculates a value
107 // to test, and one extracts the expected value from the array (or possibly
108 // calculates it at high precision). The two functors are usually simple lambda
111 template <class A, class F1, class F2>
112 test_result<typename calculate_result_type<A>::value_type> test(const A& a, F1 test_func, F2 expect_func)
114 typedef typename A::value_type row_type;
115 typedef typename row_type::value_type value_type;
117 test_result<value_type> result;
119 for(unsigned i = 0; i < a.size(); ++i)
121 const row_type& row = a[i];
123 #ifndef BOOST_NO_EXCEPTIONS
127 point = test_func(row);
128 #ifndef BOOST_NO_EXCEPTIONS
130 catch(const std::underflow_error&)
134 catch(const std::overflow_error&)
136 point = std::numeric_limits<value_type>::has_infinity ?
137 std::numeric_limits<value_type>::infinity()
138 : tools::max_value<value_type>();
140 catch(const std::exception& e)
142 std::cerr << e.what() << std::endl;
143 print_row(row, std::cerr);
144 BOOST_ERROR("Unexpected exception.");
145 // so we don't get further errors:
146 point = expect_func(row);
149 value_type expected = expect_func(row);
150 value_type err = relative_error(point, expected);
151 #ifdef BOOST_INSTRUMENT
154 std::cout << row[0] << " " << err;
155 if(std::numeric_limits<value_type>::is_specialized)
157 std::cout << " (" << err / std::numeric_limits<value_type>::epsilon() << "eps)";
159 std::cout << std::endl;
162 if(!(boost::math::isfinite)(point) && (boost::math::isfinite)(expected))
164 std::cerr << "CAUTION: Found non-finite result, when a finite value was expected at entry " << i << "\n";
165 std::cerr << "Found: " << point << " Expected " << expected << " Error: " << err << std::endl;
166 print_row(row, std::cerr);
167 BOOST_ERROR("Unexpected non-finite result");
171 std::cerr << "CAUTION: Gross error found at entry " << i << ".\n";
172 std::cerr << "Found: " << point << " Expected " << expected << " Error: " << err << std::endl;
173 print_row(row, std::cerr);
174 BOOST_ERROR("Gross error");
177 if((result.max)() == err)
183 template <class Real, class A, class F1, class F2>
184 test_result<Real> test_hetero(const A& a, F1 test_func, F2 expect_func)
186 typedef typename A::value_type row_type;
187 typedef Real value_type;
189 test_result<value_type> result;
191 for(unsigned i = 0; i < a.size(); ++i)
193 const row_type& row = a[i];
195 #ifndef BOOST_NO_EXCEPTIONS
199 point = test_func(row);
200 #ifndef BOOST_NO_EXCEPTIONS
202 catch(const std::underflow_error&)
206 catch(const std::overflow_error&)
208 point = std::numeric_limits<value_type>::has_infinity ?
209 std::numeric_limits<value_type>::infinity()
210 : tools::max_value<value_type>();
212 catch(const std::exception& e)
214 std::cerr << "Unexpected exception at entry: " << i << "\n";
215 std::cerr << e.what() << std::endl;
216 print_row(row, std::cerr);
217 BOOST_ERROR("Unexpected exception.");
218 // so we don't get further errors:
219 point = expect_func(row);
222 value_type expected = expect_func(row);
223 value_type err = relative_error(point, expected);
224 #ifdef BOOST_INSTRUMENT
227 std::cout << row[0] << " " << err;
228 if(std::numeric_limits<value_type>::is_specialized)
230 std::cout << " (" << err / std::numeric_limits<value_type>::epsilon() << "eps)";
232 std::cout << std::endl;
235 if(!(boost::math::isfinite)(point) && (boost::math::isfinite)(expected))
237 std::cerr << "CAUTION: Found non-finite result, when a finite value was expected at entry " << i << "\n";
238 std::cerr << "Found: " << point << " Expected " << expected << " Error: " << err << std::endl;
239 print_row(row, std::cerr);
240 BOOST_ERROR("Unexpected non-finite result");
244 std::cerr << "CAUTION: Gross error found at entry " << i << ".\n";
245 std::cerr << "Found: " << point << " Expected " << expected << " Error: " << err << std::endl;
246 print_row(row, std::cerr);
247 BOOST_ERROR("Gross error");
250 if((result.max)() == err)
256 template <class Val, class Exception>
257 void test_check_throw(Val v, Exception e)
264 void test_check_throw(Val val, std::domain_error const* e)
266 BOOST_CHECK(errno == EDOM);
268 if(std::numeric_limits<Val>::has_quiet_NaN)
270 BOOST_CHECK((boost::math::isnan)(val));
275 void test_check_throw(Val v, std::overflow_error const* e)
277 BOOST_CHECK(errno == ERANGE);
279 BOOST_CHECK((v >= boost::math::tools::max_value<Val>()) || (v <= -boost::math::tools::max_value<Val>()));
283 void test_check_throw(Val v, boost::math::rounding_error const* e)
285 BOOST_CHECK(errno == ERANGE);
287 if(std::numeric_limits<Val>::is_specialized && std::numeric_limits<Val>::is_integer)
289 BOOST_CHECK((v == (std::numeric_limits<Val>::max)()) || (v == (std::numeric_limits<Val>::min)()));
293 BOOST_CHECK((v == boost::math::tools::max_value<Val>()) || (v == -boost::math::tools::max_value<Val>()));
303 // exception-free testing support, ideally we'd only define this in our tests,
304 // but to keep things simple we really need it somewhere that's always included:
306 #ifdef BOOST_NO_EXCEPTIONS
307 # define BOOST_MATH_CHECK_THROW(x, ExceptionType) boost::math::tools::test_check_throw(x, static_cast<ExceptionType const*>(0));
309 # define BOOST_MATH_CHECK_THROW(x, y) BOOST_CHECK_THROW(x, y)