iPXE
Data Structures | Defines | 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...

Defines

#define setjmp_ok(test)
 Report a setjmp() test result.
#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.
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.


Define Documentation

#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 )

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.

Referenced by setjmp_test_exec().

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

Definition at line 136 of file setjmp_test.c.

Referenced by setjmp_test_exec().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
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.

References setjmp_test::expected, setjmp_test::file, setjmp_test::line, NULL, and okx.

                                                                     {

        /* Determine whether this was reached via setjmp() or longjmp() */
        if ( value == 0 ) {
                /* This is the initial call to setjmp() */
                okx ( test->expected == 0, test->file, test->line );
                okx ( jumped == NULL, test->file, test->line );
        } else {
                /* This is reached via a call to longjmp() */
                okx ( value == test->expected, test->file, test->line );
                okx ( jumped == test, test->file, test->line );
        }

        /* Clear expected jump */
        jumped = NULL;
}
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.

References assert, setjmp_test::file, setjmp_test::line, and test.

                                                    {

        /* Record expected value.  A zero passed to longjmp() should
         * result in setjmp() returning a value of one.
         */
        test->expected = ( value ? value : 1 );

        /* Record test code file and line */
        test->file = file;
        test->line = line;

        /* Record expected jump */
        jumped = test;

        /* Perform longjmp().  Should return via setjmp_okx() */
        longjmp ( test->env, value );

        /* longjmp() should never return */
        assert ( 0 );
}
static void setjmp_test_exec ( void  ) [static]

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

Definition at line 143 of file setjmp_test.c.

References DBGC, longjmp_ok, and setjmp_ok.

                                      {
        static struct setjmp_test alpha;
        static struct setjmp_test beta;
        static int iteration;

        /* This is one of the very few situations in which the
         * "for-case" pattern is justified.
         */
        for ( iteration = 0 ; iteration < 10 ; iteration++ ) {
                DBGC ( jumped, "SETJMP test iteration %d\n", iteration );
                switch ( iteration ) {
                case 0: setjmp_ok ( &alpha ); break;
                case 1: setjmp_ok ( &beta ); break;
                case 2: longjmp_ok ( &alpha, 0 );
                case 3: longjmp_ok ( &alpha, 1 );
                case 4: longjmp_ok ( &alpha, 2 );
                case 5: longjmp_ok ( &beta, 17 );
                case 6: longjmp_ok ( &beta, 29 );
                case 7: longjmp_ok ( &alpha, -1 );
                case 8: longjmp_ok ( &beta, 0 );
                case 9: longjmp_ok ( &beta, 42 );
                }
        }
}

Variable Documentation

struct setjmp_test* jumped [static]

Expected jump.

Definition at line 53 of file setjmp_test.c.

Initial value:
 {
        .name = "setjmp",
        .exec = setjmp_test_exec,
}

setjmp()/longjmp() self-test

Definition at line 169 of file setjmp_test.c.