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 #include <iostream>
14 #include <iomanip>
15 #include <stdlib.h>
16
17 #include <boost/core/lightweight_test.hpp>
18 #include <boost/current_function.hpp>
19 #include <boost/static_assert.hpp>
20 #include <boost/utility/enable_if.hpp>
21
22 enum
23 {
24 warn_on_fail,
25 error_on_fail,
26 abort_on_fail
27 };
28
29 template <class T>
digits_of(const T &)30 inline int digits_of(const T&)
31 {
32 return std::numeric_limits<T>::is_specialized ? std::numeric_limits<T>::digits : 18;
33 }
34
35
report_where(const char * file,int line,const char * function)36 inline std::ostream& report_where(const char* file, int line, const char* function)
37 {
38 if(function)
39 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "In function: "<< function << std::endl;
40 BOOST_LIGHTWEIGHT_TEST_OSTREAM << file << ":" << line;
41 return BOOST_LIGHTWEIGHT_TEST_OSTREAM;
42 }
43
44 #define BOOST_MP_REPORT_WHERE report_where(__FILE__, __LINE__, BOOST_CURRENT_FUNCTION)
45
report_severity(int severity)46 inline void report_severity(int severity)
47 {
48 if(severity == error_on_fail)
49 ++boost::detail::test_errors();
50 else if(severity == abort_on_fail)
51 {
52 ++boost::detail::test_errors();
53 abort();
54 }
55 }
56
57 #define BOOST_MP_REPORT_SEVERITY(severity) report_severity(severity)
58
59 template <class E>
report_unexpected_exception(const E & e,int severity,const char * file,int line,const char * function)60 void report_unexpected_exception(const E& e, int severity, const char* file, int line, const char* function)
61 {
62 report_where(file, line, function) << " Unexpected exception of type " << typeid(e).name() << std::endl;
63 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Errot message was: " << e.what() << std::endl;
64 BOOST_MP_REPORT_SEVERITY(severity);
65 }
66
67 #define BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity) \
68 catch(const std::exception& __e) \
69 { report_unexpected_exception(__e, severity, __FILE__, __LINE__, BOOST_CURRENT_FUNCTION); }\
70 catch(...)\
71 { BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Exception of unknown type was thrown" << std::endl; report_severity(severity); }
72
73
74 #define BOOST_CHECK_IMP(x, severity)\
75 try{ if(x){}else{\
76 BOOST_MP_REPORT_WHERE << " Failed predicate: " << BOOST_STRINGIZE(x) << std::endl;\
77 BOOST_MP_REPORT_SEVERITY(severity);\
78 }\
79 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
80
81 #define BOOST_CHECK(x) BOOST_CHECK_IMP(x, error_on_fail)
82 #define BOOST_WARN(x) BOOST_CHECK_IMP(x, warn_on_fail)
83 #define BOOST_REQUIRE(x) BOOST_CHECK_IMP(x, abort_on_fail)
84
85 #define BOOST_EQUAL_IMP(x, y, severity)\
86 try{ if(!((x) == (y))){\
87 BOOST_MP_REPORT_WHERE << " Failed check for equality: \n" \
88 << std::setprecision(digits_of(x)) << std::scientific\
89 << "Value of LHS was: " << (x) << "\n"\
90 << "Value of RHS was: " << (y) << "\n"\
91 << std::setprecision(3) << std::endl;\
92 BOOST_MP_REPORT_SEVERITY(severity);\
93 }\
94 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
95
96 #define BOOST_NE_IMP(x, y, severity)\
97 try{ if(!(x != y)){\
98 BOOST_MP_REPORT_WHERE << " Failed check for non-equality: \n" \
99 << std::setprecision(digits_of(x)) << std::scientific\
100 << "Value of LHS was: " << x << "\n"\
101 << "Value of RHS was: " << y << "\n"\
102 << std::setprecision(3) << std::endl;\
103 BOOST_MP_REPORT_SEVERITY(severity);\
104 }\
105 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
106
107 #define BOOST_LT_IMP(x, y, severity)\
108 try{ if(!(x < y)){\
109 BOOST_MP_REPORT_WHERE << " Failed check for less than: \n" \
110 << std::setprecision(digits_of(x)) << std::scientific\
111 << "Value of LHS was: " << x << "\n"\
112 << "Value of RHS was: " << y << "\n"\
113 << std::setprecision(3) << std::endl;\
114 BOOST_MP_REPORT_SEVERITY(severity);\
115 }\
116 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
117
118 #define BOOST_GT_IMP(x, y, severity)\
119 try{ if(!(x > y)){\
120 BOOST_MP_REPORT_WHERE << " Failed check for greater than: \n" \
121 << std::setprecision(digits_of(x)) << std::scientific\
122 << "Value of LHS was: " << x << "\n"\
123 << "Value of RHS was: " << y << "\n"\
124 << std::setprecision(3) << std::endl;\
125 BOOST_MP_REPORT_SEVERITY(severity);\
126 }\
127 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
128
129 #define BOOST_LE_IMP(x, y, severity)\
130 try{ if(!(x <= y)){\
131 BOOST_MP_REPORT_WHERE << " Failed check for less-than-equal-to: \n" \
132 << std::setprecision(digits_of(x)) << std::scientific\
133 << "Value of LHS was: " << x << "\n"\
134 << "Value of RHS was: " << y << "\n"\
135 << std::setprecision(3) << std::endl;\
136 BOOST_MP_REPORT_SEVERITY(severity);\
137 }\
138 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
139
140 #define BOOST_GE_IMP(x, y, severity)\
141 try{ if(!(x >= y)){\
142 BOOST_MP_REPORT_WHERE << " Failed check for greater-than-equal-to \n" \
143 << std::setprecision(digits_of(x)) << std::scientific\
144 << "Value of LHS was: " << x << "\n"\
145 << "Value of RHS was: " << y << "\n"\
146 << std::setprecision(3) << std::endl;\
147 BOOST_MP_REPORT_SEVERITY(severity);\
148 }\
149 }BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
150
151 #define BOOST_MT_CHECK_THROW_IMP(x, E, severity)\
152 try{ \
153 x;\
154 BOOST_MP_REPORT_WHERE << " Expected exception not thrown in expression " << BOOST_STRINGIZE(x) << std::endl;\
155 BOOST_MP_REPORT_SEVERITY(severity);\
156 }\
157 catch(const E&){}\
158 BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
159
160 template <class I, class J>
check_equal_collections(I a,I b,J x,J y)161 bool check_equal_collections(I a, I b, J x, J y)
162 {
163 int i = 0;
164 while(a != b)
165 {
166 if(x == y)
167 {
168 BOOST_LIGHTWEIGHT_TEST_OSTREAM << " Unexpected end of second sequence" << std::endl;
169 return false;
170 }
171 if(*a != *x)
172 {
173 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Error occured in position " << i << " of the collection." << std::endl;
174 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "First value was " << std::setprecision(digits_of(x)) << std::scientific << *a << std::endl;
175 BOOST_LIGHTWEIGHT_TEST_OSTREAM << "Second value was " << std::setprecision(digits_of(x)) << std::scientific << *x << std::endl;
176 return false;
177 }
178 ++a;
179 ++x;
180 }
181 return true;
182 }
183
184 #define BOOST_MT_CHECK_EQ_COLLECTIONS(a, b, x, y, severity)\
185 try{ \
186 if(!check_equal_collections(a, b, x, y))\
187 {\
188 BOOST_MP_REPORT_WHERE << " Collections were not equal" << std::endl;\
189 BOOST_MP_REPORT_SEVERITY(severity);\
190 }\
191 }\
192 BOOST_MP_UNEXPECTED_EXCEPTION_CHECK(severity)
193
194
195 #define BOOST_CHECK_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, error_on_fail)
196 #define BOOST_WARN_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, warn_on_fail)
197 #define BOOST_REQUIRE_EQUAL(x, y) BOOST_EQUAL_IMP(x, y, abort_on_fail)
198
199 #define BOOST_CHECK_NE(x, y) BOOST_NE_IMP(x, y, error_on_fail)
200 #define BOOST_WARN_NE(x, y) BOOST_NE_IMP(x, y, warn_on_fail)
201 #define BOOST_REQUIRE_NE(x, y) BOOST_NE_IMP(x, y, abort_on_fail)
202
203 #define BOOST_CHECK_LT(x, y) BOOST_LT_IMP(x, y, error_on_fail)
204 #define BOOST_WARN_LT(x, y) BOOST_LT_IMP(x, y, warn_on_fail)
205 #define BOOST_REQUIRE_LT(x, y) BOOST_LT_IMP(x, y, abort_on_fail)
206
207 #define BOOST_CHECK_GT(x, y) BOOST_GT_IMP(x, y, error_on_fail)
208 #define BOOST_WARN_GT(x, y) BOOST_GT_IMP(x, y, warn_on_fail)
209 #define BOOST_REQUIRE_GT(x, y) BOOST_GT_IMP(x, y, abort_on_fail)
210
211 #define BOOST_CHECK_LE(x, y) BOOST_LE_IMP(x, y, error_on_fail)
212 #define BOOST_WARN_LE(x, y) BOOST_LE_IMP(x, y, warn_on_fail)
213 #define BOOST_REQUIRE_LE(x, y) BOOST_LE_IMP(x, y, abort_on_fail)
214
215 #define BOOST_CHECK_GE(x, y) BOOST_GE_IMP(x, y, error_on_fail)
216 #define BOOST_WARN_GE(x, y) BOOST_GE_IMP(x, y, warn_on_fail)
217 #define BOOST_REQUIRE_GE(x, y) BOOST_GE_IMP(x, y, abort_on_fail)
218
219 #define BOOST_CHECK_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, error_on_fail)
220 #define BOOST_WARN_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, warn_on_fail)
221 #define BOOST_REQUIRE_THROW(x, E) BOOST_MT_CHECK_THROW_IMP(x, E, abort_on_fail)
222
223 #define BOOST_CHECK_EQUAL_COLLECTIONS(a, b, x, y) BOOST_MT_CHECK_EQ_COLLECTIONS(a, b, x, y, error_on_fail)
224 #define BOOST_WARN_EQUAL_COLLECTIONS(a, b, x, y) BOOST_MT_CHECK_EQ_COLLECTIONS(a, b, x, y, warn_on_fail)
225 #define BOOST_REQUIRE_EQUAL_COLLECTIONS(a, b, x, y) BOOST_MT_CHECK_EQ_COLLECTIONS(a, b, x, y, abort_on_fail)
226
227 #endif
228