Re: [PATCH next 13/14] test_bits: Change all the tests to be compile-time tests

From: David Laight

Date: Sun Feb 08 2026 - 06:32:42 EST


On Sat, 7 Feb 2026 23:37:49 -0500
Yury Norov <ynorov@xxxxxxxxxx> wrote:

> On Wed, Jan 21, 2026 at 02:57:30PM +0000, david.laight.linux@xxxxxxxxx wrote:
> > From: David Laight <david.laight.linux@xxxxxxxxx>
> >
> > Since all the GENMASK() values are compile-time constants they can
> > be tested with BUILD_BUG_ON() rather than KUNIT_EXPECT_EQ().
> >
> > Signed-off-by: David Laight <david.laight.linux@xxxxxxxxx>
>
> I thought, KUNIT invented this EXPECT_EQ macro for a nice printing
> and some accounting. If you want to make sure that __GENMASK() is a
> compile-time expression, it's OK. But I'd rather keep the existing
> KUNIT_EXPECT_EQ() and add
>
> BUILD_BUG_ON(!__builtin_constant_p(GENMASK()))
>
> next to that. This is actually how test_bitmap_const_eval() works.

That just tests that the value is a compile time constant.
You want an error if the compile time constant is the wrong value.

The point is there is no reason to do a run-time check if the compile
test fails.
Making them compile-time means they get tested by the compiler,
so you don't need to run the tests at all.

The original tests in one of these files were all _Static_assert()
at file scope, but that requires 'integer constant expressions'.
They need moving into a function to use BUILD_BUG_ON().

Perhaps the test framework(s) should have a standard scheme for
compile-time tests.

David

>
> Thanks,
> Yury
>
> > ---
> > lib/tests/test_bits.c | 90 +++++++++++++++++++++----------------------
> > 1 file changed, 45 insertions(+), 45 deletions(-)
> >
> > diff --git a/lib/tests/test_bits.c b/lib/tests/test_bits.c
> > index 36eb4661e78b..4d3a895f490c 100644
> > --- a/lib/tests/test_bits.c
> > +++ b/lib/tests/test_bits.c
> > @@ -32,30 +32,30 @@ static_assert(assert_type(u64, GENMASK_U64(63, 0)) == U64_MAX);
> >
> > static void __genmask_test(struct kunit *test)
> > {
> > - KUNIT_EXPECT_EQ(test, 1ul, __GENMASK(0, 0));
> > - KUNIT_EXPECT_EQ(test, 3ul, __GENMASK(1, 0));
> > - KUNIT_EXPECT_EQ(test, 6ul, __GENMASK(2, 1));
> > - KUNIT_EXPECT_EQ(test, 0xFFFFFFFFul, __GENMASK(31, 0));
> > + BUILD_BUG_ON(__GENMASK(0, 0) != 1ul);
> > + BUILD_BUG_ON(__GENMASK(1, 0) != 3ul);
> > + BUILD_BUG_ON(__GENMASK(2, 1) != 6ul);
> > + BUILD_BUG_ON(__GENMASK(31, 0) != 0xFFFFFFFFul);
> > }
...