CheckMacros.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #ifndef UNITTEST_CHECKMACROS_H
  2. #define UNITTEST_CHECKMACROS_H
  3. #include "Checks.h"
  4. #include "AssertException.h"
  5. #include "MemoryOutStream.h"
  6. #include "TestDetails.h"
  7. #ifdef CHECK
  8. #error UnitTest++ redefines CHECK
  9. #endif
  10. #define CHECK(value) \
  11. do \
  12. { \
  13. try { \
  14. if (!UnitTest::Check(value)) \
  15. testResults_.OnTestFailure( UnitTest::TestDetails(m_details, __LINE__), #value); \
  16. } \
  17. catch (...) { \
  18. testResults_.OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \
  19. "Unhandled exception in CHECK(" #value ")"); \
  20. } \
  21. } while (0)
  22. #define CHECK_EQUAL(expected, actual) \
  23. do \
  24. { \
  25. try { \
  26. UnitTest::CheckEqual(testResults_, expected, actual, UnitTest::TestDetails(m_details, __LINE__)); \
  27. } \
  28. catch (...) { \
  29. testResults_.OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \
  30. "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \
  31. } \
  32. } while (0)
  33. #define CHECK_CLOSE(expected, actual, tolerance) \
  34. do \
  35. { \
  36. try { \
  37. UnitTest::CheckClose(testResults_, expected, actual, tolerance, UnitTest::TestDetails(m_details, __LINE__)); \
  38. } \
  39. catch (...) { \
  40. testResults_.OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \
  41. "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \
  42. } \
  43. } while (0)
  44. #define CHECK_ARRAY_EQUAL(expected, actual, count) \
  45. do \
  46. { \
  47. try { \
  48. UnitTest::CheckArrayEqual(testResults_, expected, actual, count, UnitTest::TestDetails(m_details, __LINE__)); \
  49. } \
  50. catch (...) { \
  51. testResults_.OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \
  52. "Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"); \
  53. } \
  54. } while (0)
  55. #define CHECK_ARRAY_CLOSE(expected, actual, count, tolerance) \
  56. do \
  57. { \
  58. try { \
  59. UnitTest::CheckArrayClose(testResults_, expected, actual, count, tolerance, UnitTest::TestDetails(m_details, __LINE__)); \
  60. } \
  61. catch (...) { \
  62. testResults_.OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \
  63. "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \
  64. } \
  65. } while (0)
  66. #define CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, tolerance) \
  67. do \
  68. { \
  69. try { \
  70. UnitTest::CheckArray2DClose(testResults_, expected, actual, rows, columns, tolerance, UnitTest::TestDetails(m_details, __LINE__)); \
  71. } \
  72. catch (...) { \
  73. testResults_.OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), \
  74. "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \
  75. } \
  76. } while (0)
  77. #define CHECK_THROW(expression, ExpectedExceptionType) \
  78. do \
  79. { \
  80. bool caught_ = false; \
  81. try { expression; } \
  82. catch (ExpectedExceptionType const&) { caught_ = true; } \
  83. catch (...) {} \
  84. if (!caught_) \
  85. testResults_.OnTestFailure(UnitTest::TestDetails(m_details, __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \
  86. } while(0)
  87. #define CHECK_ASSERT(expression) \
  88. CHECK_THROW(expression, UnitTest::AssertException);
  89. #endif