2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28 * setjmp()/longjmp() tests
32 /* Forcibly enable assertions */
38 #include <ipxe/test.h>
40 /** A setjmp()/longjmp() test */
53 static struct setjmp_test *jumped;
56 * Report a setjmp() test result
58 * @v test setjmp()/longjmp() test
60 * This has to be implemented as a macro since if it were a function
61 * then the context saved by setjmp() would be invalidated when the
64 #define setjmp_ok( test ) do { \
67 assert ( jumped == NULL ); \
68 /* Initialise test */ \
69 (test)->expected = 0; \
70 (test)->file = __FILE__; \
71 (test)->line = __LINE__; \
72 /* Perform setjmp() */ \
73 value = setjmp ( (test)->env ); \
74 /* Report setjmp()/longjmp() result */ \
75 setjmp_return_ok ( (test), value ); \
79 * Report a setjmp()/longjmp() test result
81 * @v test setjmp()/longjmp() test
82 * @v value Value returned from setjmp()
84 * This function ends up reporting results from either setjmp() or
85 * longjmp() tests (since calls to longjmp() will return via the
86 * corresponding setjmp()). It therefore uses the test code file and
87 * line stored in the test structure, which will represent the line
88 * from which either setjmp() or longjmp() was called.
90 static void setjmp_return_ok ( struct setjmp_test *test, int value ) {
92 /* Determine whether this was reached via setjmp() or longjmp() */
94 /* This is the initial call to setjmp() */
95 okx ( test->expected == 0, test->file, test->line );
96 okx ( jumped == NULL, test->file, test->line );
98 /* This is reached via a call to longjmp() */
99 okx ( value == test->expected, test->file, test->line );
100 okx ( jumped == test, test->file, test->line );
103 /* Clear expected jump */
108 * Report a longjmp() test result
110 * @v test setjmp()/longjmp() test
111 * @v file Test code file
112 * @v line Test code line
114 static void longjmp_okx ( struct setjmp_test *test, int value,
115 const char *file, unsigned int line ) {
117 /* Record expected value. A zero passed to longjmp() should
118 * result in setjmp() returning a value of one.
120 test->expected = ( value ? value : 1 );
122 /* Record test code file and line */
126 /* Record expected jump */
129 /* Perform longjmp(). Should return via setjmp_okx() */
130 longjmp ( test->env, value );
132 /* longjmp() should never return */
135 #define longjmp_ok( test, value ) \
136 longjmp_okx ( test, value, __FILE__, __LINE__ )
139 * Perform setjmp()/longjmp() self-tests
142 static void setjmp_test_exec ( void ) {
143 static struct setjmp_test alpha;
144 static struct setjmp_test beta;
145 static int iteration;
147 /* This is one of the very few situations in which the
148 * "for-case" pattern is justified.
150 for ( iteration = 0 ; iteration < 10 ; iteration++ ) {
151 DBGC ( jumped, "SETJMP test iteration %d\n", iteration );
152 switch ( iteration ) {
153 case 0: setjmp_ok ( &alpha ); break;
154 case 1: setjmp_ok ( &beta ); break;
155 case 2: longjmp_ok ( &alpha, 0 );
156 case 3: longjmp_ok ( &alpha, 1 );
157 case 4: longjmp_ok ( &alpha, 2 );
158 case 5: longjmp_ok ( &beta, 17 );
159 case 6: longjmp_ok ( &beta, 29 );
160 case 7: longjmp_ok ( &alpha, -1 );
161 case 8: longjmp_ok ( &beta, 0 );
162 case 9: longjmp_ok ( &beta, 42 );
167 /** setjmp()/longjmp() self-test */
168 struct self_test setjmp_test __self_test = {
170 .exec = setjmp_test_exec,