Commit 5f6b99d0 authored by Arpitha Raghunandan's avatar Arpitha Raghunandan Committed by Shuah Khan
Browse files

fs: ext4: Modify inode-test.c to use KUnit parameterized testing feature



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

Signed-off-by: default avatarArpitha Raghunandan <98.arpi@gmail.com>
Signed-off-by: default avatarMarco Elver <elver@google.com>
Reviewed-by: default avatarMarco Elver <elver@google.com>
Tested-by: default avatarDavid Gow <davidgow@google.com>
Reviewed-by: default avatarDavid Gow <davidgow@google.com>
Reviewed-by: default avatarIurii Zaikin <yzaikin@google.com>
Acked-by: default avatarTheodore Ts'o <tytso@mit.edu>
Signed-off-by: default avatarShuah Khan <skhan@linuxfoundation.org>
parent fadb08e7
Loading
Loading
Loading
Loading
+164 −156
Original line number Diff line number Diff line
@@ -80,28 +80,7 @@ struct timestamp_expectation {
	bool lower_bound;
};

static time64_t get_32bit_time(const struct timestamp_expectation * const test)
{
	if (test->msb_set) {
		if (test->lower_bound)
			return LOWER_MSB_1;

		return UPPER_MSB_1;
	}

	if (test->lower_bound)
		return LOWER_MSB_0;
	return UPPER_MSB_0;
}


/*
 *  Test data is derived from the table in the Inode Timestamps section of
 *  Documentation/filesystems/ext4/inodes.rst.
 */
static void inode_test_xtimestamp_decoding(struct kunit *test)
{
	const struct timestamp_expectation test_data[] = {
static const struct timestamp_expectation test_data[] = {
	{
		.test_case_name = LOWER_BOUND_NEG_NO_EXTRA_BITS_CASE,
		.msb_set = true,
@@ -232,35 +211,64 @@ static void inode_test_xtimestamp_decoding(struct kunit *test)
	}
};

static void timestamp_expectation_to_desc(const struct timestamp_expectation *t,
					  char *desc)
{
	strscpy(desc, t->test_case_name, KUNIT_PARAM_DESC_SIZE);
}

KUNIT_ARRAY_PARAM(ext4_inode, test_data, timestamp_expectation_to_desc);

static time64_t get_32bit_time(const struct timestamp_expectation * const test)
{
	if (test->msb_set) {
		if (test->lower_bound)
			return LOWER_MSB_1;

		return UPPER_MSB_1;
	}

	if (test->lower_bound)
		return LOWER_MSB_0;
	return UPPER_MSB_0;
}


/*
 *  Test data is derived from the table in the Inode Timestamps section of
 *  Documentation/filesystems/ext4/inodes.rst.
 */
static void inode_test_xtimestamp_decoding(struct kunit *test)
{
	struct timespec64 timestamp;
	int i;

	for (i = 0; i < ARRAY_SIZE(test_data); ++i) {
		timestamp.tv_sec = get_32bit_time(&test_data[i]);
	struct timestamp_expectation *test_param =
			(struct timestamp_expectation *)(test->param_value);

	timestamp.tv_sec = get_32bit_time(test_param);
	ext4_decode_extra_time(&timestamp,
				       cpu_to_le32(test_data[i].extra_bits));
			       cpu_to_le32(test_param->extra_bits));

	KUNIT_EXPECT_EQ_MSG(test,
				    test_data[i].expected.tv_sec,
			    test_param->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);
			    test_param->test_case_name,
			    test_param->msb_set,
			    test_param->lower_bound,
			    test_param->extra_bits);
	KUNIT_EXPECT_EQ_MSG(test,
				    test_data[i].expected.tv_nsec,
			    test_param->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);
	}
			    test_param->test_case_name,
			    test_param->msb_set,
			    test_param->lower_bound,
			    test_param->extra_bits);
}

static struct kunit_case ext4_inode_test_cases[] = {
	KUNIT_CASE(inode_test_xtimestamp_decoding),
	KUNIT_CASE_PARAM(inode_test_xtimestamp_decoding, ext4_inode_gen_params),
	{}
};