diff options
author | Yuval Peress <peress@chromium.org> | 2019-07-15 14:15:10 -0600 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2019-07-24 21:16:37 +0000 |
commit | 2df71e976a0f16fe4e53c826f75193250c66212d (patch) | |
tree | 1c0c6ccadce825232df8048039f6d5c31322e9b6 /include | |
parent | 701f45f574cfcf9182e89267e5479af5eb709576 (diff) | |
download | chrome-ec-2df71e976a0f16fe4e53c826f75193250c66212d.tar.gz |
test: Add new test framework tools
Add new before/after test functions to be used for clean-up and
tear-down of tests.
Add new asserting functions:
- TEST_EQ ensures that two values are equal
- TEST_NE ensures that two values are not equal
- TEST_BITS_SET ensures that a value contains all bits in mask
- TEST_BITS_CLEARED ensures that a value doesn't contain any
bits in mask
The benefit of these is that unlike TEST_ASSERT they will
also print the values when the assersion is wrong.
BRANCH=None
BUG=b:137758297
TEST=None yet
Change-Id: I2f305ef34e541c289f22c6596f53ee5cd977c7a8
Signed-off-by: Yuval Peress <peress@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1704165
Reviewed-by: Enrico Granata <egranata@chromium.org>
Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
Diffstat (limited to 'include')
-rw-r--r-- | include/test_util.h | 29 |
1 files changed, 23 insertions, 6 deletions
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); |