]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test.hpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test.hpp
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_
5 //
6
7 #ifndef BOOST_MULTIPRECISION_TEST_HPP
8 #define BOOST_MULTIPRECISION_TEST_HPP
9
10 #include <limits>
11 #include <cmath>
12 #include <typeinfo>
13
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>
20
21 namespace detail{
22
23 template <class T>
24 inline typename boost::disable_if_c<boost::is_unsigned<T>::value || boost::multiprecision::is_unsigned_number<T>::value, T>::type
25 abs(const T& a)
26 {
27 return a < 0 ? -a : a;
28 }
29
30 template <class T>
31 inline typename boost::enable_if_c<boost::is_unsigned<T>::value || boost::multiprecision::is_unsigned_number<T>::value, T>::type
32 abs(const T& a)
33 {
34 return a;
35 }
36
37 }
38
39 template <class T>
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)
41 {
42 return a > b ? a - b : b - a;
43 }
44
45 template <class T>
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)
47 {
48 using std::abs;
49 using ::detail::abs;
50
51 T min_val = (std::numeric_limits<T>::min)();
52 T max_val = (std::numeric_limits<T>::max)();
53
54 if((a != 0) && (b != 0))
55 {
56 if(a == b)
57 return 0;
58
59 // TODO: use isfinite:
60 if(abs(b) >= max_val)
61 {
62 if(abs(a) >= max_val)
63 return 0; // one infinity is as good as another!
64 }
65 // If the result is denormalised, treat all denorms as equivalent:
66 if((a < min_val) && (a > 0))
67 a = min_val;
68 else if((a > -min_val) && (a < 0))
69 a = -min_val;
70 if((b < min_val) && (b > 0))
71 b = min_val;
72 else if((b > -min_val) && (b < 0))
73 b = -min_val;
74
75 return (std::max)(abs(T((a-b)/a)), abs(T((a-b)/b))) / std::numeric_limits<T>::epsilon();
76 }
77
78 // Handle special case where one or both are zero:
79 if(min_val == 0)
80 return abs(T(a-b));
81 if(abs(a) < min_val)
82 a = min_val;
83 if(abs(b) < min_val)
84 b = min_val;
85
86 return (std::max)(abs(T((a-b)/a)), abs(T((a-b)/b))) / std::numeric_limits<T>::epsilon();
87 }
88
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)
92 {
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));
95 }
96
97 template <class T>
98 typename boost::enable_if_c<boost::multiprecision::is_interval_number<T>::value, T>::type relative_error(T a, T b)
99 {
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);
103 }
104
105
106 enum
107 {
108 warn_on_fail,
109 error_on_fail,
110 abort_on_fail
111 };
112
113 template <class T>
114 inline T epsilon_of(const T&)
115 {
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();
118 }
119
120 template <class T>
121 inline int digits_of(const T&)
122 {
123 return std::numeric_limits<T>::is_specialized ? std::numeric_limits<T>::digits10 + 3 : std::numeric_limits<long double>::digits10 + 3;
124 }
125
126 inline std::ostream& report_where(const char* file, int line, const char* function)
127 {
128 if(function)
129 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "In function: "<< function << std::endl;
130 BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << ":" << line;
131 return BOOST_LIGHTWEIGHT_TEST_OSTREAM;
132 }
133
134 #define BOOST_MP_REPORT_WHERE report_where(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
135
136 inline void report_severity(int severity)
137 {
138 if(severity == error_on_fail)
139 ++boost::detail::test_errors();
140 else if(severity == abort_on_fail)
141 {
142 ++boost::detail::test_errors();
143 abort();
144 }
145 }
146
147 #define BOOST_MP_REPORT_SEVERITY(severity) report_severity(severity)
148
149 template <class E>
150 void report_unexpected_exception(const E& e, int severity, const char* file, int line, const char* function)
151 {
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);
155 }
156
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); }\
161 catch(...)\
162 { std::cout << "Exception of unknown type was thrown" << std::endl; report_severity(severity); }
163 #define BOOST_MP_TRY try
164 #else
165 #define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
166 #define BOOST_MP_TRY
167 #endif
168
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);\
173 }\
174 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
175
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)
179
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);\
190 }\
191 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
192
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);\
201 }\
202 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
203
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);\
212 }\
213 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
214
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);\
223 }\
224 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
225
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);\
234 }\
235 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
236
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);\
245 }\
246 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
247
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);\
256 }\
257 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
258
259 #ifndef BOOST_NO_EXCEPTIONS
260 #define BOOST_MT_CHECK_THROW_IMP(x, E, severity)\
261 BOOST_MP_TRY{ \
262 x;\
263 BOOST_MP_REPORT_WHERE << " Expected exception not thrown in expression " << BOOST_STRINGIZE(x) << std::endl;\
264 BOOST_MP_REPORT_SEVERITY(severity);\
265 }\
266 catch(const E&){}\
267 BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
268 #else
269 #define BOOST_MT_CHECK_THROW_IMP(x, E, severity)
270 #endif
271
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)
275
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)
279
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)
283
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)
287
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)
291
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)
295
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)
299
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)
303
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)
307
308 #endif