From: David Gow <davidgow@google.com>
To: Marie Zhussupova <marievic@google.com>
Cc: rmoar@google.com, shuah@kernel.org, brendan.higgins@linux.dev,
mark.rutland@arm.com, elver@google.com, dvyukov@google.com,
lucas.demarchi@intel.com, thomas.hellstrom@linux.intel.com,
rodrigo.vivi@intel.com, linux-kselftest@vger.kernel.org,
kunit-dev@googlegroups.com, kasan-dev@googlegroups.com,
intel-xe@lists.freedesktop.org, dri-devel@lists.freedesktop.org,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 6/7] kunit: Add example parameterized test with direct dynamic parameter array setup
Date: Wed, 13 Aug 2025 17:18:02 +0800 [thread overview]
Message-ID: <CABVgOSkz9LPXYv4Mfuf3Mo+65LzNeFd0kepBsALo2dm9MSc_7w@mail.gmail.com> (raw)
In-Reply-To: <20250811221739.2694336-7-marievic@google.com>
[-- Attachment #1: Type: text/plain, Size: 6195 bytes --]
On Tue, 12 Aug 2025 at 06:18, Marie Zhussupova <marievic@google.com> wrote:
>
> 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().
>
> Signed-off-by: Marie Zhussupova <marievic@google.com>
> ---
I really like this example now. Thanks!
Reviewed-by: David Gow <davidgow@google.com>
Cheers,
-- David
>
> Changes in v2:
>
> - kunit_array_gen_params() is now explicitly passed to
> KUNIT_CASE_PARAM_WITH_INIT() to be consistent with
> the parameterized test being defined by the existence
> of the generate_params() function.
> - param_init() was changed to output a log at the start
> of a parameterized test.
> - The parameter array was changed to be allocated
> using kunit_kmalloc_array(), a KUnit memory allocation
> API, as that would be the preferred/easier method. To
> still demonstrate a use of param_exit(), it now outputs
> a log at the end of the parameterized test.
> - The comments and the commit message were changed to
> reflect the parameterized testing terminology. See
> the patch series cover letter change log for the
> definitions.
>
> ---
> lib/kunit/kunit-example-test.c | 104 +++++++++++++++++++++++++++++++++
> 1 file changed, 104 insertions(+)
>
> diff --git a/lib/kunit/kunit-example-test.c b/lib/kunit/kunit-example-test.c
> index f2819ee58965..ff21511889a4 100644
> --- a/lib/kunit/kunit-example-test.c
> +++ b/lib/kunit/kunit-example-test.c
> @@ -393,6 +393,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.
> @@ -414,6 +515,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),
> {}
> };
> --
> 2.51.0.rc0.205.g4a044479a3-goog
>
[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 5281 bytes --]
next prev parent reply other threads:[~2025-08-13 9:18 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-11 22:17 [PATCH v2 0/7] kunit: Refactor and extend KUnit's parameterized testing framework Marie Zhussupova
2025-08-11 22:17 ` [PATCH v2 1/7] kunit: Add parent kunit for parameterized test context Marie Zhussupova
2025-08-12 22:22 ` Rae Moar
2025-08-13 9:17 ` David Gow
2025-08-11 22:17 ` [PATCH v2 2/7] kunit: Introduce param_init/exit for parameterized test context management Marie Zhussupova
2025-08-12 22:22 ` Rae Moar
2025-08-13 9:17 ` David Gow
2025-08-11 22:17 ` [PATCH v2 3/7] kunit: Pass parameterized test context to generate_params() Marie Zhussupova
2025-08-12 13:54 ` Rodrigo Vivi
2025-08-12 22:22 ` Rae Moar
2025-08-13 7:43 ` Marco Elver
2025-08-13 9:18 ` David Gow
2025-08-11 22:17 ` [PATCH v2 4/7] kunit: Enable direct registration of parameter arrays to a KUnit test Marie Zhussupova
2025-08-12 22:23 ` Rae Moar
2025-08-13 9:17 ` David Gow
2025-08-11 22:17 ` [PATCH v2 5/7] kunit: Add example parameterized test with shared resource management using the Resource API Marie Zhussupova
2025-08-12 22:23 ` Rae Moar
2025-08-13 9:17 ` David Gow
2025-08-11 22:17 ` [PATCH v2 6/7] kunit: Add example parameterized test with direct dynamic parameter array setup Marie Zhussupova
2025-08-12 22:23 ` Rae Moar
2025-08-13 9:18 ` David Gow [this message]
2025-08-11 22:17 ` [PATCH v2 7/7] Documentation: kunit: Document new parameterized test features Marie Zhussupova
2025-08-12 22:23 ` Rae Moar
2025-08-13 9:18 ` David Gow
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CABVgOSkz9LPXYv4Mfuf3Mo+65LzNeFd0kepBsALo2dm9MSc_7w@mail.gmail.com \
--to=davidgow@google.com \
--cc=brendan.higgins@linux.dev \
--cc=dri-devel@lists.freedesktop.org \
--cc=dvyukov@google.com \
--cc=elver@google.com \
--cc=intel-xe@lists.freedesktop.org \
--cc=kasan-dev@googlegroups.com \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=lucas.demarchi@intel.com \
--cc=marievic@google.com \
--cc=mark.rutland@arm.com \
--cc=rmoar@google.com \
--cc=rodrigo.vivi@intel.com \
--cc=shuah@kernel.org \
--cc=thomas.hellstrom@linux.intel.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).