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 |
26 | jump (int val) |
27 | { |
28 | longjmp (env: env, val: val); |
29 | } |
30 | |
31 | static int |
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(s: "Saved environment." ); |
47 | jump (val: 0); |
48 | default: |
49 | printf (format: "Jumped to %d.\n" , value); |
50 | if (value < 10) |
51 | jump (val: 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(s: "Saved environment." ); |
75 | jump (val: 0); |
76 | default: |
77 | printf (format: "Jumped to %d.\n" , value); |
78 | if (value < 10) |
79 | jump (val: 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(s: "Saved environment." ); |
103 | jump (val: 0); |
104 | default: |
105 | printf (format: "Jumped to %d.\n" , value); |
106 | if (value < 10) |
107 | jump (val: value + 1); |
108 | } |
109 | } |
110 | |
111 | if (lose || value != 10) |
112 | puts (s: "Test FAILED!" ); |
113 | else |
114 | puts (s: "Test succeeded!" ); |
115 | |
116 | return lose ? EXIT_FAILURE : EXIT_SUCCESS; |
117 | } |
118 | |
119 | #define TEST_FUNCTION do_test () |
120 | #include "../test-skeleton.c" |
121 | |