iPXE
Data Structures | Macros | Functions | Variables
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. More...
 
#define longjmp_ok(test, value)   longjmp_okx ( test, value, __FILE__, __LINE__ )
 

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. More...
 
static void longjmp_okx (struct setjmp_test *test, int value, const char *file, unsigned int line)
 Report a longjmp() test result. More...
 
static void setjmp_test_exec (void)
 Perform setjmp()/longjmp() self-tests. More...
 

Variables

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

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 )
static struct setjmp_test * jumped
Expected jump.
Definition: setjmp_test.c:53
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static int test
Definition: epic100.c:73

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.

◆ longjmp_ok

#define longjmp_ok (   test,
  value 
)    longjmp_okx ( test, value, __FILE__, __LINE__ )

Definition at line 136 of file setjmp_test.c.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ setjmp_return_ok()

static 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 }
static struct setjmp_test * jumped
Expected jump.
Definition: setjmp_test.c:53
#define okx(success, file, line)
Report test result.
Definition: test.h:44
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static int test
Definition: epic100.c:73

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

◆ longjmp_okx()

static 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 }
static struct setjmp_test * jumped
Expected jump.
Definition: setjmp_test.c:53
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
static int test
Definition: epic100.c:73

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

◆ setjmp_test_exec()

static 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 longjmp_ok(test, value)
Definition: setjmp_test.c:136
#define DBGC(...)
Definition: compiler.h:505
#define setjmp_ok(test)
Report a setjmp() test result.
Definition: setjmp_test.c:64
static struct setjmp_test * jumped
Expected jump.
Definition: setjmp_test.c:53
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.
Definition: setjmp_test.c:143

setjmp()/longjmp() self-test

Definition at line 169 of file setjmp_test.c.