Skip to content

Commit 2fb8cbc

Browse files
committed
libc/tests: add stdbit test framework and unit tests
This adds unit tests for all 70 functions in <stdbit.h>. I'm sorry for the test framework, but it makes it so I don't have to write 70 unit tests by hand. Reviewed by: adrian, des Approved by: markj (mentor) MFC after: 1 month Differential Revision: https://reviews.freebsd.org/D53660
1 parent d790b16 commit 2fb8cbc

19 files changed

+513
-0
lines changed

etc/mtree/BSD.tests.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,8 @@
382382
..
383383
ssp
384384
..
385+
stdbit
386+
..
385387
stdio
386388
..
387389
stdlib

lib/libc/tests/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ TESTS_SUBDIRS+= resolv
1414
TESTS_SUBDIRS+= rpc
1515
TESTS_SUBDIRS+= secure
1616
TESTS_SUBDIRS+= setjmp
17+
TESTS_SUBDIRS+= stdbit
1718
TESTS_SUBDIRS+= stdio
1819
TESTS_SUBDIRS+= stdlib
1920
TESTS_SUBDIRS+= stdtime

lib/libc/tests/stdbit/Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ensure libc functions are tested, not clang's builtins
2+
CFLAGS+= -fno-builtin
3+
4+
ATF_TESTS_C+= stdc_bit_ceil_test \
5+
stdc_bit_floor_test \
6+
stdc_bit_width_test \
7+
stdc_count_ones_test \
8+
stdc_count_zeros_test \
9+
stdc_first_leading_one_test \
10+
stdc_first_leading_zero_test \
11+
stdc_first_trailing_one_test \
12+
stdc_first_trailing_zero_test \
13+
stdc_has_single_bit_test \
14+
stdc_leading_ones_test \
15+
stdc_leading_zeros_test \
16+
stdc_trailing_ones_test \
17+
stdc_trailing_zeros_test
18+
19+
.include <bsd.test.mk>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright (c) 2025 Robert Clausecker <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
/*
8+
* Test framework for stdbit functions.
9+
* Requires the following macros to be defined:
10+
*
11+
* FUNCSTEM -- name of the function without type suffix
12+
* MKREFFUNC(name, type) -- macro to generate a reference
13+
* implementation of the function as a static function
14+
* named name with give argument type.
15+
*/
16+
17+
#include <sys/cdefs.h>
18+
#include <atf-c.h>
19+
#include <limits.h>
20+
#include <stdbit.h>
21+
#include <stdint.h>
22+
23+
#define ATF_TC_WITHOUT_HEAD1(stem, suffix) ATF_TC_WITHOUT_HEAD2(__CONCAT(stem, suffix))
24+
#define ATF_TC_WITHOUT_HEAD2(case) ATF_TC_WITHOUT_HEAD(case)
25+
#define ATF_TC_BODY1(stem, suffix, tc) ATF_TC_BODY2(__CONCAT(stem, suffix), tc)
26+
#define ATF_TC_BODY2(case, tc) ATF_TC_BODY(case, tc)
27+
28+
#define SUFFIX _uc
29+
#define TYPE unsigned char
30+
#define TYPE_WIDTH UCHAR_WIDTH
31+
#include "stdbit-test-kernel.c"
32+
#undef TYPE_WIDTH
33+
#undef TYPE
34+
#undef SUFFIX
35+
36+
#define SUFFIX _us
37+
#define TYPE unsigned short
38+
#define TYPE_WIDTH USHRT_WIDTH
39+
#include "stdbit-test-kernel.c"
40+
#undef TYPE_WIDTH
41+
#undef TYPE
42+
#undef SUFFIX
43+
44+
#define SUFFIX _ui
45+
#define TYPE unsigned int
46+
#define TYPE_WIDTH UINT_WIDTH
47+
#include "stdbit-test-kernel.c"
48+
#undef TYPE_WIDTH
49+
#undef TYPE
50+
#undef SUFFIX
51+
52+
#define SUFFIX _ul
53+
#define TYPE unsigned long
54+
#define TYPE_WIDTH ULONG_WIDTH
55+
#include "stdbit-test-kernel.c"
56+
#undef TYPE_WIDTH
57+
#undef TYPE
58+
#undef SUFFIX
59+
60+
#define SUFFIX _ull
61+
#define TYPE unsigned long long
62+
#define TYPE_WIDTH ULLONG_WIDTH
63+
#include "stdbit-test-kernel.c"
64+
#undef TYPE_WIDTH
65+
#undef TYPE
66+
#undef SUFFIX
67+
68+
#define ADD_CASE(stem, suffix) ADD_CASE1(__CONCAT(stem, suffix))
69+
#define ADD_CASE1(case) ATF_TP_ADD_TC(tp, case)
70+
71+
ATF_TP_ADD_TCS(tp)
72+
{
73+
ADD_CASE(FUNCSTEM, _uc);
74+
ADD_CASE(FUNCSTEM, _us);
75+
ADD_CASE(FUNCSTEM, _ui);
76+
ADD_CASE(FUNCSTEM, _ul);
77+
ADD_CASE(FUNCSTEM, _ull);
78+
79+
return (atf_no_error());
80+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright (c) 2025 Robert Clausecker <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
/*
8+
* test kernel for stdbit functions.
9+
* Requires the following macros to be defined:
10+
*
11+
* FUNCSTEM -- stem of the function name
12+
* SUFFIX -- type suffic
13+
* TYPE -- argument type
14+
* MKREFFUNC(ref, type) -- reference function builder
15+
*/
16+
17+
#define FUNC __CONCAT(FUNCSTEM, SUFFIX)
18+
#define REF __CONCAT(FUNCSTEM, __CONCAT(SUFFIX, _ref))
19+
20+
MKREFFUNC(REF, TYPE)
21+
22+
ATF_TC_WITHOUT_HEAD1(FUNCSTEM, SUFFIX);
23+
ATF_TC_BODY1(FUNCSTEM, SUFFIX, tc)
24+
{
25+
uintmax_t has, want;
26+
size_t i, j;
27+
TYPE value;
28+
29+
/* test all single-bit patterns */
30+
for (i = 0; i < TYPE_WIDTH; i++) {
31+
value = (TYPE)1 << i;
32+
has = FUNC(value);
33+
want = REF(value);
34+
ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",
35+
__XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);
36+
}
37+
38+
/* test all double-bit patterns */
39+
for (i = 0; i < TYPE_WIDTH; i++) {
40+
for (j = 0; j < i; j++) {
41+
value = (TYPE)1 << i | (TYPE)1 << j;
42+
has = FUNC(value);
43+
want = REF(value);
44+
ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",
45+
__XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);
46+
}
47+
}
48+
49+
/* test all barber-pole patterns */
50+
value = ~(TYPE)0;
51+
for (i = 0; i < TYPE_WIDTH; i++) {
52+
has = FUNC(value);
53+
want = REF(value);
54+
ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",
55+
__XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);
56+
57+
value = ~value;
58+
has = FUNC(value);
59+
want = REF(value);
60+
ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",
61+
__XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);
62+
63+
value = ~value << 1;
64+
}
65+
}
66+
67+
#undef REF
68+
#undef FUNC
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2025 Robert Clausecker <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#define FUNCSTEM stdc_bit_ceil
8+
#define MKREFFUNC(name, type) \
9+
static type \
10+
name(type value) \
11+
{ \
12+
type ceil = 1; \
13+
\
14+
while (ceil < value && ceil != 0) \
15+
ceil <<= 1; \
16+
\
17+
return (ceil); \
18+
}
19+
20+
#include "stdbit-test-framework.c"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (c) 2025 Robert Clausecker <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#define FUNCSTEM stdc_bit_floor
8+
#define MKREFFUNC(name, type) \
9+
static type \
10+
name(type value) \
11+
{ \
12+
type floor = 1; \
13+
\
14+
if (value == 0) \
15+
return (0); \
16+
\
17+
while (value != 1) { \
18+
floor <<= 1; \
19+
value >>= 1; \
20+
} \
21+
\
22+
return (floor); \
23+
}
24+
25+
#include "stdbit-test-framework.c"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2025 Robert Clausecker <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#define FUNCSTEM stdc_bit_width
8+
#define MKREFFUNC(name, type) \
9+
static unsigned \
10+
name(type value) \
11+
{ \
12+
unsigned width = 0; \
13+
\
14+
while (value != 0) { \
15+
value >>= 1; \
16+
width++; \
17+
} \
18+
\
19+
return (width); \
20+
}
21+
22+
#include "stdbit-test-framework.c"
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2025 Robert Clausecker <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#define FUNCSTEM stdc_count_ones
8+
#define MKREFFUNC(name, type) \
9+
static unsigned \
10+
name(type value) \
11+
{ \
12+
unsigned count = 0; \
13+
\
14+
while (value != 0) { \
15+
count += value & 1; \
16+
value >>= 1; \
17+
} \
18+
\
19+
return (count); \
20+
}
21+
22+
#include "stdbit-test-framework.c"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2025 Robert Clausecker <[email protected]>
3+
*
4+
* SPDX-License-Identifier: BSD-2-Clause
5+
*/
6+
7+
#define FUNCSTEM stdc_count_zeros
8+
#define MKREFFUNC(name, type) \
9+
static unsigned \
10+
name(type value) \
11+
{ \
12+
unsigned count = 0; \
13+
\
14+
value = ~value; \
15+
while (value != 0) { \
16+
count += value & 1; \
17+
value >>= 1; \
18+
} \
19+
\
20+
return (count); \
21+
}
22+
23+
#include "stdbit-test-framework.c"

0 commit comments

Comments
 (0)