test_assert.h 1.3 KB

123456789101112131415161718192021222324252627282930313233
  1. /*
  2. * This header defines an implementation of the OPENER_ASSERT macro that
  3. * can be used with Cpputest unit tests to confirm an assertion fails
  4. * under given conditions. It is conditionally included in the application
  5. * code when unit tests are enabled through the Cmake configuration; it should
  6. * not be included in the Cpputest unit test code.
  7. *
  8. * The intent is to create an assertion implementation that both immediately
  9. * stops execution after an assertion failure, as should normally be the
  10. * case, and is detectable from the unit test code. This is accomplished via
  11. * setjmp() and longjmp(), approximating the behavior of a C++ exception,
  12. * where an OPENER_ASSERTION failure results in a longjmp() back to the
  13. * unit test code, which then verifies that an assertion failure occurred.
  14. */
  15. #ifndef OPENER_TEST_ASSERT_H
  16. #define OPENER_TEST_ASSERT_H
  17. /*
  18. * Define the OPENER_ASSERT macro to call the unit test assertion verification
  19. * function. The surrounding do/while loop serves to insulate the if statement
  20. * from any surrounding if statements.
  21. */
  22. #define OPENER_ASSERT(assertion) \
  23. do {if ( !(assertion) ) test_assert_fail(__FILE__, __LINE__);} while (0)
  24. /* Function Prototypes */
  25. extern void test_assert_fail(const char *const file,
  26. const unsigned int line);
  27. #endif /* OPENER_TEST_ASSERT_H */