lib/tests/bitops_kunit.c

Source file repositories/reference/linux-study-clean/lib/tests/bitops_kunit.c

File Facts

System
Linux kernel
Corpus path
lib/tests/bitops_kunit.c
Extension
.c
Size
5555 bytes
Lines
206
Domain
Kernel Services
Bucket
lib
Inferred role
Kernel Services: implementation source
Status
source implementation candidate

Why This File Exists

Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.

Dependency Surface

Detected Declarations

Annotated Snippet

struct bitops_test_case {
	const char *str;
	const long nr;
};

static struct bitops_test_case bitops_cases[] = {
	{
		.str = "BITOPS_4",
		.nr = BITOPS_4,
	},
	{
		.str = "BITOPS_7",
		.nr = BITOPS_7,
	},
	{
		.str = "BITOPS_11",
		.nr = BITOPS_11,
	},
	{
		.str = "BITOPS_31",
		.nr = BITOPS_31,
	},
	{
		.str = "BITOPS_88",
		.nr = BITOPS_88,
	},
};

KUNIT_ARRAY_PARAM_DESC(bitops, bitops_cases, str);

static void test_set_bit_clear_bit(struct kunit *test)
{
	const struct bitops_test_case *params = test->param_value;
	DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
	int bit_set;

	bitmap_zero(bitmap, BITOPS_LENGTH);

	set_bit(params->nr, bitmap);
	KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));

	clear_bit(params->nr, bitmap);
	KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));

	bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
	KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
}

static void test_change_bit(struct kunit *test)
{
	const struct bitops_test_case *params = test->param_value;
	DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
	int bit_set;

	bitmap_zero(bitmap, BITOPS_LENGTH);

	change_bit(params->nr, bitmap);
	KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));

	change_bit(params->nr, bitmap);
	KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));

	bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
	KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
}

static void test_test_and_set_bit_test_and_clear_bit(struct kunit *test)
{
	const struct bitops_test_case *params = test->param_value;
	DECLARE_BITMAP(bitmap, BITOPS_LENGTH);
	int bit_set;

	bitmap_zero(bitmap, BITOPS_LENGTH);

	KUNIT_EXPECT_FALSE(test, test_and_set_bit(params->nr, bitmap));
	KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));

	KUNIT_EXPECT_TRUE(test, test_and_set_bit(params->nr, bitmap));
	KUNIT_EXPECT_TRUE(test, test_bit(params->nr, bitmap));

	KUNIT_EXPECT_TRUE(test, test_and_clear_bit(params->nr, bitmap));
	KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));

	KUNIT_EXPECT_FALSE(test, test_and_clear_bit(params->nr, bitmap));
	KUNIT_EXPECT_FALSE(test, test_bit(params->nr, bitmap));

	bit_set = find_first_bit(bitmap, BITOPS_LENGTH);
	KUNIT_EXPECT_EQ(test, bit_set, BITOPS_LENGTH);
}

Annotation

Implementation Notes