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 // Filter typeinfo string to generate unique filenames for serialization tests.
70 inline std::string FilterFileName(const std::string& inputString)
71 {
72  // Take the last valid 32 characters for the filename.
73  std::string fileName;
74  for (auto it = inputString.rbegin(); it != inputString.rend() &&
75  fileName.size() != 32; ++it)
76  {
77  if (std::isalnum(*it))
78  fileName.push_back(*it);
79  }
80 
81  return fileName;
82 }
83 
84 #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:70
Include all of the base components required to write mlpack methods, and the main mlpack Doxygen docu...