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 https://www.boost.org/LICENSE_1_0.txt
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:
60 if (abs(b) >= max_val)
62 if (abs(a) >= max_val)
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);
113 inline T epsilon_of(const T&)
115 BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_specialized);
116 return std::numeric_limits<T>::is_integer ? static_cast<T>(1) : std::numeric_limits<T>::epsilon();
120 inline int digits_of(const T&)
122 return std::numeric_limits<T>::is_specialized ? std::numeric_limits<T>::digits10 + 3 : std::numeric_limits<long double>::digits10 + 3;
125 inline std::ostream& report_where(const char* file, int line, const char* function)
128 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "In function: " << function << std::endl;
129 BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << ":" << line;
130 return BOOST_LIGHTWEIGHT_TEST_OSTREAM;
133 #define BOOST_MP_REPORT_WHERE report_where(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
135 inline void report_severity(int severity)
137 if (severity == error_on_fail)
138 ++boost::detail::test_errors();
139 else if (severity == abort_on_fail)
141 ++boost::detail::test_errors();
146 #define BOOST_MP_REPORT_SEVERITY(severity) report_severity(severity)
149 void report_unexpected_exception(const E& e, int severity, const char* file, int line, const char* function)
151 report_where(file, line, function) << " Unexpected exception of type " << typeid(e).name() << std::endl;
152 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Errot message was: " << e.what() << std::endl;
153 BOOST_MP_REPORT_SEVERITY(severity);
156 #ifndef BOOST_NO_EXCEPTIONS
157 #define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity) \
158 catch (const std::exception& e) \
160 report_unexpected_exception(e, severity, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); \
164 std::cout << "Exception of unknown type was thrown" << std::endl; \
165 report_severity(severity); \
167 #define BOOST_MP_TRY try
169 #define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
173 #define BOOST_CHECK_IMP(x, severity) \
181 BOOST_MP_REPORT_WHERE << " Failed predicate: " << BOOST_STRINGIZE(x) << std::endl; \
182 BOOST_MP_REPORT_SEVERITY(severity); \
185 BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
187 #define BOOST_CHECK(x) BOOST_CHECK_IMP(x, error_on_fail)
188 #define BOOST_WARN(x) BOOST_CHECK_IMP(x, warn_on_fail)
189 #define BOOST_REQUIRE(x) BOOST_CHECK_IMP(x, abort_on_fail)
191 #define BOOST_CLOSE_IMP(x, y, tol, severity) \
194 if (relative_error(x, y) > tol) \
196 BOOST_MP_REPORT_WHERE << " Failed check for closeness: \n" \
197 << std::setprecision(digits_of(x)) << std::scientific \
198 << "Value of LHS was: " << x << "\n" \
199 << "Value of RHS was: " << y << "\n" \
200 << std::setprecision(5) << std::fixed \
201 << "Relative error was: " << relative_error(x, y) << "eps\n" \
202 << "Tolerance was: " << tol << "eps" << std::endl; \
203 BOOST_MP_REPORT_SEVERITY(severity); \
206 BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
208 #define BOOST_EQUAL_IMP(x, y, severity) \
213 BOOST_MP_REPORT_WHERE << " Failed check for equality: \n" \
214 << std::setprecision(digits_of(x)) << std::scientific \
215 << "Value of LHS was: " << (x) << "\n" \
216 << "Value of RHS was: " << (y) << "\n" \
217 << std::setprecision(3) << std::endl; \
218 BOOST_MP_REPORT_SEVERITY(severity); \
221 BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
223 #define BOOST_NE_IMP(x, y, severity) \
228 BOOST_MP_REPORT_WHERE << " Failed check for non-equality: \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); \
236 BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
238 #define BOOST_LT_IMP(x, y, severity) \
243 BOOST_MP_REPORT_WHERE << " Failed check for less than: \n" \
244 << std::setprecision(digits_of(x)) << std::scientific \
245 << "Value of LHS was: " << x << "\n" \
246 << "Value of RHS was: " << y << "\n" \
247 << std::setprecision(3) << std::endl; \
248 BOOST_MP_REPORT_SEVERITY(severity); \
251 BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
253 #define BOOST_GT_IMP(x, y, severity) \
258 BOOST_MP_REPORT_WHERE << " Failed check for greater than: \n" \
259 << std::setprecision(digits_of(x)) << std::scientific \
260 << "Value of LHS was: " << x << "\n" \
261 << "Value of RHS was: " << y << "\n" \
262 << std::setprecision(3) << std::endl; \
263 BOOST_MP_REPORT_SEVERITY(severity); \
266 BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
268 #define BOOST_LE_IMP(x, y, severity) \
273 BOOST_MP_REPORT_WHERE << " Failed check for less-than-equal-to: \n" \
274 << std::setprecision(digits_of(x)) << std::scientific \
275 << "Value of LHS was: " << x << "\n" \
276 << "Value of RHS was: " << y << "\n" \
277 << std::setprecision(3) << std::endl; \
278 BOOST_MP_REPORT_SEVERITY(severity); \
281 BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
283 #define BOOST_GE_IMP(x, y, severity) \
288 BOOST_MP_REPORT_WHERE << " Failed check for greater-than-equal-to \n" \
289 << std::setprecision(digits_of(x)) << std::scientific \
290 << "Value of LHS was: " << x << "\n" \
291 << "Value of RHS was: " << y << "\n" \
292 << std::setprecision(3) << std::endl; \
293 BOOST_MP_REPORT_SEVERITY(severity); \
296 BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
298 #ifndef BOOST_NO_EXCEPTIONS
299 #define BOOST_MT_CHECK_THROW_IMP(x, E, severity) \
303 BOOST_MP_REPORT_WHERE << " Expected exception not thrown in expression " << BOOST_STRINGIZE(x) << std::endl; \
304 BOOST_MP_REPORT_SEVERITY(severity); \
306 catch (const E&) {} \
307 BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
309 #define BOOST_MT_CHECK_THROW_IMP(x, E, severity)
312 #define BOOST_CHECK_CLOSE(x, y, tol) BOOST_CLOSE_IMP(x, y, ((tol / (100 * epsilon_of(x)))), error_on_fail)
313 #define BOOST_WARN_CLOSE(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (100 * epsilon_of(x))), warn_on_fail)
314 #define BOOST_REQUIRE_CLOSE(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (100 * epsilon_of(x))), abort_on_fail)
316 #define BOOST_CHECK_CLOSE_FRACTION(x, y, tol) BOOST_CLOSE_IMP(x, y, ((tol / (epsilon_of(x)))), error_on_fail)
317 #define BOOST_WARN_CLOSE_FRACTION(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (epsilon_of(x))), warn_on_fail)
318 #define BOOST_REQUIRE_CLOSE_FRACTION(x, y, tol) BOOST_CLOSE_IMP(x, y, (tol / (epsilon_of(x))), abort_on_fail)
320 #define BOOST_CHECK_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, error_on_fail)
321 #define BOOST_WARN_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, warn_on_fail)
322 #define BOOST_REQUIRE_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, abort_on_fail)
324 #define BOOST_CHECK_NE(x, y) BOOST_NE_IMP(x, y, error_on_fail)
325 #define BOOST_WARN_NE(x, y) BOOST_NE_IMP(x, y, warn_on_fail)
326 #define BOOST_REQUIRE_NE(x, y) BOOST_NE_IMP(x, y, abort_on_fail)
328 #define BOOST_CHECK_LT(x, y) BOOST_LT_IMP(x, y, error_on_fail)
329 #define BOOST_WARN_LT(x, y) BOOST_LT_IMP(x, y, warn_on_fail)
330 #define BOOST_REQUIRE_LT(x, y) BOOST_LT_IMP(x, y, abort_on_fail)
332 #define BOOST_CHECK_GT(x, y) BOOST_GT_IMP(x, y, error_on_fail)
333 #define BOOST_WARN_GT(x, y) BOOST_GT_IMP(x, y, warn_on_fail)
334 #define BOOST_REQUIRE_GT(x, y) BOOST_GT_IMP(x, y, abort_on_fail)
336 #define BOOST_CHECK_LE(x, y) BOOST_LE_IMP(x, y, error_on_fail)
337 #define BOOST_WARN_LE(x, y) BOOST_LE_IMP(x, y, warn_on_fail)
338 #define BOOST_REQUIRE_LE(x, y) BOOST_LE_IMP(x, y, abort_on_fail)
340 #define BOOST_CHECK_GE(x, y) BOOST_GE_IMP(x, y, error_on_fail)
341 #define BOOST_WARN_GE(x, y) BOOST_GE_IMP(x, y, warn_on_fail)
342 #define BOOST_REQUIRE_GE(x, y) BOOST_GE_IMP(x, y, abort_on_fail)
344 #define BOOST_CHECK_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, error_on_fail)
345 #define BOOST_WARN_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, warn_on_fail)
346 #define BOOST_REQUIRE_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, abort_on_fail)