iPXE
Functions | Variables
test.c File Reference

Self-test infrastructure. More...

#include <stddef.h>
#include <stdio.h>
#include <errno.h>
#include <assert.h>
#include <ipxe/test.h>
#include <ipxe/init.h>
#include <ipxe/image.h>
#include <usr/profstat.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
void test_ok (int success, const char *file, unsigned int line, const char *test)
 Report test result.
static void run_tests (struct self_test *tests)
 Run self-test set.
static int run_all_tests (void)
 Run all self-tests.
static int test_image_probe (struct image *image __unused)
static int test_image_exec (struct image *image __unused)
static void test_init (void)
struct init_fn test_init_fn __init_fn (INIT_EARLY)
 Self-test initialisation function.

Variables

static struct self_testcurrent_tests
 Current self-test set.
static struct image_type test_image_type
static struct image test_image

Detailed Description

Self-test infrastructure.

Definition in file test.c.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
void test_ok ( int  success,
const char *  file,
unsigned int  line,
const char *  test 
)

Report test result.

Parameters:
successTest succeeded
fileTest code file
lineTest code line
testTest code

Definition at line 55 of file test.c.

References assert, self_test::failures, self_test::name, NULL, printf(), and self_test::total.

                                  {

        /* Sanity check */
        assert ( current_tests != NULL );

        /* Increment test counter */
        current_tests->total++;

        /* Report failure if applicable */
        if ( ! success ) {
                current_tests->failures++;
                printf ( "FAILURE: \"%s\" test failed at %s line %d: ( %s )\n",
                         current_tests->name, file, line, test );
        }
}
static void run_tests ( struct self_test tests) [static]

Run self-test set.

Definition at line 76 of file test.c.

References assert, self_test::assertion_failures, assertion_failures, self_test::exec, self_test::failures, self_test::name, NULL, printf(), and self_test::total.

Referenced by run_all_tests().

                                                  {
        unsigned int old_assertion_failures = assertion_failures;

        /* Sanity check */
        assert ( current_tests == NULL );

        /* Record current test set */
        current_tests = tests;

        /* Run tests */
        tests->exec();

        /* Clear current test set */
        current_tests = NULL;

        /* Record number of assertion failures */
        tests->assertion_failures =
                ( assertion_failures - old_assertion_failures );

        /* Print test set summary */
        if ( tests->failures || tests->assertion_failures ) {
                printf ( "FAILURE: \"%s\" %d of %d tests failed",
                         tests->name, tests->failures, tests->total );
                if ( tests->assertion_failures ) {
                        printf ( " with %d assertion failures",
                                 tests->assertion_failures );
                }
                printf ( "\n" );
        } else {
                printf ( "OK: \"%s\" %d tests passed\n",
                         tests->name, tests->total );
        }
}
static int run_all_tests ( void  ) [static]

Run all self-tests.

Return values:
rcReturn status code

Definition at line 115 of file test.c.

References self_test::assertion_failures, EINPROGRESS, self_test::failures, for_each_table_entry, printf(), profstat(), run_tests(), SELF_TESTS, self_test::total, and total.

Referenced by test_image_exec().

                                  {
        struct self_test *tests;
        unsigned int failures = 0;
        unsigned int assertions = 0;
        unsigned int total = 0;

        /* Run all compiled-in self-tests */
        printf ( "Starting self-tests\n" );
        for_each_table_entry ( tests, SELF_TESTS )
                run_tests ( tests );

        /* Print overall summary */
        for_each_table_entry ( tests, SELF_TESTS ) {
                total += tests->total;
                failures += tests->failures;
                assertions += tests->assertion_failures;
        }
        if ( failures || assertions ) {
                printf ( "FAILURE: %d of %d tests failed",
                         failures, total );
                if ( assertions ) {
                        printf ( " with %d assertion failures", assertions );
                }
                printf ( "\n" );
                return -EINPROGRESS;
        } else {
                printf ( "OK: all %d tests passed\n", total );
                profstat();
                return 0;
        }
}
static int test_image_probe ( struct image *image  __unused) [static]

Definition at line 147 of file test.c.

References ENOTTY.

                                                             {
        return -ENOTTY;
}
static int test_image_exec ( struct image *image  __unused) [static]

Definition at line 151 of file test.c.

References run_all_tests().

                                                            {
        return run_all_tests();
}
static void test_init ( void  ) [static]

Definition at line 167 of file test.c.

References DBG, rc, register_image(), and strerror().

                               {
        int rc;

        /* Register self-tests image */
        if ( ( rc = register_image ( &test_image ) ) != 0 ) {
                DBG ( "Could not register self-test image: %s\n",
                      strerror ( rc ) );
                /* No way to report failure */
                return;
        }
}
struct init_fn test_init_fn __init_fn ( INIT_EARLY  ) [read]

Self-test initialisation function.


Variable Documentation

struct self_test* current_tests [static]

Current self-test set.

Definition at line 45 of file test.c.

struct image_type test_image_type [static]
Initial value:
 {
        .name = "self-tests",
        .probe = test_image_probe,
        .exec = test_image_exec,
}

Definition at line 155 of file test.c.

struct image test_image [static]
Initial value:
 {
        .refcnt = REF_INIT ( ref_no_free ),
        .name = "<TESTS>",
        .type = &test_image_type,
}

Definition at line 161 of file test.c.