diff options
-rw-r--r-- | common/test_util.c | 6 | ||||
-rw-r--r-- | include/test_util.h | 29 |
2 files changed, 29 insertions, 6 deletions
diff --git a/common/test_util.c b/common/test_util.c index 72dc59f33d..2d2d3ba462 100644 --- a/common/test_util.c +++ b/common/test_util.c @@ -33,6 +33,12 @@ test_mockable void run_test(void) { } /* Default dummy test init */ test_mockable void test_init(void) { } +/* Default dummy before test */ +test_mockable void before_test(void) { } + +/* Default dummy after test */ +test_mockable void after_test(void) { } + #ifdef TEST_COVERAGE extern void __gcov_flush(void); diff --git a/include/test_util.h b/include/test_util.h index 7ee2fed284..6134897fa1 100644 --- a/include/test_util.h +++ b/include/test_util.h @@ -19,12 +19,14 @@ do { \ ccprintf("Running %s...", #n); \ cflush(); \ + before_test(); \ if (n() == EC_SUCCESS) { \ ccputs("OK\n"); \ } else { \ ccputs("Fail\n"); \ __test_error_count++; \ } \ + after_test(); \ } while (0) #define TEST_ASSERT(n) \ @@ -36,18 +38,27 @@ } \ } while (0) -#define __ABS(n) ((n) > 0 ? (n) : -(n)) - -#define TEST_ASSERT_ABS_LESS(n, t) \ +#define TEST_OPERATOR(a, b, op, fmt) \ do { \ - if (__ABS(n) >= t) { \ - ccprintf("%d: ASSERT_ABS_LESS failed: abs(%d) is " \ - "not less than %d\n", __LINE__, n, t); \ + if (!((a) op (b))) { \ + ccprintf("%d: ASSERSION failed: %s " #op " %s\n", \ + __LINE__, #a, #b); \ + ccprintf("\t\tEVAL: " #fmt " " #op " " #fmt "\n", \ + (a), (b)); \ task_dump_trace(); \ return EC_ERROR_UNKNOWN; \ } \ } while (0) +#define TEST_EQ(a, b, fmt) TEST_OPERATOR(a, b, ==, fmt) +#define TEST_NE(a, b, fmt) TEST_OPERATOR(a, b, !=, fmt) +#define TEST_BITS_SET(a, bits) TEST_OPERATOR(a & bits, bits, ==, %u) +#define TEST_BITS_CLEARED(a, bits) TEST_OPERATOR(a & bits, 0, ==, %u) + +#define __ABS(n) ((n) > 0 ? (n) : -(n)) + +#define TEST_ASSERT_ABS_LESS(n, t) TEST_OPERATOR(__ABS(n), t, <, %d) + #define TEST_ASSERT_ARRAY_EQ(s, d, n) \ do { \ int __i; \ @@ -107,6 +118,12 @@ void register_test_end_hook(void); */ void test_init(void); +/** Called before each test. Used for initialization. */ +void before_test(void); + +/** Called after each test. Used to clean up. */ +void after_test(void); + /* Test entry point */ void run_test(void); |