iPXE
test.c File Reference

Self-test infrastructure. More...

#include <stddef.h>
#include <stdio.h>
#include <string.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()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ test_ok()

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 56 of file test.c.

57 {
58
59 /* Sanity check */
61
62 /* Increment test counter */
63 current_tests->total++;
64
65 /* Report failure if applicable */
66 if ( ! success ) {
67 current_tests->failures++;
68 printf ( "FAILURE: \"%s\" test failed at %s line %d: ( %s )\n",
69 current_tests->name, file, line, test );
70 }
71}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
#define assert(condition)
Assert a condition at run-time.
Definition assert.h:50
static int test
Definition epic100.c:73
static struct self_test * current_tests
Current self-test set.
Definition test.c:46
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition vsprintf.c:465

References assert, current_tests, NULL, printf(), and test.

◆ run_tests()

void run_tests ( struct self_test * tests)
static

Run self-test set.

Definition at line 77 of file test.c.

77 {
78 unsigned int old_assertion_failures = assertion_failures;
79
80 /* Sanity check */
82
83 /* Record current test set */
84 current_tests = tests;
85
86 /* Run tests */
87 tests->exec();
88
89 /* Clear current test set */
91
92 /* Record number of assertion failures */
93 tests->assertion_failures =
94 ( assertion_failures - old_assertion_failures );
95
96 /* Print test set summary */
97 if ( tests->failures || tests->assertion_failures ) {
98 printf ( "FAILURE: \"%s\" %d of %d tests failed",
99 tests->name, tests->failures, tests->total );
100 if ( tests->assertion_failures ) {
101 printf ( " with %d assertion failures",
102 tests->assertion_failures );
103 }
104 printf ( "\n" );
105 } else {
106 printf ( "OK: \"%s\" %d tests passed\n",
107 tests->name, tests->total );
108 }
109}
unsigned int assertion_failures
Number of assertion failures triggered.
Definition assert.c:35
unsigned int failures
Number of test failures.
Definition test.h:23
unsigned int total
Number of tests run.
Definition test.h:21
const char * name
Test set name.
Definition test.h:17
unsigned int assertion_failures
Number of assertion failures.
Definition test.h:25
void(* exec)(void)
Run self-tests.
Definition test.h:19

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

Referenced by run_all_tests().

◆ run_all_tests()

int run_all_tests ( void )
static

Run all self-tests.

Return values
rcReturn status code

Definition at line 116 of file test.c.

116 {
117 struct self_test *tests;
118 unsigned int failures = 0;
119 unsigned int assertions = 0;
120 unsigned int total = 0;
121
122 /* Run all compiled-in self-tests */
123 printf ( "Starting %s self-tests\n", _S2 ( ARCH ) );
125 run_tests ( tests );
126
127 /* Print overall summary */
129 total += tests->total;
130 failures += tests->failures;
131 assertions += tests->assertion_failures;
132 }
133 if ( failures || assertions ) {
134 printf ( "FAILURE: %d of %d tests failed",
135 failures, total );
136 if ( assertions ) {
137 printf ( " with %d assertion failures", assertions );
138 }
139 printf ( "\n" );
140 return -EINPROGRESS;
141 } else {
142 printf ( "OK: all %d tests passed\n", total );
143 profstat();
144 return 0;
145 }
146}
#define EINPROGRESS
Operation in progress.
Definition errno.h:419
#define _S2(x)
Stringify expanded argument.
Definition compiler.h:53
void profstat(void)
Print profiling statistics.
Definition profstat.c:41
A self-test set.
Definition test.h:15
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition tables.h:386
static void run_tests(struct self_test *tests)
Run self-test set.
Definition test.c:77
#define SELF_TESTS
Self-test table.
Definition test.h:29

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

Referenced by test_image_exec().

◆ test_image_probe()

int test_image_probe ( struct image *image __unused)
static

Definition at line 148 of file test.c.

148 {
149 return -ENOTTY;
150}
#define ENOTTY
Inappropriate I/O control operation.
Definition errno.h:595

References __unused, and ENOTTY.

◆ test_image_exec()

int test_image_exec ( struct image *image __unused)
static

Definition at line 152 of file test.c.

152 {
153 return run_all_tests();
154}
static int run_all_tests(void)
Run all self-tests.
Definition test.c:116

References __unused, and run_all_tests().

◆ test_init()

void test_init ( void )
static

Definition at line 169 of file test.c.

169 {
170 int rc;
171
172 /* Register self-tests image */
173 if ( ( rc = register_image ( &test_image ) ) != 0 ) {
174 DBG ( "Could not register self-test image: %s\n",
175 strerror ( rc ) );
176 /* No way to report failure */
177 return;
178 }
179}
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
#define DBG(...)
Print a debugging message.
Definition compiler.h:498
int register_image(struct image *image)
Register executable image.
Definition image.c:315
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
static struct image test_image
Definition test.c:162

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

Referenced by __init_fn().

◆ __init_fn()

struct init_fn test_init_fn __init_fn ( INIT_EARLY )

Self-test initialisation function.

References __init_fn, INIT_EARLY, and test_init().

Variable Documentation

◆ current_tests

struct self_test* current_tests
static

Current self-test set.

Definition at line 46 of file test.c.

Referenced by run_tests(), and test_ok().

◆ test_image_type

struct image_type test_image_type
static
Initial value:
= {
.name = "self-tests",
.probe = test_image_probe,
.exec = test_image_exec,
}
static int test_image_exec(struct image *image __unused)
Definition test.c:152
static int test_image_probe(struct image *image __unused)
Definition test.c:148

Definition at line 156 of file test.c.

156 {
157 .name = "self-tests",
158 .probe = test_image_probe,
159 .exec = test_image_exec,
160};

◆ test_image

struct image test_image
static
Initial value:
= {
.refcnt = REF_INIT ( ref_no_free ),
.name = "<TESTS>",
}
uint32_t type
Operating system type.
Definition ena.h:1
#define IMAGE_STATIC_NAME
Image name is statically allocated.
Definition image.h:92
#define IMAGE_STATIC
Image is statically allocated.
Definition image.h:89
void ref_no_free(struct refcnt *refcnt __unused)
Do not free reference-counted object.
Definition refcnt.c:102
#define REF_INIT(free_fn)
Initialise a static reference counter.
Definition refcnt.h:78
static struct image_type test_image_type
Definition test.c:156

Definition at line 162 of file test.c.

162 {
163 .refcnt = REF_INIT ( ref_no_free ),
164 .name = "<TESTS>",
165 .flags = ( IMAGE_STATIC | IMAGE_STATIC_NAME ),
167};

Referenced by test_init().