linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Arpitha Raghunandan <98.arpi@gmail.com>
To: brendanhiggins@google.com, skhan@linuxfoundation.org,
	elver@google.com, yzaikin@google.com, tytso@mit.edu,
	adilger.kernel@dilger.ca
Cc: Arpitha Raghunandan <98.arpi@gmail.com>,
	linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com,
	linux-kernel@vger.kernel.org,
	linux-kernel-mentees@lists.linuxfoundation.org,
	linux-ext4@vger.kernel.org
Subject: [PATCH v2 1/2] kunit: Support for Parameterized Testing
Date: Fri, 23 Oct 2020 20:35:36 +0530	[thread overview]
Message-ID: <20201023150536.282568-1-98.arpi@gmail.com> (raw)

Implementation of support for parameterized testing in KUnit.

Signed-off-by: Arpitha Raghunandan <98.arpi@gmail.com>
---
Changes v1->v2:
- Use of a generator method to access test case parameters

 include/kunit/test.h | 45 ++++++++++++++++++++++++++++++++++++++++++++
 lib/kunit/test.c     | 20 +++++++++++++++++++-
 2 files changed, 64 insertions(+), 1 deletion(-)

diff --git a/include/kunit/test.h b/include/kunit/test.h
index a423fffefea0..c417ac140326 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -141,6 +141,7 @@ struct kunit;
 struct kunit_case {
 	void (*run_case)(struct kunit *test);
 	const char *name;
+	void* (*generate_params)(struct kunit *test, void *prev);
 
 	/* private: internal use only. */
 	bool success;
@@ -162,6 +163,9 @@ static inline char *kunit_status_to_string(bool status)
  * &struct kunit_case for an example on how to use it.
  */
 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
+#define KUNIT_CASE_PARAM(test_name, gen_params)			\
+		{ .run_case = test_name, .name = #test_name,	\
+		  .generate_params = gen_params }
 
 /**
  * struct kunit_suite - describes a related collection of &struct kunit_case
@@ -208,6 +212,15 @@ struct kunit {
 	const char *name; /* Read only after initialization! */
 	char *log; /* Points at case log after initialization */
 	struct kunit_try_catch try_catch;
+	/* param_values points to test case parameters in parameterized tests */
+	void *param_values;
+	/*
+	 * current_param stores the index of the parameter in
+	 * the array of parameters in parameterized tests.
+	 * current_param + 1 is printed to indicate the parameter
+	 * that causes the test to fail in case of test failure.
+	 */
+	int current_param;
 	/*
 	 * success starts as true, and may only be set to false during a
 	 * test case; thus, it is safe to update this across multiple
@@ -1742,4 +1755,36 @@ do {									       \
 						fmt,			       \
 						##__VA_ARGS__)
 
+/**
+ * kunit_param_generator_helper() - Helper method for test parameter generators
+ * 				    required in parameterized tests.
+ * @test: The test context object.
+ * @prev_param: a pointer to the previous test parameter, NULL for first parameter.
+ * @param_array: a user-supplied pointer to an array of test parameters.
+ * @array_size: number of test parameters in the array.
+ * @type_size: size of one test parameter.
+ */
+static inline void *kunit_param_generator_helper(struct kunit *test,
+					void *prev_param,
+					void *param_array,
+					size_t array_size,
+					size_t type_size)
+{
+	KUNIT_ASSERT_EQ(test, (prev_param - param_array) % type_size, 0);
+
+	if (!prev_param)
+		return param_array;
+
+	KUNIT_ASSERT_GE(test, prev_param, param_array);
+
+	if (prev_param + type_size < param_array + (array_size * type_size))
+		return prev_param + type_size;
+	else
+		return NULL;
+}
+
+#define KUNIT_PARAM_GENERATOR_HELPER(test, prev_param, param_array, param_type) \
+	kunit_param_generator_helper(test, prev_param, param_array,		\
+				ARRAY_SIZE(param_array), sizeof(param_type))
+
 #endif /* _KUNIT_TEST_H */
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index 750704abe89a..0e6ffe6384a7 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -127,6 +127,11 @@ unsigned int kunit_test_case_num(struct kunit_suite *suite,
 }
 EXPORT_SYMBOL_GPL(kunit_test_case_num);
 
+static void kunit_print_failed_param(struct kunit *test)
+{
+	kunit_err(test, "\n\tTest failed at parameter: %d\n", test->current_param + 1);
+}
+
 static void kunit_print_string_stream(struct kunit *test,
 				      struct string_stream *stream)
 {
@@ -168,6 +173,8 @@ static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
 	assert->format(assert, stream);
 
 	kunit_print_string_stream(test, stream);
+	if (test->param_values)
+		kunit_print_failed_param(test);
 
 	WARN_ON(string_stream_destroy(stream));
 }
@@ -239,7 +246,18 @@ static void kunit_run_case_internal(struct kunit *test,
 		}
 	}
 
-	test_case->run_case(test);
+	if (!test_case->generate_params) {
+		test_case->run_case(test);
+	} else {
+		test->param_values = test_case->generate_params(test, NULL);
+		test->current_param = 0;
+
+		while (test->param_values) {
+			test_case->run_case(test);
+			test->param_values = test_case->generate_params(test, test->param_values);
+			test->current_param++;
+		}
+	}
 }
 
 static void kunit_case_internal_cleanup(struct kunit *test)
-- 
2.25.1


             reply	other threads:[~2020-10-23 15:06 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-23 15:05 Arpitha Raghunandan [this message]
2020-10-23 15:06 ` [PATCH v2 2/2] fs: ext4: Modify inode-test.c to use KUnit parameterized testing feature Arpitha Raghunandan
2020-10-23 17:33   ` kernel test robot
2020-10-26 18:11   ` Iurii Zaikin
2020-10-26 18:19     ` Arpitha Raghunandan
2020-10-23 18:08 ` [PATCH v2 1/2] kunit: Support for Parameterized Testing kernel test robot
2020-10-23 18:14 ` kernel test robot
2020-10-23 18:48 ` Marco Elver
2020-10-23 18:56   ` Marco Elver
2020-10-24  5:23   ` Arpitha Raghunandan

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=20201023150536.282568-1-98.arpi@gmail.com \
    --to=98.arpi@gmail.com \
    --cc=adilger.kernel@dilger.ca \
    --cc=brendanhiggins@google.com \
    --cc=elver@google.com \
    --cc=kunit-dev@googlegroups.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-kernel-mentees@lists.linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=skhan@linuxfoundation.org \
    --cc=tytso@mit.edu \
    --cc=yzaikin@google.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).