summaryrefslogtreecommitdiff
path: root/tests/lib
diff options
context:
space:
mode:
authorYu-Ping Wu <yupingso@chromium.org>2021-12-02 10:54:32 +0800
committerFelix Held <felix-coreboot@felixheld.de>2021-12-08 00:51:43 +0000
commit00c834dc26348487f40e835630ab11cf30fe30f6 (patch)
tree7140ac20ca8d8e490f35f31b72636d33d921ed5b /tests/lib
parent04cf42775cca3f300dc3e8240bc70e706f091ca0 (diff)
lib: Fix log2_ceil() for 0xffffffff
Current log2_ceil(x) is defined as log2(x * 2 - 1). When x is larger than (1 << 31), (x * 2 - 1) won't fit in u32, leading to incorrect result. Therefore, correct it as (log2(x - 1) + 1). Also add unit tests for inline functions in lib.h. BUG=none TEST=make tests/lib/lib-test BRANCH=none Change-Id: If868f793b909a6ad7fc48a7affac15e2c714fa2e Signed-off-by: Yu-Ping Wu <yupingso@chromium.org> Reviewed-on: https://review.coreboot.org/c/coreboot/+/59834 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Julius Werner <jwerner@chromium.org> Reviewed-by: Paul Menzel <paulepanter@mailbox.org>
Diffstat (limited to 'tests/lib')
-rw-r--r--tests/lib/Makefile.inc3
-rw-r--r--tests/lib/lib-test.c60
2 files changed, 63 insertions, 0 deletions
diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc
index 0aea0ce62a..9a624767ca 100644
--- a/tests/lib/Makefile.inc
+++ b/tests/lib/Makefile.inc
@@ -1,5 +1,6 @@
# SPDX-License-Identifier: GPL-2.0-only
+tests-y += lib-test
tests-y += string-test
tests-y += b64_decode-test
tests-y += hexstrtobin-test
@@ -40,6 +41,8 @@ tests-y += cbfs-lookup-no-mcache-test
tests-y += cbfs-lookup-has-mcache-test
tests-y += lzma-test
+lib-test-srcs += tests/lib/lib-test.c
+
string-test-srcs += tests/lib/string-test.c
string-test-srcs += src/lib/string.c
diff --git a/tests/lib/lib-test.c b/tests/lib/lib-test.c
new file mode 100644
index 0000000000..adaa2f9500
--- /dev/null
+++ b/tests/lib/lib-test.c
@@ -0,0 +1,60 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+#include <lib.h>
+#include <tests/test.h>
+
+void test_popcnt(void **state)
+{
+ assert_int_equal(popcnt(0x0), 0);
+ assert_int_equal(popcnt(0x10), 1);
+ assert_int_equal(popcnt(0x10010010), 3);
+ assert_int_equal(popcnt(0xffffffff), 32);
+}
+
+void test_clz(void **state)
+{
+ assert_int_equal(clz(0x0), 32);
+ assert_int_equal(clz(0xf), 28);
+ assert_int_equal(clz(0x80000000), 0);
+ assert_int_equal(clz(0xffffffff), 0);
+}
+
+void test_log2(void **state)
+{
+ assert_int_equal(log2(0x0), -1);
+ assert_int_equal(log2(0x1), 0);
+ assert_int_equal(log2(0x5), 2);
+ assert_int_equal(log2(0x80000000), 31);
+ assert_int_equal(log2(0xffffffff), 31);
+}
+
+void test_ffs(void **state)
+{
+ assert_int_equal(__ffs(0x0), -1);
+ assert_int_equal(__ffs(0x1), 0);
+ assert_int_equal(__ffs(0x1010), 4);
+ assert_int_equal(__ffs(0x10000000), 28);
+ assert_int_equal(__ffs(0xffffffff), 0);
+}
+
+void test_log2_ceil(void **state)
+{
+ assert_int_equal(log2_ceil(0x0), -1);
+ assert_int_equal(log2_ceil(0x1), 0);
+ assert_int_equal(log2_ceil(0x5), 3);
+ assert_int_equal(log2_ceil(0x80000000), 31);
+ assert_int_equal(log2_ceil(0xffffffff), 32);
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_popcnt),
+ cmocka_unit_test(test_clz),
+ cmocka_unit_test(test_log2),
+ cmocka_unit_test(test_ffs),
+ cmocka_unit_test(test_log2_ceil),
+ };
+
+ return cb_run_group_tests(tests, NULL, NULL);
+}