aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/tests/stdbit/stdbit-test-kernel.c
diff options
context:
space:
mode:
authorRobert Clausecker <fuz@FreeBSD.org>2025-11-18 17:33:12 +0000
committerRobert Clausecker <fuz@FreeBSD.org>2025-11-30 19:21:52 +0000
commit2fb8cbc6ef1b3cc6cd60e5db07f8305623f9b044 (patch)
tree1fcb68705ab2799af1c6150e6d64942423fdf8d2 /lib/libc/tests/stdbit/stdbit-test-kernel.c
parentd790b16bbf0c3055ef00200d01659dfb3c0e5e5e (diff)
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
Diffstat (limited to 'lib/libc/tests/stdbit/stdbit-test-kernel.c')
-rw-r--r--lib/libc/tests/stdbit/stdbit-test-kernel.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/lib/libc/tests/stdbit/stdbit-test-kernel.c b/lib/libc/tests/stdbit/stdbit-test-kernel.c
new file mode 100644
index 000000000000..d584e391ff6f
--- /dev/null
+++ b/lib/libc/tests/stdbit/stdbit-test-kernel.c
@@ -0,0 +1,68 @@
+/*
+ * Copyright (c) 2025 Robert Clausecker <fuz@FreeBSD.org>
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+/*
+ * test kernel for stdbit functions.
+ * Requires the following macros to be defined:
+ *
+ * FUNCSTEM -- stem of the function name
+ * SUFFIX -- type suffic
+ * TYPE -- argument type
+ * MKREFFUNC(ref, type) -- reference function builder
+ */
+
+#define FUNC __CONCAT(FUNCSTEM, SUFFIX)
+#define REF __CONCAT(FUNCSTEM, __CONCAT(SUFFIX, _ref))
+
+MKREFFUNC(REF, TYPE)
+
+ATF_TC_WITHOUT_HEAD1(FUNCSTEM, SUFFIX);
+ATF_TC_BODY1(FUNCSTEM, SUFFIX, tc)
+{
+ uintmax_t has, want;
+ size_t i, j;
+ TYPE value;
+
+ /* test all single-bit patterns */
+ for (i = 0; i < TYPE_WIDTH; i++) {
+ value = (TYPE)1 << i;
+ has = FUNC(value);
+ want = REF(value);
+ ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",
+ __XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);
+ }
+
+ /* test all double-bit patterns */
+ for (i = 0; i < TYPE_WIDTH; i++) {
+ for (j = 0; j < i; j++) {
+ value = (TYPE)1 << i | (TYPE)1 << j;
+ has = FUNC(value);
+ want = REF(value);
+ ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",
+ __XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);
+ }
+ }
+
+ /* test all barber-pole patterns */
+ value = ~(TYPE)0;
+ for (i = 0; i < TYPE_WIDTH; i++) {
+ has = FUNC(value);
+ want = REF(value);
+ ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",
+ __XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);
+
+ value = ~value;
+ has = FUNC(value);
+ want = REF(value);
+ ATF_CHECK_EQ_MSG(has, want, "%s(%#jx) == %#jx != %#jx == %s(%#jx)",
+ __XSTRING(FUNC), (uintmax_t)value, has, want, __XSTRING(REF), (uintmax_t)value);
+
+ value = ~value << 1;
+ }
+}
+
+#undef REF
+#undef FUNC