Main MRPT website > C++ reference for MRPT 1.5.3
test_macros.hpp
Go to the documentation of this file.
1 #ifndef SOPUHS_TESTS_MACROS_HPP
2 #define SOPUHS_TESTS_MACROS_HPP
3 
4 #include <sophus/types.hpp>
5 
6 namespace Sophus {
7 namespace details {
8 
9 template <class Scalar>
10 class Pretty {
11  public:
12  static std::string impl(Scalar s) { return FormatString("%", s); }
13 };
14 
15 template <class Scalar, int M, int N>
16 class Pretty<Eigen::Matrix<Scalar, M, N>> {
17  public:
18  static std::string impl(Matrix<Scalar, M, N> const& v) {
19  return FormatString("\n%\n", v);
20  }
21 };
22 
23 template <class T>
24 std::string pretty(T const& v) {
25  return Pretty<T>::impl(v);
26 }
27 
28 template <class... Args>
29 void testFailed(bool& passed, char const* func, char const* file, int line,
30  std::string const& msg) {
31  std::cerr << FormatString("Test failed in function %, file %, line %\n", func,
32  file, line);
33  std::cerr << msg << "\n\n";
34  passed = false;
35 }
36 } // namespace details
37 
38 void processTestResult(bool passed) {
39  if (!passed) {
40  std::cerr << "failed!" << std::endl << std::endl;
41  exit(-1);
42  }
43  std::cerr << "passed." << std::endl << std::endl;
44 }
45 } // namespace Sophus
46 
47 #define SOPHUS_STRINGIFY(x) #x
48 
49 // GenericTests whether condition is true.
50 // The in-out parameter passed will be set to false if test fails.
51 #define SOPHUS_TEST(passed, condition, ...) \
52  do { \
53  if (!(condition)) { \
54  std::string msg = Sophus::details::FormatString( \
55  "condition ``%`` is false\n", SOPHUS_STRINGIFY(condition)); \
56  msg += Sophus::details::FormatString(__VA_ARGS__); \
57  Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \
58  msg); \
59  } \
60  } while (false)
61 
62 // GenericTests whether left is equal to right given a threshold.
63 // The in-out parameter passed will be set to false if test fails.
64 #define SOPHUS_TEST_EQUAL(passed, left, right, ...) \
65  do { \
66  if (left != right) { \
67  std::string msg = Sophus::details::FormatString( \
68  "% (=%) is not equal to % (=%)\n", SOPHUS_STRINGIFY(left), \
69  Sophus::details::pretty(left), SOPHUS_STRINGIFY(right), \
70  Sophus::details::pretty(right)); \
71  msg += Sophus::details::FormatString(__VA_ARGS__); \
72  Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \
73  msg); \
74  } \
75  } while (false)
76 
77 // GenericTests whether left is equal to right given a threshold.
78 // The in-out parameter passed will be set to false if test fails.
79 #define SOPHUS_TEST_NEQ(passed, left, right, ...) \
80  do { \
81  if (left == right) { \
82  std::string msg = Sophus::details::FormatString( \
83  "% (=%) shoudl not be equal to % (=%)\n", SOPHUS_STRINGIFY(left), \
84  Sophus::details::pretty(left), SOPHUS_STRINGIFY(right), \
85  Sophus::details::pretty(right)); \
86  msg += Sophus::details::FormatString(__VA_ARGS__); \
87  Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \
88  msg); \
89  } \
90  } while (false)
91 
92 // GenericTests whether left is approximatly equal to right given a threshold.
93 // The in-out parameter passed will be set to false if test fails.
94 #define SOPHUS_TEST_APPROX(passed, left, right, thr, ...) \
95  do { \
96  auto nrm = Sophus::metric((left), (right)); \
97  if (!(nrm < (thr))) { \
98  std::string msg = Sophus::details::FormatString( \
99  "% (=%) is not approx % (=%); % is % \n", SOPHUS_STRINGIFY(left), \
100  Sophus::details::pretty(left), SOPHUS_STRINGIFY(right), \
101  Sophus::details::pretty(right), SOPHUS_STRINGIFY(thr), \
102  Sophus::details::pretty(thr)); \
103  msg += Sophus::details::FormatString(__VA_ARGS__); \
104  Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \
105  msg); \
106  } \
107  } while (false)
108 
109 #endif // SOPUHS_TESTS_MACROS_HPP
void testFailed(bool &passed, char const *func, char const *file, int line, std::string const &msg)
Definition: test_macros.hpp:29
std::string FormatString(char const *text, Args &&... args)
Definition: common.hpp:86
std::string pretty(T const &v)
Definition: test_macros.hpp:24
Eigen::Matrix< Scalar, M, N > Matrix
Definition: types.hpp:38
static std::string impl(Scalar s)
Definition: test_macros.hpp:12
void processTestResult(bool passed)
Definition: test_macros.hpp:38
static std::string impl(Matrix< Scalar, M, N > const &v)
Definition: test_macros.hpp:18



Page generated by Doxygen 1.8.13 for MRPT 1.5.3 at Tue Oct 31 07:27:35 UTC 2017