[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20250322-fixed-type-genmasks-v7-4-da380ff1c5b9@wanadoo.fr>
Date: Sat, 22 Mar 2025 18:23:15 +0900
From: Vincent Mailhol via B4 Relay <devnull+mailhol.vincent.wanadoo.fr@...nel.org>
To: Yury Norov <yury.norov@...il.com>,
Lucas De Marchi <lucas.demarchi@...el.com>,
Rasmus Villemoes <linux@...musvillemoes.dk>,
Jani Nikula <jani.nikula@...ux.intel.com>,
Joonas Lahtinen <joonas.lahtinen@...ux.intel.com>,
Rodrigo Vivi <rodrigo.vivi@...el.com>,
Tvrtko Ursulin <tursulin@...ulin.net>, David Airlie <airlied@...il.com>,
Simona Vetter <simona@...ll.ch>, Andrew Morton <akpm@...ux-foundation.org>
Cc: linux-kernel@...r.kernel.org, intel-gfx@...ts.freedesktop.org,
dri-devel@...ts.freedesktop.org, Andi Shyti <andi.shyti@...ux.intel.com>,
David Laight <David.Laight@...LAB.COM>,
Andy Shevchenko <andriy.shevchenko@...ux.intel.com>,
Vincent Mailhol <mailhol.vincent@...adoo.fr>
Subject: [PATCH v7 4/5] test_bits: add tests for GENMASK_U*()
From: Lucas De Marchi <lucas.demarchi@...el.com>
Add some additional tests in lib/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@...el.com>
Signed-off-by: Vincent Mailhol <mailhol.vincent@...adoo.fr>
---
Changelog:
v6 -> v7:
v5 -> v6:
- No changes.
v4 -> v5:
- Revert v4 change. GENMASK_U8()/GENMASK_U16() are now back to
u8/u16.
v3 -> v4:
- Adjust the type of GENMASK_U8()/GENMASK_U16() from u8/u16 to
unsigned int.
- Reorder the tests to match the order in which the macros are
declared in bits.h.
---
lib/test_bits.c | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
diff --git a/lib/test_bits.c b/lib/test_bits.c
index c7b38d91e1f16d42b7ca92e62fbd6c19b37e76a0..f443476f3265c463c1219b13c1ef9663d238d58b 100644
--- a/lib/test_bits.c
+++ b/lib/test_bits.c
@@ -5,6 +5,16 @@
#include <kunit/test.h>
#include <linux/bits.h>
+#include <linux/types.h>
+
+#define assert_type(t, x) _Generic(x, t: x, default: 0)
+
+static_assert(assert_type(unsigned long, GENMASK(31, 0)) == U32_MAX);
+static_assert(assert_type(unsigned long long, GENMASK_ULL(63, 0)) == U64_MAX);
+static_assert(assert_type(u8, GENMASK_U8(7, 0)) == U8_MAX);
+static_assert(assert_type(u16, GENMASK_U16(15, 0)) == U16_MAX);
+static_assert(assert_type(u32, GENMASK_U32(31, 0)) == U32_MAX);
+static_assert(assert_type(u64, GENMASK_U64(63, 0)) == U64_MAX);
static void genmask_test(struct kunit *test)
@@ -14,11 +24,21 @@ static void genmask_test(struct kunit *test)
KUNIT_EXPECT_EQ(test, 6ul, GENMASK(2, 1));
KUNIT_EXPECT_EQ(test, 0xFFFFFFFFul, GENMASK(31, 0));
+ KUNIT_EXPECT_EQ(test, 1u, GENMASK_U8(0, 0));
+ KUNIT_EXPECT_EQ(test, 3u, GENMASK_U16(1, 0));
+ KUNIT_EXPECT_EQ(test, 0x10000, GENMASK_U32(16, 16));
+
#ifdef TEST_GENMASK_FAILURES
/* these should fail compilation */
GENMASK(0, 1);
GENMASK(0, 10);
GENMASK(9, 10);
+
+ GENMASK_U32(0, 31);
+ GENMASK_U64(64, 0);
+ GENMASK_U32(32, 0);
+ GENMASK_U16(16, 0);
+ GENMASK_U8(8, 0);
#endif
--
2.48.1
Powered by blists - more mailing lists