linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] kunit: Support for Parameterized Testing
@ 2020-10-10 14:53 Arpitha Raghunandan
  2020-10-10 14:55 ` [PATCH 2/2] fs: ext4: Modify inode-test.c to use KUnit parameterized testing feature Arpitha Raghunandan
  2020-10-12 11:00 ` [PATCH 1/2] kunit: Support for Parameterized Testing Marco Elver
  0 siblings, 2 replies; 5+ messages in thread
From: Arpitha Raghunandan @ 2020-10-10 14:53 UTC (permalink / raw)
  To: brendanhiggins, skhan, yzaikin, elver, tytso, adilger.kernel
  Cc: Arpitha Raghunandan, linux-kselftest, kunit-dev,
	linux-kernel-mentees, linux-ext4, linux-kernel

Implementation of support for parameterized testing in KUnit.

Signed-off-by: Arpitha Raghunandan <98.arpi@gmail.com>
---
 include/kunit/test.h | 29 +++++++++++++++++++++++++++++
 lib/kunit/test.c     | 44 +++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/include/kunit/test.h b/include/kunit/test.h
index 59f3144f009a..4740d66269b4 100644
--- a/include/kunit/test.h
+++ b/include/kunit/test.h
@@ -140,10 +140,14 @@ struct kunit;
 struct kunit_case {
 	void (*run_case)(struct kunit *test);
 	const char *name;
+	void* (*get_params)(void);
+	int max_parameters_count;
+	int parameter_size;
 
 	/* private: internal use only. */
 	bool success;
 	char *log;
+	bool parameterized;
 };
 
 static inline char *kunit_status_to_string(bool status)
@@ -162,6 +166,11 @@ static inline char *kunit_status_to_string(bool status)
  */
 #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
 
+#define KUNIT_CASE_PARAM(test_name, getparams, count, size)				\
+		{ .run_case = test_name, .name = #test_name,				\
+		  .parameterized = true, .get_params = (void* (*)(void))getparams,	\
+		  .max_parameters_count = count, .parameter_size = size }
+
 /**
  * struct kunit_suite - describes a related collection of &struct kunit_case
  *
@@ -206,6 +215,23 @@ struct kunit {
 	/* private: internal use only. */
 	const char *name; /* Read only after initialization! */
 	char *log; /* Points at case log after initialization */
+	bool parameterized; /* True for parameterized tests */
+	/* param_values stores the test parameters
+	 * for parameterized tests.
+	 */
+	void *param_values;
+	/* max_parameters_count indicates maximum number of
+	 * parameters for parameterized tests.
+	 */
+	int max_parameters_count;
+	/* iterator_count is used by the iterator method
+	 * for parameterized tests.
+	 */
+	int iterator_count;
+	/* parameter_size indicates size of a single test case
+	 * for parameterized tests.
+	 */
+	int parameter_size;
 	struct kunit_try_catch try_catch;
 	/*
 	 * success starts as true, and may only be set to false during a
@@ -225,6 +251,7 @@ struct kunit {
 };
 
 void kunit_init_test(struct kunit *test, const char *name, char *log);
+void kunit_init_param_test(struct kunit *test, struct kunit_case *test_case);
 
 int kunit_run_tests(struct kunit_suite *suite);
 
@@ -237,6 +264,8 @@ int __kunit_test_suites_init(struct kunit_suite **suites);
 
 void __kunit_test_suites_exit(struct kunit_suite **suites);
 
+void *get_test_case_parameters(struct kunit *test);
+
 /**
  * kunit_test_suites() - used to register one or more &struct kunit_suite
  *			 with KUnit.
diff --git a/lib/kunit/test.c b/lib/kunit/test.c
index c36037200310..ab9e13c81d4a 100644
--- a/lib/kunit/test.c
+++ b/lib/kunit/test.c
@@ -142,6 +142,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->iterator_count);
+}
+
 static void kunit_print_string_stream(struct kunit *test,
 				      struct string_stream *stream)
 {
@@ -182,6 +187,9 @@ static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
 
 	assert->format(assert, stream);
 
+	if (test->parameterized)
+		kunit_print_failed_param(test);
+
 	kunit_print_string_stream(test, stream);
 
 	WARN_ON(string_stream_destroy(stream));
@@ -236,6 +244,18 @@ void kunit_init_test(struct kunit *test, const char *name, char *log)
 }
 EXPORT_SYMBOL_GPL(kunit_init_test);
 
+void kunit_init_param_test(struct kunit *test, struct kunit_case *test_case)
+{
+	spin_lock_init(&test->lock);
+	INIT_LIST_HEAD(&test->resources);
+	test->parameterized = true;
+	test->param_values = (void *)(test_case->get_params());
+	test->max_parameters_count = test_case->max_parameters_count;
+	test->parameter_size = test_case->parameter_size;
+	test->iterator_count = 0;
+}
+EXPORT_SYMBOL_GPL(kunit_init_param_test);
+
 /*
  * Initializes and runs test case. Does not clean up or do post validations.
  */
@@ -254,7 +274,14 @@ static void kunit_run_case_internal(struct kunit *test,
 		}
 	}
 
-	test_case->run_case(test);
+	if (!test->parameterized) {
+		test_case->run_case(test);
+	} else {
+		int i;
+
+		for (i = 0; i < test->max_parameters_count; i++)
+			test_case->run_case(test);
+	}
 }
 
 static void kunit_case_internal_cleanup(struct kunit *test)
@@ -343,6 +370,8 @@ static void kunit_run_case_catch_errors(struct kunit_suite *suite,
 	struct kunit test;
 
 	kunit_init_test(&test, test_case->name, test_case->log);
+	if (test_case->parameterized)
+		kunit_init_param_test(&test, test_case);
 	try_catch = &test.try_catch;
 
 	kunit_try_catch_init(try_catch,
@@ -407,6 +436,19 @@ void __kunit_test_suites_exit(struct kunit_suite **suites)
 }
 EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
 
+/*
+ * Iterator method for the parameterized test cases
+ */
+void *get_test_case_parameters(struct kunit *test)
+{
+	int index = test->iterator_count * test->parameter_size;
+
+	if (test->iterator_count != test->max_parameters_count)
+		test->iterator_count++;
+	return (test->param_values + index);
+}
+EXPORT_SYMBOL_GPL(get_test_case_parameters);
+
 /*
  * Used for static resources and when a kunit_resource * has been created by
  * kunit_alloc_resource().  When an init function is supplied, @data is passed
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] fs: ext4: Modify inode-test.c to use KUnit parameterized testing feature
  2020-10-10 14:53 [PATCH 1/2] kunit: Support for Parameterized Testing Arpitha Raghunandan
@ 2020-10-10 14:55 ` Arpitha Raghunandan
  2020-10-11  0:02   ` kernel test robot
  2020-10-12 11:00 ` [PATCH 1/2] kunit: Support for Parameterized Testing Marco Elver
  1 sibling, 1 reply; 5+ messages in thread
From: Arpitha Raghunandan @ 2020-10-10 14:55 UTC (permalink / raw)
  To: brendanhiggins, skhan, yzaikin, elver, tytso, adilger.kernel
  Cc: Arpitha Raghunandan, linux-kselftest, kunit-dev,
	linux-kernel-mentees, linux-ext4, linux-kernel

Modifies fs/ext4/inode-test.c to use the parameterized testing
feature of KUnit.

Signed-off-by: Arpitha Raghunandan <98.arpi@gmail.com>
---
 fs/ext4/inode-test.c | 64 +++++++++++++++++++++++++-------------------
 1 file changed, 36 insertions(+), 28 deletions(-)

diff --git a/fs/ext4/inode-test.c b/fs/ext4/inode-test.c
index d62d802c9c12..691ef0a4ffe1 100644
--- a/fs/ext4/inode-test.c
+++ b/fs/ext4/inode-test.c
@@ -72,6 +72,8 @@
 #define UPPER_BOUND_NONNEG_EXTRA_BITS_1_CASE\
 	"2446-05-10 Upper bound of 32bit >=0 timestamp. All extra sec bits on"
 
+#define NUMBER_OF_TESTCASES 16
+
 struct timestamp_expectation {
 	const char *test_case_name;
 	struct timespec64 expected;
@@ -101,7 +103,36 @@ static time64_t get_32bit_time(const struct timestamp_expectation * const test)
  */
 static void inode_test_xtimestamp_decoding(struct kunit *test)
 {
-	const struct timestamp_expectation test_data[] = {
+	struct timespec64 timestamp;
+
+	struct timestamp_expectation *test_data =
+		(struct timestamp_expectation *)get_test_case_parameters(test);
+
+	timestamp.tv_sec = get_32bit_time(test_data);
+	ext4_decode_extra_time(&timestamp,
+			       cpu_to_le32(test_data->extra_bits));
+
+	KUNIT_EXPECT_EQ_MSG(test,
+			    test_data->expected.tv_sec,
+			    timestamp.tv_sec,
+			    CASE_NAME_FORMAT,
+			    test_data->test_case_name,
+			    test_data->msb_set,
+			    test_data->lower_bound,
+			    test_data->extra_bits);
+	KUNIT_EXPECT_EQ_MSG(test,
+			    test_data->expected.tv_nsec,
+			    timestamp.tv_nsec,
+			    CASE_NAME_FORMAT,
+			    test_data->test_case_name,
+			    test_data->msb_set,
+			    test_data->lower_bound,
+			    test_data->extra_bits);
+}
+
+struct timestamp_expectation *get_test_parameters(void)
+{
+	static struct timestamp_expectation test_data[] = {
 		{
 			.test_case_name = LOWER_BOUND_NEG_NO_EXTRA_BITS_CASE,
 			.msb_set = true,
@@ -231,36 +262,13 @@ static void inode_test_xtimestamp_decoding(struct kunit *test)
 			.expected = {.tv_sec = 0x37fffffffLL, .tv_nsec = 0L},
 		}
 	};
-
-	struct timespec64 timestamp;
-	int i;
-
-	for (i = 0; i < ARRAY_SIZE(test_data); ++i) {
-		timestamp.tv_sec = get_32bit_time(&test_data[i]);
-		ext4_decode_extra_time(&timestamp,
-				       cpu_to_le32(test_data[i].extra_bits));
-
-		KUNIT_EXPECT_EQ_MSG(test,
-				    test_data[i].expected.tv_sec,
-				    timestamp.tv_sec,
-				    CASE_NAME_FORMAT,
-				    test_data[i].test_case_name,
-				    test_data[i].msb_set,
-				    test_data[i].lower_bound,
-				    test_data[i].extra_bits);
-		KUNIT_EXPECT_EQ_MSG(test,
-				    test_data[i].expected.tv_nsec,
-				    timestamp.tv_nsec,
-				    CASE_NAME_FORMAT,
-				    test_data[i].test_case_name,
-				    test_data[i].msb_set,
-				    test_data[i].lower_bound,
-				    test_data[i].extra_bits);
-	}
+	return test_data;
 }
 
 static struct kunit_case ext4_inode_test_cases[] = {
-	KUNIT_CASE(inode_test_xtimestamp_decoding),
+	KUNIT_CASE_PARAM(inode_test_xtimestamp_decoding,
+			get_test_parameters, NUMBER_OF_TESTCASES,
+			sizeof(struct timestamp_expectation)),
 	{}
 };
 
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] fs: ext4: Modify inode-test.c to use KUnit parameterized testing feature
  2020-10-10 14:55 ` [PATCH 2/2] fs: ext4: Modify inode-test.c to use KUnit parameterized testing feature Arpitha Raghunandan
@ 2020-10-11  0:02   ` kernel test robot
  0 siblings, 0 replies; 5+ messages in thread
From: kernel test robot @ 2020-10-11  0:02 UTC (permalink / raw)
  To: Arpitha Raghunandan, brendanhiggins, skhan, yzaikin, elver,
	tytso, adilger.kernel
  Cc: kbuild-all, clang-built-linux, Arpitha Raghunandan,
	linux-kselftest, kunit-dev, linux-kernel-mentees

[-- Attachment #1: Type: text/plain, Size: 6342 bytes --]

Hi Arpitha,

Thank you for the patch! Perhaps something to improve:

[auto build test WARNING on ext4/dev]
[also build test WARNING on linus/master v5.9-rc8 next-20201009]
[cannot apply to tytso-fscrypt/master]
[If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch]

url:    https://github.com/0day-ci/linux/commits/Arpitha-Raghunandan/kunit-Support-for-Parameterized-Testing/20201011-051918
base:   https://git.kernel.org/pub/scm/linux/kernel/git/tytso/ext4.git dev
config: arm-randconfig-r031-20201011 (attached as .config)
compiler: clang version 12.0.0 (https://github.com/llvm/llvm-project 9b5b3050237db3642ed7ab1bdb3ffa2202511b99)
reproduce (this is a W=1 build):
        wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # install arm cross compiling tool for clang build
        # apt-get install binutils-arm-linux-gnueabi
        # https://github.com/0day-ci/linux/commit/0cd253a8f2af3fd4e88c9ec8d7327bb26302c1da
        git remote add linux-review https://github.com/0day-ci/linux
        git fetch --no-tags linux-review Arpitha-Raghunandan/kunit-Support-for-Parameterized-Testing/20201011-051918
        git checkout 0cd253a8f2af3fd4e88c9ec8d7327bb26302c1da
        # save the attached .config to linux build tree
        COMPILER_INSTALL_PATH=$HOME/0day COMPILER=clang make.cross ARCH=arm 

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>

All warnings (new ones prefixed by >>):

>> fs/ext4/inode-test.c:133:31: warning: no previous prototype for function 'get_test_parameters' [-Wmissing-prototypes]
   struct timestamp_expectation *get_test_parameters(void)
                                 ^
   fs/ext4/inode-test.c:133:1: note: declare 'static' if the function is not intended to be used outside of this translation unit
   struct timestamp_expectation *get_test_parameters(void)
   ^
   static 
   1 warning generated.

vim +/get_test_parameters +133 fs/ext4/inode-test.c

   132	
 > 133	struct timestamp_expectation *get_test_parameters(void)
   134	{
   135		static struct timestamp_expectation test_data[] = {
   136			{
   137				.test_case_name = LOWER_BOUND_NEG_NO_EXTRA_BITS_CASE,
   138				.msb_set = true,
   139				.lower_bound = true,
   140				.extra_bits = 0,
   141				.expected = {.tv_sec = -0x80000000LL, .tv_nsec = 0L},
   142			},
   143	
   144			{
   145				.test_case_name = UPPER_BOUND_NEG_NO_EXTRA_BITS_CASE,
   146				.msb_set = true,
   147				.lower_bound = false,
   148				.extra_bits = 0,
   149				.expected = {.tv_sec = -1LL, .tv_nsec = 0L},
   150			},
   151	
   152			{
   153				.test_case_name = LOWER_BOUND_NONNEG_NO_EXTRA_BITS_CASE,
   154				.msb_set = false,
   155				.lower_bound = true,
   156				.extra_bits = 0,
   157				.expected = {0LL, 0L},
   158			},
   159	
   160			{
   161				.test_case_name = UPPER_BOUND_NONNEG_NO_EXTRA_BITS_CASE,
   162				.msb_set = false,
   163				.lower_bound = false,
   164				.extra_bits = 0,
   165				.expected = {.tv_sec = 0x7fffffffLL, .tv_nsec = 0L},
   166			},
   167	
   168			{
   169				.test_case_name = LOWER_BOUND_NEG_LO_1_CASE,
   170				.msb_set = true,
   171				.lower_bound = true,
   172				.extra_bits = 1,
   173				.expected = {.tv_sec = 0x80000000LL, .tv_nsec = 0L},
   174			},
   175	
   176			{
   177				.test_case_name = UPPER_BOUND_NEG_LO_1_CASE,
   178				.msb_set = true,
   179				.lower_bound = false,
   180				.extra_bits = 1,
   181				.expected = {.tv_sec = 0xffffffffLL, .tv_nsec = 0L},
   182			},
   183	
   184			{
   185				.test_case_name = LOWER_BOUND_NONNEG_LO_1_CASE,
   186				.msb_set = false,
   187				.lower_bound = true,
   188				.extra_bits = 1,
   189				.expected = {.tv_sec = 0x100000000LL, .tv_nsec = 0L},
   190			},
   191	
   192			{
   193				.test_case_name = UPPER_BOUND_NONNEG_LO_1_CASE,
   194				.msb_set = false,
   195				.lower_bound = false,
   196				.extra_bits = 1,
   197				.expected = {.tv_sec = 0x17fffffffLL, .tv_nsec = 0L},
   198			},
   199	
   200			{
   201				.test_case_name = LOWER_BOUND_NEG_HI_1_CASE,
   202				.msb_set = true,
   203				.lower_bound = true,
   204				.extra_bits =  2,
   205				.expected = {.tv_sec = 0x180000000LL, .tv_nsec = 0L},
   206			},
   207	
   208			{
   209				.test_case_name = UPPER_BOUND_NEG_HI_1_CASE,
   210				.msb_set = true,
   211				.lower_bound = false,
   212				.extra_bits = 2,
   213				.expected = {.tv_sec = 0x1ffffffffLL, .tv_nsec = 0L},
   214			},
   215	
   216			{
   217				.test_case_name = LOWER_BOUND_NONNEG_HI_1_CASE,
   218				.msb_set = false,
   219				.lower_bound = true,
   220				.extra_bits = 2,
   221				.expected = {.tv_sec = 0x200000000LL, .tv_nsec = 0L},
   222			},
   223	
   224			{
   225				.test_case_name = UPPER_BOUND_NONNEG_HI_1_CASE,
   226				.msb_set = false,
   227				.lower_bound = false,
   228				.extra_bits = 2,
   229				.expected = {.tv_sec = 0x27fffffffLL, .tv_nsec = 0L},
   230			},
   231	
   232			{
   233				.test_case_name = UPPER_BOUND_NONNEG_HI_1_NS_1_CASE,
   234				.msb_set = false,
   235				.lower_bound = false,
   236				.extra_bits = 6,
   237				.expected = {.tv_sec = 0x27fffffffLL, .tv_nsec = 1L},
   238			},
   239	
   240			{
   241				.test_case_name = LOWER_BOUND_NONNEG_HI_1_NS_MAX_CASE,
   242				.msb_set = false,
   243				.lower_bound = true,
   244				.extra_bits = 0xFFFFFFFF,
   245				.expected = {.tv_sec = 0x300000000LL,
   246					     .tv_nsec = MAX_NANOSECONDS},
   247			},
   248	
   249			{
   250				.test_case_name = LOWER_BOUND_NONNEG_EXTRA_BITS_1_CASE,
   251				.msb_set = false,
   252				.lower_bound = true,
   253				.extra_bits = 3,
   254				.expected = {.tv_sec = 0x300000000LL, .tv_nsec = 0L},
   255			},
   256	
   257			{
   258				.test_case_name = UPPER_BOUND_NONNEG_EXTRA_BITS_1_CASE,
   259				.msb_set = false,
   260				.lower_bound = false,
   261				.extra_bits = 3,
   262				.expected = {.tv_sec = 0x37fffffffLL, .tv_nsec = 0L},
   263			}
   264		};
   265		return test_data;
   266	}
   267	

---
0-DAY CI Kernel Test Service, Intel Corporation
https://lists.01.org/hyperkitty/list/kbuild-all@lists.01.org

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 29882 bytes --]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] kunit: Support for Parameterized Testing
  2020-10-10 14:53 [PATCH 1/2] kunit: Support for Parameterized Testing Arpitha Raghunandan
  2020-10-10 14:55 ` [PATCH 2/2] fs: ext4: Modify inode-test.c to use KUnit parameterized testing feature Arpitha Raghunandan
@ 2020-10-12 11:00 ` Marco Elver
  2020-10-15  5:28   ` Arpitha Raghunandan
  1 sibling, 1 reply; 5+ messages in thread
From: Marco Elver @ 2020-10-12 11:00 UTC (permalink / raw)
  To: Arpitha Raghunandan
  Cc: Brendan Higgins, skhan, yzaikin, Theodore Ts'o,
	Andreas Dilger, open list:KERNEL SELFTEST FRAMEWORK,
	KUnit Development, linux-kernel-mentees, linux-ext4, LKML

On Sat, 10 Oct 2020 at 16:54, Arpitha Raghunandan <98.arpi@gmail.com> wrote:
> Implementation of support for parameterized testing in KUnit.
>
> Signed-off-by: Arpitha Raghunandan <98.arpi@gmail.com>
> ---
>  include/kunit/test.h | 29 +++++++++++++++++++++++++++++
>  lib/kunit/test.c     | 44 +++++++++++++++++++++++++++++++++++++++++++-
>  2 files changed, 72 insertions(+), 1 deletion(-)
>
> diff --git a/include/kunit/test.h b/include/kunit/test.h
> index 59f3144f009a..4740d66269b4 100644
> --- a/include/kunit/test.h
> +++ b/include/kunit/test.h
> @@ -140,10 +140,14 @@ struct kunit;
>  struct kunit_case {
>         void (*run_case)(struct kunit *test);
>         const char *name;
> +       void* (*get_params)(void);
> +       int max_parameters_count;
> +       int parameter_size;
>
>         /* private: internal use only. */
>         bool success;
>         char *log;
> +       bool parameterized;

Why do you need this bool? Doesn't get_params being non-NULL tell you
if the test case is parameterized?

>  };
>
>  static inline char *kunit_status_to_string(bool status)
> @@ -162,6 +166,11 @@ static inline char *kunit_status_to_string(bool status)
>   */
>  #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
>
> +#define KUNIT_CASE_PARAM(test_name, getparams, count, size)                            \
> +               { .run_case = test_name, .name = #test_name,                            \
> +                 .parameterized = true, .get_params = (void* (*)(void))getparams,      \
> +                 .max_parameters_count = count, .parameter_size = size }
> +

I think this interface is overly complex. For one, if the only purpose
of the getparams function is to return a pointer to some array, then
there are only few cases where I see getparams being a function could
be useful.

Instead, could we make the getparams function behave like a generator?
Because then you do not need count, nor size. Its function signature
would be:

void* (*generate_params)(void* prev_param);

The protocol would be:

- The first call to generate_params is passed prev_param of NULL, and
returns a pointer to the first parameter P[0].

- Every nth successive call to generate_params is passed the previous
parameter P[n-1].

- When no more parameters are available, generate_params returns NULL.

- (generate_params should otherwise be stateless, but this is only
relevant if concurrent calls are expected.)


>  /**
>   * struct kunit_suite - describes a related collection of &struct kunit_case
>   *
> @@ -206,6 +215,23 @@ struct kunit {
>         /* private: internal use only. */
>         const char *name; /* Read only after initialization! */
>         char *log; /* Points at case log after initialization */
> +       bool parameterized; /* True for parameterized tests */
> +       /* param_values stores the test parameters
> +        * for parameterized tests.
> +        */
> +       void *param_values;
> +       /* max_parameters_count indicates maximum number of
> +        * parameters for parameterized tests.
> +        */
> +       int max_parameters_count;
> +       /* iterator_count is used by the iterator method
> +        * for parameterized tests.
> +        */
> +       int iterator_count;
> +       /* parameter_size indicates size of a single test case
> +        * for parameterized tests.
> +        */
> +       int parameter_size;

All of this would become much simpler if you used the generator
approach. Likely only 1 field would be required, which is the current
param.

>         struct kunit_try_catch try_catch;
>         /*
>          * success starts as true, and may only be set to false during a
> @@ -225,6 +251,7 @@ struct kunit {
>  };
>
>  void kunit_init_test(struct kunit *test, const char *name, char *log);
> +void kunit_init_param_test(struct kunit *test, struct kunit_case *test_case);
>
>  int kunit_run_tests(struct kunit_suite *suite);
>
> @@ -237,6 +264,8 @@ int __kunit_test_suites_init(struct kunit_suite **suites);
>
>  void __kunit_test_suites_exit(struct kunit_suite **suites);
>
> +void *get_test_case_parameters(struct kunit *test);
> +
>  /**
>   * kunit_test_suites() - used to register one or more &struct kunit_suite
>   *                      with KUnit.
> diff --git a/lib/kunit/test.c b/lib/kunit/test.c
> index c36037200310..ab9e13c81d4a 100644
> --- a/lib/kunit/test.c
> +++ b/lib/kunit/test.c
> @@ -142,6 +142,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->iterator_count);
> +}
> +
>  static void kunit_print_string_stream(struct kunit *test,
>                                       struct string_stream *stream)
>  {
> @@ -182,6 +187,9 @@ static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
>
>         assert->format(assert, stream);
>
> +       if (test->parameterized)
> +               kunit_print_failed_param(test);
> +
>         kunit_print_string_stream(test, stream);
>
>         WARN_ON(string_stream_destroy(stream));
> @@ -236,6 +244,18 @@ void kunit_init_test(struct kunit *test, const char *name, char *log)
>  }
>  EXPORT_SYMBOL_GPL(kunit_init_test);
>
> +void kunit_init_param_test(struct kunit *test, struct kunit_case *test_case)
> +{
> +       spin_lock_init(&test->lock);
> +       INIT_LIST_HEAD(&test->resources);
> +       test->parameterized = true;
> +       test->param_values = (void *)(test_case->get_params());
> +       test->max_parameters_count = test_case->max_parameters_count;
> +       test->parameter_size = test_case->parameter_size;
> +       test->iterator_count = 0;
> +}
> +EXPORT_SYMBOL_GPL(kunit_init_param_test);
> +
>  /*
>   * Initializes and runs test case. Does not clean up or do post validations.
>   */
> @@ -254,7 +274,14 @@ static void kunit_run_case_internal(struct kunit *test,
>                 }
>         }
>
> -       test_case->run_case(test);
> +       if (!test->parameterized) {
> +               test_case->run_case(test);
> +       } else {
> +               int i;
> +
> +               for (i = 0; i < test->max_parameters_count; i++)
> +                       test_case->run_case(test);

With a generator approach, here you'd call generate_params. Most
likely, you'll need to stash its result somewhere, e.g. test->param,
so it can be retrieved by the test case.

> +       }
>  }
>
>  static void kunit_case_internal_cleanup(struct kunit *test)
> @@ -343,6 +370,8 @@ static void kunit_run_case_catch_errors(struct kunit_suite *suite,
>         struct kunit test;
>
>         kunit_init_test(&test, test_case->name, test_case->log);
> +       if (test_case->parameterized)
> +               kunit_init_param_test(&test, test_case);
>         try_catch = &test.try_catch;
>
>         kunit_try_catch_init(try_catch,
> @@ -407,6 +436,19 @@ void __kunit_test_suites_exit(struct kunit_suite **suites)
>  }
>  EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
>
> +/*
> + * Iterator method for the parameterized test cases
> + */
> +void *get_test_case_parameters(struct kunit *test)
> +{
> +       int index = test->iterator_count * test->parameter_size;
> +
> +       if (test->iterator_count != test->max_parameters_count)
> +               test->iterator_count++;

This is quite confusing, because if get_test_case_parameters is called
multiple times within the same test case, we'll iterate through all
the test case params in the same test case? I think this function
should not have side-effects (like normal getters).

But if you use the generator approach, you'll likely not need this
function anyway.

> +       return (test->param_values + index);

Braces not needed.

> +}
> +EXPORT_SYMBOL_GPL(get_test_case_parameters);
> +
>  /*
>   * Used for static resources and when a kunit_resource * has been created by
>   * kunit_alloc_resource().  When an init function is supplied, @data is passed
> --
> 2.25.1
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] kunit: Support for Parameterized Testing
  2020-10-12 11:00 ` [PATCH 1/2] kunit: Support for Parameterized Testing Marco Elver
@ 2020-10-15  5:28   ` Arpitha Raghunandan
  0 siblings, 0 replies; 5+ messages in thread
From: Arpitha Raghunandan @ 2020-10-15  5:28 UTC (permalink / raw)
  To: Marco Elver
  Cc: Brendan Higgins, skhan, yzaikin, Theodore Ts'o,
	Andreas Dilger, open list:KERNEL SELFTEST FRAMEWORK,
	KUnit Development, linux-kernel-mentees, linux-ext4, LKML

On 12/10/20 4:30 pm, Marco Elver wrote:
> On Sat, 10 Oct 2020 at 16:54, Arpitha Raghunandan <98.arpi@gmail.com> wrote:
>> Implementation of support for parameterized testing in KUnit.
>>
>> Signed-off-by: Arpitha Raghunandan <98.arpi@gmail.com>
>> ---
>>  include/kunit/test.h | 29 +++++++++++++++++++++++++++++
>>  lib/kunit/test.c     | 44 +++++++++++++++++++++++++++++++++++++++++++-
>>  2 files changed, 72 insertions(+), 1 deletion(-)
>>
>> diff --git a/include/kunit/test.h b/include/kunit/test.h
>> index 59f3144f009a..4740d66269b4 100644
>> --- a/include/kunit/test.h
>> +++ b/include/kunit/test.h
>> @@ -140,10 +140,14 @@ struct kunit;
>>  struct kunit_case {
>>         void (*run_case)(struct kunit *test);
>>         const char *name;
>> +       void* (*get_params)(void);
>> +       int max_parameters_count;
>> +       int parameter_size;
>>
>>         /* private: internal use only. */
>>         bool success;
>>         char *log;
>> +       bool parameterized;
> 
> Why do you need this bool? Doesn't get_params being non-NULL tell you
> if the test case is parameterized?
>Yeah, this will. 
>>  };
>>
>>  static inline char *kunit_status_to_string(bool status)
>> @@ -162,6 +166,11 @@ static inline char *kunit_status_to_string(bool status)
>>   */
>>  #define KUNIT_CASE(test_name) { .run_case = test_name, .name = #test_name }
>>
>> +#define KUNIT_CASE_PARAM(test_name, getparams, count, size)                            \
>> +               { .run_case = test_name, .name = #test_name,                            \
>> +                 .parameterized = true, .get_params = (void* (*)(void))getparams,      \
>> +                 .max_parameters_count = count, .parameter_size = size }
>> +
> 
> I think this interface is overly complex. For one, if the only purpose
> of the getparams function is to return a pointer to some array, then
> there are only few cases where I see getparams being a function could
> be useful.
> 
> Instead, could we make the getparams function behave like a generator?
> Because then you do not need count, nor size. Its function signature
> would be:
> 
> void* (*generate_params)(void* prev_param);
> 
> The protocol would be:
> 
> - The first call to generate_params is passed prev_param of NULL, and
> returns a pointer to the first parameter P[0].
> 
> - Every nth successive call to generate_params is passed the previous
> parameter P[n-1].
> 
> - When no more parameters are available, generate_params returns NULL.
> 
> - (generate_params should otherwise be stateless, but this is only
> relevant if concurrent calls are expected.)
> 
> 
>>  /**
>>   * struct kunit_suite - describes a related collection of &struct kunit_case
>>   *
>> @@ -206,6 +215,23 @@ struct kunit {
>>         /* private: internal use only. */
>>         const char *name; /* Read only after initialization! */
>>         char *log; /* Points at case log after initialization */
>> +       bool parameterized; /* True for parameterized tests */
>> +       /* param_values stores the test parameters
>> +        * for parameterized tests.
>> +        */
>> +       void *param_values;
>> +       /* max_parameters_count indicates maximum number of
>> +        * parameters for parameterized tests.
>> +        */
>> +       int max_parameters_count;
>> +       /* iterator_count is used by the iterator method
>> +        * for parameterized tests.
>> +        */
>> +       int iterator_count;
>> +       /* parameter_size indicates size of a single test case
>> +        * for parameterized tests.
>> +        */
>> +       int parameter_size;
> 
> All of this would become much simpler if you used the generator
> approach. Likely only 1 field would be required, which is the current
> param.
> 
>>         struct kunit_try_catch try_catch;
>>         /*
>>          * success starts as true, and may only be set to false during a
>> @@ -225,6 +251,7 @@ struct kunit {
>>  };
>>
>>  void kunit_init_test(struct kunit *test, const char *name, char *log);
>> +void kunit_init_param_test(struct kunit *test, struct kunit_case *test_case);
>>
>>  int kunit_run_tests(struct kunit_suite *suite);
>>
>> @@ -237,6 +264,8 @@ int __kunit_test_suites_init(struct kunit_suite **suites);
>>
>>  void __kunit_test_suites_exit(struct kunit_suite **suites);
>>
>> +void *get_test_case_parameters(struct kunit *test);
>> +
>>  /**
>>   * kunit_test_suites() - used to register one or more &struct kunit_suite
>>   *                      with KUnit.
>> diff --git a/lib/kunit/test.c b/lib/kunit/test.c
>> index c36037200310..ab9e13c81d4a 100644
>> --- a/lib/kunit/test.c
>> +++ b/lib/kunit/test.c
>> @@ -142,6 +142,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->iterator_count);
>> +}
>> +
>>  static void kunit_print_string_stream(struct kunit *test,
>>                                       struct string_stream *stream)
>>  {
>> @@ -182,6 +187,9 @@ static void kunit_fail(struct kunit *test, struct kunit_assert *assert)
>>
>>         assert->format(assert, stream);
>>
>> +       if (test->parameterized)
>> +               kunit_print_failed_param(test);
>> +
>>         kunit_print_string_stream(test, stream);
>>
>>         WARN_ON(string_stream_destroy(stream));
>> @@ -236,6 +244,18 @@ void kunit_init_test(struct kunit *test, const char *name, char *log)
>>  }
>>  EXPORT_SYMBOL_GPL(kunit_init_test);
>>
>> +void kunit_init_param_test(struct kunit *test, struct kunit_case *test_case)
>> +{
>> +       spin_lock_init(&test->lock);
>> +       INIT_LIST_HEAD(&test->resources);
>> +       test->parameterized = true;
>> +       test->param_values = (void *)(test_case->get_params());
>> +       test->max_parameters_count = test_case->max_parameters_count;
>> +       test->parameter_size = test_case->parameter_size;
>> +       test->iterator_count = 0;
>> +}
>> +EXPORT_SYMBOL_GPL(kunit_init_param_test);
>> +
>>  /*
>>   * Initializes and runs test case. Does not clean up or do post validations.
>>   */
>> @@ -254,7 +274,14 @@ static void kunit_run_case_internal(struct kunit *test,
>>                 }
>>         }
>>
>> -       test_case->run_case(test);
>> +       if (!test->parameterized) {
>> +               test_case->run_case(test);
>> +       } else {
>> +               int i;
>> +
>> +               for (i = 0; i < test->max_parameters_count; i++)
>> +                       test_case->run_case(test);
> 
> With a generator approach, here you'd call generate_params. Most
> likely, you'll need to stash its result somewhere, e.g. test->param,
> so it can be retrieved by the test case.
> 
>> +       }
>>  }
>>
>>  static void kunit_case_internal_cleanup(struct kunit *test)
>> @@ -343,6 +370,8 @@ static void kunit_run_case_catch_errors(struct kunit_suite *suite,
>>         struct kunit test;
>>
>>         kunit_init_test(&test, test_case->name, test_case->log);
>> +       if (test_case->parameterized)
>> +               kunit_init_param_test(&test, test_case);
>>         try_catch = &test.try_catch;
>>
>>         kunit_try_catch_init(try_catch,
>> @@ -407,6 +436,19 @@ void __kunit_test_suites_exit(struct kunit_suite **suites)
>>  }
>>  EXPORT_SYMBOL_GPL(__kunit_test_suites_exit);
>>
>> +/*
>> + * Iterator method for the parameterized test cases
>> + */
>> +void *get_test_case_parameters(struct kunit *test)
>> +{
>> +       int index = test->iterator_count * test->parameter_size;
>> +
>> +       if (test->iterator_count != test->max_parameters_count)
>> +               test->iterator_count++;
> 
> This is quite confusing, because if get_test_case_parameters is called
> multiple times within the same test case, we'll iterate through all
> the test case params in the same test case? I think this function
> should not have side-effects (like normal getters).
> 
> But if you use the generator approach, you'll likely not need this
> function anyway.
>
The generator approach sounds good. I will work on it for the next version.
 
>> +       return (test->param_values + index);
> 
> Braces not needed.
> 
I will fix this.
>> +}
>> +EXPORT_SYMBOL_GPL(get_test_case_parameters);
>> +
>>  /*
>>   * Used for static resources and when a kunit_resource * has been created by
>>   * kunit_alloc_resource().  When an init function is supplied, @data is passed
>> --
>> 2.25.1
>>


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-10-15  5:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-10 14:53 [PATCH 1/2] kunit: Support for Parameterized Testing Arpitha Raghunandan
2020-10-10 14:55 ` [PATCH 2/2] fs: ext4: Modify inode-test.c to use KUnit parameterized testing feature Arpitha Raghunandan
2020-10-11  0:02   ` kernel test robot
2020-10-12 11:00 ` [PATCH 1/2] kunit: Support for Parameterized Testing Marco Elver
2020-10-15  5:28   ` Arpitha Raghunandan

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).