In some cases we may want to generate params based on existing zero terminated array, but with some entries filtered out. Extend macro KUNIT_ZERO_ARRAY_PARAM to accept filter function and provide example how to use it. $ ./tools/testing/kunit/kunit.py run \ --kunitconfig ./lib/kunit/.kunitconfig *.example_params* [ ] Starting KUnit Kernel (1/1)... [ ] ============================================================ [ ] ========================= example ========================= [ ] =================== example_params_test =================== [ ] [SKIPPED] example value 3 [ ] [PASSED] example value 2 [ ] [PASSED] example value 1 [ ] [SKIPPED] example value 0 [ ] =============== [PASSED] example_params_test =============== [ ] =================== example_params_test =================== [ ] [SKIPPED] example value 3 [ ] [PASSED] example value 2 [ ] [PASSED] example value 1 [ ] =============== [PASSED] example_params_test =============== [ ] =================== example_params_test =================== [ ] [PASSED] example value 2 [ ] [PASSED] example value 1 [ ] =============== [PASSED] example_params_test =============== [ ] ===================== [PASSED] example ===================== [ ] ============================================================ [ ] Testing complete. Ran 9 tests: passed: 6, skipped: 3 Signed-off-by: Michal Wajdeczko <michal.wajdeczko@xxxxxxxxx> Cc: David Gow <davidgow@xxxxxxxxxx> Cc: Rae Moar <rmoar@xxxxxxxxxx> --- include/kunit/test.h | 19 +++++++++++++++++-- lib/kunit/kunit-example-test.c | 9 +++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/include/kunit/test.h b/include/kunit/test.h index 280113ceb6a6..8a87d1ce37e0 100644 --- a/include/kunit/test.h +++ b/include/kunit/test.h @@ -1515,20 +1515,24 @@ do { \ } /** - * KUNIT_ZERO_ARRAY_PARAM() - Define test parameter generator from a zero terminated array. + * KUNIT_FILTERED_ZERO_ARRAY_PARAM() - Define test parameter generator from a zero terminated array. * @name: prefix for the test parameter generator function. * @array: zero terminated array of test parameters. * @get_desc: function to convert param to description; NULL to use default + * @filter: function to filter out unwanted params (like duplicates); can be NULL * * Define function @name_gen_params which uses zero terminated @array to generate parameters. */ -#define KUNIT_ZERO_ARRAY_PARAM(name, array, get_desc) \ +#define KUNIT_FILTERED_ZERO_ARRAY_PARAM(name, array, get_desc, filter) \ static const void *name##_gen_params(const void *prev, char *desc) \ { \ typeof((array)[0]) *__prev = prev; \ typeof(__prev) __next = __prev ? __prev + 1 : (array); \ void (*__get_desc)(typeof(__next), char *) = get_desc; \ + bool (*__filter)(typeof(__prev), typeof(__next)) = filter; \ for (; memchr_inv(__next, 0, sizeof(*__next)); __prev = __next++) { \ + if (__filter && !__filter(__prev, __next)) \ + continue; \ if (__get_desc) \ __get_desc(__next, desc); \ return __next; \ @@ -1536,6 +1540,17 @@ do { \ return NULL; \ } +/** + * KUNIT_ZERO_ARRAY_PARAM() - Define test parameter generator from a zero terminated array. + * @name: prefix for the test parameter generator function. + * @array: zero terminated array of test parameters. + * @get_desc: function to convert param to description; NULL to use default + * + * Define function @name_gen_params which uses zero terminated @array to generate parameters. + */ +#define KUNIT_ZERO_ARRAY_PARAM(name, array, get_desc) \ + KUNIT_FILTERED_ZERO_ARRAY_PARAM(name, array, get_desc, NULL) + // TODO(dlatypov@xxxxxxxxxx): consider eventually migrating users to explicitly // include resource.h themselves if they need it. #include <kunit/resource.h> diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c index ad9ebcfd513e..a3268754392c 100644 --- a/lib/kunit/kunit-example-test.c +++ b/lib/kunit/kunit-example-test.c @@ -201,8 +201,16 @@ static void example_param_get_desc(const struct example_param *p, char *desc) snprintf(desc, KUNIT_PARAM_DESC_SIZE, "example value %d", p->value); } +static bool example_param_filter(const struct example_param *prev, + const struct example_param *next) +{ + return next->value < 3; +} + KUNIT_ARRAY_PARAM(example, example_params_array, example_param_get_desc); KUNIT_ZERO_ARRAY_PARAM(example_zero, example_params_array, example_param_get_desc); +KUNIT_FILTERED_ZERO_ARRAY_PARAM(example_filter, example_params_array, example_param_get_desc, + example_param_filter); /* * This test shows the use of params. @@ -248,6 +256,7 @@ static struct kunit_case example_test_cases[] = { KUNIT_CASE(example_static_stub_test), KUNIT_CASE_PARAM(example_params_test, example_gen_params), KUNIT_CASE_PARAM(example_params_test, example_zero_gen_params), + KUNIT_CASE_PARAM(example_params_test, example_filter_gen_params), KUNIT_CASE_SLOW(example_slow_test), {} }; -- 2.25.1