test_setjmp.c 939 B

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