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  // LCOV_EXCL_START
41  std::cerr << "failed!" << std::endl << std::endl;
42  exit(-1);
43  // LCOV_EXCL_STOP
44  }
45  std::cerr << "passed." << std::endl << std::endl;
46 }
47 } // namespace Sophus
48 
49 #define SOPHUS_STRINGIFY(x) #x
50 
53 #define SOPHUS_TEST(passed, condition, ...) \
54  do { \
55  if (!(condition)) { \
56  std::string msg = Sophus::details::FormatString( \
57  "condition ``%`` is false\n", SOPHUS_STRINGIFY(condition)); \
58  msg += Sophus::details::FormatString(__VA_ARGS__); \
59  Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \
60  msg); \
61  } \
62  } while (false)
63 
66 #define SOPHUS_TEST_EQUAL(passed, left, right, ...) \
67  do { \
68  if (left != right) { \
69  std::string msg = Sophus::details::FormatString( \
70  "% (=%) is not equal to % (=%)\n", SOPHUS_STRINGIFY(left), \
71  Sophus::details::pretty(left), SOPHUS_STRINGIFY(right), \
72  Sophus::details::pretty(right)); \
73  msg += Sophus::details::FormatString(__VA_ARGS__); \
74  Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \
75  msg); \
76  } \
77  } while (false)
78 
81 #define SOPHUS_TEST_NEQ(passed, left, right, ...) \
82  do { \
83  if (left == right) { \
84  std::string msg = Sophus::details::FormatString( \
85  "% (=%) shoudl not be equal to % (=%)\n", SOPHUS_STRINGIFY(left), \
86  Sophus::details::pretty(left), SOPHUS_STRINGIFY(right), \
87  Sophus::details::pretty(right)); \
88  msg += Sophus::details::FormatString(__VA_ARGS__); \
89  Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \
90  msg); \
91  } \
92  } while (false)
93 
96 #define SOPHUS_TEST_APPROX(passed, left, right, thr, ...) \
97  do { \
98  auto nrm = Sophus::maxMetric((left), (right)); \
99  if (!(nrm < (thr))) { \
100  std::string msg = Sophus::details::FormatString( \
101  "% (=%) is not approx % (=%); % is %; nrm is %\n", \
102  SOPHUS_STRINGIFY(left), Sophus::details::pretty(left), \
103  SOPHUS_STRINGIFY(right), Sophus::details::pretty(right), \
104  SOPHUS_STRINGIFY(thr), Sophus::details::pretty(thr), nrm); \
105  msg += Sophus::details::FormatString(__VA_ARGS__); \
106  Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \
107  msg); \
108  } \
109  } while (false)
110 
113 #define SOPHUS_TEST_NOT_APPROX(passed, left, right, thr, ...) \
114  do { \
115  auto nrm = Sophus::maxMetric((left), (right)); \
116  if (nrm < (thr)) { \
117  std::string msg = Sophus::details::FormatString( \
118  "% (=%) is approx % (=%), but it should not!\n % is %; nrm is %\n", \
119  SOPHUS_STRINGIFY(left), Sophus::details::pretty(left), \
120  SOPHUS_STRINGIFY(right), Sophus::details::pretty(right), \
121  SOPHUS_STRINGIFY(thr), Sophus::details::pretty(thr), nrm); \
122  msg += Sophus::details::FormatString(__VA_ARGS__); \
123  Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \
124  msg); \
125  } \
126  } while (false)
127 
128 #endif // SOPUHS_TESTS_MACROS_HPP
Eigen
Definition: rxso2.hpp:16
types.hpp
Sophus::details::testFailed
void testFailed(bool &passed, char const *func, char const *file, int line, std::string const &msg)
Definition: test_macros.hpp:29
Sophus::Matrix
Eigen::Matrix< Scalar, M, N > Matrix
Definition: types.hpp:41
Sophus
Definition: average.hpp:17
Sophus::processTestResult
void processTestResult(bool passed)
Definition: test_macros.hpp:38
Sophus::details::Pretty< Eigen::Matrix< Scalar, M, N > >::impl
static std::string impl(Matrix< Scalar, M, N > const &v)
Definition: test_macros.hpp:18
Sophus::details::Pretty::impl
static std::string impl(Scalar s)
Definition: test_macros.hpp:12
Sophus::details::Pretty
Definition: test_macros.hpp:10
Sophus::details::pretty
std::string pretty(T const &v)
Definition: test_macros.hpp:24
Sophus::details::FormatString
std::string FormatString(char const *text, Args &&... args)
Definition: common.hpp:91


sophus
Author(s): Hauke Strasdat
autogenerated on Wed Mar 2 2022 01:01:48