.. _program_listing_file_sophus_test_macros.hpp: Program Listing for File test_macros.hpp ======================================== |exhale_lsh| :ref:`Return to documentation for file ` (``sophus/test_macros.hpp``) .. |exhale_lsh| unicode:: U+021B0 .. UPWARDS ARROW WITH TIP LEFTWARDS .. code-block:: cpp #pragma once #include #include #include namespace Sophus { namespace details { template class Pretty { public: static std::string impl(Scalar s) { std::stringstream sstr; sstr << s; return sstr.str(); } }; template class Pretty::value>> { public: static std::string impl(Ptr ptr) { std::stringstream sstr; sstr << std::intptr_t(ptr); return sstr.str(); } }; template class Pretty, void> { public: static std::string impl(Matrix const& v) { std::stringstream sstr; sstr << "\n" << v << "\n"; return sstr.str(); } }; template std::string pretty(T const& v) { return Pretty::impl(v); } template void testFailed(bool& passed, char const* func, char const* file, int line, std::string const& msg) { std::fprintf(stderr, "Test failed in function '%s', " "file '%s', line %d.\n", func, file, line); std::cerr << msg << "\n\n"; passed = false; } } // namespace details void processTestResult(bool passed) { if (!passed) { // LCOV_EXCL_START std::cerr << "failed!" << std::endl << std::endl; exit(-1); // LCOV_EXCL_STOP } std::cerr << "passed." << std::endl << std::endl; } } // namespace Sophus #define SOPHUS_STRINGIFY(x) #x #define SOPHUS_TEST(passed, condition, descr, ...) \ do { \ if (!(condition)) { \ std::string msg = SOPHUS_FMT_STR("condition ``{}`` is false\n", \ SOPHUS_STRINGIFY(condition)); \ msg += SOPHUS_FMT_STR(descr, ##__VA_ARGS__); \ Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \ msg); \ } \ } while (false) #define SOPHUS_TEST_EQUAL(passed, left, right, descr, ...) \ do { \ if (left != right) { \ std::string msg = SOPHUS_FMT_STR( \ "{} (={}) is not equal to {} (={})\n", SOPHUS_STRINGIFY(left), \ Sophus::details::pretty(left), SOPHUS_STRINGIFY(right), \ Sophus::details::pretty(right)); \ msg += SOPHUS_FMT_STR(descr, ##__VA_ARGS__); \ Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \ msg); \ } \ } while (false) #define SOPHUS_TEST_NEQ(passed, left, right, descr, ...) \ do { \ if (left == right) { \ std::string msg = SOPHUS_FMT_STR( \ "{} (={}) should not be equal to {} (={})\n", \ SOPHUS_STRINGIFY(left), Sophus::details::pretty(left), \ SOPHUS_STRINGIFY(right), Sophus::details::pretty(right)); \ msg += SOPHUS_FMT_STR(descr, ##__VA_ARGS__); \ Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \ msg); \ } \ } while (false) #define SOPHUS_TEST_APPROX(passed, left, right, thr, descr, ...) \ do { \ auto nrm = Sophus::maxMetric((left), (right)); \ if (!(nrm < (thr))) { \ std::string msg = SOPHUS_FMT_STR( \ "{} (={}) is not approx {} (={}); {} is {}; nrm is {}\n", \ SOPHUS_STRINGIFY(left), Sophus::details::pretty(left), \ SOPHUS_STRINGIFY(right), Sophus::details::pretty(right), \ SOPHUS_STRINGIFY(thr), Sophus::details::pretty(thr), \ Sophus::details::pretty(nrm)); \ msg += SOPHUS_FMT_STR(descr, ##__VA_ARGS__); \ Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \ msg); \ } \ } while (false) #define SOPHUS_TEST_NOT_APPROX(passed, left, right, thr, descr, ...) \ do { \ auto nrm = Sophus::maxMetric((left), (right)); \ if (nrm < (thr)) { \ std::string msg = SOPHUS_FMT_STR( \ "{} (={}) is approx {} (={}), but it should not!\n {} is {}; nrm " \ "is {}\n", \ SOPHUS_STRINGIFY(left), Sophus::details::pretty(left), \ SOPHUS_STRINGIFY(right), Sophus::details::pretty(right), \ SOPHUS_STRINGIFY(thr), Sophus::details::pretty(thr), \ Sophus::details::pretty(nrm)); \ msg += SOPHUS_FMT_STR(descr, ##__VA_ARGS__); \ Sophus::details::testFailed(passed, SOPHUS_FUNCTION, __FILE__, __LINE__, \ msg); \ } \ } while (false)