/extlibs/UnitTest++/source/CheckMacros.h

https://bitbucket.org/hugoruscitti/pilascpp · C++ Header · 122 lines · 105 code · 17 blank · 0 comment · 9 complexity · 878ea94d6e6547828578a7487865e303 MD5 · raw file

  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. #include "CurrentTest.h"
  8. #ifdef CHECK
  9. #error UnitTest++ redefines CHECK
  10. #endif
  11. #ifdef CHECK_EQUAL
  12. #error UnitTest++ redefines CHECK_EQUAL
  13. #endif
  14. #ifdef CHECK_CLOSE
  15. #error UnitTest++ redefines CHECK_CLOSE
  16. #endif
  17. #ifdef CHECK_ARRAY_EQUAL
  18. #error UnitTest++ redefines CHECK_ARRAY_EQUAL
  19. #endif
  20. #ifdef CHECK_ARRAY_CLOSE
  21. #error UnitTest++ redefines CHECK_ARRAY_CLOSE
  22. #endif
  23. #ifdef CHECK_ARRAY2D_CLOSE
  24. #error UnitTest++ redefines CHECK_ARRAY2D_CLOSE
  25. #endif
  26. #define CHECK(value) \
  27. do \
  28. { \
  29. try { \
  30. if (!UnitTest::Check(value)) \
  31. UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), #value); \
  32. } \
  33. catch (...) { \
  34. UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
  35. "Unhandled exception in CHECK(" #value ")"); \
  36. } \
  37. } while (0)
  38. #define CHECK_EQUAL(expected, actual) \
  39. do \
  40. { \
  41. try { \
  42. UnitTest::CheckEqual(*UnitTest::CurrentTest::Results(), expected, actual, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
  43. } \
  44. catch (...) { \
  45. UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
  46. "Unhandled exception in CHECK_EQUAL(" #expected ", " #actual ")"); \
  47. } \
  48. } while (0)
  49. #define CHECK_CLOSE(expected, actual, tolerance) \
  50. do \
  51. { \
  52. try { \
  53. UnitTest::CheckClose(*UnitTest::CurrentTest::Results(), expected, actual, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
  54. } \
  55. catch (...) { \
  56. UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
  57. "Unhandled exception in CHECK_CLOSE(" #expected ", " #actual ")"); \
  58. } \
  59. } while (0)
  60. #define CHECK_ARRAY_EQUAL(expected, actual, count) \
  61. do \
  62. { \
  63. try { \
  64. UnitTest::CheckArrayEqual(*UnitTest::CurrentTest::Results(), expected, actual, count, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
  65. } \
  66. catch (...) { \
  67. UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
  68. "Unhandled exception in CHECK_ARRAY_EQUAL(" #expected ", " #actual ")"); \
  69. } \
  70. } while (0)
  71. #define CHECK_ARRAY_CLOSE(expected, actual, count, tolerance) \
  72. do \
  73. { \
  74. try { \
  75. UnitTest::CheckArrayClose(*UnitTest::CurrentTest::Results(), expected, actual, count, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
  76. } \
  77. catch (...) { \
  78. UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
  79. "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \
  80. } \
  81. } while (0)
  82. #define CHECK_ARRAY2D_CLOSE(expected, actual, rows, columns, tolerance) \
  83. do \
  84. { \
  85. try { \
  86. UnitTest::CheckArray2DClose(*UnitTest::CurrentTest::Results(), expected, actual, rows, columns, tolerance, UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__)); \
  87. } \
  88. catch (...) { \
  89. UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), \
  90. "Unhandled exception in CHECK_ARRAY_CLOSE(" #expected ", " #actual ")"); \
  91. } \
  92. } while (0)
  93. #define CHECK_THROW(expression, ExpectedExceptionType) \
  94. do \
  95. { \
  96. bool caught_ = false; \
  97. try { expression; } \
  98. catch (ExpectedExceptionType const&) { caught_ = true; } \
  99. catch (...) {} \
  100. if (!caught_) \
  101. UnitTest::CurrentTest::Results()->OnTestFailure(UnitTest::TestDetails(*UnitTest::CurrentTest::Details(), __LINE__), "Expected exception: \"" #ExpectedExceptionType "\" not thrown"); \
  102. } while(0)
  103. #define CHECK_ASSERT(expression) \
  104. CHECK_THROW(expression, UnitTest::AssertException);
  105. #endif