1 #ifndef jb_testing_check_close_enough_hpp 2 #define jb_testing_check_close_enough_hpp 4 #include <boost/multi_array.hpp> 5 #include <boost/test/unit_test.hpp> 14 #ifndef JB_TESTING_MAX_DIFFERENCES_PRINTED 15 #define JB_TESTING_MAX_DIFFERENCES_PRINTED 8 16 #endif // JB_TESTING_MAX_DIFFERENCES_PRINTED 33 typename std::enable_if<std::is_integral<value_t>::value>::type* =
nullptr>
35 using value_type = value_t;
36 value_type value_tol =
static_cast<value_type
>(tol);
40 if ((num_a < num_b - value_tol) or (num_a > num_b + value_tol)) {
63 typename std::enable_if<std::is_floating_point<value_t>::value>::type* =
66 using value_type = value_t;
67 value_type
const eps = tol * std::numeric_limits<value_type>::epsilon();
68 if (std::abs(num_a) <= eps) {
69 return std::abs(num_b) <= eps;
71 return std::abs((num_a - num_b) / num_b) <= eps;
94 template <
typename value_t>
96 std::complex<value_t> com_a, std::complex<value_t> com_b,
int tol = 1) {
114 template <
typename value_t>
124 template <
typename floating>
126 BOOST_CHECK_SMALL(t, floating(small));
132 template <
typename real>
134 BOOST_CHECK_SMALL(t.real(), real(small));
135 BOOST_CHECK_SMALL(t.imag(), real(small));
139 template <
typename precision_t>
140 std::complex<precision_t>
format(precision_t v[2]) {
141 return std::complex<precision_t>(v[0], v[1]);
145 template <
typename value_type>
161 typename real,
typename std::enable_if<
162 std::is_floating_point<real>::value>::type* =
nullptr>
164 if (std::numeric_limits<real>::is_integer) {
165 return std::abs(actual - expected);
167 if (std::abs(expected) < std::numeric_limits<real>::epsilon()) {
168 return std::abs(actual);
170 return std::abs((actual - expected) / expected);
185 typename std::enable_if<std::is_integral<integral>::value>::type* =
nullptr>
187 if (actual > expected) {
188 return actual - expected;
190 return expected - actual;
204 template <
typename real>
223 template <
typename real>
258 template <
typename collection_t>
260 collection_t
const& a, collection_t
const& b,
int tol = 1,
262 BOOST_CHECK_EQUAL(a.size(), b.size());
263 if (a.size() != b.size()) {
268 for (std::size_t i = 0; i != a.size(); ++i) {
272 if (++count <= max_differences_printed) {
275 "in item i=" << i <<
" difference higher than tolerance=" << tol
276 <<
", actual[i]=" <<
format(a[i]) <<
", expected[i]=" 298 template <
typename T, std::
size_t K>
300 boost::multi_array<T, K>
const& a, boost::multi_array<T, K>
const& b,
303 BOOST_CHECK_EQUAL(a.num_elements(), b.num_elements());
304 if (a.num_elements() != b.num_elements()) {
309 for (std::size_t i = 0; i != a.num_elements(); ++i) {
313 if (++count <= max_differences_printed) {
316 "in item i=" << i <<
" difference higher than tolerance=" << tol
317 <<
", actual[i]=" <<
format(a.data()[i])
318 <<
", expected[i]=" <<
format(b.data()[i])
319 <<
", error=" <<
error);
341 template <
typename value_t>
343 std::size_t
size, value_t
const* a, value_t
const* b,
int tol = 1,
346 for (std::size_t i = 0; i !=
size; ++i) {
350 if (++count <= max_differences_printed) {
353 "in item i=" << i <<
" difference higher than tolerance=" << tol
354 <<
", a[i]=" <<
format(a[i]) <<
", b[i]=" <<
format(b[i])
355 <<
", error=" <<
error);
364 #endif // jb_testing_check_close_enough_hpp
real relative_error(real actual, real expected)
Calculate the relative error between two float point numbers.
void check_small(floating t, double small)
Verify that a floating point value is "close enough" to a small number.
std::complex< precision_t > format(precision_t v[2])
Wrap FFTW-style complex numbers in std::complex for iostreaming.
bool check_collection_close_enough(collection_t const &a, collection_t const &b, int tol=1, int max_differences_printed=JB_TESTING_MAX_DIFFERENCES_PRINTED)
Given two collections of numbers of the same value type, find the differences that are out of a given...
#define JB_TESTING_MAX_DIFFERENCES_PRINTED
The top-level namespace for the JayBeams library.
bool check_close_enough(value_t num_a, value_t num_b, int tol)
Given two numbers of the same integer type check if the difference is within a given tolerance...