JayBeams  0.1
Another project to have fun coding.
ut_check_close_enough.cpp
Go to the documentation of this file.
2 
3 #include <boost/test/unit_test.hpp>
4 #include <complex>
5 
6 /**
7  * @test check_close_enough functionality for integer type
8  *
9  * Usually I do not spend time testing helper functions for tests, but
10  * in this case the helper has a lot of template code and we want to
11  * validate this before too long.
12  */
13 BOOST_AUTO_TEST_CASE(check_close_enough_integer) {
14  using namespace jb::testing;
15  using test_type = int;
16  int tol = 1;
17  test_type a = 10;
18  test_type b = 11;
19  test_type c = a + 2 * tol;
20  BOOST_CHECK_MESSAGE(
21  check_close_enough(a, b, tol),
22  "a=" << a << ", and b=" << b << " are not within tolerance=" << tol);
23  BOOST_CHECK_MESSAGE(
24  not check_close_enough(a, c, tol),
25  "a=" << a << ", and c=" << c << " are within tolerance=" << tol);
26 }
27 
28 /**
29  * @test check_close_enough functionality for float type
30  */
31 BOOST_AUTO_TEST_CASE(check_close_enough_float) {
32  using namespace jb::testing;
33  using test_type = float;
34  int tol = 3;
35  test_type a = 10.00;
36  test_type b = a + std::numeric_limits<test_type>::epsilon();
37  test_type c = a + 10 * tol * std::numeric_limits<test_type>::epsilon();
38  BOOST_CHECK_MESSAGE(
39  check_close_enough(a, b, tol),
40  "a=" << a << ", and b=" << b << " are not within tolerance=" << tol);
41  BOOST_CHECK_MESSAGE(
42  not check_close_enough(a, c, tol),
43  "a=" << a << ", and c=" << c << " are within tolerance=" << tol);
44 }
45 
46 /**
47  * @test check_close_enough functionality for double type
48  */
49 BOOST_AUTO_TEST_CASE(check_close_enough_double) {
50  using namespace jb::testing;
51  using test_type = double;
52  int tol = 3;
53  test_type a = 10.00;
54  test_type b = a + std::numeric_limits<test_type>::epsilon();
55  test_type c = a + 10 * tol * std::numeric_limits<test_type>::epsilon();
56  BOOST_CHECK_MESSAGE(
57  check_close_enough(a, b, tol),
58  "a=" << a << ", and b=" << b << " are not within tolerance=" << tol);
59  BOOST_CHECK_MESSAGE(
60  not check_close_enough(a, c, tol),
61  "a=" << a << ", and c=" << c << " are within tolerance=" << tol);
62 }
63 
64 /**
65  * @test check_close_enough functionality for complex of integer type
66  */
67 BOOST_AUTO_TEST_CASE(check_close_enough_complex_integer) {
68  using namespace jb::testing;
69  using value_type = int;
70  using test_type = std::complex<value_type>;
71  int tol = 3;
72  test_type a = {10, 5};
73  test_type com_eps = {tol, tol};
74  test_type b = a + com_eps;
75  test_type c = 2 * com_eps + a;
76  BOOST_CHECK_MESSAGE(
77  check_close_enough(a, b, tol),
78  "a=" << a << ", and b=" << b << " are not within tolerance=" << tol);
79  BOOST_CHECK_MESSAGE(
80  not check_close_enough(a, c, tol),
81  "a=" << a << ", and c=" << c << " are within tolerance=" << tol);
82 }
83 
84 /**
85  * @test check_close_enough functionality for vector of float type
86  */
87 BOOST_AUTO_TEST_CASE(check_close_enough_float_vector) {
88  using namespace jb::testing;
89  using value_type = float;
90  using test_type = std::vector<value_type>;
91  int tol = 3;
92  int nsamples = 20;
93  value_type num_a = 10.00;
94  test_type a(nsamples, num_a);
95  value_type num_b = num_a + std::numeric_limits<value_type>::epsilon();
96  test_type b(nsamples, num_b);
97  BOOST_CHECK_MESSAGE(
99  "a and b are not within tolerance=" << tol);
100 }
101 
102 /**
103  * @test check_close_enough functionality for vector of float type
104  *
105  * TODO(#93) cleanup after boost-1.58.0 is no longer supported.
106  */
107 #if BOOST_VERSION <= 105800
109  check_close_enough_float_vector_failure, JB_TESTING_MAX_DIFFERENCES_PRINTED)
110 
111 BOOST_AUTO_TEST_CASE(check_close_enough_float_vector_failure)
112 #else
114  check_close_enough_float_vector_failure,
115  *boost::unit_test::expected_failures(JB_TESTING_MAX_DIFFERENCES_PRINTED))
116 #endif // BOOST_VERSION <= 105800
117 {
118  using namespace jb::testing;
119  using value_type = float;
120  using test_type = std::vector<value_type>;
121  int tol = 3;
122  int nsamples = 20;
123  value_type num_a = 10.00;
124  test_type a(nsamples, num_a);
125  value_type num_b =
126  num_a +
127  (value_type)(10 * tol) * std::numeric_limits<value_type>::epsilon();
128  test_type b(nsamples, num_b);
129  BOOST_CHECK_MESSAGE(
130  not check_collection_close_enough(a, b, tol),
131  "a and c are within tolerance=" << tol);
132 }
133 
134 /**
135  * @test check_close_enough functionality for multi_array of 3 dimension
136  * complex double type
137  *
138  * TODO(#93) cleanup after boost-1.58.0 is no longer supported.
139  */
140 #if BOOST_VERSION <= 105800
142  check_close_enough_complex_double_multi_array,
144 
145 BOOST_AUTO_TEST_CASE(check_close_enough_complex_double_multi_array)
146 #else
148  check_close_enough_complex_double_multi_array,
149  *boost::unit_test::expected_failures(JB_TESTING_MAX_DIFFERENCES_PRINTED))
150 #endif // BOOST_VERSION <= 105800
151 {
152  using namespace jb::testing;
153  using value_type = double;
154  using complex_type = std::complex<value_type>;
155  using test_type = boost::multi_array<complex_type, 3>;
156  int tol = 3;
157  int S = 20;
158  int V = 4;
159  int nsamples = 2000;
160  complex_type num_a = {10.00, 5.00};
161  complex_type com_eps = {std::numeric_limits<value_type>::epsilon(),
162  std::numeric_limits<value_type>::epsilon()};
163  complex_type num_b = num_a + com_eps;
164  complex_type num_c = num_a + 10.0 * tol * com_eps;
165 
166  test_type a(boost::extents[S][V][nsamples]);
167  test_type b(boost::extents[S][V][nsamples]);
168  test_type c(boost::extents[S][V][nsamples]);
169 
170  for (int i = 0; i != S; ++i) {
171  for (int j = 0; j != V; ++j) {
172  for (int k = 0; k != nsamples; ++k) {
173  a[i][j][k] = num_a;
174  b[i][j][k] = num_b;
175  c[i][j][k] = num_c;
176  }
177  }
178  }
179 
180  BOOST_CHECK_MESSAGE(
182  "a and b are not within tolerance=" << tol);
183  BOOST_CHECK_MESSAGE(
184  not check_collection_close_enough(a, c, tol),
185  "a and c are within tolerance=" << tol);
186 }
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...
BOOST_AUTO_TEST_CASE(check_close_enough_integer)
std::size_t nsamples(container_type const &a)
Count the elements in the last dimension of a vector-like container.
Helper functions and classes to simplify unit tests.
#define JB_TESTING_MAX_DIFFERENCES_PRINTED
BOOST_AUTO_TEST_CASE_EXPECTED_FAILURES(check_close_enough_float_vector_failure, JB_TESTING_MAX_DIFFERENCES_PRINTED)
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...