Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

authored by

Marie Zhussupova and committed by
Shuah Khan
f9687f35 a03e3caa

+104
+104
lib/kunit/kunit-example-test.c
··· 389 389 } 390 390 391 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 + 492 + /* 392 493 * Here we make a list of all the test cases we want to add to the test suite 393 494 * below. 394 495 */ ··· 510 409 KUNIT_CASE_PARAM(example_params_test, example_gen_params), 511 410 KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init, kunit_array_gen_params, 512 411 example_param_init, NULL), 412 + KUNIT_CASE_PARAM_WITH_INIT(example_params_test_with_init_dynamic_arr, 413 + kunit_array_gen_params, example_param_init_dynamic_arr, 414 + example_param_exit_dynamic_arr), 513 415 KUNIT_CASE_SLOW(example_slow_test), 514 416 {} 515 417 };