Update copyright notices with scripts/update-copyrights
[glibc.git] / setjmp / tst-setjmp.c
blob2484fc56dcdccb6739bfafdc05b1e11d68e2ea99
1 /* Copyright (C) 1991-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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 <http://www.gnu.org/licenses/>. */
18 #include <stdio.h>
19 #include <setjmp.h>
20 #include <stdlib.h>
22 static jmp_buf env;
23 static int last_value = -1, lose = 0;
25 static void
26 jump (int val)
28 longjmp (env, val);
31 int
32 main (void)
34 int value;
36 value = setjmp (env);
37 if (value != last_value + 1)
39 fputs("Shouldn't have ", stdout);
40 lose = 1;
42 last_value = value;
43 switch (value)
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);
54 if (!lose && value == 10)
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);
61 last_value = -1;
62 lose = 0;
64 value = setjmp (env);
65 if (value != last_value + 1)
67 fputs("Shouldn't have ", stdout);
68 lose = 1;
70 last_value = value;
71 switch (value)
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);
83 if (!lose && value == 10)
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;
92 value = _setjmp (env);
93 if (value != last_value + 1)
95 fputs("Shouldn't have ", stdout);
96 lose = 1;
98 last_value = value;
99 switch (value)
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);
111 if (lose || value != 10)
112 puts ("Test FAILED!");
113 else
114 puts ("Test succeeded!");
116 return lose ? EXIT_FAILURE : EXIT_SUCCESS;