iPXE
setjmp_test.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 /** @file
00027  *
00028  * setjmp()/longjmp() tests
00029  *
00030  */
00031 
00032 /* Forcibly enable assertions */
00033 #undef NDEBUG
00034 
00035 #include <stddef.h>
00036 #include <assert.h>
00037 #include <setjmp.h>
00038 #include <ipxe/test.h>
00039 
00040 /** A setjmp()/longjmp() test */
00041 struct setjmp_test {
00042         /** Jump buffer */
00043         jmp_buf env;
00044         /** Expected value */
00045         int expected;
00046         /** Test code file */
00047         const char *file;
00048         /** Test code line */
00049         unsigned int line;
00050 };
00051 
00052 /** Expected jump */
00053 static struct setjmp_test *jumped;
00054 
00055 /**
00056  * Report a setjmp() test result
00057  *
00058  * @v test              setjmp()/longjmp() test
00059  *
00060  * This has to be implemented as a macro since if it were a function
00061  * then the context saved by setjmp() would be invalidated when the
00062  * function returned.
00063  */
00064 #define setjmp_ok( test ) do {                                          \
00065         int value;                                                      \
00066         /* Sanity check */                                              \
00067         assert ( jumped == NULL );                                      \
00068         /* Initialise test */                                           \
00069         (test)->expected = 0;                                           \
00070         (test)->file = __FILE__;                                        \
00071         (test)->line = __LINE__;                                        \
00072         /* Perform setjmp() */                                          \
00073         value = setjmp ( (test)->env );                                 \
00074         /* Report setjmp()/longjmp() result */                          \
00075         setjmp_return_ok ( (test), value );                             \
00076         } while ( 0 )
00077 
00078 /**
00079  * Report a setjmp()/longjmp() test result
00080  *
00081  * @v test              setjmp()/longjmp() test
00082  * @v value             Value returned from setjmp()
00083  *
00084  * This function ends up reporting results from either setjmp() or
00085  * longjmp() tests (since calls to longjmp() will return via the
00086  * corresponding setjmp()).  It therefore uses the test code file and
00087  * line stored in the test structure, which will represent the line
00088  * from which either setjmp() or longjmp() was called.
00089  */
00090 static void setjmp_return_ok ( struct setjmp_test *test, int value ) {
00091 
00092         /* Determine whether this was reached via setjmp() or longjmp() */
00093         if ( value == 0 ) {
00094                 /* This is the initial call to setjmp() */
00095                 okx ( test->expected == 0, test->file, test->line );
00096                 okx ( jumped == NULL, test->file, test->line );
00097         } else {
00098                 /* This is reached via a call to longjmp() */
00099                 okx ( value == test->expected, test->file, test->line );
00100                 okx ( jumped == test, test->file, test->line );
00101         }
00102 
00103         /* Clear expected jump */
00104         jumped = NULL;
00105 }
00106 
00107 /**
00108  * Report a longjmp() test result
00109  *
00110  * @v test              setjmp()/longjmp() test
00111  * @v file              Test code file
00112  * @v line              Test code line
00113  */
00114 static void __attribute__ (( noreturn ))
00115 longjmp_okx ( struct setjmp_test *test, int value,
00116               const char *file, unsigned int line ) {
00117 
00118         /* Record expected value.  A zero passed to longjmp() should
00119          * result in setjmp() returning a value of one.
00120          */
00121         test->expected = ( value ? value : 1 );
00122 
00123         /* Record test code file and line */
00124         test->file = file;
00125         test->line = line;
00126 
00127         /* Record expected jump */
00128         jumped = test;
00129 
00130         /* Perform longjmp().  Should return via setjmp_okx() */
00131         longjmp ( test->env, value );
00132 
00133         /* longjmp() should never return */
00134         assert ( 0 );
00135 }
00136 #define longjmp_ok( test, value ) \
00137         longjmp_okx ( test, value, __FILE__, __LINE__ )
00138 
00139 /**
00140  * Perform setjmp()/longjmp() self-tests
00141  *
00142  */
00143 static void setjmp_test_exec ( void ) {
00144         static struct setjmp_test alpha;
00145         static struct setjmp_test beta;
00146         static int iteration;
00147 
00148         /* This is one of the very few situations in which the
00149          * "for-case" pattern is justified.
00150          */
00151         for ( iteration = 0 ; iteration < 10 ; iteration++ ) {
00152                 DBGC ( jumped, "SETJMP test iteration %d\n", iteration );
00153                 switch ( iteration ) {
00154                 case 0: setjmp_ok ( &alpha ); break;
00155                 case 1: setjmp_ok ( &beta ); break;
00156                 case 2: longjmp_ok ( &alpha, 0 );
00157                 case 3: longjmp_ok ( &alpha, 1 );
00158                 case 4: longjmp_ok ( &alpha, 2 );
00159                 case 5: longjmp_ok ( &beta, 17 );
00160                 case 6: longjmp_ok ( &beta, 29 );
00161                 case 7: longjmp_ok ( &alpha, -1 );
00162                 case 8: longjmp_ok ( &beta, 0 );
00163                 case 9: longjmp_ok ( &beta, 42 );
00164                 }
00165         }
00166 }
00167 
00168 /** setjmp()/longjmp() self-test */
00169 struct self_test setjmp_test __self_test = {
00170         .name = "setjmp",
00171         .exec = setjmp_test_exec,
00172 };