diff options
author | Daniel Latypov <dlatypov@google.com> | 2022-10-01 03:26:37 +0300 |
---|---|---|
committer | Shuah Khan <skhan@linuxfoundation.org> | 2022-12-13 00:13:47 +0300 |
commit | 697365c086791372945037557f99bc164e2db855 (patch) | |
tree | d2eb60b5634c34fed99c391c462c55df97fac34f /include/kunit/test.h | |
parent | 101e32a025da386ba6f6efbfe3e75b6ec5a358aa (diff) | |
download | linux-697365c086791372945037557f99bc164e2db855.tar.xz |
kunit: eliminate KUNIT_INIT_*_ASSERT_STRUCT macros
These macros exist because passing an initializer list to other macros
is hard.
The goal of these macros is to generate a line like
struct $ASSERT_TYPE __assertion = $APPROPRIATE_INITIALIZER;
e.g.
struct kunit_unary_assertion __assertion = {
.condition = "foo()",
.expected_true = true
};
But the challenge is you can't pass `{.condition=..., .expect_true=...}`
as a macro argument, since the comma means you're actually passing two
arguments, `{.condition=...` and `.expect_true=....}`.
So we'd made custom macros for each different initializer-list shape.
But we can work around this with the following generic macro
#define KUNIT_INIT_ASSERT(initializers...) { initializers }
Note: this has the downside that we have to rename some macros arguments
to not conflict with the struct field names (e.g. `expected_true`).
It's a bit gross, but probably worth reducing the # of macros.
Signed-off-by: Daniel Latypov <dlatypov@google.com>
Reviewed-by: David Gow <davidgow@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
Diffstat (limited to 'include/kunit/test.h')
-rw-r--r-- | include/kunit/test.h | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/include/kunit/test.h b/include/kunit/test.h index cde97dc4eed5..d7f60e8aab30 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -515,22 +515,25 @@ void kunit_do_failed_assertion(struct kunit *test, fmt, \ ##__VA_ARGS__) +/* Helper to safely pass around an initializer list to other macros. */ +#define KUNIT_INIT_ASSERT(initializers...) { initializers } + #define KUNIT_UNARY_ASSERTION(test, \ assert_type, \ - condition, \ - expected_true, \ + condition_, \ + expected_true_, \ fmt, \ ...) \ do { \ - if (likely(!!(condition) == !!expected_true)) \ + if (likely(!!(condition_) == !!expected_true_)) \ break; \ \ _KUNIT_FAILED(test, \ assert_type, \ kunit_unary_assert, \ kunit_unary_assert_format, \ - KUNIT_INIT_UNARY_ASSERT_STRUCT(#condition, \ - expected_true), \ + KUNIT_INIT_ASSERT(.condition = #condition_, \ + .expected_true = expected_true_), \ fmt, \ ##__VA_ARGS__); \ } while (0) @@ -590,9 +593,9 @@ do { \ assert_type, \ assert_class, \ format_func, \ - KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ - __left, \ - __right), \ + KUNIT_INIT_ASSERT(.text = &__text, \ + .left_value = __left, \ + .right_value = __right), \ fmt, \ ##__VA_ARGS__); \ } while (0) @@ -651,9 +654,9 @@ do { \ assert_type, \ kunit_binary_str_assert, \ kunit_binary_str_assert_format, \ - KUNIT_INIT_BINARY_ASSERT_STRUCT(&__text, \ - __left, \ - __right), \ + KUNIT_INIT_ASSERT(.text = &__text, \ + .left_value = __left, \ + .right_value = __right), \ fmt, \ ##__VA_ARGS__); \ } while (0) @@ -706,7 +709,7 @@ do { \ assert_type, \ kunit_ptr_not_err_assert, \ kunit_ptr_not_err_assert_format, \ - KUNIT_INIT_PTR_NOT_ERR_STRUCT(#ptr, __ptr), \ + KUNIT_INIT_ASSERT(.text = #ptr, .value = __ptr), \ fmt, \ ##__VA_ARGS__); \ } while (0) |