mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Marie Zhussupova <marievic@google.com>
To: rmoar@google.com, davidgow@google.com, shuah@kernel.org,
	 brendan.higgins@linux.dev
Cc: 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,
	 Marie Zhussupova <marievic@google.com>
Subject: [PATCH 3/9] kunit: Pass additional context to generate_params for parameterized testing
Date: Tue, 29 Jul 2025 19:36:41 +0000	[thread overview]
Message-ID: <20250729193647.3410634-4-marievic@google.com> (raw)
In-Reply-To: <20250729193647.3410634-1-marievic@google.com>

To enable more complex parameterized test scenarios,
the `generate_params` function sometimes needs additional
context beyond just the previously generated parameter.
This patch modifies the `generate_params` function signature
to include an extra `struct kunit *test` argument, giving
users access to the parent kunit test's context when
generating subsequent parameters.

The `struct kunit *test` argument was added as the first parameter
to the function signature as it aligns with the convention
of other KUnit functions that accept `struct kunit *test` first.
This also mirrors the "this" or "self" reference found
in object-oriented programming languages.

Signed-off-by: Marie Zhussupova <marievic@google.com>
---
 include/kunit/test.h | 9 ++++++---
 lib/kunit/test.c     | 5 +++--
 2 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/include/kunit/test.h b/include/kunit/test.h
index d8dac7efd745..4ba65dc35710 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -128,7 +128,8 @@ struct kunit_attributes {
 struct kunit_case {
 	void (*run_case)(struct kunit *test);
 	const char *name;
-	const void* (*generate_params)(const void *prev, char *desc);
+	const void* (*generate_params)(struct kunit *test,
+				       const void *prev, char *desc);
 	struct kunit_attributes attr;
 
 	/*
@@ -1701,7 +1702,8 @@ do {									       \
  * Define function @name_gen_params which uses @array to generate parameters.
  */
 #define KUNIT_ARRAY_PARAM(name, array, get_desc)						\
-	static const void *name##_gen_params(const void *prev, char *desc)			\
+	static const void *name##_gen_params(struct kunit *test,				\
+					     const void *prev, char *desc)			\
 	{											\
 		typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array);	\
 		if (__next - (array) < ARRAY_SIZE((array))) {					\
@@ -1722,7 +1724,8 @@ do {									       \
  * Define function @name_gen_params which uses @array to generate parameters.
  */
 #define KUNIT_ARRAY_PARAM_DESC(name, array, desc_member)					\
-	static const void *name##_gen_params(const void *prev, char *desc)			\
+	static const void *name##_gen_params(struct kunit *test,				\
+					     const void *prev, char *desc)			\
 	{											\
 		typeof((array)[0]) *__next = prev ? ((typeof(__next)) prev) + 1 : (array);	\
 		if (__next - (array) < ARRAY_SIZE((array))) {					\
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index d80b5990d85d..f50ef82179c4 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -696,7 +696,7 @@ int kunit_run_tests(struct kunit_suite *suite)
 			/* Get initial param. */
 			param_desc[0] = '\0';
 			/* TODO: Make generate_params try-catch */
-			curr_param = test_case->generate_params(NULL, param_desc);
+			curr_param = test_case->generate_params(&test, NULL, param_desc);
 			test_case->status = KUNIT_SKIPPED;
 			kunit_log(KERN_INFO, &test, KUNIT_SUBTEST_INDENT KUNIT_SUBTEST_INDENT
 				  "KTAP version 1\n");
@@ -727,7 +727,8 @@ int kunit_run_tests(struct kunit_suite *suite)
 
 				/* Get next param. */
 				param_desc[0] = '\0';
-				curr_param = test_case->generate_params(curr_param, param_desc);
+				curr_param = test_case->generate_params(&test, curr_param,
+									param_desc);
 			}
 		}
 
-- 
2.50.1.552.g942d659e1b-goog


  parent reply	other threads:[~2025-07-29 19:37 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-07-29 19:36 [PATCH 0/9] kunit: Refactor and extend KUnit's Marie Zhussupova
2025-07-29 19:36 ` [PATCH 1/9] kunit: Add parent kunit for parameterized test context Marie Zhussupova
2025-08-02  9:44   ` David Gow
2025-08-05 15:17   ` Rae Moar
2025-08-08 12:20     ` Marie Zhussupova
2025-07-29 19:36 ` [PATCH 2/9] kunit: Introduce param_init/exit for parameterized test shared context management Marie Zhussupova
2025-07-30 13:50   ` kernel test robot
2025-08-02  9:44   ` David Gow
2025-08-05 15:17   ` Rae Moar
2025-08-08 13:01     ` Marie Zhussupova
2025-07-29 19:36 ` Marie Zhussupova [this message]
2025-07-30 14:32   ` [PATCH 3/9] kunit: Pass additional context to generate_params for parameterized testing kernel test robot
2025-08-02  9:44   ` David Gow
2025-08-05 15:18   ` Rae Moar
2025-07-29 19:36 ` [PATCH 4/9] kcsan: test: Update parameter generator to new signature Marie Zhussupova
2025-08-02  9:44   ` David Gow
2025-08-07 11:11     ` Marco Elver
2025-07-29 19:36 ` [PATCH 5/9] drm/xe: " Marie Zhussupova
2025-08-02  9:44   ` David Gow
2025-08-07 10:59     ` Lucas De Marchi
2025-07-29 19:36 ` [PATCH 6/9] kunit: Enable direct registration of parameter arrays to a KUnit test Marie Zhussupova
2025-07-29 20:14   ` Rae Moar
2025-07-29 20:26     ` Rae Moar
2025-07-31 15:58   ` Dan Carpenter
2025-07-31 20:01     ` Marie Zhussupova
2025-08-02  9:44   ` David Gow
2025-08-05 15:18   ` Rae Moar
2025-08-08  1:22     ` Marie Zhussupova
2025-07-29 19:36 ` [PATCH 7/9] kunit: Add example parameterized test with shared resources and direct static parameter array setup Marie Zhussupova
2025-07-30 12:25   ` kernel test robot
2025-08-02  9:44   ` David Gow
2025-08-06 17:50     ` Marie Zhussupova
2025-08-05 15:19   ` Rae Moar
2025-07-29 19:36 ` [PATCH 8/9] kunit: Add example parameterized test with direct dynamic " Marie Zhussupova
2025-08-02  9:44   ` David Gow
2025-08-05 15:19   ` Rae Moar
2025-07-29 19:36 ` [PATCH 9/9] Documentation: kunit: Document new parameterized test features Marie Zhussupova
2025-08-02  9:45   ` David Gow
2025-08-08 13:02     ` Marie Zhussupova
2025-08-05 15:19   ` Rae Moar
2025-08-08 13:05     ` Marie Zhussupova

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=20250729193647.3410634-4-marievic@google.com \
    --to=marievic@google.com \
    --cc=brendan.higgins@linux.dev \
    --cc=davidgow@google.com \
    --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=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

all inboxes | Powered by JetHome®