test_assert.h 1.6 KB

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