162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Test cases for functions and macros in bits.h
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <kunit/test.h>
762306a36Sopenharmony_ci#include <linux/bits.h>
862306a36Sopenharmony_ci
962306a36Sopenharmony_ci
1062306a36Sopenharmony_cistatic void genmask_test(struct kunit *test)
1162306a36Sopenharmony_ci{
1262306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 1ul, GENMASK(0, 0));
1362306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 3ul, GENMASK(1, 0));
1462306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 6ul, GENMASK(2, 1));
1562306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 0xFFFFFFFFul, GENMASK(31, 0));
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#ifdef TEST_GENMASK_FAILURES
1862306a36Sopenharmony_ci	/* these should fail compilation */
1962306a36Sopenharmony_ci	GENMASK(0, 1);
2062306a36Sopenharmony_ci	GENMASK(0, 10);
2162306a36Sopenharmony_ci	GENMASK(9, 10);
2262306a36Sopenharmony_ci#endif
2362306a36Sopenharmony_ci
2462306a36Sopenharmony_ci
2562306a36Sopenharmony_ci}
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_cistatic void genmask_ull_test(struct kunit *test)
2862306a36Sopenharmony_ci{
2962306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 1ull, GENMASK_ULL(0, 0));
3062306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 3ull, GENMASK_ULL(1, 0));
3162306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 0x000000ffffe00000ull, GENMASK_ULL(39, 21));
3262306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 0xffffffffffffffffull, GENMASK_ULL(63, 0));
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#ifdef TEST_GENMASK_FAILURES
3562306a36Sopenharmony_ci	/* these should fail compilation */
3662306a36Sopenharmony_ci	GENMASK_ULL(0, 1);
3762306a36Sopenharmony_ci	GENMASK_ULL(0, 10);
3862306a36Sopenharmony_ci	GENMASK_ULL(9, 10);
3962306a36Sopenharmony_ci#endif
4062306a36Sopenharmony_ci}
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_cistatic void genmask_input_check_test(struct kunit *test)
4362306a36Sopenharmony_ci{
4462306a36Sopenharmony_ci	unsigned int x, y;
4562306a36Sopenharmony_ci	int z, w;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	/* Unknown input */
4862306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(x, 0));
4962306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(0, x));
5062306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(x, y));
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(z, 0));
5362306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(0, z));
5462306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(z, w));
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci	/* Valid input */
5762306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(1, 1));
5862306a36Sopenharmony_ci	KUNIT_EXPECT_EQ(test, 0, GENMASK_INPUT_CHECK(39, 21));
5962306a36Sopenharmony_ci}
6062306a36Sopenharmony_ci
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_cistatic struct kunit_case bits_test_cases[] = {
6362306a36Sopenharmony_ci	KUNIT_CASE(genmask_test),
6462306a36Sopenharmony_ci	KUNIT_CASE(genmask_ull_test),
6562306a36Sopenharmony_ci	KUNIT_CASE(genmask_input_check_test),
6662306a36Sopenharmony_ci	{}
6762306a36Sopenharmony_ci};
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_cistatic struct kunit_suite bits_test_suite = {
7062306a36Sopenharmony_ci	.name = "bits-test",
7162306a36Sopenharmony_ci	.test_cases = bits_test_cases,
7262306a36Sopenharmony_ci};
7362306a36Sopenharmony_cikunit_test_suite(bits_test_suite);
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ciMODULE_LICENSE("GPL");
76