comparison packages/language/c/libc/setjmp/current/tests/setjmp.c @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children e0c0827131d1
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 //===========================================================================
2 //
3 // setjmp.c
4 //
5 // Tests for ANSI standard setjmp() and longjmp() functions
6 //
7 //===========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //===========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): jlarmour
35 // Contributors:
36 // Date: 2000-04-30
37 // Purpose:
38 // Description: Contains test code for C library setjmp() and longjmp()
39 // functions
40 // The ANSI standard allows setjmp calls to be used ONLY in the
41 // forms:
42 // while (setjmp (jumpbuf))
43 // while (setjmp (jumpbuf) < 42)
44 // while (!setjmp (jumpbuf))
45 // setjmp (jumpbuf);
46 // result = setjmp(jumpbuf);
47 //
48 // Or "while" can also be "if" or "switch" or the implicit
49 // while of "for".
50 // Usage:
51 //
52 //####DESCRIPTIONEND####
53 //
54 //===========================================================================
55
56 // INCLUDES
57
58 #include <cyg/infra/testcase.h> // Test infrastructure header
59 #include <setjmp.h> // Header for what we're testing
60
61 // GLOBALS
62
63 static jmp_buf jbuf, jbuf2, jbuf3;
64
65
66 // FUNCTIONS
67
68 static int
69 test_fun( void )
70 {
71 volatile int i=0; // add something to the stack to confuse things
72
73 longjmp( jbuf, 5 );
74 i+=5;
75
76 return i;
77 } // test_fun();
78
79
80 int
81 main( int argc, char *argv[] )
82 {
83 static int static_i=0; // temporary variable
84 int j=0; // ditto
85 volatile int vol_k=0, vol_l=0; // ditto
86
87 CYG_TEST_INIT();
88
89 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library "
90 "setjmp()/longjmp() functions");
91
92 // Test 1
93 if ( setjmp(jbuf) == 0 )
94 {
95 static_i=1;
96 longjmp(jbuf, 1);
97 static_i=2;
98 } // if
99
100 CYG_TEST_PASS_FAIL( static_i==1, "Simple test 1" );
101
102 // Test 2
103 j=2;
104
105 if ( !setjmp(jbuf) )
106 {
107 static_i=3;
108 longjmp(jbuf, 1);
109 static_i=4;
110 j=3;
111 } // if
112
113 CYG_TEST_PASS_FAIL( (static_i==3) && (j==2), "Simple test 2" );
114
115 // Test 3
116
117 static_i=0;
118 switch (setjmp(jbuf))
119 {
120 case 0:
121 static_i++;
122 if (static_i!=1)
123 CYG_TEST_FAIL("Test of value passed to longjmp()");
124 else
125 longjmp(jbuf, 5);
126 break;
127 case 5:
128 static_i++;
129 CYG_TEST_PASS_FAIL( (static_i==2),"Test of value passed to longjmp()");
130 break;
131 default:
132 CYG_TEST_FAIL( "Test of value passed to longjmp()");
133 break;
134 } // switch
135
136 // Test 3
137
138 static_i=0;
139 switch (setjmp(jbuf))
140 {
141 case 0:
142 static_i++;
143 if (static_i!=1)
144 CYG_TEST_FAIL("Test of value passed to longjmp() being 0");
145 else
146 longjmp(jbuf, 0);
147 break;
148 case 1:
149 static_i++;
150 CYG_TEST_PASS_FAIL( (static_i==2),
151 "Test of value passed to longjmp() being 0");
152 break;
153 default:
154 CYG_TEST_FAIL( "Test of value passed to longjmp() being 0");
155 break;
156 } // switch
157
158
159 // Test 4
160
161 vol_k=0;
162 static_i=0;
163 setjmp( jbuf );
164
165 static_i++;
166 setjmp( jbuf2 );
167
168 static_i++;
169 setjmp( jbuf3 );
170
171 if (vol_k==0)
172 {
173 vol_k++;
174 longjmp( jbuf2, 1 );
175 }
176 else
177 {
178 CYG_TEST_PASS_FAIL( (vol_k==1) && (static_i==3), "Multiple setjmp's" );
179 }
180
181 // Test 5
182
183 vol_k=3;
184 if ( (j=setjmp(jbuf)) == 0 )
185 {
186 volatile int l; // put something extra on the stack to confuse things
187
188 vol_k++;
189 l=test_fun();
190 l++;
191 vol_k=l;
192 } // if
193
194 CYG_TEST_PASS_FAIL( (j==5) && (vol_k==4), "longjmp from a function" );
195
196 // Test 6
197
198 vol_k=0;
199
200 vol_l=setjmp(jbuf);
201
202 vol_k += 3;
203
204 if (!vol_l)
205 test_fun();
206
207 vol_l=setjmp(jbuf);
208
209 vol_k += 7;
210
211 if (!vol_l)
212 test_fun();
213
214 CYG_TEST_PASS_FAIL ( (vol_k == 20), "Repeated longjmps from a function" );
215
216 // CYG_TEST_NA("Testing is not applicable to this configuration");
217
218 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library "
219 "setjmp()/longjmp() functions");
220
221 } // main()
222
223 // EOF setjmp.c