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