2 * Copyright (C) 2011 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
28 * Self-test infrastructure
32 /* Forcibly enable assertions */
39 #include <ipxe/test.h>
40 #include <ipxe/init.h>
41 #include <ipxe/image.h>
42 #include <usr/profstat.h>
44 /** Current self-test set */
45 static struct self_test
*current_tests
;
50 * @v success Test succeeded
51 * @v file Test code file
52 * @v line Test code line
55 void test_ok ( int success
, const char *file
, unsigned int line
,
59 assert ( current_tests
!= NULL
);
61 /* Increment test counter */
62 current_tests
->total
++;
64 /* Report failure if applicable */
66 current_tests
->failures
++;
67 printf ( "FAILURE: \"%s\" test failed at %s line %d: ( %s )\n",
68 current_tests
->name
, file
, line
, test
);
76 static void run_tests ( struct self_test
*tests
) {
77 unsigned int old_assertion_failures
= assertion_failures
;
80 assert ( current_tests
== NULL
);
82 /* Record current test set */
83 current_tests
= tests
;
88 /* Clear current test set */
91 /* Record number of assertion failures */
92 tests
->assertion_failures
=
93 ( assertion_failures
- old_assertion_failures
);
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
);
105 printf ( "OK: \"%s\" %d tests passed\n",
106 tests
->name
, tests
->total
);
113 * @ret rc Return status code
115 static int run_all_tests ( void ) {
116 struct self_test
*tests
;
117 unsigned int failures
= 0;
118 unsigned int assertions
= 0;
119 unsigned int total
= 0;
121 /* Run all compiled-in self-tests */
122 printf ( "Starting self-tests\n" );
123 for_each_table_entry ( tests
, SELF_TESTS
)
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
;
132 if ( failures
|| assertions
) {
133 printf ( "FAILURE: %d of %d tests failed",
136 printf ( " with %d assertion failures", assertions
);
141 printf ( "OK: all %d tests passed\n", total
);
147 static int test_image_probe ( struct image
*image __unused
) {
151 static int test_image_exec ( struct image
*image __unused
) {
152 return run_all_tests();
155 static struct image_type test_image_type
= {
156 .name
= "self-tests",
157 .probe
= test_image_probe
,
158 .exec
= test_image_exec
,
161 static struct image test_image
= {
162 .refcnt
= REF_INIT ( ref_no_free
),
164 .type
= &test_image_type
,
167 static void test_init ( void ) {
170 /* Register self-tests image */
171 if ( ( rc
= register_image ( &test_image
) ) != 0 ) {
172 DBG ( "Could not register self-test image: %s\n",
174 /* No way to report failure */
179 /** Self-test initialisation function */
180 struct init_fn test_init_fn
__init_fn ( INIT_EARLY
) = {
181 .initialise
= test_init
,