diff options
author | Marek Vasut | 2023-03-10 04:33:13 +0100 |
---|---|---|
committer | Simon Glass | 2023-03-14 16:08:51 -0600 |
commit | fa847bb409d6a07bbd923e7889b485e943d75689 (patch) | |
tree | 88aa1717fd693549dec86e30d04803be976a812e /include/test | |
parent | 20aaff677d8bc3cc2d529d859aa1ea65f5a4db7d (diff) |
test: Wrap assert macros in ({ ... }) and fix missing semicolons
Wrap the assert macros in ({ ... }) so they can be safely used both as
right side argument as well as in conditionals without curly brackets
around them. In the process, find a bunch of missing semicolons, fix
them.
Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Marek Vasut <marek.vasut+renesas@mailbox.org>
Diffstat (limited to 'include/test')
-rw-r--r-- | include/test/ut.h | 152 |
1 files changed, 102 insertions, 50 deletions
diff --git a/include/test/ut.h b/include/test/ut.h index 2b0dab32f68..dddf9ad241f 100644 --- a/include/test/ut.h +++ b/include/test/ut.h @@ -125,36 +125,47 @@ int ut_check_console_dump(struct unit_test_state *uts, int total_bytes); fmt, ##args) /* Assert that a condition is non-zero */ -#define ut_assert(cond) \ +#define ut_assert(cond) ({ \ + int __ret = 0; \ + \ if (!(cond)) { \ ut_fail(uts, __FILE__, __LINE__, __func__, #cond); \ - return CMD_RET_FAILURE; \ - } + __ret = CMD_RET_FAILURE; \ + } \ + __ret; \ +}) /* Assert that a condition is non-zero, with printf() string */ -#define ut_assertf(cond, fmt, args...) \ +#define ut_assertf(cond, fmt, args...) ({ \ + int __ret = 0; \ + \ if (!(cond)) { \ ut_failf(uts, __FILE__, __LINE__, __func__, #cond, \ fmt, ##args); \ - return CMD_RET_FAILURE; \ - } + __ret = CMD_RET_FAILURE; \ + } \ + __ret; \ +}) /* Assert that two int expressions are equal */ -#define ut_asserteq(expr1, expr2) { \ +#define ut_asserteq(expr1, expr2) ({ \ unsigned int _val1 = (expr1), _val2 = (expr2); \ + int __ret = 0; \ \ if (_val1 != _val2) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ #expr1 " == " #expr2, \ "Expected %#x (%d), got %#x (%d)", \ _val1, _val1, _val2, _val2); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ -} + __ret; \ +}) /* Assert that two 64 int expressions are equal */ -#define ut_asserteq_64(expr1, expr2) { \ +#define ut_asserteq_64(expr1, expr2) ({ \ u64 _val1 = (expr1), _val2 = (expr2); \ + int __ret = 0; \ \ if (_val1 != _val2) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ @@ -164,43 +175,49 @@ int ut_check_console_dump(struct unit_test_state *uts, int total_bytes); (unsigned long long)_val1, \ (unsigned long long)_val2, \ (unsigned long long)_val2); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ -} + __ret; \ +}) /* Assert that two string expressions are equal */ -#define ut_asserteq_str(expr1, expr2) { \ +#define ut_asserteq_str(expr1, expr2) ({ \ const char *_val1 = (expr1), *_val2 = (expr2); \ + int __ret = 0; \ \ if (strcmp(_val1, _val2)) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ #expr1 " = " #expr2, \ "Expected \"%s\", got \"%s\"", _val1, _val2); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ -} + __ret; \ +}) /* * Assert that two string expressions are equal, up to length of the * first */ -#define ut_asserteq_strn(expr1, expr2) { \ +#define ut_asserteq_strn(expr1, expr2) ({ \ const char *_val1 = (expr1), *_val2 = (expr2); \ int _len = strlen(_val1); \ + int __ret = 0; \ \ if (memcmp(_val1, _val2, _len)) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ #expr1 " = " #expr2, \ "Expected \"%.*s\", got \"%.*s\"", \ _len, _val1, _len, _val2); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ -} + __ret; \ +}) /* Assert that two memory areas are equal */ -#define ut_asserteq_mem(expr1, expr2, len) { \ +#define ut_asserteq_mem(expr1, expr2, len) ({ \ const u8 *_val1 = (u8 *)(expr1), *_val2 = (u8 *)(expr2); \ const uint __len = len; \ + int __ret = 0; \ \ if (memcmp(_val1, _val2, __len)) { \ char __buf1[64 + 1] = "\0"; \ @@ -211,128 +228,163 @@ int ut_check_console_dump(struct unit_test_state *uts, int total_bytes); #expr1 " = " #expr2, \ "Expected \"%s\", got \"%s\"", \ __buf1, __buf2); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ -} + __ret; \ +}) /* Assert that two pointers are equal */ -#define ut_asserteq_ptr(expr1, expr2) { \ +#define ut_asserteq_ptr(expr1, expr2) ({ \ const void *_val1 = (expr1), *_val2 = (expr2); \ + int __ret = 0; \ \ if (_val1 != _val2) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ #expr1 " = " #expr2, \ "Expected %p, got %p", _val1, _val2); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ -} + __ret; \ +}) /* Assert that two addresses (converted from pointers) are equal */ -#define ut_asserteq_addr(expr1, expr2) { \ +#define ut_asserteq_addr(expr1, expr2) ({ \ ulong _val1 = map_to_sysmem(expr1); \ ulong _val2 = map_to_sysmem(expr2); \ + int __ret = 0; \ \ if (_val1 != _val2) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ #expr1 " = " #expr2, \ "Expected %lx, got %lx", _val1, _val2); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ -} + __ret; \ +}) /* Assert that a pointer is NULL */ -#define ut_assertnull(expr) { \ +#define ut_assertnull(expr) ({ \ const void *_val = (expr); \ + int __ret = 0; \ \ - if (_val) { \ + if (_val) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ #expr " != NULL", \ "Expected NULL, got %p", _val); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ -} + __ret; \ +}) /* Assert that a pointer is not NULL */ -#define ut_assertnonnull(expr) { \ +#define ut_assertnonnull(expr) ({ \ const void *_val = (expr); \ + int __ret = 0; \ \ - if (!_val) { \ + if (!_val) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ #expr " = NULL", \ "Expected non-null, got NULL"); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ -} + __ret; \ +}) /* Assert that a pointer is not an error pointer */ -#define ut_assertok_ptr(expr) { \ +#define ut_assertok_ptr(expr) ({ \ const void *_val = (expr); \ + int __ret = 0; \ \ if (IS_ERR(_val)) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ #expr " = NULL", \ "Expected pointer, got error %ld", \ PTR_ERR(_val)); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ -} + __ret; \ +}) /* Assert that an operation succeeds (returns 0) */ #define ut_assertok(cond) ut_asserteq(0, cond) /* Assert that the next console output line matches */ -#define ut_assert_nextline(fmt, args...) \ +#define ut_assert_nextline(fmt, args...) ({ \ + int __ret = 0; \ + \ if (ut_check_console_line(uts, fmt, ##args)) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ "console", "\nExpected '%s',\n got '%s'", \ uts->expect_str, uts->actual_str); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ + __ret; \ +}) /* Assert that the next console output line matches up to the length */ -#define ut_assert_nextlinen(fmt, args...) \ +#define ut_assert_nextlinen(fmt, args...) ({ \ + int __ret = 0; \ + \ if (ut_check_console_linen(uts, fmt, ##args)) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ "console", "\nExpected '%s',\n got '%s'", \ uts->expect_str, uts->actual_str); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ + __ret; \ +}) /* Assert that there is a 'next' console output line, and skip it */ -#define ut_assert_skipline() \ +#define ut_assert_skipline() ({ \ + int __ret = 0; \ + \ if (ut_check_skipline(uts)) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ "console", "\nExpected a line, got end"); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ + __ret; \ +}) /* Assert that a following console output line matches */ -#define ut_assert_skip_to_line(fmt, args...) \ +#define ut_assert_skip_to_line(fmt, args...) ({ \ + int __ret = 0; \ + \ if (ut_check_skip_to_line(uts, fmt, ##args)) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ "console", "\nExpected '%s',\n got to '%s'", \ uts->expect_str, uts->actual_str); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ + __ret; \ +}) /* Assert that there is no more console output */ -#define ut_assert_console_end() \ +#define ut_assert_console_end() ({ \ + int __ret = 0; \ + \ if (ut_check_console_end(uts)) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ "console", "Expected no more output, got '%s'",\ uts->actual_str); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ + __ret; \ +}) /* Assert that the next lines are print_buffer() dump at an address */ -#define ut_assert_nextlines_are_dump(total_bytes) \ +#define ut_assert_nextlines_are_dump(total_bytes) ({ \ + int __ret = 0; \ + \ if (ut_check_console_dump(uts, total_bytes)) { \ ut_failf(uts, __FILE__, __LINE__, __func__, \ "console", \ "Expected dump of length %x bytes, got '%s'", \ total_bytes, uts->actual_str); \ - return CMD_RET_FAILURE; \ + __ret = CMD_RET_FAILURE; \ } \ + __ret; \ +}) /* Assert that the next console output line is empty */ #define ut_assert_nextline_empty() \ |