setjmp_test.c 2.6 KB

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