1 ///////////////////////////////////////////////////////////////
2 // Copyright 2012 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_
7 #ifndef BOOST_MULTIPRECISION_TEST_HPP
8 #define BOOST_MULTIPRECISION_TEST_HPP
14 #include <boost/detail/lightweight_test.hpp>
15 #include <boost/current_function.hpp>
16 #include <boost/static_assert.hpp>
17 #include <boost/utility/enable_if.hpp>
18 #include <boost/type_traits/is_unsigned.hpp>
19 #include <boost/multiprecision/number.hpp>
24 inline typename boost::disable_if_c<boost::is_unsigned<T>::value || boost::multiprecision::is_unsigned_number<T>::value, T>::type
27 return a < 0 ? -a : a;
31 inline typename boost::enable_if_c<boost::is_unsigned<T>::value || boost::multiprecision::is_unsigned_number<T>::value, T>::type
40 typename boost::enable_if_c<boost::multiprecision::number_category<T>::value == boost::multiprecision::number_kind_integer, T>::type relative_error(T a, T b)
42 return a > b ? a - b : b - a;
46 typename boost::disable_if_c<(boost::multiprecision::number_category<T>::value == boost::multiprecision::number_kind_integer) || boost::multiprecision::is_interval_number<T>::value, T>::type relative_error(T a, T b)
51 T min_val = (std::numeric_limits<T>::min)();
52 T max_val = (std::numeric_limits<T>::max)();
54 if((a != 0) && (b != 0))
59 // TODO: use isfinite:
63 return 0; // one infinity is as good as another!
65 // If the result is denormalised, treat all denorms as equivalent:
66 if((a < min_val) && (a > 0))
68 else if((a > -min_val) && (a < 0))
70 if((b < min_val) && (b > 0))
72 else if((b > -min_val) && (b < 0))
75 return (std::max)(abs(T((a-b)/a)), abs(T((a-b)/b))) / std::numeric_limits<T>::epsilon();
78 // Handle special case where one or both are zero:
86 return (std::max)(abs(T((a-b)/a)), abs(T((a-b)/b))) / std::numeric_limits<T>::epsilon();
89 template <class T, class U>
90 typename boost::mpl::if_c<boost::is_convertible<T, U>::value, U, T>::type
91 relative_error(T a, U b)
93 typedef typename boost::mpl::if_c<boost::is_convertible<T, U>::value, U, T>::type cast_type;
94 return relative_error<cast_type>(static_cast<cast_type>(a), static_cast<cast_type>(b));
98 typename boost::enable_if_c<boost::multiprecision::is_interval_number<T>::value, T>::type relative_error(T a, T b)
100 typename boost::multiprecision::component_type<T>::type am = median(a);
101 typename boost::multiprecision::component_type<T>::type bm = median(b);
102 return relative_error<typename boost::multiprecision::component_type<T>::type>(am, bm);
114 inline T epsilon_of(const T&)
116 BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_specialized);
117 return std::numeric_limits<T>::is_integer ? static_cast<T>(1) : std::numeric_limits<T>::epsilon();
121 inline int digits_of(const T&)
123 return std::numeric_limits<T>::is_specialized ? std::numeric_limits<T>::digits10 + 3 : std::numeric_limits<long double>::digits10 + 3;
126 inline std::ostream& report_where(const char* file, int line, const char* function)
129 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "In function: "<< function << std::endl;
130 BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << ":" << line;
131 return BOOST_LIGHTWEIGHT_TEST_OSTREAM;
134 #define BOOST_MP_REPORT_WHERE report_where(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
136 inline void report_severity(int severity)
138 if(severity == error_on_fail)
139 ++boost::detail::test_errors();
140 else if(severity == abort_on_fail)
142 ++boost::detail::test_errors();
147 #define BOOST_MP_REPORT_SEVERITY(severity) report_severity(severity)
150 void report_unexpected_exception(const E& e, int severity, const char* file, int line, const char* function)
152 report_where(file, line, function) << " Unexpected exception of type " << typeid(e).name() << std::endl;
153 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Errot message was: " << e.what() << std::endl;
154 BOOST_MP_REPORT_SEVERITY(severity);
157 #ifndef BOOST_NO_EXCEPTIONS
158 #define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity) \
159 catch(const std::exception& e) \
160 { report_unexpected_exception(e, severity, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); }\
162 { std::cout << "Exception of unknown type was thrown" << std::endl; report_severity(severity); }
163 #define BOOST_MP_TRY try
165 #define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
169 #define BOOST_CHECK_IMP(x, severity)\
170 BOOST_MP_TRY{ if(x){}else{\
171 BOOST_MP_REPORT_WHERE << " Failed predicate: " << BOOST_STRINGIZE(x) << std::endl;\
172 BOOST_MP_REPORT_SEVERITY(severity);\
174 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
176 #define BOOST_CHECK(x) BOOST_CHECK_IMP(x, error_on_fail)
177 #define BOOST_WARN(x) BOOST_CHECK_IMP(x, warn_on_fail)
178 #define BOOST_REQUIRE(x) BOOST_CHECK_IMP(x, abort_on_fail)
180 #define BOOST_CLOSE_IMP(x, y, tol, severity)\
181 BOOST_MP_TRY{ if(relative_error(x, y) > tol){\
182 BOOST_MP_REPORT_WHERE << " Failed check for closeness: \n" \
183 << std::setprecision(digits_of(x)) << std::scientific\
184 << "Value of LHS was: " << x << "\n"\
185 << "Value of RHS was: " << y << "\n"\
186 << std::setprecision(5) << std::fixed\
187 << "Relative error was: " << relative_error(x, y) << "eps\n"\
188 << "Tolerance was: " << tol << "eps" << std::endl;\
189 BOOST_MP_REPORT_SEVERITY(severity);\
191 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
193 #define BOOST_EQUAL_IMP(x, y, severity)\
194 BOOST_MP_TRY{ if(!((x) == (y))){\
195 BOOST_MP_REPORT_WHERE << " Failed check for equality: \n" \
196 << std::setprecision(digits_of(x)) << std::scientific\
197 << "Value of LHS was: " << (x) << "\n"\
198 << "Value of RHS was: " << (y) << "\n"\
199 << std::setprecision(3) << std::endl;\
200 BOOST_MP_REPORT_SEVERITY(severity);\
202 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
204 #define BOOST_NE_IMP(x, y, severity)\
205 BOOST_MP_TRY{ if(!(x != y)){\
206 BOOST_MP_REPORT_WHERE << " Failed check for non-equality: \n" \
207 << std::setprecision(digits_of(x)) << std::scientific\
208 << "Value of LHS was: " << x << "\n"\
209 << "Value of RHS was: " << y << "\n"\
210 << std::setprecision(3) << std::endl;\
211 BOOST_MP_REPORT_SEVERITY(severity);\
213 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
215 #define BOOST_LT_IMP(x, y, severity)\
216 BOOST_MP_TRY{ if(!(x < y)){\
217 BOOST_MP_REPORT_WHERE << " Failed check for less than: \n" \
218 << std::setprecision(digits_of(x)) << std::scientific\
219 << "Value of LHS was: " << x << "\n"\
220 << "Value of RHS was: " << y << "\n"\
221 << std::setprecision(3) << std::endl;\
222 BOOST_MP_REPORT_SEVERITY(severity);\
224 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
226 #define BOOST_GT_IMP(x, y, severity)\
227 BOOST_MP_TRY{ if(!(x > y)){\
228 BOOST_MP_REPORT_WHERE << " Failed check for greater than: \n" \
229 << std::setprecision(digits_of(x)) << std::scientific\
230 << "Value of LHS was: " << x << "\n"\
231 << "Value of RHS was: " << y << "\n"\
232 << std::setprecision(3) << std::endl;\
233 BOOST_MP_REPORT_SEVERITY(severity);\
235 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
237 #define BOOST_LE_IMP(x, y, severity)\
238 BOOST_MP_TRY{ if(!(x <= y)){\
239 BOOST_MP_REPORT_WHERE << " Failed check for less-than-equal-to: \n" \
240 << std::setprecision(digits_of(x)) << std::scientific\
241 << "Value of LHS was: " << x << "\n"\
242 << "Value of RHS was: " << y << "\n"\
243 << std::setprecision(3) << std::endl;\
244 BOOST_MP_REPORT_SEVERITY(severity);\
246 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
248 #define BOOST_GE_IMP(x, y, severity)\
249 BOOST_MP_TRY{ if(!(x >= y)){\
250 BOOST_MP_REPORT_WHERE << " Failed check for greater-than-equal-to \n" \
251 << std::setprecision(digits_of(x)) << std::scientific\
252 << "Value of LHS was: " << x << "\n"\
253 << "Value of RHS was: " << y << "\n"\
254 << std::setprecision(3) << std::endl;\
255 BOOST_MP_REPORT_SEVERITY(severity);\
257 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
259 #ifndef BOOST_NO_EXCEPTIONS
260 #define BOOST_MT_CHECK_THROW_IMP(x, E, severity)\
263 BOOST_MP_REPORT_WHERE << " Expected exception not thrown in expression " << BOOST_STRINGIZE(x) << std::endl;\
264 BOOST_MP_REPORT_SEVERITY(severity);\
267 BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
269 #define BOOST_MT_CHECK_THROW_IMP(x, E, severity)
272 #define BOOST_CHECK_CLOSE(x, y, tol) BOOST_CLOSE_IMP(x, y, ((tol / (100 * epsilon_of(x)))), error_on_fail)
273 #define BOOST_WARN_CLOSE(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (100 * epsilon_of(x))), warn_on_fail)
274 #define BOOST_REQUIRE_CLOSE(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (100 * epsilon_of(x))), abort_on_fail)
276 #define BOOST_CHECK_CLOSE_FRACTION(x, y, tol) BOOST_CLOSE_IMP(x, y, ((tol / (epsilon_of(x)))), error_on_fail)
277 #define BOOST_WARN_CLOSE_FRACTION(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (epsilon_of(x))), warn_on_fail)
278 #define BOOST_REQUIRE_CLOSE_FRACTION(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (epsilon_of(x))), abort_on_fail)
280 #define BOOST_CHECK_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, error_on_fail)
281 #define BOOST_WARN_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, warn_on_fail)
282 #define BOOST_REQUIRE_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, abort_on_fail)
284 #define BOOST_CHECK_NE(x, y) BOOST_NE_IMP(x, y, error_on_fail)
285 #define BOOST_WARN_NE(x, y) BOOST_NE_IMP(x, y, warn_on_fail)
286 #define BOOST_REQUIRE_NE(x, y) BOOST_NE_IMP(x, y, abort_on_fail)
288 #define BOOST_CHECK_LT(x, y) BOOST_LT_IMP(x, y, error_on_fail)
289 #define BOOST_WARN_LT(x, y) BOOST_LT_IMP(x, y, warn_on_fail)
290 #define BOOST_REQUIRE_LT(x, y) BOOST_LT_IMP(x, y, abort_on_fail)
292 #define BOOST_CHECK_GT(x, y) BOOST_GT_IMP(x, y, error_on_fail)
293 #define BOOST_WARN_GT(x, y) BOOST_GT_IMP(x, y, warn_on_fail)
294 #define BOOST_REQUIRE_GT(x, y) BOOST_GT_IMP(x, y, abort_on_fail)
296 #define BOOST_CHECK_LE(x, y) BOOST_LE_IMP(x, y, error_on_fail)
297 #define BOOST_WARN_LE(x, y) BOOST_LE_IMP(x, y, warn_on_fail)
298 #define BOOST_REQUIRE_LE(x, y) BOOST_LE_IMP(x, y, abort_on_fail)
300 #define BOOST_CHECK_GE(x, y) BOOST_GE_IMP(x, y, error_on_fail)
301 #define BOOST_WARN_GE(x, y) BOOST_GE_IMP(x, y, warn_on_fail)
302 #define BOOST_REQUIRE_GE(x, y) BOOST_GE_IMP(x, y, abort_on_fail)
304 #define BOOST_CHECK_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, error_on_fail)
305 #define BOOST_WARN_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, warn_on_fail)
306 #define BOOST_REQUIRE_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, abort_on_fail)