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