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. More...
 
static void run_tests (struct self_test *tests)
 Run self-test set. More...
 
static int run_all_tests (void)
 Run all self-tests. More...
 
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. More...
 

Variables

static struct self_testcurrent_tests
 Current self-test set. More...
 
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 55 of file test.c.

56  {
57 
58  /* Sanity check */
59  assert ( current_tests != NULL );
60 
61  /* Increment test counter */
63 
64  /* Report failure if applicable */
65  if ( ! success ) {
67  printf ( "FAILURE: \"%s\" test failed at %s line %d: ( %s )\n",
68  current_tests->name, file, line, test );
69  }
70 }
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
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
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
static struct self_test * current_tests
Current self-test set.
Definition: test.c:45
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static int test
Definition: epic100.c:73

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

◆ run_tests()

static void run_tests ( struct self_test tests)
static

Run self-test set.

Definition at line 76 of file test.c.

76  {
77  unsigned int old_assertion_failures = assertion_failures;
78 
79  /* Sanity check */
80  assert ( current_tests == NULL );
81 
82  /* Record current test set */
83  current_tests = tests;
84 
85  /* Run tests */
86  tests->exec();
87 
88  /* Clear current test set */
90 
91  /* Record number of assertion failures */
92  tests->assertion_failures =
93  ( assertion_failures - old_assertion_failures );
94 
95  /* Print test set summary */
96  if ( tests->failures || tests->assertion_failures ) {
97  printf ( "FAILURE: \"%s\" %d of %d tests failed",
98  tests->name, tests->failures, tests->total );
99  if ( tests->assertion_failures ) {
100  printf ( " with %d assertion failures",
101  tests->assertion_failures );
102  }
103  printf ( "\n" );
104  } else {
105  printf ( "OK: \"%s\" %d tests passed\n",
106  tests->name, tests->total );
107  }
108 }
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
unsigned int failures
Number of test failures.
Definition: test.h:23
unsigned int total
Number of tests run.
Definition: test.h:21
void(* exec)(void)
Run self-tests.
Definition: test.h:19
const char * name
Test set name.
Definition: test.h:17
unsigned int assertion_failures
Number of assertion failures triggered.
Definition: assert.c:35
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
unsigned int assertion_failures
Number of assertion failures.
Definition: test.h:25
static struct self_test * current_tests
Current self-test set.
Definition: test.c:45
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

References assert(), self_test::assertion_failures, 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()

static int run_all_tests ( void  )
static

Run all self-tests.

Return values
rcReturn status code

Definition at line 115 of file test.c.

115  {
116  struct self_test *tests;
117  unsigned int failures = 0;
118  unsigned int assertions = 0;
119  unsigned int total = 0;
120 
121  /* Run all compiled-in self-tests */
122  printf ( "Starting self-tests\n" );
124  run_tests ( tests );
125 
126  /* Print overall summary */
127  for_each_table_entry ( tests, SELF_TESTS ) {
128  total += tests->total;
129  failures += tests->failures;
130  assertions += tests->assertion_failures;
131  }
132  if ( failures || assertions ) {
133  printf ( "FAILURE: %d of %d tests failed",
134  failures, total );
135  if ( assertions ) {
136  printf ( " with %d assertion failures", assertions );
137  }
138  printf ( "\n" );
139  return -EINPROGRESS;
140  } else {
141  printf ( "OK: all %d tests passed\n", total );
142  profstat();
143  return 0;
144  }
145 }
int printf(const char *fmt,...)
Write a formatted string to the console.
Definition: vsprintf.c:464
unsigned int failures
Number of test failures.
Definition: test.h:23
unsigned int total
Number of tests run.
Definition: test.h:21
A self-test set.
Definition: test.h:15
void profstat(void)
Print profiling statistics.
Definition: profstat.c:40
#define EINPROGRESS
Operation in progress.
Definition: errno.h:418
#define SELF_TESTS
Self-test table.
Definition: test.h:29
unsigned int assertion_failures
Number of assertion failures.
Definition: test.h:25
#define for_each_table_entry(pointer, table)
Iterate through all entries within a linker table.
Definition: tables.h:358
static void run_tests(struct self_test *tests)
Run self-test set.
Definition: test.c:76

References 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()

static int test_image_probe ( struct image *image  __unused)
static

Definition at line 147 of file test.c.

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

References ENOTTY.

◆ test_image_exec()

static int test_image_exec ( struct image *image  __unused)
static

Definition at line 151 of file test.c.

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

References run_all_tests().

◆ test_init()

static void test_init ( void  )
static

Definition at line 167 of file test.c.

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

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

◆ __init_fn()

struct init_fn test_init_fn __init_fn ( INIT_EARLY  )

Self-test initialisation function.

Variable Documentation

◆ current_tests

struct self_test* current_tests
static

Current self-test set.

Definition at line 45 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:151
static int test_image_probe(struct image *image __unused)
Definition: test.c:147

Definition at line 155 of file test.c.

◆ test_image

struct image test_image
static
Initial value:
= {
.refcnt = REF_INIT ( ref_no_free ),
.name = "<TESTS>",
.type = &test_image_type,
}
static struct image_type test_image_type
Definition: test.c:155
#define REF_INIT(free_fn)
Initialise a static reference counter.
Definition: refcnt.h:77
void ref_no_free(struct refcnt *refcnt __unused)
Do not free reference-counted object.
Definition: refcnt.c:101

Definition at line 161 of file test.c.

Referenced by test_init().