[PATCH] unit-tests: optionally show elapsed time

Bartosz Golaszewski bartekgola at gmail.com
Wed Jul 30 17:10:03 UTC 2014


File bbunit.c doesn't compile with WANT_TIMING enabled due
to a missing comma at line 80. This patch fixes it, adds a
config option for elapsed time display and replaces the custom
timeval_diff() function with standard timersub() macro.

Signed-off-by: Bartosz Golaszewski <bartekgola at gmail.com>
---
 Config.in      |  8 ++++++++
 libbb/bbunit.c | 22 ++++------------------
 2 files changed, 12 insertions(+), 18 deletions(-)

diff --git a/Config.in b/Config.in
index b83beb5..81ea26d 100644
--- a/Config.in
+++ b/Config.in
@@ -683,6 +683,14 @@ config UNIT_TEST
 	  test cases) as a Busybox applet. This results in bigger code, so you
 	  probably don't want this option in production builds.
 
+config UNIT_TEST_TIMER
+	bool "Show elapsed time for unit-tests"
+	default n
+	depends on UNIT_TEST
+	help
+	  Say Y here if you want the unit-test framework to show the time
+	  spent on test execution.
+
 config WERROR
 	bool "Abort compilation on any warning"
 	default n
diff --git a/libbb/bbunit.c b/libbb/bbunit.c
index 2560144..7091843 100644
--- a/libbb/bbunit.c
+++ b/libbb/bbunit.c
@@ -17,8 +17,6 @@
 
 #include "libbb.h"
 
-#define WANT_TIMING 0
-
 static llist_t *tests = NULL;
 static unsigned tests_registered = 0;
 static int test_retval;
@@ -34,24 +32,12 @@ void bbunit_settestfailed(void)
 	test_retval = -1;
 }
 
-#if WANT_TIMING
-static void timeval_diff(struct timeval* res,
-				const struct timeval* x,
-				const struct timeval* y)
-{
-	long udiff = x->tv_usec - y->tv_usec;
-
-	res->tv_sec = x->tv_sec - y->tv_sec - (udiff < 0);
-	res->tv_usec = (udiff >= 0 ? udiff : udiff + 1000000);
-}
-#endif
-
 int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) MAIN_EXTERNALLY_VISIBLE;
 int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 {
 	unsigned tests_run = 0;
 	unsigned tests_failed = 0;
-#if WANT_TIMING
+#if ENABLE_UNIT_TEST_TIMER
 	struct timeval begin;
 	struct timeval end;
 	struct timeval time_spent;
@@ -74,10 +60,10 @@ int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 		el = el->next;
 	}
 
-#if WANT_TIMING
+#if ENABLE_UNIT_TEST_TIMER
 	gettimeofday(&end, NULL);
-	timeval_diff(&time_spent, &end, &begin);
-	bb_error_msg("Elapsed time %u.%06u seconds"
+	timersub(&end, &begin, &time_spent);
+	bb_error_msg("Elapsed time: %u.%06u seconds",
 			(int)time_spent.tv_sec,
 			(int)time_spent.tv_usec);
 #endif
-- 
1.9.1



More information about the busybox mailing list