diff options
-rw-r--r-- | common/build.mk | 1 | ||||
-rw-r--r-- | common/test_util.c | 36 | ||||
-rw-r--r-- | core/host/task.c | 4 | ||||
-rw-r--r-- | include/test_util.h | 50 | ||||
-rw-r--r-- | test/flash.c | 25 | ||||
-rw-r--r-- | test/kb_mkbp.c | 36 | ||||
-rw-r--r-- | test/kb_scan.c | 73 | ||||
-rw-r--r-- | test/lid_sw.c | 36 | ||||
-rw-r--r-- | test/mutex.c | 8 | ||||
-rw-r--r-- | test/pingpong.c | 8 | ||||
-rw-r--r-- | test/stress.c | 15 | ||||
-rw-r--r-- | test/timer_calib.c | 5 | ||||
-rw-r--r-- | test/timer_dos.c | 8 | ||||
-rw-r--r-- | test/utils.c | 44 |
14 files changed, 126 insertions, 223 deletions
diff --git a/common/build.mk b/common/build.mk index ae7ca4fd80..5df925b7d0 100644 --- a/common/build.mk +++ b/common/build.mk @@ -48,3 +48,4 @@ common-$(CONFIG_TMP006)+=tmp006.o common-$(CONFIG_TSU6721)+=tsu6721.o common-$(CONFIG_USB_PORT_POWER_SMART)+=usb_port_power_smart.o common-$(CONFIG_USB_PORT_POWER_DUMB)+=usb_port_power_dumb.o +common-$(TEST_BUILD)+=test_util.o diff --git a/common/test_util.c b/common/test_util.c new file mode 100644 index 0000000000..8594f3468e --- /dev/null +++ b/common/test_util.c @@ -0,0 +1,36 @@ +/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + * + * Test utilities. + */ + +#include "console.h" +#include "test_util.h" +#include "util.h" + +int __test_error_count; + +/* Weak reference function as an entry point for unit test */ +test_mockable void run_test(void) { } + +void test_reset(void) +{ + __test_error_count = 0; +} + +void test_print_result(void) +{ + if (__test_error_count) + ccprintf("Fail! (%d tests)\n", __test_error_count); + else + ccprintf("Pass!\n"); +} + +static int command_run_test(int argc, char **argv) +{ + run_test(); + return EC_SUCCESS; +} +DECLARE_CONSOLE_COMMAND(runtest, command_run_test, + NULL, NULL, NULL); diff --git a/core/host/task.c b/core/host/task.c index 2049320e04..7c9e0622fb 100644 --- a/core/host/task.c +++ b/core/host/task.c @@ -13,6 +13,7 @@ #include "common.h" #include "task.h" #include "task_id.h" +#include "test_util.h" #include "timer.h" struct emu_task_t { @@ -45,9 +46,6 @@ void __idle(void *d) task_wait_event(-1); } -/* Weak reference function as an entry point for unit test */ -test_mockable void run_test(void) { } - void _run_test(void *d) { run_test(); diff --git a/include/test_util.h b/include/test_util.h new file mode 100644 index 0000000000..40ce8c2633 --- /dev/null +++ b/include/test_util.h @@ -0,0 +1,50 @@ +/* Copyright (c) 2013 The Chromium OS Authors. All rights reserved. + * Use of this source code is governed by a BSD-style license that can be + * found in the LICENSE file. + */ + +/* Various utility for unit testing */ + +#ifndef __CROS_EC_TEST_UTIL_H +#define __CROS_EC_TEST_UTIL_H + +#include "common.h" +#include "console.h" + +#define RUN_TEST(n) \ + do { \ + ccprintf("Running %s...", #n); \ + cflush(); \ + if (n() == EC_SUCCESS) { \ + ccputs("OK\n"); \ + } else { \ + ccputs("Fail\n"); \ + __test_error_count++; \ + } \ + } while (0) + +#define TEST_ASSERT(n) \ + do { \ + if (!(n)) { \ + ccprintf("ASSERTION failed: %s\n", #n); \ + return EC_ERROR_UNKNOWN; \ + } \ + } while (0) + +#define TEST_CHECK(n) \ + do { \ + if (n) \ + return EC_SUCCESS; \ + else \ + return EC_ERROR_UNKNOWN; \ + } while (0) + +void run_test(void); + +void test_reset(void); + +void test_print_result(void); + +extern int __test_error_count; + +#endif /* __CROS_EC_TEST_UTIL_H */ diff --git a/test/flash.c b/test/flash.c index 67ee61ec27..02144e756c 100644 --- a/test/flash.c +++ b/test/flash.c @@ -13,6 +13,7 @@ #include "hooks.h" #include "host_command.h" #include "system.h" +#include "test_util.h" #include "timer.h" #include "util.h" @@ -78,17 +79,6 @@ int gpio_get_level(enum gpio_signal signal) /*****************************************************************************/ /* Test utilities */ -#define RUN_TEST(n) \ - do { \ - ccprintf("Running %s...", #n); \ - cflush(); \ - if (n() == EC_SUCCESS) { \ - ccputs("OK\n"); \ - } else { \ - ccputs("Fail\n"); \ - error_count++; \ - } \ - } while (0) static void begin_verify(void) { @@ -113,12 +103,6 @@ static int verify_write(int offset, int size, const char *data) } -#define TEST_ASSERT(x) \ - do { \ - if (!(x)) \ - return EC_ERROR_UNKNOWN; \ - } while (0) - #define VERIFY_NO_WRITE(off, sz, d) \ do { \ begin_verify(); \ @@ -418,10 +402,7 @@ int TaskTest(void *data) return EC_SUCCESS; } -static int command_run_test(int argc, char **argv) +void run_test(void) { - run_test_step1(); /* Never returns */ - return EC_ERROR_UNKNOWN; + run_test_step1(); } -DECLARE_CONSOLE_COMMAND(runtest, command_run_test, - NULL, NULL, NULL); diff --git a/test/kb_mkbp.c b/test/kb_mkbp.c index a8e57bfe57..fc7a9255de 100644 --- a/test/kb_mkbp.c +++ b/test/kb_mkbp.c @@ -13,10 +13,9 @@ #include "keyboard_mkbp.h" #include "keyboard_protocol.h" #include "keyboard_scan.h" +#include "test_util.h" #include "util.h" -static int error_count; - static uint8_t state[KEYBOARD_COLS]; static int ec_int_level; @@ -44,24 +43,6 @@ int lid_is_open(void) /*****************************************************************************/ /* Test utilities */ -#define RUN_TEST(n) \ - do { \ - ccprintf("Running %s...", #n); \ - cflush(); \ - if (n() == EC_SUCCESS) { \ - ccputs("OK\n"); \ - } else { \ - ccputs("Fail\n"); \ - error_count++; \ - } \ - } while (0) - -#define TEST_ASSERT(n) \ - do { \ - if (!(n)) \ - return EC_ERROR_UNKNOWN; \ - } while (0) - #define FIFO_EMPTY() (ec_int_level == 1) #define FIFO_NOT_EMPTY() (ec_int_level == 0) @@ -227,24 +208,13 @@ int fifo_underrun(void) void run_test(void) { - error_count = 0; ec_int_level = 1; + test_reset(); RUN_TEST(single_key_press); RUN_TEST(test_fifo_size); RUN_TEST(test_enable); RUN_TEST(fifo_underrun); - if (error_count == 0) - ccprintf("Pass!\n"); - else - ccprintf("Fail!\n"); -} - -static int command_run_test(int argc, char **argv) -{ - run_test(); - return EC_SUCCESS; + test_print_result(); } -DECLARE_CONSOLE_COMMAND(runtest, command_run_test, - NULL, NULL, NULL); diff --git a/test/kb_scan.c b/test/kb_scan.c index 756d87b8ee..3822e155ae 100644 --- a/test/kb_scan.c +++ b/test/kb_scan.c @@ -13,6 +13,7 @@ #include "keyboard_scan.h" #include "lid_switch.h" #include "task.h" +#include "test_util.h" #include "timer.h" #include "util.h" @@ -20,26 +21,6 @@ #define KEYDOWN_RETRY 10 #define NO_KEYDOWN_DELAY_MS 100 -#define TEST_ASSERT(n) \ - do { \ - if (n() != EC_SUCCESS) { \ - ccprintf("%s failed.\n", #n); \ - return EC_ERROR_UNKNOWN; \ - } \ - } while (0) - -#define RUN_TEST(n) \ - do { \ - ccprintf("Running %s...", #n); \ - cflush(); \ - if (n() == EC_SUCCESS) { \ - ccputs("OK\n"); \ - } else { \ - ccputs("Fail\n"); \ - error_count++; \ - } \ - } while (0) - #define CHECK_KEY_COUNT(old, expected) \ do { \ if (verify_key_presses(old, expected) != EC_SUCCESS) \ @@ -50,7 +31,6 @@ static uint8_t mock_state[KEYBOARD_COLS]; static int column_driven; static int fifo_add_count; -static int error_count; static int lid_open; #ifdef CONFIG_LID_SWITCH @@ -138,41 +118,41 @@ int deghost_test(void) { /* Test we can detect a keypress */ mock_key(1, 1, 1); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); mock_key(1, 1, 0); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); /* (1, 1) (1, 2) (2, 1) (2, 2) form ghosting keys */ mock_key(1, 1, 1); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); mock_key(2, 2, 1); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); mock_key(1, 2, 1); mock_key(2, 1, 1); - TEST_ASSERT(expect_no_keychange); + TEST_ASSERT(expect_no_keychange() == EC_SUCCESS); mock_key(2, 1, 0); mock_key(1, 2, 0); - TEST_ASSERT(expect_no_keychange); + TEST_ASSERT(expect_no_keychange() == EC_SUCCESS); mock_key(2, 2, 0); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); mock_key(1, 1, 0); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); /* (1, 1) (2, 0) (2, 1) don't form ghosting keys */ mock_key(1, 1, 1); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); mock_key(2, 0, 1); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); mock_key(1, 0, 1); mock_key(2, 1, 1); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); mock_key(1, 0, 0); mock_key(2, 1, 0); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); mock_key(2, 0, 0); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); mock_key(1, 1, 0); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); return EC_SUCCESS; } @@ -242,15 +222,15 @@ int lid_test(void) { lid_open = 0; mock_key(1, 1, 1); - TEST_ASSERT(expect_no_keychange); + TEST_ASSERT(expect_no_keychange() == EC_SUCCESS); mock_key(1, 1, 0); - TEST_ASSERT(expect_no_keychange); + TEST_ASSERT(expect_no_keychange() == EC_SUCCESS); lid_open = 1; mock_key(1, 1, 1); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); mock_key(1, 1, 0); - TEST_ASSERT(expect_keychange); + TEST_ASSERT(expect_keychange() == EC_SUCCESS); return EC_SUCCESS; } @@ -258,8 +238,8 @@ int lid_test(void) void run_test(void) { - error_count = 0; lid_open = 1; + test_reset(); RUN_TEST(deghost_test); RUN_TEST(debounce_test); @@ -267,16 +247,5 @@ void run_test(void) RUN_TEST(lid_test); #endif - if (error_count == 0) - ccprintf("Pass!\n"); - else - ccprintf("Fail!\n"); -} - -static int command_run_test(int argc, char **argv) -{ - run_test(); - return EC_SUCCESS; + test_print_result(); } -DECLARE_CONSOLE_COMMAND(runtest, command_run_test, - NULL, NULL, NULL); diff --git a/test/lid_sw.c b/test/lid_sw.c index 4993147dcb..cf04d91efb 100644 --- a/test/lid_sw.c +++ b/test/lid_sw.c @@ -10,32 +10,13 @@ #include "hooks.h" #include "host_command.h" #include "lid_switch.h" +#include "test_util.h" #include "timer.h" #include "util.h" -static int error_count; - static int mock_lid; static int lid_hook_count; -#define RUN_TEST(n) \ - do { \ - ccprintf("Running %s...", #n); \ - cflush(); \ - if (n() == EC_SUCCESS) { \ - ccputs("OK\n"); \ - } else { \ - ccputs("Fail\n"); \ - error_count++; \ - } \ - } while (0) - -#define TEST_ASSERT(n) \ - do { \ - if (!(n)) \ - return EC_ERROR_UNKNOWN; \ - } while (0) - int gpio_get_level(enum gpio_signal signal) { if (signal == GPIO_LID_OPEN) @@ -107,21 +88,10 @@ static int test_debounce(void) void run_test(void) { - error_count = 0; + test_reset(); RUN_TEST(test_hook); RUN_TEST(test_debounce); - if (error_count) - ccprintf("Fail!\n", error_count); - else - ccprintf("Pass!\n"); -} - -static int command_run_test(int argc, char **argv) -{ - run_test(); - return EC_SUCCESS; + test_print_result(); } -DECLARE_CONSOLE_COMMAND(runtest, command_run_test, - NULL, NULL, NULL); diff --git a/test/mutex.c b/test/mutex.c index e795fa15b7..cc4202378c 100644 --- a/test/mutex.c +++ b/test/mutex.c @@ -118,11 +118,3 @@ void run_test(void) { task_wake(TASK_ID_MTX1); } - -static int command_run_test(int argc, char **argv) -{ - run_test(); - return EC_SUCCESS; -} -DECLARE_CONSOLE_COMMAND(runtest, command_run_test, - NULL, NULL, NULL); diff --git a/test/pingpong.c b/test/pingpong.c index 4cf5d53ced..202dc8be38 100644 --- a/test/pingpong.c +++ b/test/pingpong.c @@ -63,11 +63,3 @@ void run_test(void) task_wake(TASK_ID_TICK); task_wake(TASK_ID_TESTA); } - -static int command_run_test(int argc, char **argv) -{ - run_test(); - return EC_SUCCESS; -} -DECLARE_CONSOLE_COMMAND(runtest, command_run_test, - NULL, NULL, NULL); diff --git a/test/stress.c b/test/stress.c index 8a69d84b19..e62212e69f 100644 --- a/test/stress.c +++ b/test/stress.c @@ -9,6 +9,7 @@ #include "console.h" #include "ec_commands.h" #include "i2c.h" +#include "test_util.h" #include "timer.h" #include "util.h" @@ -147,9 +148,9 @@ static int test_adc(void) } #endif -static int command_run_test(int argc, char **argv) +void run_test(void) { - error_count = 0; + test_reset(); #ifdef CONFIG_I2C RUN_STRESS_TEST("I2C Stress Test", test_i2c, I2C_TEST_ITERATION); @@ -158,13 +159,5 @@ static int command_run_test(int argc, char **argv) RUN_STRESS_TEST("ADC Stress Test", test_adc, ADC_TEST_ITERATION); #endif - if (error_count) { - ccprintf("Failed %d tests!\n", error_count); - return EC_ERROR_UNKNOWN; - } else { - ccprintf("Pass!\n"); - return EC_SUCCESS; - } + test_print_result(); } -DECLARE_CONSOLE_COMMAND(runtest, command_run_test, - NULL, NULL, NULL); diff --git a/test/timer_calib.c b/test/timer_calib.c index e64cc6a769..65dab2900f 100644 --- a/test/timer_calib.c +++ b/test/timer_calib.c @@ -56,10 +56,7 @@ int timer_calib_task(void *data) return EC_SUCCESS; } -static int command_run_test(int argc, char **argv) +void run_test(void) { task_wake(TASK_ID_TESTTMR); - return EC_SUCCESS; } -DECLARE_CONSOLE_COMMAND(runtest, command_run_test, - NULL, NULL, NULL); diff --git a/test/timer_dos.c b/test/timer_dos.c index 327206e6df..73b06d681e 100644 --- a/test/timer_dos.c +++ b/test/timer_dos.c @@ -53,11 +53,3 @@ void run_test(void) task_wake(TASK_ID_TMRB); task_wake(TASK_ID_TMRA); } - -static int command_run_test(int argc, char **argv) -{ - run_test(); - return EC_SUCCESS; -} -DECLARE_CONSOLE_COMMAND(runtest, command_run_test, - NULL, NULL, NULL); diff --git a/test/utils.c b/test/utils.c index 81fe2ef072..1aabbb81d7 100644 --- a/test/utils.c +++ b/test/utils.c @@ -9,37 +9,10 @@ #include "console.h" #include "shared_mem.h" #include "system.h" +#include "test_util.h" #include "timer.h" #include "util.h" -static int error_count; - -#define RUN_TEST(n) \ - do { \ - ccprintf("Running %s...", #n); \ - cflush(); \ - if (n() == EC_SUCCESS) { \ - ccputs("OK\n"); \ - } else { \ - ccputs("Fail\n"); \ - error_count++; \ - } \ - } while (0) - -#define TEST_ASSERT(n) \ - do { \ - if (!(n)) \ - return EC_ERROR_UNKNOWN; \ - } while (0) - -#define TEST_CHECK(n) \ - do { \ - if (n) \ - return EC_SUCCESS; \ - else \ - return EC_ERROR_UNKNOWN; \ - } while (0) - static int test_strlen(void) { TEST_CHECK(strlen("this is a string") == 16); @@ -111,7 +84,7 @@ static int test_scratchpad(void) void run_test(void) { - error_count = 0; + test_reset(); RUN_TEST(test_strlen); RUN_TEST(test_strcasecmp); @@ -121,16 +94,5 @@ void run_test(void) RUN_TEST(test_shared_mem); RUN_TEST(test_scratchpad); - if (error_count) - ccprintf("Failed %d tests!\n", error_count); - else - ccprintf("Pass!\n"); -} - -static int command_run_test(int argc, char **argv) -{ - run_test(); - return EC_SUCCESS; + test_print_result(); } -DECLARE_CONSOLE_COMMAND(runtest, command_run_test, - NULL, NULL, NULL); |