From 909c317b2deb184d95c7d289cbe6603e209ed72d Mon Sep 17 00:00:00 2001 From: Yidi Lin Date: Tue, 31 Oct 2023 14:57:04 +0800 Subject: commonlib: Add GCD function Implement a simple GCD function. BUG=b:307790895 TEST=emerge-geralt coreboot TEST=make tests/commonlib/bsd/gcd-test Change-Id: I21819cda4299b3809b8ca7a95cbdc6a87e4b3481 Signed-off-by: Yidi Lin Reviewed-on: https://review.coreboot.org/c/coreboot/+/78798 Reviewed-by: Martin L Roth Reviewed-by: Julius Werner Reviewed-by: Yu-Ping Wu Tested-by: build bot (Jenkins) --- tests/commonlib/bsd/Makefile.inc | 4 ++++ tests/commonlib/bsd/gcd-test.c | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tests/commonlib/bsd/gcd-test.c (limited to 'tests/commonlib') diff --git a/tests/commonlib/bsd/Makefile.inc b/tests/commonlib/bsd/Makefile.inc index 56664d037c..bf17b6d56d 100644 --- a/tests/commonlib/bsd/Makefile.inc +++ b/tests/commonlib/bsd/Makefile.inc @@ -1,5 +1,9 @@ # SPDX-License-Identifier: GPL-2.0-only tests-y += helpers-test +tests-y += gcd-test helpers-test-srcs += tests/commonlib/bsd/helpers-test.c + +gcd-test-srcs += tests/commonlib/bsd/gcd-test.c +gcd-test-srcs += src/commonlib/bsd/gcd.c diff --git a/tests/commonlib/bsd/gcd-test.c b/tests/commonlib/bsd/gcd-test.c new file mode 100644 index 0000000000..13fad86ec4 --- /dev/null +++ b/tests/commonlib/bsd/gcd-test.c @@ -0,0 +1,27 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#include +#include + +static void test_gcd32(void **state) +{ + assert_int_equal(gcd32(17, 11), 1); + assert_int_equal(gcd32(64, 36), 4); + assert_int_equal(gcd32(90, 123), 3); + assert_int_equal(gcd32(65536, 339584), 128); + assert_int_equal(gcd32(1, 1), 1); + assert_int_equal(gcd32(1, 123), 1); + assert_int_equal(gcd32(123, 1), 1); + assert_int_equal(gcd32(1, UINT32_MAX), 1); + assert_int_equal(gcd32(UINT32_MAX, 1), 1); + assert_int_equal(gcd32(UINT32_MAX, UINT32_MAX), UINT32_MAX); +} + +int main(void) +{ + const struct CMUnitTest tests[] = { + cmocka_unit_test(test_gcd32), + }; + + return cb_run_group_tests(tests, NULL, NULL); +} -- cgit v1.2.3