Test.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* ----------------------------------------------------------------------------
  2. * GTSAM Copyright 2010, Georgia Tech Research Corporation,
  3. * Atlanta, Georgia 30332-0415
  4. * All Rights Reserved
  5. * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
  6. * See LICENSE for the license information
  7. * -------------------------------------------------------------------------- */
  8. ///////////////////////////////////////////////////////////////////////////////
  9. //
  10. // TEST.H
  11. //
  12. // This file contains the Test class along with the macros which make effective
  13. // in the harness.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #ifndef TEST_H
  17. #define TEST_H
  18. #include <cmath>
  19. #include <boost/lexical_cast.hpp>
  20. class TestResult;
  21. class Test
  22. {
  23. public:
  24. Test (const std::string& testName);
  25. Test (const std::string& testName, const std::string& filename, long lineNumber, bool safeCheck);
  26. virtual ~Test() {};
  27. virtual void run (TestResult& result) = 0;
  28. void setNext(Test *test);
  29. Test *getNext () const;
  30. std::string getName() const {return name_;}
  31. std::string getFilename() const {return filename_;}
  32. long getLineNumber() const {return lineNumber_;}
  33. bool safe() const {return safeCheck_;}
  34. protected:
  35. bool check (long expected, long actual, TestResult& result, const std::string& fileName, long lineNumber);
  36. bool check (const std::string& expected, const std::string& actual, TestResult& result, const std::string& fileName, long lineNumber);
  37. std::string name_;
  38. Test *next_;
  39. std::string filename_;
  40. long lineNumber_; /// This is the line line number of the test, rather than the a single check
  41. bool safeCheck_;
  42. };
  43. /**
  44. * Normal test will wrap execution in a try/catch block to catch exceptions more effectively
  45. */
  46. #define TEST(testGroup, testName)\
  47. class testGroup##testName##Test : public Test \
  48. { public: testGroup##testName##Test () : Test (#testName "Test", __FILE__, __LINE__, true) {} \
  49. virtual ~testGroup##testName##Test () {};\
  50. void run (TestResult& result_) override;} \
  51. testGroup##testName##Instance; \
  52. void testGroup##testName##Test::run (TestResult& result_)
  53. /**
  54. * Declare friend in a class to test its private methods
  55. */
  56. #define FRIEND_TEST(testGroup, testName) \
  57. friend class testGroup##testName##Test;
  58. /**
  59. * For debugging only: use TEST_UNSAFE to allow debuggers to have access to exceptions, as this
  60. * will not wrap execution with a try/catch block
  61. */
  62. #define TEST_UNSAFE(testGroup, testName)\
  63. class testGroup##testName##Test : public Test \
  64. { public: testGroup##testName##Test () : Test (#testName "Test", __FILE__, __LINE__, false) {} \
  65. virtual ~testGroup##testName##Test () {};\
  66. void run (TestResult& result_) override;} \
  67. testGroup##testName##Instance; \
  68. void testGroup##testName##Test::run (TestResult& result_)
  69. /**
  70. * Use this to disable unwanted tests without commenting them out.
  71. */
  72. #define TEST_DISABLED(testGroup, testName)\
  73. void testGroup##testName##Test(TestResult& result_, const std::string& name_)
  74. /*
  75. * Convention for tests:
  76. * - "EXPECT" is a test that will not end execution of the series of tests
  77. * - Otherwise, upon a failure, the test will end
  78. *
  79. * Usage:
  80. * EXPECT is useful when checking several different parts of an condition so
  81. * that a failure of one check won't hide another failure.
  82. *
  83. * Note: Exception tests are not available in a EXPECT form, as exceptions rarely
  84. * fit the criteria of an assertion that does not need to be true to continue
  85. */
  86. /* True ASSERTs: tests end at first failure */
  87. #define CHECK(condition)\
  88. { if (!(condition)) \
  89. { result_.addFailure (Failure (name_, __FILE__,__LINE__, #condition)); return; } }
  90. #define THROWS_EXCEPTION(condition)\
  91. { try { condition; \
  92. result_.addFailure (Failure (name_, __FILE__,__LINE__, std::string("Didn't throw: ") + boost::lexical_cast<std::string>(#condition))); \
  93. return; } \
  94. catch (...) {} }
  95. #define CHECK_EXCEPTION(condition, exception_name)\
  96. { try { condition; \
  97. result_.addFailure (Failure (name_, __FILE__,__LINE__, std::string("Didn't throw: ") + boost::lexical_cast<std::string>(#condition))); \
  98. return; } \
  99. catch (exception_name&) {} \
  100. catch (...) { \
  101. result_.addFailure (Failure (name_, __FILE__,__LINE__, std::string("Wrong exception: ") + boost::lexical_cast<std::string>(#condition) + boost::lexical_cast<std::string>(", expected: ") + boost::lexical_cast<std::string>(#exception_name))); \
  102. return; } }
  103. #define EQUALITY(expected,actual)\
  104. { if (!assert_equal(expected,actual)) \
  105. result_.addFailure(Failure(name_, __FILE__, __LINE__, #expected, #actual)); }
  106. #define CHECK_EQUAL(expected,actual)\
  107. { if ((expected) == (actual)) return; result_.addFailure(Failure(name_, __FILE__, __LINE__, boost::lexical_cast<std::string>(expected), boost::lexical_cast<std::string>(actual))); }
  108. #define LONGS_EQUAL(expected,actual)\
  109. { long actualTemp = actual; \
  110. long expectedTemp = expected; \
  111. if ((expectedTemp) != (actualTemp)) \
  112. { result_.addFailure (Failure (name_, __FILE__, __LINE__, boost::lexical_cast<std::string>(expectedTemp), \
  113. boost::lexical_cast<std::string>(actualTemp))); return; } }
  114. #define DOUBLES_EQUAL(expected,actual,threshold)\
  115. { double actualTemp = actual; \
  116. double expectedTemp = expected; \
  117. if (!std::isfinite(actualTemp) || !std::isfinite(expectedTemp) || fabs ((expectedTemp)-(actualTemp)) > threshold) \
  118. { result_.addFailure (Failure (name_, __FILE__, __LINE__, \
  119. boost::lexical_cast<std::string>((double)expectedTemp), boost::lexical_cast<std::string>((double)actualTemp))); return; } }
  120. /* EXPECTs: tests will continue running after a failure */
  121. #define EXPECT(condition)\
  122. { if (!(condition)) \
  123. { result_.addFailure (Failure (name_, __FILE__,__LINE__, #condition)); } }
  124. #define EXPECT_LONGS_EQUAL(expected,actual)\
  125. { long actualTemp = actual; \
  126. long expectedTemp = expected; \
  127. if ((expectedTemp) != (actualTemp)) \
  128. { result_.addFailure (Failure (name_, __FILE__, __LINE__, boost::lexical_cast<std::string>(expectedTemp), \
  129. boost::lexical_cast<std::string>(actualTemp))); } }
  130. #define EXPECT_DOUBLES_EQUAL(expected,actual,threshold)\
  131. { double actualTemp = actual; \
  132. double expectedTemp = expected; \
  133. if (!std::isfinite(actualTemp) || !std::isfinite(expectedTemp) || fabs ((expectedTemp)-(actualTemp)) > threshold) \
  134. { result_.addFailure (Failure (name_, __FILE__, __LINE__, \
  135. boost::lexical_cast<std::string>((double)expectedTemp), boost::lexical_cast<std::string>((double)actualTemp))); } }
  136. #define FAIL(text) \
  137. { result_.addFailure (Failure (name_, __FILE__, __LINE__,(text))); return; }
  138. #endif