Skip to content

Commit f9687f3

Browse files
Marie Zhussupovashuahkh
authored andcommitted
kunit: Add example parameterized test with direct dynamic parameter array setup
Introduce example_params_test_with_init_dynamic_arr(). This new KUnit test demonstrates directly assigning a dynamic parameter array, using the kunit_register_params_array() macro, to a parameterized test context. It highlights the use of param_init() and param_exit() for initialization and exit of a parameterized test, and their registration to the test case with KUNIT_CASE_PARAM_WITH_INIT(). Link: https://lore.kernel.org/r/20250826091341.1427123-7-davidgow@google.com Reviewed-by: Rae Moar <rmoar@google.com> Reviewed-by: David Gow <davidgow@google.com> Signed-off-by: Marie Zhussupova <marievic@google.com> Signed-off-by: David Gow <davidgow@google.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent a03e3ca commit f9687f3

1 file changed

Lines changed: 104 additions & 0 deletions

File tree

lib/kunit/kunit-example-test.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,107 @@ static void example_params_test_with_init(struct kunit *test)
388388
kunit_put_resource(res);
389389
}
390390

391+
/*
392+
* Helper function to create a parameter array of Fibonacci numbers. This example
393+
* highlights a parameter generation scenario that is:
394+
* 1. Not feasible to fully pre-generate at compile time.
395+
* 2. Challenging to implement with a standard generate_params() function,
396+
* as it only provides the previous parameter, while Fibonacci requires
397+
* access to two preceding values for calculation.
398+
*/
399+
static void *make_fibonacci_params(struct kunit *test, size_t seq_size)
400+
{
401+
int *seq;
402+
403+
if (seq_size <= 0)
404+
return NULL;
405+
/*
406+
* Using kunit_kmalloc_array here ties the lifetime of the array to
407+
* the parameterized test i.e. it will get automatically cleaned up
408+
* by KUnit after the parameterized test finishes.
409+
*/
410+
seq = kunit_kmalloc_array(test, seq_size, sizeof(int), GFP_KERNEL);
411+
412+
if (!seq)
413+
return NULL;
414+
if (seq_size >= 1)
415+
seq[0] = 0;
416+
if (seq_size >= 2)
417+
seq[1] = 1;
418+
for (int i = 2; i < seq_size; i++)
419+
seq[i] = seq[i - 1] + seq[i - 2];
420+
return seq;
421+
}
422+
423+
/*
424+
* This is an example of a function that provides a description for each of the
425+
* parameters.
426+
*/
427+
static void example_param_dynamic_arr_get_desc(struct kunit *test, const void *p, char *desc)
428+
{
429+
const int *fib_num = p;
430+
431+
snprintf(desc, KUNIT_PARAM_DESC_SIZE, "fibonacci param: %d", *fib_num);
432+
}
433+
434+
/*
435+
* Example of a parameterized test param_init() function that registers a dynamic
436+
* array of parameters.
437+
*/
438+
static int example_param_init_dynamic_arr(struct kunit *test)
439+
{
440+
size_t seq_size;
441+
int *fibonacci_params;
442+
443+
kunit_info(test, "initializing parameterized test\n");
444+
445+
seq_size = 6;
446+
fibonacci_params = make_fibonacci_params(test, seq_size);
447+
448+
if (!fibonacci_params)
449+
return -ENOMEM;
450+
451+
/*
452+
* Passes the dynamic parameter array information to the parameterized test
453+
* context struct kunit. The array and its metadata will be stored in
454+
* test->parent->params_array. The array itself will be located in
455+
* params_data.params.
456+
*
457+
* Note that you will need to pass kunit_array_gen_params() as the
458+
* generator function to KUNIT_CASE_PARAM_WITH_INIT() when registering
459+
* a parameter array this route.
460+
*/
461+
kunit_register_params_array(test, fibonacci_params, seq_size,
462+
example_param_dynamic_arr_get_desc);
463+
return 0;
464+
}
465+
466+
/*
467+
* Example of a parameterized test param_exit() function that outputs a log
468+
* at the end of the parameterized test. It could also be used for any other
469+
* teardown logic.
470+
*/
471+
static void example_param_exit_dynamic_arr(struct kunit *test)
472+
{
473+
kunit_info(test, "exiting parameterized test\n");
474+
}
475+
476+
/*
477+
* Example of test that uses the registered dynamic array to perform assertions
478+
* and expectations.
479+
*/
480+
static void example_params_test_with_init_dynamic_arr(struct kunit *test)
481+
{
482+
const int *param = test->param_value;
483+
int param_val;
484+
485+
/* By design, param pointer will not be NULL. */
486+
KUNIT_ASSERT_NOT_NULL(test, param);
487+
488+
param_val = *param;
489+
KUNIT_EXPECT_EQ(test, param_val - param_val, 0);
490+
}
491+
391492
/*
392493
* Here we make a list of all the test cases we want to add to the test suite
393494
* below.
@@ -409,6 +510,9 @@ static struct kunit_case example_test_cases[] = {
409510
KUNIT_CASE_PARAM(example_params_test, example_gen_params),
410511
KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init, kunit_array_gen_params,
411512
example_param_init, NULL),
513+
KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init_dynamic_arr,
514+
kunit_array_gen_params, example_param_init_dynamic_arr,
515+
example_param_exit_dynamic_arr),
412516
KUNIT_CASE_SLOW(example_slow_test),
413517
{}
414518
};

0 commit comments

Comments
 (0)