check_assert.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * This header contains definitions implementing a method for writing test
  3. * cases to confirm an OPENER_ASSERTION failure using the CHECK_ASSERT macro.
  4. * Only code implementing Cpputest test cases should include this header; it
  5. * should not be included by application code.
  6. */
  7. #include <setjmp.h>
  8. /* See OpENerTests.cpp for descriptions. */
  9. extern jmp_buf assert_jump;
  10. extern jmp_buf *assert_jump_enabled;
  11. /*
  12. * This macro is intended to be used in the unit test code, not the
  13. * application code, to verify a given expression, typically a call to a
  14. * function being tested, generates an assertion via a failed OPENER_ASSERT
  15. * condition. For example:
  16. *
  17. * CHECK_ASSERT(func());
  18. *
  19. * The above statement will pass if an OPENER_ASSERT fails during func(),
  20. * or cause the test to fail if func() returns normally.
  21. *
  22. * These statements are enclosed within a do/while block to keep the if
  23. * statement isolated from surrounding if statements.
  24. */
  25. #define CHECK_ASSERT(exp) \
  26. do { \
  27. /* Enable an expected assertion by storing a non-NULL pointer. */ \
  28. UT_PTR_SET(assert_jump_enabled, &assert_jump); \
  29. \
  30. /* Store the assertion jump location. */ \
  31. if (setjmp(assert_jump) == 0) { \
  32. \
  33. /* Code under test, which should longjmp() instead of return. */ \
  34. exp; \
  35. \
  36. /* Fail if the above expression did not generate an assertion. */ \
  37. FAIL("Did not assert as expected."); \
  38. } \
  39. } while (0)