check_assert.h 2.2 KB

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