tst-setjmp.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* Copyright (C) 1991, 1992, 1997, 1998, 2000 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, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #include <stdio.h>
  16. #include <setjmp.h>
  17. #include <stdlib.h>
  18. static jmp_buf env;
  19. static int last_value = -1, lose = 0;
  20. __attribute__ ((__noreturn__))
  21. static void
  22. jump (int val)
  23. {
  24. longjmp (env, val);
  25. }
  26. int
  27. main (void)
  28. {
  29. int value;
  30. value = setjmp (env);
  31. if (value != last_value + 1)
  32. {
  33. fputs("Shouldn't have ", stdout);
  34. lose = 1;
  35. }
  36. last_value = value;
  37. switch (value)
  38. {
  39. case 0:
  40. puts("Saved environment.");
  41. jump (0);
  42. default:
  43. printf ("Jumped to %d.\n", value);
  44. if (value < 10)
  45. jump (value + 1);
  46. }
  47. if (!lose && value == 10)
  48. {
  49. /* Do a second test, this time without `setjmp' being a macro.
  50. This is not required by ISO C but we have this for compatibility. */
  51. #undef setjmp
  52. extern int setjmp (jmp_buf);
  53. last_value = -1;
  54. lose = 0;
  55. value = setjmp (env);
  56. if (value != last_value + 1)
  57. {
  58. fputs("Shouldn't have ", stdout);
  59. lose = 1;
  60. }
  61. last_value = value;
  62. switch (value)
  63. {
  64. case 0:
  65. puts("Saved environment.");
  66. jump (0);
  67. default:
  68. printf ("Jumped to %d.\n", value);
  69. if (value < 10)
  70. jump (value + 1);
  71. }
  72. }
  73. if (!lose && value == 10)
  74. {
  75. /* And again for the `_setjmp' function. */
  76. #ifndef _setjmp
  77. extern int _setjmp (jmp_buf);
  78. #endif
  79. last_value = -1;
  80. lose = 0;
  81. value = _setjmp (env);
  82. if (value != last_value + 1)
  83. {
  84. fputs("Shouldn't have ", stdout);
  85. lose = 1;
  86. }
  87. last_value = value;
  88. switch (value)
  89. {
  90. case 0:
  91. puts("Saved environment.");
  92. jump (0);
  93. default:
  94. printf ("Jumped to %d.\n", value);
  95. if (value < 10)
  96. jump (value + 1);
  97. }
  98. }
  99. if (lose || value != 10)
  100. puts ("Test FAILED!");
  101. else
  102. puts ("Test succeeded!");
  103. return lose ? EXIT_FAILURE : EXIT_SUCCESS;
  104. }