test_catch_tools.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_TESTS_TEST_CATCH_TOOLS_HPP
13 #define MLPACK_TESTS_TEST_CATCH_TOOLS_HPP
14 
15 #include <mlpack/core.hpp>
16 #include <boost/version.hpp>
17 
18 #include "catch.hpp"
19 
20 // Require the approximation L to be within a relative error of E respect to the
21 // actual value R.
22 #define REQUIRE_RELATIVE_ERR(L, R, E) \
23  REQUIRE(std::abs((R) - (L)) <= (E) * std::abs(R))
24 
25 // Check the values of two matrices.
26 inline void CheckMatrices(const arma::mat& a,
27  const arma::mat& b,
28  double tolerance = 1e-5)
29 {
30  REQUIRE(a.n_rows == b.n_rows);
31  REQUIRE(a.n_cols == b.n_cols);
32 
33  for (size_t i = 0; i < a.n_elem; ++i)
34  {
35  if (std::abs(a[i]) < tolerance / 2)
36  REQUIRE(b[i] == Approx(0.0).margin(tolerance / 2));
37  else
38  REQUIRE(a[i] == Approx(b[i]).epsilon(tolerance / 100));
39  }
40 }
41 
42 // Check the values of two unsigned matrices.
43 inline void CheckMatrices(const arma::Mat<size_t>& a,
44  const arma::Mat<size_t>& b)
45 {
46  REQUIRE(a.n_rows == b.n_rows);
47  REQUIRE(a.n_cols == b.n_cols);
48 
49  for (size_t i = 0; i < a.n_elem; ++i)
50  REQUIRE(a[i] == b[i]);
51 }
52 
53 // Check the values of two cubes.
54 inline void CheckMatrices(const arma::cube& a,
55  const arma::cube& b,
56  double tolerance = 1e-5)
57 {
58  REQUIRE(a.n_rows == b.n_rows);
59  REQUIRE(a.n_cols == b.n_cols);
60  REQUIRE(a.n_slices == b.n_slices);
61 
62  for (size_t i = 0; i < a.n_elem; ++i)
63  {
64  if (std::abs(a[i]) < tolerance / 2)
65  REQUIRE(b[i] == Approx(0.0).margin(tolerance / 2));
66  else
67  REQUIRE(a[i] == Approx(b[i]).epsilon(tolerance / 100));
68  }
69 }
70 
71 // Check if two matrices are different.
72 inline void CheckMatricesNotEqual(const arma::mat& a,
73  const arma::mat& b,
74  double tolerance = 1e-5)
75 {
76  bool areDifferent = false;
77 
78  // Only check the elements if the dimensions are equal.
79  if (a.n_rows == b.n_rows && a.n_cols == b.n_cols)
80  {
81  for (size_t i = 0; i < a.n_elem; ++i)
82  {
83  if (std::abs(a[i]) < tolerance / 2 &&
84  b[i] > tolerance / 2)
85  {
86  areDifferent = true;
87  break;
88  }
89  else if (std::abs(a[i] - b[i]) > tolerance)
90  {
91  areDifferent = true;
92  break;
93  }
94  }
95  }
96  else
97  areDifferent = true;
98 
99  if (!areDifferent)
100  FAIL("The matrices are equal.");
101 }
102 
103 // Check if two unsigned matrices are different.
104 inline void CheckMatricesNotEqual(const arma::Mat<size_t>& a,
105  const arma::Mat<size_t>& b)
106 {
107  bool areDifferent = false;
108 
109  // Only check the elements if the dimensions are equal.
110  if (a.n_rows == b.n_rows && a.n_cols == b.n_cols)
111  {
112  for (size_t i = 0; i < a.n_elem; ++i)
113  {
114  if (a[i] != b[i])
115  {
116  areDifferent = true;
117  break;
118  }
119  }
120  }
121  else
122  areDifferent = true;
123 
124  if (!areDifferent)
125  FAIL("The matrices are equal.");
126 }
127 
128 // Check if two cubes are different.
129 inline void CheckMatricesNotEqual(const arma::cube& a,
130  const arma::cube& b,
131  double tolerance = 1e-5)
132 {
133  bool areDifferent = false;
134 
135  // Only check the elements if the dimensions are equal.
136  if (a.n_rows == b.n_rows && a.n_cols == b.n_cols &&
137  a.n_slices == b.n_slices)
138  {
139  for (size_t i = 0; i < a.n_elem; ++i)
140  {
141  if (std::abs(a[i]) < tolerance / 2 &&
142  b[i] > tolerance / 2)
143  {
144  areDifferent = true;
145  break;
146  }
147  else if (std::abs(a[i] - b[i]) > tolerance)
148  {
149  areDifferent = true;
150  break;
151  }
152  }
153  }
154  else
155  areDifferent = true;
156 
157  if (!areDifferent)
158  FAIL("The matrices are equal.");
159 }
160 
161 // Filter typeinfo string to generate unique filenames for serialization tests.
162 inline std::string FilterFileName(const std::string& inputString)
163 {
164  // Take the last valid 32 characters for the filename.
165  std::string fileName;
166  for (auto it = inputString.rbegin(); it != inputString.rend() &&
167  fileName.size() != 32; ++it)
168  {
169  if (std::isalnum(*it))
170  fileName.push_back(*it);
171  }
172 
173  return fileName;
174 }
175 
176 #endif
void CheckMatrices(const arma::mat &a, const arma::mat &b, double tolerance=1e-5)
std::string FilterFileName(const std::string &inputString)
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...
void CheckMatricesNotEqual(const arma::mat &a, const arma::mat &b, double tolerance=1e-5)