tst-setjmp.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Copyright (C) 1991-2026 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #include <stdio.h>
  15. #include <setjmp.h>
  16. #include <stdlib.h>
  17. static jmp_buf env;
  18. static int last_value = -1, lose = 0;
  19. static __attribute__ ((__noreturn__)) void
  20. jump (int val)
  21. {
  22. longjmp (env, val);
  23. }
  24. static int
  25. do_test (void)
  26. {
  27. int value;
  28. value = setjmp (env);
  29. if (value != last_value + 1)
  30. {
  31. fputs("Shouldn't have ", stdout);
  32. lose = 1;
  33. }
  34. last_value = value;
  35. switch (value)
  36. {
  37. case 0:
  38. puts("Saved environment.");
  39. jump (0);
  40. default:
  41. printf ("Jumped to %d.\n", value);
  42. if (value < 10)
  43. jump (value + 1);
  44. }
  45. if (!lose && value == 10)
  46. {
  47. /* Do a second test, this time without `setjmp' being a macro.
  48. This is not required by ISO C but we have this for compatibility. */
  49. #undef setjmp
  50. extern int setjmp (jmp_buf);
  51. last_value = -1;
  52. lose = 0;
  53. value = setjmp (env);
  54. if (value != last_value + 1)
  55. {
  56. fputs("Shouldn't have ", stdout);
  57. lose = 1;
  58. }
  59. last_value = value;
  60. switch (value)
  61. {
  62. case 0:
  63. puts("Saved environment.");
  64. jump (0);
  65. default:
  66. printf ("Jumped to %d.\n", value);
  67. if (value < 10)
  68. jump (value + 1);
  69. }
  70. }
  71. if (!lose && value == 10)
  72. {
  73. /* And again for the `_setjmp' function. */
  74. #ifndef _setjmp
  75. extern int _setjmp (jmp_buf);
  76. #endif
  77. last_value = -1;
  78. lose = 0;
  79. value = _setjmp (env);
  80. if (value != last_value + 1)
  81. {
  82. fputs("Shouldn't have ", stdout);
  83. lose = 1;
  84. }
  85. last_value = value;
  86. switch (value)
  87. {
  88. case 0:
  89. puts("Saved environment.");
  90. jump (0);
  91. default:
  92. printf ("Jumped to %d.\n", value);
  93. if (value < 10)
  94. jump (value + 1);
  95. }
  96. }
  97. if (lose || value != 10)
  98. puts ("Test FAILED!");
  99. else
  100. puts ("Test succeeded!");
  101. return lose ? EXIT_FAILURE : EXIT_SUCCESS;
  102. }
  103. #define TEST_FUNCTION do_test ()
  104. #include "../test-skeleton.c"