Ticket #7296: utils.2.hpp

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

The new (i.e., already patched by utils-new.patch) utils.hpp

Line 
1/**
2 * \file util.hpp
3 *
4 * \brief Utility macros/functions for testing and debugging purpose.
5 *
6 * Copyright (c) 2009-2012, Marco Guazzone
7 *
8 * Distributed under the Boost Software License, Version 1.0. (See
9 * accompanying file LICENSE_1_0.txt or copy at
10 * http://www.boost.org/LICENSE_1_0.txt)
11 *
12 * \author Marco Guazzone, marco.guazzone@gmail.com
13 */
14
15#ifndef BOOST_NUMERIC_UBLAS_TEST_UTILS_HPP
16#define BOOST_NUMERIC_UBLAS_TEST_UTILS_HPP
17
18
19#include <cmath>
20#include <iostream>
21#include <limits>
22#include <stdexcept>
23
24
25namespace boost { namespace numeric { namespace ublas { namespace test { namespace detail { namespace /*<unnamed>*/ {
26
27template <typename T1, typename T2, typename T3>
28inline
29bool close_to(T1 x, T2 y, T3 tol)
30{
31 if (::std::isnan(x) || ::std::isnan(y))
32 {
33 // According to IEEE, NaN are different event by itself
34 return false;
35 }
36 return ::std::abs(x-y) <= (::std::max(::std::abs(x), ::std::abs(y))*tol);
37}
38
39}}}}}} // Namespace boost::numeric::ublas::test::detail::<unnamed>
40
41
42/// Expand its argument.
43#define BOOST_UBLAS_TEST_EXPAND_(x) x
44
45
46/// Transform its argument into a string.
47#define BOOST_UBLAS_TEST_STRINGIFY_(x) #x
48
49
50/// Concatenate its two \e string arguments.
51#define BOOST_UBLAS_TEST_JOIN_(x,y) x ## y
52
53
54/// Output the message \a x if in debug-mode; otherwise output nothing.
55#ifndef NDEBUG
56# define BOOST_UBLAS_DEBUG_TRACE(x) ::std::cerr << "[Debug>> " << BOOST_UBLAS_TEST_EXPAND_(x) << ::std::endl
57#else
58# define BOOST_UBLAS_DEBUG_TRACE(x) /**/
59#endif // NDEBUG
60
61
62/// Define the name of the entire test suite.
63#define DCS_TEST_SUITE(m) ::std::cerr << "--- Test Suite: " << m << " ---" << ::std::endl;
64
65
66/// Define the beginning of a test suite.
67#define BOOST_UBLAS_TEST_BEGIN() /* [BOOST_UBLAS_TEST_BEGIN] */ \
68 { \
69 /* Begin of Test Suite */ \
70 unsigned int test_fails__(0) \
71 /* [/BOOST_UBLAS_TEST_BEGIN] */
72
73
74/// Define a test case \a x inside the current test suite.
75#define BOOST_UBLAS_TEST_DEF(x) void BOOST_UBLAS_TEST_EXPAND_(x)(unsigned int& test_fails__)
76
77
78/// Call the test case \a x.
79#define BOOST_UBLAS_TEST_DO(x) /* [BOOST_UBLAS_TEST_DO] */ \
80 try \
81 { \
82 BOOST_UBLAS_TEST_EXPAND_(x)(test_fails__); \
83 } \
84 catch (::std::exception& e) \
85 { \
86 ++test_fails__; \
87 BOOST_UBLAS_TEST_ERROR( e.what() ); \
88 } \
89 catch (...) \
90 { \
91 ++test_fails__; \
92 } \
93 /* [/BOOST_UBLAS_TEST_DO] */
94
95
96/// Define the end of a test suite.
97#define BOOST_UBLAS_TEST_END() /* [BOOST_UBLAS_TEST_END] */ \
98 if (test_fails__ > 0) \
99 { \
100 ::std::cerr << "Number of failed tests: " << test_fails__ << ::std::endl; \
101 } \
102 else \
103 { \
104 ::std::cerr << "No failed test" << ::std::endl; \
105 } \
106 } /* End of test suite */ \
107 /* [/BOOST_UBLAS_TEST_END] */
108
109
110/// Check the truth of assertion \a x.
111#define BOOST_UBLAS_TEST_CHECK(x) /* [BOOST_UBLAS_TEST_CHECK] */ \
112 if (!(x)) \
113 { \
114 BOOST_UBLAS_TEST_ERROR( "Failed assertion: " << BOOST_UBLAS_TEST_STRINGIFY_(x) ); \
115 ++test_fails__; \
116 } \
117 /* [/BOOST_UBLAS_TEST_CHECK] */
118
119
120/// Check for the equality of \a x against \a y.
121#define BOOST_UBLA_TEST_CHECK_EQ(x,y) /* [BOOST_UBLA_TEST_CHECK_EQUAL] */ \
122 if (!(BOOST_UBLAS_TEST_PARAM_EXPAND_(x) == BOOST_UBLAS_TEST_PARAM_EXPAND_(y))) \
123 { \
124 BOOST_UBLAS_TEST_ERROR( "Failed assertion: (" << BOOST_UBLAS_TEST_STRINGIFY_(x) << " == " << BOOST_UBLAS_TEST_STRINGIFY_(y) << ")" ); \
125 ++test_fails__; \
126 } \
127 /* [/BOOST_UBLA_TEST_CHECK_EQUAL] */
128
129
130/// Alias for macro \c BOOST_UBLA_TEST_CHECK_EQ (for backward compatibility).
131#define BOOST_UBLA_TEST_CHECK_EQUAL(x,y) BOOST_UBLA_TEST_CHECK_EQ(x,y)
132
133
134/// Check that \a x and \a y are close with respect to a given precision.
135#define BOOST_UBLAS_TEST_CHECK_CLOSE(x,y,e) /* [BOOST_UBLAS_TEST_CHECK_PRECISION] */ \
136 if (!::boost::numeric::ublas::test::detail::close_to(BOOST_UBLAS_TEST_EXPAND_(x), BOOST_UBLAS_TEST_EXPAND_(y), BOOST_UBLAS_TEST_EXPAND_(e))) \
137 { \
138 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) << "]" ); \
139 ++test_fails__; \
140 } \
141 /* [/BOOST_UBLAS_TEST_CHECK_PRECISION] */
142
143
144/// Alias for macro \c BOOST_UBLAS_TEST_CHECK_CLOSE (for backward compatibility),
145#define BOOST_UBLAS_TEST_CHECK_PRECISION(x,y,e) BOOST_UBLAS_TEST_CHECK_CLOSE(x,y,e)
146
147
148/// Check that \a x is close to \a y with respect to a given relative precision.
149#define BOOST_UBLAS_TEST_CHECK_REL_PRECISION(x,y,e) /* [BOOST_UBLAS_TEST_CHECK_REL_PRECISION] */ \
150 if (!::boost::numeric::ublas::test::detail::close_to(BOOST_UBLAS_TEST_EXPAND_(x)/BOOST_UBLAS_TEST_EXPAND_(y), 1.0, BOOST_UBLAS_TEST_EXPAND_(e))) \
151 { \
152 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) << "]" ); \
153 ++test_fails__; \
154 } \
155 /* [/BOOST_UBLAS_TEST_CHECK_REL_PRECISION] */
156
157
158/// Check that elements of \a x and \a y are equal.
159#define BOOST_UBLAS_TEST_CHECK_VECTOR_EQ(x,y,n) /* [BOOST_UBLAS_TEST_CHECK_VECTOR_EQ] */ \
160 if (BOOST_UBLAS_TEST_EXPAND_(n) > 0) \
161 { \
162 unsigned long n__ = BOOST_UBLAS_TEST_EXPAND_(n); \
163 for (unsigned long i__ = n__; i__ > 0; --i__) \
164 { \
165 if (!(BOOST_UBLAS_TEST_EXPAND_(x)[n__-i__]==BOOST_UBLAS_TEST_EXPAND_(y)[n__-i__])) \
166 { \
167 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__ << "]" ); \
168 ++test_fails__; \
169 } \
170 } \
171 } \
172 /* [/BOOST_UBLAS_TEST_CHECK_VECTOR_EQ] */
173
174
175/// Check that elements of \a x and \a y are close with respect to a given precision.
176#define BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE(x,y,n,e) /* [BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE] */ \
177 if (BOOST_UBLAS_TEST_EXPAND_(n) > 0) \
178 { \
179 unsigned long n__ = BOOST_UBLAS_TEST_EXPAND_(n); \
180 for (unsigned long i__ = n__; i__ > 0; --i__) \
181 { \
182 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))) \
183 { \
184 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__ << "]" ); \
185 ++test_fails__; \
186 } \
187 } \
188 } \
189 /* [/BOOST_UBLAS_TEST_CHECK_VECTOR_CLOSE] */
190
191
192/// Check that elements of matrices \a x and \a y are equal.
193#define BOOST_UBLAS_TEST_CHECK_MATRIX_EQ(x,y,nr,nc) /* [BOOST_UBLAS_TEST_CHECK_MATRIX_EQ] */ \
194 for (unsigned long i__ = 0; i__ < BOOST_UBLAS_TEST_EXPAND_(nr); ++i__) \
195 { \
196 for (unsigned long j__ = 0; j__ < BOOST_UBLAS_TEST_EXPAND_(nc); ++j__) \
197 { \
198 if (!(BOOST_UBLAS_TEST_EXPAND_(x)(i__,j__)==BOOST_UBLAS_TEST_EXPAND_(y)(i__,j__))) \
199 { \
200 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) << "]" ); \
201 } \
202 } \
203 } \
204 /* [/BOOST_UBLAS_TEST_CHECK_MATRIX_EQ] */
205
206
207/// Check that elements of matrices \a x and \a y are close with respect to a given precision.
208#define BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE(x,y,nr,nc,e) /* [BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE] */ \
209 for (unsigned long i__ = 0; i__ < BOOST_UBLAS_TEST_EXPAND_(nr); ++i__) \
210 { \
211 for (unsigned long j__ = 0; j__ < BOOST_UBLAS_TEST_EXPAND_(nc); ++j__) \
212 { \
213 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))) \
214 { \
215 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) << "]" ); \
216 } \
217 } \
218 } \
219 /* [/BOOST_UBLAS_TEST_CHECK_MATRIX_CLOSE] */
220
221
222///< Output the error message \a x.
223#define BOOST_UBLAS_TEST_ERROR(x) ::std::cerr << "[Error (" << __FILE__ << ":" << __func__ << ":" << __LINE__ << ")>> " << BOOST_UBLAS_TEST_EXPAND_(x) << ::std::endl
224
225#endif // BOOST_NUMERIC_UBLAS_TEST_UTILS_HPP