iPXE
setjmp_test.c File Reference

setjmp()/longjmp() tests More...

#include <stddef.h>
#include <assert.h>
#include <setjmp.h>
#include <ipxe/test.h>

Go to the source code of this file.

Data Structures

struct  setjmp_test
 A setjmp()/longjmp() test. More...

Macros

#define setjmp_ok(test)
 Report a setjmp() test result.
#define longjmp_ok(test, value)

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
static void setjmp_return_ok (struct setjmp_test *test, int value)
 Report a setjmp()/longjmp() test result.
static void longjmp_okx (struct setjmp_test *test, int value, const char *file, unsigned int line)
 Report a longjmp() test result.
static void setjmp_test_exec (void)
 Perform setjmp()/longjmp() self-tests.

Variables

static struct setjmp_testjumped
 Expected jump.
struct self_test setjmp_test __self_test
 setjmp()/longjmp() self-test

Detailed Description

setjmp()/longjmp() tests

Definition in file setjmp_test.c.

Macro Definition Documentation

◆ setjmp_ok

#define setjmp_ok ( test)
Value:
do { \
int value; \
/* Sanity check */ \
assert ( jumped == NULL ); \
/* Initialise test */ \
(test)->expected = 0; \
(test)->file = __FILE__; \
(test)->line = __LINE__; \
/* Perform setjmp() */ \
value = setjmp ( (test)->env ); \
/* Report setjmp()/longjmp() result */ \
setjmp_return_ok ( (test), value ); \
} while ( 0 )
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
pseudo_bit_t value[0x00020]
Definition arbel.h:2
static int test
Definition epic100.c:73
static struct setjmp_test * jumped
Expected jump.
Definition setjmp_test.c:53

Report a setjmp() test result.

Parameters
testsetjmp()/longjmp() test

This has to be implemented as a macro since if it were a function then the context saved by setjmp() would be invalidated when the function returned.

Definition at line 64 of file setjmp_test.c.

64#define setjmp_ok( test ) do { \
65 int value; \
66 /* Sanity check */ \
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 ); \
76 } while ( 0 )

Referenced by setjmp_test_exec().

◆ longjmp_ok

#define longjmp_ok ( test,
value )
Value:
longjmp_okx ( test, value, __FILE__, __LINE__ )
static void longjmp_okx(struct setjmp_test *test, int value, const char *file, unsigned int line)
Report a longjmp() test result.

Definition at line 136 of file setjmp_test.c.

136#define longjmp_ok( test, value ) \
137 longjmp_okx ( test, value, __FILE__, __LINE__ )

Referenced by setjmp_test_exec().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ setjmp_return_ok()

void setjmp_return_ok ( struct setjmp_test * test,
int value )
static

Report a setjmp()/longjmp() test result.

Parameters
testsetjmp()/longjmp() test
valueValue returned from setjmp()

This function ends up reporting results from either setjmp() or longjmp() tests (since calls to longjmp() will return via the corresponding setjmp()). It therefore uses the test code file and line stored in the test structure, which will represent the line from which either setjmp() or longjmp() was called.

Definition at line 90 of file setjmp_test.c.

90 {
91
92 /* Determine whether this was reached via setjmp() or longjmp() */
93 if ( value == 0 ) {
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 );
97 } else {
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 );
101 }
102
103 /* Clear expected jump */
104 jumped = NULL;
105}
#define okx(success, file, line)
Report test result.
Definition test.h:44

References jumped, NULL, okx, test, and value.

◆ longjmp_okx()

void longjmp_okx ( struct setjmp_test * test,
int value,
const char * file,
unsigned int line )
static

Report a longjmp() test result.

Parameters
testsetjmp()/longjmp() test
fileTest code file
lineTest code line

Definition at line 115 of file setjmp_test.c.

116 {
117
118 /* Record expected value. A zero passed to longjmp() should
119 * result in setjmp() returning a value of one.
120 */
121 test->expected = ( value ? value : 1 );
122
123 /* Record test code file and line */
124 test->file = file;
125 test->line = line;
126
127 /* Record expected jump */
128 jumped = test;
129
130 /* Perform longjmp(). Should return via setjmp_okx() */
131 longjmp ( test->env, value );
132
133 /* longjmp() should never return */
134 assert ( 0 );
135}
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50

References assert, setjmp_test::file, jumped, setjmp_test::line, longjmp_okx(), test, and value.

Referenced by longjmp_okx().

◆ setjmp_test_exec()

void setjmp_test_exec ( void )
static

Perform setjmp()/longjmp() self-tests.

Definition at line 143 of file setjmp_test.c.

143 {
144 static struct setjmp_test alpha;
145 static struct setjmp_test beta;
146 static int iteration;
147
148 /* This is one of the very few situations in which the
149 * "for-case" pattern is justified.
150 */
151 for ( iteration = 0 ; iteration < 10 ; iteration++ ) {
152 DBGC ( jumped, "SETJMP test iteration %d\n", iteration );
153 switch ( iteration ) {
154 case 0: setjmp_ok ( &alpha ); break;
155 case 1: setjmp_ok ( &beta ); break;
156 case 2: longjmp_ok ( &alpha, 0 );
157 case 3: longjmp_ok ( &alpha, 1 );
158 case 4: longjmp_ok ( &alpha, 2 );
159 case 5: longjmp_ok ( &beta, 17 );
160 case 6: longjmp_ok ( &beta, 29 );
161 case 7: longjmp_ok ( &alpha, -1 );
162 case 8: longjmp_ok ( &beta, 0 );
163 case 9: longjmp_ok ( &beta, 42 );
164 }
165 }
166}
#define DBGC(...)
Definition compiler.h:505
#define setjmp_ok(test)
Report a setjmp() test result.
Definition setjmp_test.c:64
#define longjmp_ok(test, value)
A setjmp()/longjmp() test.
Definition setjmp_test.c:41

References DBGC, jumped, longjmp_ok, and setjmp_ok.

Variable Documentation

◆ jumped

struct setjmp_test* jumped
static

Expected jump.

Definition at line 53 of file setjmp_test.c.

Referenced by longjmp_okx(), setjmp_return_ok(), and setjmp_test_exec().

◆ __self_test

struct self_test setjmp_test __self_test
Initial value:
= {
.name = "setjmp",
}
static void setjmp_test_exec(void)
Perform setjmp()/longjmp() self-tests.

setjmp()/longjmp() self-test

Definition at line 169 of file setjmp_test.c.

169 {
170 .name = "setjmp",
171 .exec = setjmp_test_exec,
172};