Skip to content

Commit 0405eef

Browse files
lucasdemarchiYuryNorov
authored andcommitted
test_bits: add tests for GENMASK_U*()
Add some additional tests in lib/tests/test_bits.c to cover the expected/non-expected values of the fixed-type GENMASK_U*() macros. Also check that the result value matches the expected type. Since those are known at build time, use static_assert() instead of normal kunit tests. Signed-off-by: Lucas De Marchi <lucas.demarchi@intel.com> Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Signed-off-by: Yury Norov <yury.norov@gmail.com>
1 parent 4fd225f commit 0405eef

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lib/tests/test_bits.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@
55

66
#include <kunit/test.h>
77
#include <linux/bits.h>
8+
#include <linux/types.h>
9+
10+
#define assert_type(t, x) _Generic(x, t: x, default: 0)
11+
12+
static_assert(assert_type(unsigned long, GENMASK(31, 0)) == U32_MAX);
13+
static_assert(assert_type(unsigned long long, GENMASK_ULL(63, 0)) == U64_MAX);
14+
static_assert(assert_type(u8, GENMASK_U8(7, 0)) == U8_MAX);
15+
static_assert(assert_type(u16, GENMASK_U16(15, 0)) == U16_MAX);
16+
static_assert(assert_type(u32, GENMASK_U32(31, 0)) == U32_MAX);
17+
static_assert(assert_type(u64, GENMASK_U64(63, 0)) == U64_MAX);
818

919

1020
static void genmask_test(struct kunit *test)
@@ -14,11 +24,21 @@ static void genmask_test(struct kunit *test)
1424
KUNIT_EXPECT_EQ(test, 6ul, GENMASK(2, 1));
1525
KUNIT_EXPECT_EQ(test, 0xFFFFFFFFul, GENMASK(31, 0));
1626

27+
KUNIT_EXPECT_EQ(test, 1u, GENMASK_U8(0, 0));
28+
KUNIT_EXPECT_EQ(test, 3u, GENMASK_U16(1, 0));
29+
KUNIT_EXPECT_EQ(test, 0x10000, GENMASK_U32(16, 16));
30+
1731
#ifdef TEST_GENMASK_FAILURES
1832
/* these should fail compilation */
1933
GENMASK(0, 1);
2034
GENMASK(0, 10);
2135
GENMASK(9, 10);
36+
37+
GENMASK_U32(0, 31);
38+
GENMASK_U64(64, 0);
39+
GENMASK_U32(32, 0);
40+
GENMASK_U16(16, 0);
41+
GENMASK_U8(8, 0);
2242
#endif
2343

2444

0 commit comments

Comments
 (0)