test_setjmp.c 812 B

1234567891011121314151617181920212223242526272829303132333435
  1. #include <setjmp.h>
  2. #include <stdio.h>
  3. #include "unity.h"
  4. #include "esp_system.h"
  5. typedef struct {
  6. jmp_buf jmp_env;
  7. uint32_t retval;
  8. volatile bool inner_called;
  9. } setjmp_test_ctx_t;
  10. static __attribute__((noreturn)) void inner(setjmp_test_ctx_t *ctx)
  11. {
  12. printf("inner, retval=0x%x\n", ctx->retval);
  13. ctx->inner_called = true;
  14. longjmp(ctx->jmp_env, ctx->retval);
  15. TEST_FAIL_MESSAGE("Should not reach here");
  16. }
  17. TEST_CASE("setjmp and longjmp", "[newlib]")
  18. {
  19. const uint32_t expected = 0x12345678;
  20. setjmp_test_ctx_t ctx = {
  21. .retval = expected
  22. };
  23. uint32_t ret = setjmp(ctx.jmp_env);
  24. if (!ctx.inner_called) {
  25. TEST_ASSERT_EQUAL(0, ret);
  26. inner(&ctx);
  27. } else {
  28. TEST_ASSERT_EQUAL(expected, ret);
  29. }
  30. TEST_ASSERT(ctx.inner_called);
  31. }