1 /* vi: set sw=4 ts=4: */
3 * bbunit: Simple unit-testing framework for Busybox.
5 * Copyright (C) 2014 by Bartosz Golaszewski <bartekgola@gmail.com>
7 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
10 //kbuild:lib-$(CONFIG_UNIT_TEST) += bbunit.o
11 //applet:IF_UNIT_TEST(APPLET(unit, BB_DIR_USR_BIN, BB_SUID_DROP))
13 //usage:#define unit_trivial_usage
15 //usage:#define unit_full_usage "\n\n"
16 //usage: "Run the unit-test suite"
22 static llist_t *tests = NULL;
23 static unsigned tests_registered = 0;
24 static int test_retval;
26 void bbunit_registertest(struct bbunit_listelem *test)
28 llist_add_to_end(&tests, test);
32 void bbunit_settestfailed(void)
38 static void timeval_diff(struct timeval* res,
39 const struct timeval* x,
40 const struct timeval* y)
42 long udiff = x->tv_usec - y->tv_usec;
44 res->tv_sec = x->tv_sec - y->tv_sec - (udiff < 0);
45 res->tv_usec = (udiff >= 0 ? udiff : udiff + 1000000);
49 int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) MAIN_EXTERNALLY_VISIBLE;
50 int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
52 unsigned tests_run = 0;
53 unsigned tests_failed = 0;
57 struct timeval time_spent;
58 gettimeofday(&begin, NULL);
61 bb_error_msg("Running %d test(s)...", tests_registered);
63 struct bbunit_listelem* el = llist_pop(&tests);
66 bb_error_msg("Case: [%s]", el->name);
69 if (test_retval < 0) {
70 bb_error_msg("[ERROR] [%s]: TEST FAILED", el->name);
78 gettimeofday(&end, NULL);
79 timeval_diff(&time_spent, &end, &begin);
80 bb_error_msg("Elapsed time %u.%06u seconds",
81 (int)time_spent.tv_sec,
82 (int)time_spent.tv_usec);
84 if (tests_failed > 0) {
85 bb_error_msg("[ERROR] %u test(s) FAILED", tests_failed);
88 bb_error_msg("All tests passed");