Skip to content

Commit

Permalink
kunit: Support for Parameterized Testing
Browse files Browse the repository at this point in the history
Implementation of support for parameterized testing in KUnit.

Signed-off-by: Arpitha Raghunandan <98.arpi@gmail.com>
  • Loading branch information
arpi-r authored and intel-lab-lkp committed Oct 23, 2020
1 parent 1322181 commit 2c09a79
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 1 deletion.
45 changes: 45 additions & 0 deletions include/kunit/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,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;
Expand All @@ -161,6 +162,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
Expand Down Expand Up @@ -207,6 +211,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
Expand Down Expand Up @@ -1701,4 +1714,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 */
20 changes: 19 additions & 1 deletion lib/kunit/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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->current_param + 1);
}

static void kunit_print_string_stream(struct kunit *test,
struct string_stream *stream)
{
Expand Down Expand Up @@ -183,6 +188,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));
}
Expand Down Expand Up @@ -254,7 +261,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)
Expand Down

0 comments on commit 2c09a79

Please sign in to comment.