Commit f9687f35 authored by Marie Zhussupova's avatar Marie Zhussupova Committed by Shuah Khan
Browse files

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: default avatarRae Moar <rmoar@google.com>
Reviewed-by: default avatarDavid Gow <davidgow@google.com>
Signed-off-by: default avatarMarie Zhussupova <marievic@google.com>
Signed-off-by: default avatarDavid Gow <davidgow@google.com>
Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent a03e3caa
Loading
Loading
Loading
Loading
+104 −0
Original line number Diff line number Diff line
@@ -388,6 +388,107 @@ static void example_params_test_with_init(struct kunit *test)
	kunit_put_resource(res);
}

/*
 * Helper function to create a parameter array of Fibonacci numbers. This example
 * highlights a parameter generation scenario that is:
 * 1. Not feasible to fully pre-generate at compile time.
 * 2. Challenging to implement with a standard generate_params() function,
 * as it only provides the previous parameter, while Fibonacci requires
 * access to two preceding values for calculation.
 */
static void *make_fibonacci_params(struct kunit *test, size_t seq_size)
{
	int *seq;

	if (seq_size <= 0)
		return NULL;
	/*
	 * Using kunit_kmalloc_array here ties the lifetime of the array to
	 * the parameterized test i.e. it will get automatically cleaned up
	 * by KUnit after the parameterized test finishes.
	 */
	seq = kunit_kmalloc_array(test, seq_size, sizeof(int), GFP_KERNEL);

	if (!seq)
		return NULL;
	if (seq_size >= 1)
		seq[0] = 0;
	if (seq_size >= 2)
		seq[1] = 1;
	for (int i = 2; i < seq_size; i++)
		seq[i] = seq[i - 1] + seq[i - 2];
	return seq;
}

/*
 * This is an example of a function that provides a description for each of the
 * parameters.
 */
static void example_param_dynamic_arr_get_desc(struct kunit *test, const void *p, char *desc)
{
	const int *fib_num = p;

	snprintf(desc, KUNIT_PARAM_DESC_SIZE, "fibonacci param: %d", *fib_num);
}

/*
 * Example of a parameterized test param_init() function that registers a dynamic
 * array of parameters.
 */
static int example_param_init_dynamic_arr(struct kunit *test)
{
	size_t seq_size;
	int *fibonacci_params;

	kunit_info(test, "initializing parameterized test\n");

	seq_size = 6;
	fibonacci_params = make_fibonacci_params(test, seq_size);

	if (!fibonacci_params)
		return -ENOMEM;

	/*
	 * Passes the dynamic parameter array information to the parameterized test
	 * context struct kunit. The array and its metadata will be stored in
	 * test->parent->params_array. The array itself will be located in
	 * params_data.params.
	 *
	 * Note that you will need to pass kunit_array_gen_params() as the
	 * generator function to KUNIT_CASE_PARAM_WITH_INIT() when registering
	 * a parameter array this route.
	 */
	kunit_register_params_array(test, fibonacci_params, seq_size,
				    example_param_dynamic_arr_get_desc);
	return 0;
}

/*
 * Example of a parameterized test param_exit() function that outputs a log
 * at the end of the parameterized test. It could also be used for any other
 * teardown logic.
 */
static void example_param_exit_dynamic_arr(struct kunit *test)
{
	kunit_info(test, "exiting parameterized test\n");
}

/*
 * Example of test that uses the registered dynamic array to perform assertions
 * and expectations.
 */
static void example_params_test_with_init_dynamic_arr(struct kunit *test)
{
	const int *param = test->param_value;
	int param_val;

	/* By design, param pointer will not be NULL. */
	KUNIT_ASSERT_NOT_NULL(test, param);

	param_val = *param;
	KUNIT_EXPECT_EQ(test, param_val - param_val, 0);
}

/*
 * Here we make a list of all the test cases we want to add to the test suite
 * below.
@@ -409,6 +510,9 @@ static struct kunit_case example_test_cases[] = {
	KUNIT_CASE_PARAM(example_params_test, example_gen_params),
	KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init, kunit_array_gen_params,
				   example_param_init, NULL),
	KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init_dynamic_arr,
				   kunit_array_gen_params, example_param_init_dynamic_arr,
				   example_param_exit_dynamic_arr),
	KUNIT_CASE_SLOW(example_slow_test),
	{}
};