1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
5 * kselftest_harness.h: simple C unit test helper.
7 * See documentation in Documentation/dev-tools/kselftest.rst
9 * API inspired by code.google.com/p/googletest
17 * #include "../kselftest_harness.h"
19 * TEST(standalone_test) {
21 * EXPECT_GT(10, stuff) {
22 * stuff_state_t state;
23 * enumerate_stuff_state(&state);
24 * TH_LOG("expectation failed with state: %s", state.msg);
27 * ASSERT_NE(some_stuff, NULL) TH_LOG("how did it happen?!");
29 * EXPECT_EQ(0, last_stuff);
32 * FIXTURE(my_fixture) {
34 * int awesomeness_level;
36 * FIXTURE_SETUP(my_fixture) {
37 * self->data = mytype_new();
38 * ASSERT_NE(NULL, self->data);
40 * FIXTURE_TEARDOWN(my_fixture) {
41 * mytype_free(self->data);
43 * TEST_F(my_fixture, data_is_good) {
44 * EXPECT_EQ(1, is_my_data_good(self->data));
50 #ifndef __KSELFTEST_HARNESS_H
51 #define __KSELFTEST_HARNESS_H
54 #include <asm/types.h>
61 #include <sys/types.h>
65 #define TEST_TIMEOUT_DEFAULT 30
67 /* Utilities exposed to the test definitions */
69 # define TH_LOG_STREAM stderr
72 #ifndef TH_LOG_ENABLED
73 # define TH_LOG_ENABLED 1
80 * @...: optional arguments
86 * Optional debug logging function available for use in tests.
87 * Logging may be enabled or disabled by defining TH_LOG_ENABLED.
88 * E.g., #define TH_LOG_ENABLED 1
90 * If no definition is provided, logging is enabled by default.
92 * If there is no way to print an error message for the process running the
93 * test (e.g. not allowed to write to stderr), it is still possible to get the
94 * ASSERT_* number for which the test failed. This behavior can be enabled by
95 * writing `_metadata->no_print = true;` before the check sequence that is
96 * unable to print. When an error occur, instead of printing an error message
97 * and calling `abort(3)`, the test process call `_exit(2)` with the assert
98 * number as argument, which is then printed by the parent process.
100 #define TH_LOG(fmt, ...) do { \
101 if (TH_LOG_ENABLED) \
102 __TH_LOG(fmt, ##__VA_ARGS__); \
105 /* Unconditional logger for internal use. */
106 #define __TH_LOG(fmt, ...) \
107 fprintf(TH_LOG_STREAM, "%s:%d:%s:" fmt "\n", \
108 __FILE__, __LINE__, _metadata->name, ##__VA_ARGS__)
111 * XFAIL(statement, fmt, ...)
113 * @statement: statement to run after reporting XFAIL
114 * @fmt: format string
115 * @...: optional arguments
117 * This forces a "pass" after reporting a failure with an XFAIL prefix,
118 * and runs "statement", which is usually "return" or "goto skip".
120 #define XFAIL(statement, fmt, ...) do { \
121 if (TH_LOG_ENABLED) { \
122 fprintf(TH_LOG_STREAM, "[ XFAIL! ] " fmt "\n", \
125 /* TODO: find a way to pass xfail to test runner process. */ \
126 _metadata->passed = 1; \
127 _metadata->trigger = 0; \
132 * TEST(test_name) - Defines the test function and creates the registration
135 * @test_name: test name
139 * TEST(name) { implementation }
141 * Defines a test by name.
142 * Names must be unique and tests must not be run in parallel. The
143 * implementation containing block is a function and scoping should be treated
144 * as such. Returning early may be performed with a bare "return;" statement.
146 * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
148 #define TEST(test_name) __TEST_IMPL(test_name, -1)
151 * TEST_SIGNAL(test_name, signal)
153 * @test_name: test name
154 * @signal: signal number
158 * TEST_SIGNAL(name, signal) { implementation }
160 * Defines a test by name and the expected term signal.
161 * Names must be unique and tests must not be run in parallel. The
162 * implementation containing block is a function and scoping should be treated
163 * as such. Returning early may be performed with a bare "return;" statement.
165 * EXPECT_* and ASSERT_* are valid in a TEST() { } context.
167 #define TEST_SIGNAL(test_name, signal) __TEST_IMPL(test_name, signal)
169 #define __TEST_IMPL(test_name, _signal) \
170 static void test_name(struct __test_metadata *_metadata); \
171 static inline void wrapper_##test_name( \
172 struct __test_metadata *_metadata, \
173 struct __fixture_variant_metadata *variant) \
175 test_name(_metadata); \
177 static struct __test_metadata _##test_name##_object = \
178 { .name = #test_name, \
179 .fn = &wrapper_##test_name, \
180 .fixture = &_fixture_global, \
181 .termsig = _signal, \
182 .timeout = TEST_TIMEOUT_DEFAULT, }; \
183 static void __attribute__((constructor)) _register_##test_name(void) \
185 __register_test(&_##test_name##_object); \
187 static void test_name( \
188 struct __test_metadata __attribute__((unused)) *_metadata)
191 * FIXTURE_DATA(datatype_name) - Wraps the struct name so we have one less
192 * argument to pass around
194 * @datatype_name: datatype name
198 * FIXTURE_DATA(datatype name)
200 * This call may be used when the type of the fixture data
201 * is needed. In general, this should not be needed unless
202 * the *self* is being passed to a helper directly.
204 #define FIXTURE_DATA(datatype_name) struct _test_data_##datatype_name
207 * FIXTURE(fixture_name) - Called once per fixture to setup the data and
210 * @fixture_name: fixture name
214 * FIXTURE(datatype name) {
219 * Defines the data provided to TEST_F()-defined tests as *self*. It should be
220 * populated and cleaned up using FIXTURE_SETUP() and FIXTURE_TEARDOWN().
222 #define FIXTURE(fixture_name) \
223 FIXTURE_VARIANT(fixture_name); \
224 static struct __fixture_metadata _##fixture_name##_fixture_object = \
225 { .name = #fixture_name, }; \
226 static void __attribute__((constructor)) \
227 _register_##fixture_name##_data(void) \
229 __register_fixture(&_##fixture_name##_fixture_object); \
231 FIXTURE_DATA(fixture_name)
234 * FIXTURE_SETUP(fixture_name) - Prepares the setup function for the fixture.
235 * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
237 * @fixture_name: fixture name
241 * FIXTURE_SETUP(fixture name) { implementation }
243 * Populates the required "setup" function for a fixture. An instance of the
244 * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
247 * ASSERT_* are valid for use in this context and will prempt the execution
248 * of any dependent fixture tests.
250 * A bare "return;" statement may be used to return early.
252 #define FIXTURE_SETUP(fixture_name) \
253 void fixture_name##_setup( \
254 struct __test_metadata __attribute__((unused)) *_metadata, \
255 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
256 const FIXTURE_VARIANT(fixture_name) \
257 __attribute__((unused)) *variant)
260 * FIXTURE_TEARDOWN(fixture_name)
261 * *_metadata* is included so that EXPECT_* and ASSERT_* work correctly.
263 * @fixture_name: fixture name
267 * FIXTURE_TEARDOWN(fixture name) { implementation }
269 * Populates the required "teardown" function for a fixture. An instance of the
270 * datatype defined with FIXTURE_DATA() will be exposed as *self* for the
271 * implementation to clean up.
273 * A bare "return;" statement may be used to return early.
275 #define FIXTURE_TEARDOWN(fixture_name) \
276 void fixture_name##_teardown( \
277 struct __test_metadata __attribute__((unused)) *_metadata, \
278 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self)
281 * FIXTURE_VARIANT(fixture_name) - Optionally called once per fixture
282 * to declare fixture variant
284 * @fixture_name: fixture name
288 * FIXTURE_VARIANT(datatype name) {
293 * Defines type of constant parameters provided to FIXTURE_SETUP() and TEST_F()
294 * as *variant*. Variants allow the same tests to be run with different
297 #define FIXTURE_VARIANT(fixture_name) struct _fixture_variant_##fixture_name
300 * FIXTURE_VARIANT_ADD(fixture_name, variant_name) - Called once per fixture
301 * variant to setup and register the data
303 * @fixture_name: fixture name
304 * @variant_name: name of the parameter set
308 * FIXTURE_ADD(datatype name) {
313 * Defines a variant of the test fixture, provided to FIXTURE_SETUP() and
314 * TEST_F() as *variant*. Tests of each fixture will be run once for each
317 #define FIXTURE_VARIANT_ADD(fixture_name, variant_name) \
318 extern FIXTURE_VARIANT(fixture_name) \
319 _##fixture_name##_##variant_name##_variant; \
320 static struct __fixture_variant_metadata \
321 _##fixture_name##_##variant_name##_object = \
322 { .name = #variant_name, \
323 .data = &_##fixture_name##_##variant_name##_variant}; \
324 static void __attribute__((constructor)) \
325 _register_##fixture_name##_##variant_name(void) \
327 __register_fixture_variant(&_##fixture_name##_fixture_object, \
328 &_##fixture_name##_##variant_name##_object); \
330 FIXTURE_VARIANT(fixture_name) \
331 _##fixture_name##_##variant_name##_variant =
334 * TEST_F(fixture_name, test_name) - Emits test registration and helpers for
335 * fixture-based test cases
337 * @fixture_name: fixture name
338 * @test_name: test name
342 * TEST_F(fixture, name) { implementation }
344 * Defines a test that depends on a fixture (e.g., is part of a test case).
345 * Very similar to TEST() except that *self* is the setup instance of fixture's
346 * datatype exposed for use by the implementation.
348 * Warning: use of ASSERT_* here will skip TEARDOWN.
350 /* TODO(wad) register fixtures on dedicated test lists. */
351 #define TEST_F(fixture_name, test_name) \
352 __TEST_F_IMPL(fixture_name, test_name, -1, TEST_TIMEOUT_DEFAULT)
354 #define TEST_F_SIGNAL(fixture_name, test_name, signal) \
355 __TEST_F_IMPL(fixture_name, test_name, signal, TEST_TIMEOUT_DEFAULT)
357 #define TEST_F_TIMEOUT(fixture_name, test_name, timeout) \
358 __TEST_F_IMPL(fixture_name, test_name, -1, timeout)
360 #define __TEST_F_IMPL(fixture_name, test_name, signal, tmout) \
361 static void fixture_name##_##test_name( \
362 struct __test_metadata *_metadata, \
363 FIXTURE_DATA(fixture_name) *self, \
364 const FIXTURE_VARIANT(fixture_name) *variant); \
365 static inline void wrapper_##fixture_name##_##test_name( \
366 struct __test_metadata *_metadata, \
367 struct __fixture_variant_metadata *variant) \
369 /* fixture data is alloced, setup, and torn down per call. */ \
370 FIXTURE_DATA(fixture_name) self; \
371 memset(&self, 0, sizeof(FIXTURE_DATA(fixture_name))); \
372 fixture_name##_setup(_metadata, &self, variant->data); \
373 /* Let setup failure terminate early. */ \
374 if (!_metadata->passed) \
376 fixture_name##_##test_name(_metadata, &self, variant->data); \
377 fixture_name##_teardown(_metadata, &self); \
379 static struct __test_metadata \
380 _##fixture_name##_##test_name##_object = { \
381 .name = #test_name, \
382 .fn = &wrapper_##fixture_name##_##test_name, \
383 .fixture = &_##fixture_name##_fixture_object, \
387 static void __attribute__((constructor)) \
388 _register_##fixture_name##_##test_name(void) \
390 __register_test(&_##fixture_name##_##test_name##_object); \
392 static void fixture_name##_##test_name( \
393 struct __test_metadata __attribute__((unused)) *_metadata, \
394 FIXTURE_DATA(fixture_name) __attribute__((unused)) *self, \
395 const FIXTURE_VARIANT(fixture_name) \
396 __attribute__((unused)) *variant)
399 * TEST_HARNESS_MAIN - Simple wrapper to run the test harness
405 * Use once to append a main() to the test file.
407 #define TEST_HARNESS_MAIN \
408 static void __attribute__((constructor)) \
409 __constructor_order_last(void) \
411 if (!__constructor_order) \
412 __constructor_order = _CONSTRUCTOR_ORDER_BACKWARD; \
414 int main(int argc, char **argv) { \
415 return test_harness_run(argc, argv); \
421 * Operators for use in TEST() and TEST_F().
422 * ASSERT_* calls will stop test execution immediately.
423 * EXPECT_* calls will emit a failure warning, note it, and continue.
427 * ASSERT_EQ(expected, seen)
429 * @expected: expected value
430 * @seen: measured value
432 * ASSERT_EQ(expected, measured): expected == measured
434 #define ASSERT_EQ(expected, seen) \
435 __EXPECT(expected, #expected, seen, #seen, ==, 1)
438 * ASSERT_NE(expected, seen)
440 * @expected: expected value
441 * @seen: measured value
443 * ASSERT_NE(expected, measured): expected != measured
445 #define ASSERT_NE(expected, seen) \
446 __EXPECT(expected, #expected, seen, #seen, !=, 1)
449 * ASSERT_LT(expected, seen)
451 * @expected: expected value
452 * @seen: measured value
454 * ASSERT_LT(expected, measured): expected < measured
456 #define ASSERT_LT(expected, seen) \
457 __EXPECT(expected, #expected, seen, #seen, <, 1)
460 * ASSERT_LE(expected, seen)
462 * @expected: expected value
463 * @seen: measured value
465 * ASSERT_LE(expected, measured): expected <= measured
467 #define ASSERT_LE(expected, seen) \
468 __EXPECT(expected, #expected, seen, #seen, <=, 1)
471 * ASSERT_GT(expected, seen)
473 * @expected: expected value
474 * @seen: measured value
476 * ASSERT_GT(expected, measured): expected > measured
478 #define ASSERT_GT(expected, seen) \
479 __EXPECT(expected, #expected, seen, #seen, >, 1)
482 * ASSERT_GE(expected, seen)
484 * @expected: expected value
485 * @seen: measured value
487 * ASSERT_GE(expected, measured): expected >= measured
489 #define ASSERT_GE(expected, seen) \
490 __EXPECT(expected, #expected, seen, #seen, >=, 1)
495 * @seen: measured value
497 * ASSERT_NULL(measured): NULL == measured
499 #define ASSERT_NULL(seen) \
500 __EXPECT(NULL, "NULL", seen, #seen, ==, 1)
505 * @seen: measured value
507 * ASSERT_TRUE(measured): measured != 0
509 #define ASSERT_TRUE(seen) \
510 __EXPECT(0, "0", seen, #seen, !=, 1)
515 * @seen: measured value
517 * ASSERT_FALSE(measured): measured == 0
519 #define ASSERT_FALSE(seen) \
520 __EXPECT(0, "0", seen, #seen, ==, 1)
523 * ASSERT_STREQ(expected, seen)
525 * @expected: expected value
526 * @seen: measured value
528 * ASSERT_STREQ(expected, measured): !strcmp(expected, measured)
530 #define ASSERT_STREQ(expected, seen) \
531 __EXPECT_STR(expected, seen, ==, 1)
534 * ASSERT_STRNE(expected, seen)
536 * @expected: expected value
537 * @seen: measured value
539 * ASSERT_STRNE(expected, measured): strcmp(expected, measured)
541 #define ASSERT_STRNE(expected, seen) \
542 __EXPECT_STR(expected, seen, !=, 1)
545 * EXPECT_EQ(expected, seen)
547 * @expected: expected value
548 * @seen: measured value
550 * EXPECT_EQ(expected, measured): expected == measured
552 #define EXPECT_EQ(expected, seen) \
553 __EXPECT(expected, #expected, seen, #seen, ==, 0)
556 * EXPECT_NE(expected, seen)
558 * @expected: expected value
559 * @seen: measured value
561 * EXPECT_NE(expected, measured): expected != measured
563 #define EXPECT_NE(expected, seen) \
564 __EXPECT(expected, #expected, seen, #seen, !=, 0)
567 * EXPECT_LT(expected, seen)
569 * @expected: expected value
570 * @seen: measured value
572 * EXPECT_LT(expected, measured): expected < measured
574 #define EXPECT_LT(expected, seen) \
575 __EXPECT(expected, #expected, seen, #seen, <, 0)
578 * EXPECT_LE(expected, seen)
580 * @expected: expected value
581 * @seen: measured value
583 * EXPECT_LE(expected, measured): expected <= measured
585 #define EXPECT_LE(expected, seen) \
586 __EXPECT(expected, #expected, seen, #seen, <=, 0)
589 * EXPECT_GT(expected, seen)
591 * @expected: expected value
592 * @seen: measured value
594 * EXPECT_GT(expected, measured): expected > measured
596 #define EXPECT_GT(expected, seen) \
597 __EXPECT(expected, #expected, seen, #seen, >, 0)
600 * EXPECT_GE(expected, seen)
602 * @expected: expected value
603 * @seen: measured value
605 * EXPECT_GE(expected, measured): expected >= measured
607 #define EXPECT_GE(expected, seen) \
608 __EXPECT(expected, #expected, seen, #seen, >=, 0)
613 * @seen: measured value
615 * EXPECT_NULL(measured): NULL == measured
617 #define EXPECT_NULL(seen) \
618 __EXPECT(NULL, "NULL", seen, #seen, ==, 0)
623 * @seen: measured value
625 * EXPECT_TRUE(measured): 0 != measured
627 #define EXPECT_TRUE(seen) \
628 __EXPECT(0, "0", seen, #seen, !=, 0)
633 * @seen: measured value
635 * EXPECT_FALSE(measured): 0 == measured
637 #define EXPECT_FALSE(seen) \
638 __EXPECT(0, "0", seen, #seen, ==, 0)
641 * EXPECT_STREQ(expected, seen)
643 * @expected: expected value
644 * @seen: measured value
646 * EXPECT_STREQ(expected, measured): !strcmp(expected, measured)
648 #define EXPECT_STREQ(expected, seen) \
649 __EXPECT_STR(expected, seen, ==, 0)
652 * EXPECT_STRNE(expected, seen)
654 * @expected: expected value
655 * @seen: measured value
657 * EXPECT_STRNE(expected, measured): strcmp(expected, measured)
659 #define EXPECT_STRNE(expected, seen) \
660 __EXPECT_STR(expected, seen, !=, 0)
662 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
664 /* Support an optional handler after and ASSERT_* or EXPECT_*. The approach is
665 * not thread-safe, but it should be fine in most sane test scenarios.
667 * Using __bail(), which optionally abort()s, is the easiest way to early
668 * return while still providing an optional block to the API consumer.
670 #define OPTIONAL_HANDLER(_assert) \
671 for (; _metadata->trigger; _metadata->trigger = \
672 __bail(_assert, _metadata->no_print, _metadata->step))
674 #define __INC_STEP(_metadata) \
675 if (_metadata->passed && _metadata->step < 255) \
678 #define __EXPECT(_expected, _expected_str, _seen, _seen_str, _t, _assert) do { \
679 /* Avoid multiple evaluation of the cases */ \
680 __typeof__(_expected) __exp = (_expected); \
681 __typeof__(_seen) __seen = (_seen); \
682 if (_assert) __INC_STEP(_metadata); \
683 if (!(__exp _t __seen)) { \
684 unsigned long long __exp_print = (uintptr_t)__exp; \
685 unsigned long long __seen_print = (uintptr_t)__seen; \
686 __TH_LOG("Expected %s (%llu) %s %s (%llu)", \
687 _expected_str, __exp_print, #_t, \
688 _seen_str, __seen_print); \
689 _metadata->passed = 0; \
690 /* Ensure the optional handler is triggered */ \
691 _metadata->trigger = 1; \
693 } while (0); OPTIONAL_HANDLER(_assert)
695 #define __EXPECT_STR(_expected, _seen, _t, _assert) do { \
696 const char *__exp = (_expected); \
697 const char *__seen = (_seen); \
698 if (_assert) __INC_STEP(_metadata); \
699 if (!(strcmp(__exp, __seen) _t 0)) { \
700 __TH_LOG("Expected '%s' %s '%s'.", __exp, #_t, __seen); \
701 _metadata->passed = 0; \
702 _metadata->trigger = 1; \
704 } while (0); OPTIONAL_HANDLER(_assert)
707 #define __LIST_APPEND(head, item) \
709 /* Circular linked list where only prev is circular. */ \
710 if (head == NULL) { \
716 if (__constructor_order == _CONSTRUCTOR_ORDER_FORWARD) { \
718 item->prev = head->prev; \
719 item->prev->next = item; \
723 item->next->prev = item; \
729 struct __test_metadata;
730 struct __fixture_variant_metadata;
732 /* Contains all the information about a fixture. */
733 struct __fixture_metadata {
735 struct __test_metadata *tests;
736 struct __fixture_variant_metadata *variant;
737 struct __fixture_metadata *prev, *next;
738 } _fixture_global __attribute__((unused)) = {
740 .prev = &_fixture_global,
743 static struct __fixture_metadata *__fixture_list = &_fixture_global;
744 static int __constructor_order;
746 #define _CONSTRUCTOR_ORDER_FORWARD 1
747 #define _CONSTRUCTOR_ORDER_BACKWARD -1
749 static inline void __register_fixture(struct __fixture_metadata *f)
751 __LIST_APPEND(__fixture_list, f);
754 struct __fixture_variant_metadata {
757 struct __fixture_variant_metadata *prev, *next;
761 __register_fixture_variant(struct __fixture_metadata *f,
762 struct __fixture_variant_metadata *variant)
764 __LIST_APPEND(f->variant, variant);
767 /* Contains all the information for test execution and status checking. */
768 struct __test_metadata {
770 void (*fn)(struct __test_metadata *,
771 struct __fixture_variant_metadata *);
772 pid_t pid; /* pid of test when being run */
773 struct __fixture_metadata *fixture;
776 int trigger; /* extra handler after the evaluation */
777 int timeout; /* seconds to wait for test timeout */
778 bool timed_out; /* did this test timeout instead of exiting? */
780 bool no_print; /* manual trigger when TH_LOG_STREAM is not available */
781 struct __test_metadata *prev, *next;
785 * Since constructors are called in reverse order, reverse the test
786 * list so tests are run in source declaration order.
787 * https://gcc.gnu.org/onlinedocs/gccint/Initialization.html
788 * However, it seems not all toolchains do this correctly, so use
789 * __constructor_order to detect which direction is called first
790 * and adjust list building logic to get things running in the right
793 static inline void __register_test(struct __test_metadata *t)
795 __LIST_APPEND(t->fixture->tests, t);
798 static inline int __bail(int for_realz, bool no_print, __u8 step)
808 struct __test_metadata *__active_test;
809 static void __timeout_handler(int sig, siginfo_t *info, void *ucontext)
811 struct __test_metadata *t = __active_test;
813 /* Sanity check handler execution environment. */
815 fprintf(TH_LOG_STREAM,
816 "no active test in SIGALRM handler!?\n");
819 if (sig != SIGALRM || sig != info->si_signo) {
820 fprintf(TH_LOG_STREAM,
821 "%s: SIGALRM handler caught signal %d!?\n",
822 t->name, sig != SIGALRM ? sig : info->si_signo);
827 kill(t->pid, SIGKILL);
830 void __wait_for_test(struct __test_metadata *t)
832 struct sigaction action = {
833 .sa_sigaction = __timeout_handler,
834 .sa_flags = SA_SIGINFO,
836 struct sigaction saved_action;
839 if (sigaction(SIGALRM, &action, &saved_action)) {
841 fprintf(TH_LOG_STREAM,
842 "%s: unable to install SIGALRM handler\n",
847 t->timed_out = false;
849 waitpid(t->pid, &status, 0);
851 if (sigaction(SIGALRM, &saved_action, NULL)) {
853 fprintf(TH_LOG_STREAM,
854 "%s: unable to uninstall SIGALRM handler\n",
858 __active_test = NULL;
862 fprintf(TH_LOG_STREAM,
863 "%s: Test terminated by timeout\n", t->name);
864 } else if (WIFEXITED(status)) {
865 t->passed = t->termsig == -1 ? !WEXITSTATUS(status) : 0;
866 if (t->termsig != -1) {
867 fprintf(TH_LOG_STREAM,
868 "%s: Test exited normally "
869 "instead of by signal (code: %d)\n",
871 WEXITSTATUS(status));
872 } else if (!t->passed) {
873 fprintf(TH_LOG_STREAM,
874 "%s: Test failed at step #%d\n",
876 WEXITSTATUS(status));
878 } else if (WIFSIGNALED(status)) {
880 if (WTERMSIG(status) == SIGABRT) {
881 fprintf(TH_LOG_STREAM,
882 "%s: Test terminated by assertion\n",
884 } else if (WTERMSIG(status) == t->termsig) {
887 fprintf(TH_LOG_STREAM,
888 "%s: Test terminated unexpectedly "
894 fprintf(TH_LOG_STREAM,
895 "%s: Test ended in some other way [%u]\n",
901 void __run_test(struct __fixture_metadata *f,
902 struct __fixture_variant_metadata *variant,
903 struct __test_metadata *t)
905 /* reset test struct */
911 printf("[ RUN ] %s%s%s.%s\n",
912 f->name, variant->name[0] ? "." : "", variant->name, t->name);
915 printf("ERROR SPAWNING TEST CHILD\n");
917 } else if (t->pid == 0) {
919 /* return the step that failed or 0 */
920 _exit(t->passed ? 0 : t->step);
924 printf("[ %4s ] %s%s%s.%s\n", (t->passed ? "OK" : "FAIL"),
925 f->name, variant->name[0] ? "." : "", variant->name, t->name);
928 static int test_harness_run(int __attribute__((unused)) argc,
929 char __attribute__((unused)) **argv)
931 struct __fixture_variant_metadata no_variant = { .name = "", };
932 struct __fixture_variant_metadata *v;
933 struct __fixture_metadata *f;
934 struct __test_metadata *t;
936 unsigned int case_count = 0, test_count = 0;
937 unsigned int count = 0;
938 unsigned int pass_count = 0;
940 for (f = __fixture_list; f; f = f->next) {
941 for (v = f->variant ?: &no_variant; v; v = v->next) {
943 for (t = f->tests; t; t = t->next)
948 /* TODO(wad) add optional arguments similar to gtest. */
949 printf("[==========] Running %u tests from %u test cases.\n",
950 test_count, case_count);
951 for (f = __fixture_list; f; f = f->next) {
952 for (v = f->variant ?: &no_variant; v; v = v->next) {
953 for (t = f->tests; t; t = t->next) {
963 printf("[==========] %u / %u tests passed.\n", pass_count, count);
964 printf("[ %s ]\n", (ret ? "FAILED" : "PASSED"));
968 static void __attribute__((constructor)) __constructor_order_first(void)
970 if (!__constructor_order)
971 __constructor_order = _CONSTRUCTOR_ORDER_FORWARD;
974 #endif /* __KSELFTEST_HARNESS_H */