Ticket #7296: utils.4.hpp

File utils.4.hpp, 14.3 KB (added by Marco Guazzone <marco.guazzone@…>, 10 years ago)

The new utils.hpp file that you should obtain after applying the utils-fix_complex-201208281922.patch patch.

Line 
1/**
2 * \file util.hpp
3 *
4 * \brief Utility macros/functions for testing and debugging purpose.
5 *
6 * Basic usage:
7 * <pre>
8 * BOOST_UBLAS_TEST_DEF( test_case_1 )
9 * {
10 * // do your test stuff
11 * }
12 *
13 * BOOST_UBLAS_TEST_DEF( test_case_2 )
14 * {
15 * // do your test stuff
16 * }
17 *
18 * // ...
19 *
20 * BOOST_UBLAS_TEST_DEF( test_case_n )
21 * {
22 * // do your test stuff
23 * }
24 *
25 * int main()
26 * {
27 * BOOST_UBLAS_TEST_SUITE( "My Test Suite" ); // optional
28 *
29 * BOOST_UBLAS_TEST_BEGIN();
30 * BOOST_UBLAS_TEST_DO( test_case_1 );
31 * BOOST_UBLAS_TEST_DO( test_case_2 );
32 * // ...
33 * BOOST_UBLAS_TEST_DO( test_case_n );
34 * BOOST_UBLAS_TEST_END();
35 * }
36 * </pre>
37 * Inside each <em>test_case_<code>k</code></em> you can use the various
38 * \c BOOST_UBLAS_TEST_CHECK* macros.
39 *
40 * <hr/>
41 *
42 * Copyright (c) 2009-2012, Marco Guazzone
43 *
44 * Distributed under the Boost Software License, Version 1.0. (See
45 * accompanying file LICENSE_1_0.txt or copy at
46 * http://www.boost.org/LICENSE_1_0.txt)
47 *
48 * \author Marco Guazzone, marco.guazzone@gmail.com
49 */
50
51#ifndef BOOST_NUMERIC_UBLAS_TEST_UTILS_HPP
52#define BOOST_NUMERIC_UBLAS_TEST_UTILS_HPP
53
54
55#include <boost/numeric/ublas/detail/config.hpp>
56#include <boost/numeric/ublas/traits.hpp>
57#include <cmath>
58#include <complex>
59#include <iostream>
60#include <limits>
61#include <stdexcept>
62
63
64namespace boost { namespace numeric { namespace ublas { namespace test { namespace detail { namespace /*<unnamed>*/ {
65
66/// Check if the given complex number is a NaN.
67template <typename T>
68BOOST_UBLAS_INLINE
69bool isnan(::std::complex<T> const& z)
70{
71 // According to IEEE, NaN is different even by itself
72 return (z != z) || ::std::isnan(z.real()) || ::std::isnan(z.imag());
73}
74
75/// Check if two (real) numbers are close each other (wrt a given tolerance).
76template <typename T1, typename T2, typename T3>
77BOOST_UBLAS_INLINE
78bool close_to(T1 x, T2 y, T3 tol)
79{
80 typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type,
81 T3>::promote_type real_type;
82
83 if (::std::isnan(x) || ::std::isnan(y))
84 {
85 // According to IEEE, NaN is different even by itself
86 return false;
87 }
88 return ::std::abs(x-y) <= (::std::max(static_cast<real_type>(::std::abs(x)), static_cast<real_type>(::std::abs(y)))*tol);
89}
90
91/// Check if two complex numbers are close each other (wrt a given tolerance).
92template <typename T1, typename T2, typename T3>
93BOOST_UBLAS_INLINE
94bool close_to(::std::complex<T1> const& x, ::std::complex<T2> const& y, T3 tol)
95{
96 typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type,
97 T3>::promote_type real_type;
98
99 if (isnan(x) || isnan(y))
100 {
101 // According to IEEE, NaN is different even by itself
102 return false;
103 }
104 ::std::complex<real_type> xx(x);
105 ::std::complex<real_type> yy(y);
106 return ::std::abs(xx-yy) <= (::std::max(::std::abs(xx), ::std::abs(yy))*tol);
107}
108
109/// Check if two (real) numbers are close each other (wrt a given tolerance).
110template <typename T1, typename T2, typename T3>
111BOOST_UBLAS_INLINE
112bool rel_close_to(T1 x, T2 y, T3 tol)
113{
114 typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type,
115 T3>::promote_type real_type;
116
117 if (::std::isnan(x) || ::std::isnan(y))
118 {
119 // According to IEEE, NaN is different even by itself
120 return false;
121 }
122 return ::std::abs(x-y)/::std::abs(y) <= tol;
123}
124
125/// Check if two complex numbers are close each other (wrt a given tolerance).
126template <typename T1, typename T2, typename T3>
127BOOST_UBLAS_INLINE
128bool rel_close_to(::std::complex<T1> const& x, ::std::complex<T2> const& y, T3 tol)
129{
130 typedef typename promote_traits<typename promote_traits<T1,T2>::promote_type,
131 T3>::promote_type real_type;
132
133 if (isnan(x) || isnan(y))
134 {
135 // According to IEEE, NaN is different even by itself
136 return false;
137 }
138 ::std::complex<real_type> xx(x);
139 ::std::complex<real_type> yy(y);
140 return ::std::abs(xx-yy)/::std::abs(yy) <= tol;
141}
142
143}}}}}} // Namespace boost::numeric::ublas::test::detail::<unnamed>
144
145
146/// Expand its argument.
147#define BOOST_UBLAS_TEST_EXPAND_(x) x
148
149
150/// Transform its argument into a string.
151#define BOOST_UBLAS_TEST_STRINGIFY_(x) #x
152
153
154/// Concatenate its two \e string arguments.
155#define BOOST_UBLAS_TEST_JOIN_(x,y) x ## y
156
157
158/// Output the message \a x if in debug-mode; otherwise output nothing.
159#ifndef NDEBUG
160# define BOOST_UBLAS_DEBUG_TRACE(x) ::std::cerr << "[Debug>> " << BOOST_UBLAS_TEST_EXPAND_(x) << ::std::endl
161#else
162# define BOOST_UBLAS_DEBUG_TRACE(x) /**/
163#endif // NDEBUG
164
165
166/// Define the name of the entire test suite.
167#define BOOST_UBLAS_TEST_SUITE(m) ::std::cerr << "--- Test Suite: " << m << " ---" << ::std::endl;
168
169
170/// Define the beginning of a test suite.
171#define BOOST_UBLAS_TEST_BEGIN() /* [BOOST_UBLAS_TEST_BEGIN] */ \
172 { \
173 /* Begin of Test Suite */ \
174 unsigned int test_fails__(0) \
175 /* [/BOOST_UBLAS_TEST_BEGIN] */
176
177
178/// Define a test case \a x inside the current test suite.
179#define BOOST_UBLAS_TEST_DEF(x) static void BOOST_UBLAS_TEST_EXPAND_(x)(unsigned int& test_fails__)
180
181
182/// Call the test case \a x.
183#define BOOST_UBLAS_TEST_DO(x) /* [BOOST_UBLAS_TEST_DO] */ \
184 try \
185 { \
186 BOOST_UBLAS_TEST_EXPAND_(x)(test_fails__); \
187 } \
188 catch (::std::exception& e) \
189 { \
190 ++test_fails__; \
191 BOOST_UBLAS_TEST_ERROR( e.what() ); \
192 } \
193 catch (...) \
194 { \
195 ++test_fails__; \
196 } \
197 /* [/BOOST_UBLAS_TEST_DO] */
198
199
200/// Define the end of a test suite.
201#define BOOST_UBLAS_TEST_END() /* [BOOST_UBLAS_TEST_END] */ \
202 if (test_fails__ > 0) \
203 { \
204 ::std::cerr << "Number of failed tests: " << test_fails__ << ::std::endl; \
205 } \
206 else \
207 { \
208 ::std::cerr << "No failed test" << ::std::endl; \
209 } \
210 } /* End of test suite */ \
211 /* [/BOOST_UBLAS_TEST_END] */
212
213
214/// Output the message \a m.
215#define BOOST_UBLAS_TEST_TRACE(m) ::std::cerr << "[Info>> " << BOOST_UBLAS_TEST_EXPAND_(m) << ::std::endl
216
217
218/// Check the truth of assertion \a x.
219#define BOOST_UBLAS_TEST_CHECK(x) /* [BOOST_UBLAS_TEST_CHECK] */ \
220 if (!(x)) \
221 { \
222 BOOST_UBLAS_TEST_ERROR( "Failed assertion: " << BOOST_UBLAS_TEST_STRINGIFY_(x) ); \
223 ++test_fails__; \
224 } \
225 /* [/BOOST_UBLAS_TEST_CHECK] */
226
227
228/// Check for the equality of \a x against \a y.
229#define BOOST_UBLAS_TEST_CHECK_EQ(x,y) /* [BOOST_UBLAS_TEST_CHECK_EQUAL] */ \
230 if (!(BOOST_UBLAS_TEST_EXPAND_(x) == BOOST_UBLAS_TEST_EXPAND_(y))) \
231 { \
232 BOOST_UBLAS_TEST_ERROR( "Failed assertion: (" << BOOST_UBLAS_TEST_STRINGIFY_(x) << " == " << BOOST_UBLAS_TEST_STRINGIFY_(y) << ")" ); \
233 ++test_fails__; \
234 } \
235 /* [/BOOST_UBLAS_TEST_CHECK_EQUAL] */
236
237
238/// Alias for macro \c BOOST_UBLAS_TEST_CHECK_EQ (for backward compatibility).
239#define BOOST_UBLAS_TEST_CHECK_EQUAL(x,y) BOOST_UBLAS_TEST_CHECK_EQ(x,y)
240
241
242/// Check that \a x and \a y are close with respect to a given precision.
243#define BOOST_UBLAS_TEST_CHECK_CLOSE(x,y,e) /* [BOOST_UBLAS_TEST_CHECK_PRECISION] */ \
244 if (!::boost::numeric::ublas::test::detail::close_to(BOOST_UBLAS_TEST_EXPAND_(x), BOOST_UBLAS_TEST_EXPAND_(y), BOOST_UBLAS_TEST_EXPAND_(e))) \
245 { \
246 BOOST_UBLAS_TEST_ERROR( "Failed assertion: abs(" << BOOST_UBLAS_TEST_STRINGIFY_(x) << "-" << BOOST_UBLAS_TEST_STRINGIFY_(y) << ") <= " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " [with " << BOOST_UBLAS_TEST_STRINGIFY_(x) << " == " << BOOST_UBLAS_TEST_EXPAND_(x) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y) << " == " << BOOST_UBLAS_TEST_EXPAND_(y) << " and " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " == " << BOOST_UBLAS_TEST_EXPAND_(e) << "]" ); \
247 ++test_fails__; \
248 } \
249 /* [/BOOST_UBLAS_TEST_CHECK_PRECISION] */
250
251
252/// Alias for macro \c BOOST_UBLAS_TEST_CHECK_CLOSE (for backward compatibility),
253#define BOOST_UBLAS_TEST_CHECK_PRECISION(x,y,e) BOOST_UBLAS_TEST_CHECK_CLOSE(x,y,e)
254
255
256/// Check that \a x is close to \a y with respect to a given relative precision.
257#define BOOST_UBLAS_TEST_CHECK_REL_CLOSE(x,y,e) /* [BOOST_UBLAS_TEST_CHECK_REL_PRECISION] */ \
258 if (!::boost::numeric::ublas::test::detail::rel_close_to(BOOST_UBLAS_TEST_EXPAND_(x), BOOST_UBLAS_TEST_EXPAND_(y), BOOST_UBLAS_TEST_EXPAND_(e))) \
259 { \
260 BOOST_UBLAS_TEST_ERROR( "Failed assertion: abs((" << BOOST_UBLAS_TEST_STRINGIFY_(x) << "-" << BOOST_UBLAS_TEST_STRINGIFY_(y) << ")/" << BOOST_UBLAS_TEST_STRINGIFY_(y) << ") <= " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " [with " << BOOST_UBLAS_TEST_STRINGIFY_(x) << " == " << BOOST_UBLAS_TEST_EXPAND_(x) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y) << " == " << BOOST_UBLAS_TEST_EXPAND_(y) << " and " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " == " << BOOST_UBLAS_TEST_EXPAND_(e) << "]" ); \
261 ++test_fails__; \
262 } \
263 /* [/BOOST_UBLAS_TEST_CHECK_REL_PRECISION] */
264
265
266/// Alias for macro \c BOOST_UBLAS_TEST_CHECK_REL_CLOSE (for backward compatibility),
267#define BOOST_UBLAS_TEST_CHECK_REL_PRECISION(x,y,e) BOOST_UBLAS_TEST_CHECK_REL_CLOSE(x,y,e)
268
269
270/// Check that elements of \a x and \a y are equal.
271#define BOOST_UBLAS_TEST_CHECK_VECTOR_EQ(x,y,n) /* [BOOST_UBLAS_TEST_CHECK_VECTOR_EQ] */ \
272 if (BOOST_UBLAS_TEST_EXPAND_(n) > 0) \
273 { \
274 unsigned long n__ = BOOST_UBLAS_TEST_EXPAND_(n); \
275 for (unsigned long i__ = n__; i__ > 0; --i__) \
276 { \
277 if (!(BOOST_UBLAS_TEST_EXPAND_(x)[n__-i__]==BOOST_UBLAS_TEST_EXPAND_(y)[n__-i__])) \
278 { \
279 BOOST_UBLAS_TEST_ERROR( "Failed assertion: (" << BOOST_UBLAS_TEST_STRINGIFY_(x[i__]) << "==" << BOOST_UBLAS_TEST_STRINGIFY_(y[i__]) << ")" << " [with " << BOOST_UBLAS_TEST_STRINGIFY_(x[i__]) << " == " << BOOST_UBLAS_TEST_EXPAND_(x)[n__-i__] << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y[i__]) << " == " << BOOST_UBLAS_TEST_EXPAND_(y)[n__-i__] << ", " << BOOST_UBLAS_TEST_STRINGIFY_(i__) << " == " << i__ << " and " << BOOST_UBLAS_TEST_STRINGIFY_(n) << " == " << n__ << "]" ); \
280 ++test_fails__; \
281 } \
282 } \
283 } \
284 /* [/BOOST_UBLAS_TEST_CHECK_VECTOR_EQ] */
285
286
287/// Check that elements of \a x and \a y are close with respect to a given precision.
288#define BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE(x,y,n,e) /* [BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE] */ \
289 if (BOOST_UBLAS_TEST_EXPAND_(n) > 0) \
290 { \
291 unsigned long n__ = BOOST_UBLAS_TEST_EXPAND_(n); \
292 for (unsigned long i__ = n__; i__ > 0; --i__) \
293 { \
294 if (!::boost::numeric::ublas::test::detail::close_to(BOOST_UBLAS_TEST_EXPAND_(x)[n__-i__], BOOST_UBLAS_TEST_EXPAND_(y)[n__-i__], BOOST_UBLAS_TEST_EXPAND_(e))) \
295 { \
296 BOOST_UBLAS_TEST_ERROR( "Failed assertion: abs((" << BOOST_UBLAS_TEST_STRINGIFY_(x[i__]) << "-" << BOOST_UBLAS_TEST_STRINGIFY_(y[i__]) << ") <= " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " [with " << BOOST_UBLAS_TEST_STRINGIFY_(x[i__]) << " == " << BOOST_UBLAS_TEST_EXPAND_(x)[n__-i__] << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y[i__]) << " == " << BOOST_UBLAS_TEST_EXPAND_(y)[n__-i__] << ", " << BOOST_UBLAS_TEST_STRINGIFY_(i__) << " == " << i__ << " and " << BOOST_UBLAS_TEST_STRINGIFY_(n) << " == " << n__ << "]" ); \
297 ++test_fails__; \
298 } \
299 } \
300 } \
301 /* [/BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE] */
302
303
304/// Check that elements of matrices \a x and \a y are equal.
305#define BOOST_UBLAS_TEST_CHECK_MATRIX_EQ(x,y,nr,nc) /* [BOOST_UBLAS_TEST_CHECK_MATRIX_EQ] */ \
306 for (unsigned long i__ = 0; i__ < BOOST_UBLAS_TEST_EXPAND_(nr); ++i__) \
307 { \
308 for (unsigned long j__ = 0; j__ < BOOST_UBLAS_TEST_EXPAND_(nc); ++j__) \
309 { \
310 if (!(BOOST_UBLAS_TEST_EXPAND_(x)(i__,j__)==BOOST_UBLAS_TEST_EXPAND_(y)(i__,j__))) \
311 { \
312 BOOST_UBLAS_TEST_ERROR( "Failed assertion: (" << BOOST_UBLAS_TEST_STRINGIFY_(x(i__,j__)) << " == " << BOOST_UBLAS_TEST_STRINGIFY_(y(i__,j__)) << ") [with " << BOOST_UBLAS_TEST_STRINGIFY_(x(i__,j__)) << " == " << BOOST_UBLAS_TEST_EXPAND_(x)(i__,j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y(i__,j__)) << " == " << BOOST_UBLAS_TEST_EXPAND_(y)(i__,j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(i__) << " == " << i__ << ", " << BOOST_UBLAS_TEST_STRINGIFY_(j__) << " == " << BOOST_UBLAS_TEST_EXPAND_(j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(nr) << " == " << BOOST_UBLAS_TEST_EXPAND_(nr) << " and " << BOOST_UBLAS_TEST_STRINGIFY_(nc) << " == " << BOOST_UBLAS_TEST_EXPAND_(nc) << "]" ); \
313 } \
314 } \
315 } \
316 /* [/BOOST_UBLAS_TEST_CHECK_MATRIX_EQ] */
317
318
319/// Check that elements of matrices \a x and \a y are close with respect to a given precision.
320#define BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE(x,y,nr,nc,e) /* [BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE] */ \
321 for (unsigned long i__ = 0; i__ < BOOST_UBLAS_TEST_EXPAND_(nr); ++i__) \
322 { \
323 for (unsigned long j__ = 0; j__ < BOOST_UBLAS_TEST_EXPAND_(nc); ++j__) \
324 { \
325 if (!::boost::numeric::ublas::test::detail::close_to(BOOST_UBLAS_TEST_EXPAND_(x)(i__,j__), BOOST_UBLAS_TEST_EXPAND_(y)(i__,j__), BOOST_UBLAS_TEST_EXPAND_(e))) \
326 { \
327 BOOST_UBLAS_TEST_ERROR( "Failed assertion: abs((" << BOOST_UBLAS_TEST_STRINGIFY_(x(i__,j__)) << "-" << BOOST_UBLAS_TEST_STRINGIFY_(y(i__,j__)) << ") <= " << BOOST_UBLAS_TEST_STRINGIFY_(e) << " [with " << BOOST_UBLAS_TEST_STRINGIFY_(x(i__,j__)) << " == " << BOOST_UBLAS_TEST_EXPAND_(x)(i__,j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(y(i__,j__)) << " == " << BOOST_UBLAS_TEST_EXPAND_(y)(i__,j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(i__) << " == " << i__ << ", " << BOOST_UBLAS_TEST_STRINGIFY_(j__) << " == " << BOOST_UBLAS_TEST_EXPAND_(j__) << ", " << BOOST_UBLAS_TEST_STRINGIFY_(nr) << " == " << BOOST_UBLAS_TEST_EXPAND_(nr) << " and " << BOOST_UBLAS_TEST_STRINGIFY_(nc) << " == " << BOOST_UBLAS_TEST_EXPAND_(nc) << "]" ); \
328 } \
329 } \
330 } \
331 /* [/BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE] */
332
333
334///< Output the error message \a x.
335#define BOOST_UBLAS_TEST_ERROR(x) ::std::cerr << "[Error (" << __FILE__ << ":" << __func__ << ":" << __LINE__ << ")>> " << BOOST_UBLAS_TEST_EXPAND_(x) << ::std::endl
336
337#endif // BOOST_NUMERIC_UBLAS_TEST_UTILS_HPP