mlpack  master
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 // This is only necessary for pre-1.36 Boost.Test.
19 #if BOOST_VERSION < 103600
20 
21 #include <boost/test/floating_point_comparison.hpp>
22 #include <boost/test/auto_unit_test.hpp>
23 
24 // This depends on other macros. Probably not a great idea... but it works, and
25 // we only need it for ancient Boost versions.
26 #define BOOST_REQUIRE_GE( L, R ) \
27  BOOST_REQUIRE_EQUAL( (L >= R), true )
28 
29 #define BOOST_REQUIRE_NE( L, R ) \
30  BOOST_REQUIRE_EQUAL( (L != R), true )
31 
32 #define BOOST_REQUIRE_LE( L, R ) \
33  BOOST_REQUIRE_EQUAL( (L <= R), true )
34 
35 #define BOOST_REQUIRE_LT( L, R ) \
36  BOOST_REQUIRE_EQUAL( (L < R), true )
37 
38 #define BOOST_REQUIRE_GT( L, R ) \
39  BOOST_REQUIRE_EQUAL( (L > R), true )
40 
41 #endif
42 
43 // Require the approximation L to be within a relative error of E respect to the
44 // actual value R.
45 #define REQUIRE_RELATIVE_ERR( L, R, E ) \
46  BOOST_REQUIRE_LE( std::abs((R) - (L)), (E) * std::abs(R))
47 
48 // Check the values of two matrices.
49 inline void CheckMatrices(arma::mat& a, arma::mat& b)
50 {
51  BOOST_REQUIRE_EQUAL(a.n_rows, b.n_rows);
52  BOOST_REQUIRE_EQUAL(a.n_cols, b.n_cols);
53 
54  for (size_t i = 0; i < a.n_elem; ++i)
55  {
56  if (std::abs(a[i]) < 1e-5)
57  BOOST_REQUIRE_SMALL(b[i], 1e-5);
58  else
59  BOOST_REQUIRE_CLOSE(a[i], b[i], 1e-5);
60  }
61 }
62 
63 // Check the values of two unsigned matrices.
64 inline void CheckMatrices(arma::Mat<size_t>& a, arma::Mat<size_t>& b)
65 {
66  BOOST_REQUIRE_EQUAL(a.n_rows, b.n_rows);
67  BOOST_REQUIRE_EQUAL(a.n_cols, b.n_cols);
68 
69  for (size_t i = 0; i < a.n_elem; ++i)
70  BOOST_REQUIRE_EQUAL(a[i], b[i]);
71 }
72 
73 #endif
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
void CheckMatrices(arma::mat &a, arma::mat &b)
Definition: test_tools.hpp:49