Mercurial > ecos
annotate packages/language/c/libc/current/tests/setjmp/setjmp.c @ 64:c38311975d4f ecos-sw-2000-01-28
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
| author | jlarmour |
|---|---|
| date | Fri, 28 Jan 2000 04:59:39 +0000 |
| parents | 443894e2e912 |
| children | bf00f99aec69 |
| rev | line source |
|---|---|
| 0 | 1 //=========================================================================== |
| 2 // | |
| 3 // setjmp.c | |
| 4 // | |
| 5 // Tests for ANSI standard setjmp() and longjmp() functions | |
| 6 // | |
| 7 //=========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
9 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
10 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
11 // The contents of this file are subject to the Red Hat eCos Public License |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
13 // compliance with the License. You may obtain a copy of the License at |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
14 // http://sourceware.cygnus.com/ecos |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
15 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
16 // Software distributed under the License is distributed on an |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
18 // License for the specific language governing rights and limitations under |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
19 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
20 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
21 // The Original Code is eCos - Embedded Configurable Operating System, |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
22 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
24 // The Initial Developer of the Original Code is Red Hat. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
25 // Portions created by Red Hat are |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
27 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
28 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
29 // |
| 0 | 30 //####COPYRIGHTEND#### |
| 31 //=========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 2 | 34 // Author(s): jlarmour |
| 35 // Contributors: jlarmour | |
| 0 | 36 // Date: 1998-02-13 |
| 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 // Declarations for test system: | |
| 57 // | |
| 58 // TESTCASE_TYPE=CYG_TEST_MODULE | |
| 59 // COMPOUND_TESTCASE | |
| 60 | |
| 61 // CONFIGURATION | |
| 62 | |
| 63 #include <pkgconf/libc.h> // Configuration header | |
| 64 | |
| 65 // INCLUDES | |
| 66 | |
| 67 #include <cyg/infra/testcase.h> // Test infrastructure header | |
| 68 #include <setjmp.h> // Header for what we're testing | |
| 69 #include <sys/cstartup.h> // C library initialisation | |
| 70 | |
| 71 // GLOBALS | |
| 72 | |
| 73 #ifdef CYGPKG_LIBC | |
| 74 | |
| 75 static jmp_buf jbuf, jbuf2, jbuf3; | |
| 76 | |
| 77 | |
| 78 // FUNCTIONS | |
| 79 | |
| 80 static int | |
| 81 test_fun( void ) | |
| 82 { | |
| 83 volatile int i=0; // add something to the stack to confuse things | |
| 84 | |
| 85 longjmp( jbuf, 5 ); | |
| 86 i+=5; | |
| 87 | |
| 88 return i; | |
| 89 } // test_fun(); | |
| 90 | |
| 91 #endif // ifdef CYGPKG_LIBC | |
| 92 | |
| 93 externC void | |
| 94 cyg_package_start( void ) | |
| 95 { | |
| 96 #ifdef CYGPKG_LIBC | |
| 97 cyg_iso_c_start(); | |
| 98 #else | |
| 99 (void)main(0, NULL); | |
| 100 #endif | |
| 101 } // cyg_package_start() | |
| 102 | |
| 103 | |
| 104 int | |
| 105 main( int argc, char *argv[] ) | |
| 106 { | |
| 107 #ifdef CYGPKG_LIBC | |
| 108 static int static_i=0; // temporary variable | |
| 109 int j=0; // ditto | |
| 110 volatile int vol_k=0, vol_l=0; // ditto | |
| 111 #endif | |
| 112 | |
| 113 CYG_TEST_INIT(); | |
| 114 | |
| 115 CYG_TEST_INFO("Starting tests from testcase " __FILE__ " for C library " | |
| 116 "setjmp()/longjmp() functions"); | |
| 117 | |
| 118 #ifdef CYGPKG_LIBC | |
| 119 // Test 1 | |
| 120 if ( setjmp(jbuf) == 0 ) | |
| 121 { | |
| 122 static_i=1; | |
| 123 longjmp(jbuf, 1); | |
| 124 static_i=2; | |
| 125 } // if | |
| 126 | |
| 127 CYG_TEST_PASS_FAIL( static_i==1, "Simple test 1" ); | |
| 128 | |
| 129 // Test 2 | |
| 130 j=2; | |
| 131 | |
| 132 if ( !setjmp(jbuf) ) | |
| 133 { | |
| 134 static_i=3; | |
| 135 longjmp(jbuf, 1); | |
| 136 static_i=4; | |
| 137 j=3; | |
| 138 } // if | |
| 139 | |
| 140 CYG_TEST_PASS_FAIL( (static_i==3) && (j==2), "Simple test 2" ); | |
| 141 | |
| 142 // Test 3 | |
| 143 | |
| 144 static_i=0; | |
| 145 switch (setjmp(jbuf)) | |
| 146 { | |
| 147 case 0: | |
| 148 static_i++; | |
| 149 if (static_i!=1) | |
| 150 CYG_TEST_FAIL("Test of value passed to longjmp()"); | |
| 151 else | |
| 152 longjmp(jbuf, 5); | |
| 153 break; | |
| 154 case 5: | |
| 155 static_i++; | |
| 156 CYG_TEST_PASS_FAIL( (static_i==2),"Test of value passed to longjmp()"); | |
| 157 break; | |
| 158 default: | |
| 159 CYG_TEST_FAIL( "Test of value passed to longjmp()"); | |
| 160 break; | |
| 161 } // switch | |
| 162 | |
| 163 // Test 3 | |
| 164 | |
| 165 static_i=0; | |
| 166 switch (setjmp(jbuf)) | |
| 167 { | |
| 168 case 0: | |
| 169 static_i++; | |
| 170 if (static_i!=1) | |
| 171 CYG_TEST_FAIL("Test of value passed to longjmp() being 0"); | |
| 172 else | |
| 173 longjmp(jbuf, 0); | |
| 174 break; | |
| 175 case 1: | |
| 176 static_i++; | |
| 177 CYG_TEST_PASS_FAIL( (static_i==2), | |
| 178 "Test of value passed to longjmp() being 0"); | |
| 179 break; | |
| 180 default: | |
| 181 CYG_TEST_FAIL( "Test of value passed to longjmp() being 0"); | |
| 182 break; | |
| 183 } // switch | |
| 184 | |
| 185 | |
| 186 // Test 4 | |
| 187 | |
| 188 vol_k=0; | |
| 189 static_i=0; | |
| 190 setjmp( jbuf ); | |
| 191 | |
| 192 static_i++; | |
| 193 setjmp( jbuf2 ); | |
| 194 | |
| 195 static_i++; | |
| 196 setjmp( jbuf3 ); | |
| 197 | |
| 198 if (vol_k==0) | |
| 199 { | |
| 200 vol_k++; | |
| 201 longjmp( jbuf2, 1 ); | |
| 202 } | |
| 203 else | |
| 204 { | |
| 205 CYG_TEST_PASS_FAIL( (vol_k==1) && (static_i==3), "Multiple setjmp's" ); | |
| 206 } | |
| 207 | |
| 208 // Test 5 | |
| 209 | |
| 210 vol_k=3; | |
| 211 if ( (j=setjmp(jbuf)) == 0 ) | |
| 212 { | |
| 213 volatile int l; // put something extra on the stack to confuse things | |
| 214 | |
| 215 vol_k++; | |
| 216 l=test_fun(); | |
| 217 l++; | |
| 218 vol_k=l; | |
| 219 } // if | |
| 220 | |
| 221 CYG_TEST_PASS_FAIL( (j==5) && (vol_k==4), "longjmp from a function" ); | |
| 222 | |
| 223 // Test 6 | |
| 224 | |
| 225 vol_k=0; | |
| 226 | |
| 227 vol_l=setjmp(jbuf); | |
| 228 | |
| 229 vol_k += 3; | |
| 230 | |
| 231 if (!vol_l) | |
| 232 test_fun(); | |
| 233 | |
| 234 vol_l=setjmp(jbuf); | |
| 235 | |
| 236 vol_k += 7; | |
| 237 | |
| 238 if (!vol_l) | |
| 239 test_fun(); | |
| 240 | |
| 241 CYG_TEST_PASS_FAIL ( (vol_k == 20), "Repeated longjmps from a function" ); | |
| 242 | |
| 243 #else // ifndef CYGPKG_LIBC | |
| 2 | 244 CYG_TEST_NA("Testing is not applicable to this configuration"); |
| 0 | 245 #endif // ifndef CYGPKG_LIBC |
| 246 | |
| 247 CYG_TEST_FINISH("Finished tests from testcase " __FILE__ " for C library " | |
| 248 "setjmp()/longjmp() functions"); | |
| 249 | |
| 250 } // main() | |
| 251 | |
| 252 // EOF setjmp.c |
