diff options
-rw-r--r-- | Makefile.rules | 20 | ||||
-rw-r--r-- | Makefile.toolchain | 8 | ||||
-rw-r--r-- | common/test_util.c | 32 | ||||
-rw-r--r-- | core/host/main.c | 3 | ||||
-rw-r--r-- | include/test_util.h | 6 | ||||
-rw-r--r-- | test/mutex.c | 3 | ||||
-rw-r--r-- | test/pingpong.c | 5 | ||||
-rwxr-xr-x | util/run_host_test | 2 |
8 files changed, 73 insertions, 6 deletions
diff --git a/Makefile.rules b/Makefile.rules index 4def77851c..93fe9eb7bc 100644 --- a/Makefile.rules +++ b/Makefile.rules @@ -84,7 +84,7 @@ $(host-test-targets): host-%: @set -e ; \ echo " BUILD host - build/host/$*" ; \ $(MAKE) --no-print-directory BOARD=host PROJECT=$* \ - V=$(V) out=build/host/$* TEST_BUILD=y EMU_BUILD=y \ + V=$(V) out=build/host/$* TEST_BUILD=y EMU_BUILD=y $(TEST_FLAG) \ CROSS_COMPILE= build/host/$*/$*.exe $(run-test-targets): run-%: host-% @@ -93,6 +93,24 @@ $(run-test-targets): run-%: host-% hosttests: $(host-test-targets) runtests: $(run-test-targets) +cov-test-targets=$(foreach t,$(test-list-host),build/host/$(t).info) +bldversion=$(shell (./util/getversion.sh ; echo VERSION) | $(CPP) -P) + +# lcov fails when multiple instances run at the same time. +# We need to run them sequentially by using flock +cmd_lcov=flock .lcov_lock -c "lcov -q -o $@ -c -d build/host/$*" +cmd_report_cov=genhtml -q -o build/host/coverage_rpt -t \ + "EC Unittest "$(bldversion) $^ + +build/host/%.info: run-% + $(call quiet,lcov,COV ) + +coverage: TEST_FLAG=TEST_COVERAGE=y +coverage: $(cov-test-targets) + $(call quiet,report_cov,REPORT ) + +.PHONY: coverage + $(out)/firmware_image.lds: common/firmware_image.lds.S $(call quiet,lds,LDS ) $(out)/%.lds: core/$(CORE)/ec.lds.S diff --git a/Makefile.toolchain b/Makefile.toolchain index 2679e2b5f1..c9de7aaf78 100644 --- a/Makefile.toolchain +++ b/Makefile.toolchain @@ -30,11 +30,14 @@ CFLAGS_TEST=$(if $(TEST_BUILD),-DTEST_BUILD \ -DTEST_TASKFILE=$(PROJECT).tasklist,) \ $(if $(EMU_BUILD),-DEMU_BUILD) \ $(if $($(PROJECT)-scale),-DTEST_TIME_SCALE=$($(PROJECT)-scale)) +CFLAGS_COVERAGE=$(if $(TEST_COVERAGE),-fprofile-arcs -ftest-coverage \ + -DTEST_COVERAGE,) CFLAGS_DEFINE=-DOUTDIR=$(out) -DCHIP=$(CHIP) -DBOARD_TASKFILE=ec.tasklist \ -DBOARD=$(BOARD) -DBOARD_$(BOARD) -DCORE=$(CORE) \ -DCHIP_$(CHIP) -DCHIP_VARIANT=$(CHIP_VARIANT) \ -DCHIP_VARIANT_$(CHIP_VARIANT) -DPROJECT=$(PROJECT) -CPPFLAGS=$(CFLAGS_DEFINE) $(CFLAGS_INCLUDE) $(CFLAGS_TEST) $(EXTRA_CFLAGS) +CPPFLAGS=$(CFLAGS_DEFINE) $(CFLAGS_INCLUDE) $(CFLAGS_TEST) \ + $(EXTRA_CFLAGS) $(CFLAGS_COVERAGE) CFLAGS=$(CPPFLAGS) $(CFLAGS_CPU) $(CFLAGS_DEBUG) $(CFLAGS_WARN) $(CFLAGS_y) FTDIVERSION=$(shell $(PKG_CONFIG) --modversion libftdi1 2>/dev/null) @@ -51,4 +54,5 @@ BUILD_CFLAGS= $(LIBFTDI_CFLAGS) $(CPPFLAGS) -O3 $(CFLAGS_DEBUG) $(CFLAGS_WARN) HOST_CFLAGS=$(CPPFLAGS) -O3 $(CFLAGS_DEBUG) $(CFLAGS_WARN) LDFLAGS=-nostdlib -X BUILD_LDFLAGS=$(LIBFTDI_LDLIBS) -HOST_TEST_LDFLAGS=-T core/host/host_exe.lds -lrt -pthread +HOST_TEST_LDFLAGS=-T core/host/host_exe.lds -lrt -pthread \ + $(if $(TEST_COVERAGE),-fprofile-arcs,) diff --git a/common/test_util.c b/common/test_util.c index 7cd9850645..b9b6a7a13f 100644 --- a/common/test_util.c +++ b/common/test_util.c @@ -5,6 +5,9 @@ * Test utilities. */ +#include <signal.h> +#include <stdlib.h> + #include "console.h" #include "test_util.h" #include "util.h" @@ -14,11 +17,40 @@ int __test_error_count; /* Weak reference function as an entry point for unit test */ test_mockable void run_test(void) { } +#ifdef TEST_COVERAGE +extern void __gcov_flush(void); + +void test_end_hook(int sig) +{ + __gcov_flush(); + exit(0); +} + +void register_test_end_hook(void) +{ + signal(SIGTERM, test_end_hook); +} +#else +void register_test_end_hook(void) +{ +} +#endif + void test_reset(void) { __test_error_count = 0; } +void test_pass(void) +{ + ccprintf("Pass!\n"); +} + +void test_fail(void) +{ + ccprintf("Fail!\n"); +} + void test_print_result(void) { if (__test_error_count) diff --git a/core/host/main.c b/core/host/main.c index f69107af4a..132a5fcaaf 100644 --- a/core/host/main.c +++ b/core/host/main.c @@ -8,11 +8,14 @@ #include "flash.h" #include "hooks.h" #include "task.h" +#include "test_util.h" #include "timer.h" #include "uart.h" int main(void) { + register_test_end_hook(); + flash_pre_init(); timer_init(); diff --git a/include/test_util.h b/include/test_util.h index 7386a87a4a..3faccb8c79 100644 --- a/include/test_util.h +++ b/include/test_util.h @@ -62,10 +62,16 @@ return EC_ERROR_UNKNOWN; \ } while (0) +void register_test_end_hook(void); + void run_test(void); void test_reset(void); +void test_pass(void); + +void test_fail(void); + void test_print_result(void); int test_get_error_count(void); diff --git a/test/mutex.c b/test/mutex.c index cc4202378c..866a004a58 100644 --- a/test/mutex.c +++ b/test/mutex.c @@ -9,6 +9,7 @@ #include "console.h" #include "common.h" #include "task.h" +#include "test_util.h" #include "timer.h" #include "util.h" @@ -108,7 +109,7 @@ int mutex_main_task(void *unused) rdelay = prng(rdelay); } - ccprintf("Pass!\n"); + test_pass(); task_wait_event(0); return EC_SUCCESS; diff --git a/test/pingpong.c b/test/pingpong.c index 202dc8be38..852dbc3bdf 100644 --- a/test/pingpong.c +++ b/test/pingpong.c @@ -8,6 +8,7 @@ #include "common.h" #include "console.h" #include "task.h" +#include "test_util.h" #include "timer.h" #include "util.h" @@ -31,9 +32,9 @@ int TaskAbc(void *data) if (myid == 2 && wake_count[myid] == TEST_COUNT) { if (wake_count[0] == TEST_COUNT && wake_count[1] == TEST_COUNT) - ccputs("Pass!\n"); + test_pass(); else - ccputs("Fail!\n"); + test_fail(); wake_count[0] = wake_count[1] = wake_count[2] = 0; task_wait_event(-1); } else { diff --git a/util/run_host_test b/util/run_host_test index b70b0eb80b..e36e8d8280 100755 --- a/util/run_host_test +++ b/util/run_host_test @@ -45,6 +45,8 @@ def RunOnce(test_name, log, timeout_secs): return RESULT_ID_REBOOT else: raise + finally: + child.kill(15) log = StringIO() tee_log = Tee(log) |