1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
//===========================================================================
//
// setjmp.c
//
// Tests for ANSI standard setjmp() and longjmp() functions
//
//===========================================================================
// ####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
//
// eCos is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 or (at your option) any later
// version.
//
// eCos is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License
// along with eCos; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// As a special exception, if other files instantiate templates or use
// macros or inline functions from this file, or you compile this file
// and link it with other works to produce a work based on this file,
// this file does not by itself cause the resulting work to be covered by
// the GNU General Public License. However the source code for this file
// must still be made available in accordance with section (3) of the GNU
// General Public License v2.
//
// This exception does not invalidate any other reasons why a work based
// on this file might be covered by the GNU General Public License.
// -------------------------------------------
// ####ECOSGPLCOPYRIGHTEND####
//===========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s): jlarmour
// Contributors:
// Date: 2000-04-30
// Purpose:
// Description: Contains test code for C library setjmp() and longjmp()
// functions
// The ANSI standard allows setjmp calls to be used ONLY in the
// forms:
// while (setjmp (jumpbuf))
// while (setjmp (jumpbuf) < 42)
// while (!setjmp (jumpbuf))
// setjmp (jumpbuf);
// result = setjmp(jumpbuf);
//
// Or "while" can also be "if" or "switch" or the implicit
// while of "for".
// Usage:
//
//####DESCRIPTIONEND####
//
//===========================================================================
// INCLUDES
#include <pkgconf/system.h>
#include <cyg/infra/testcase.h> // Test infrastructure header
#include <setjmp.h> // Header for what we're testing
// GLOBALS
static jmp_buf jbuf, jbuf2, jbuf3;
// FUNCTIONS
static int
test_fun( void )
{
volatile int i=0; // add something to the stack to confuse things
longjmp( jbuf, 5 );
i+=5;
return i;
} // test_fun();
#ifndef CYGPKG_LIBC_STARTUP
void cyg_user_start(void)
#else
int
main( int argc, char *argv[] )
#endif
{
static int static_i=0; // temporary variable
int j=0; // ditto
volatile int vol_k=0, vol_l=0; // ditto
CYG_TEST_INIT();
CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
"setjmp()/longjmp() functions");
// Test 1
if ( setjmp(jbuf) == 0 )
{
static_i=1;
longjmp(jbuf, 1);
static_i=2;
} // if
CYG_TEST_PASS_FAIL( static_i==1, "Simple test 1" );
// Test 2
j=2;
if ( !setjmp(jbuf) )
{
static_i=3;
longjmp(jbuf, 1);
static_i=4;
j=3;
} // if
CYG_TEST_PASS_FAIL( (static_i==3) && (j==2), "Simple test 2" );
// Test 3
static_i=0;
switch (setjmp(jbuf))
{
case 0:
static_i++;
if (static_i!=1)
CYG_TEST_FAIL("Test of value passed to longjmp()");
else
longjmp(jbuf, 5);
break;
case 5:
static_i++;
CYG_TEST_PASS_FAIL( (static_i==2),"Test of value passed to longjmp()");
break;
default:
CYG_TEST_FAIL( "Test of value passed to longjmp()");
break;
} // switch
// Test 3
static_i=0;
switch (setjmp(jbuf))
{
case 0:
static_i++;
if (static_i!=1)
CYG_TEST_FAIL("Test of value passed to longjmp() being 0");
else
longjmp(jbuf, 0);
break;
case 1:
static_i++;
CYG_TEST_PASS_FAIL( (static_i==2),
"Test of value passed to longjmp() being 0");
break;
default:
CYG_TEST_FAIL( "Test of value passed to longjmp() being 0");
break;
} // switch
// Test 4
vol_k=0;
static_i=0;
setjmp( jbuf );
static_i++;
setjmp( jbuf2 );
static_i++;
setjmp( jbuf3 );
if (vol_k==0)
{
vol_k++;
longjmp( jbuf2, 1 );
}
else
{
CYG_TEST_PASS_FAIL( (vol_k==1) && (static_i==3), "Multiple setjmp's" );
}
// Test 5
vol_k=3;
if ( (j=setjmp(jbuf)) == 0 )
{
volatile int l; // put something extra on the stack to confuse things
vol_k++;
l=test_fun();
l++;
vol_k=l;
} // if
CYG_TEST_PASS_FAIL( (j==5) && (vol_k==4), "longjmp from a function" );
// Test 6
vol_k=0;
vol_l=setjmp(jbuf);
vol_k += 3;
if (!vol_l)
test_fun();
vol_l=setjmp(jbuf);
vol_k += 7;
if (!vol_l)
test_fun();
CYG_TEST_PASS_FAIL ( (vol_k == 20), "Repeated longjmps from a function" );
// CYG_TEST_NA("Testing is not applicable to this configuration");
CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
"setjmp()/longjmp() functions");
} // main()
// EOF setjmp.c
|