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