From 1eb02f7fda0ccc5a132350ccb131bb6130d1b63c Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 4 Nov 2022 22:50:16 +0100 Subject: [PATCH 1/2] core/macros: fix SIGNOF() macro with size_t --- core/include/macros/math.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/include/macros/math.h b/core/include/macros/math.h index 9e4e84a1ba..2ef269935b 100644 --- a/core/include/macros/math.h +++ b/core/include/macros/math.h @@ -31,7 +31,7 @@ extern "C" { unsigned int: 1, \ unsigned long: 1, \ unsigned long long: 1, \ - default: (long long)(a) < 0 ? -1 : 1) + default: ((a) > 0LL) ? 1 : -1) /** * @brief Calculates @p a/ @p b with arithmetic rounding */ From 8f8bb6ce84103e6271b46c4a0a95b0277b879077 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Fri, 4 Nov 2022 22:50:53 +0100 Subject: [PATCH 2/2] tests/unittests: core: add test for SIGNOF(size_t) --- core/include/macros/math.h | 2 +- tests/unittests/tests-core/tests-core-macros.c | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/core/include/macros/math.h b/core/include/macros/math.h index 2ef269935b..92b987d58c 100644 --- a/core/include/macros/math.h +++ b/core/include/macros/math.h @@ -31,7 +31,7 @@ extern "C" { unsigned int: 1, \ unsigned long: 1, \ unsigned long long: 1, \ - default: ((a) > 0LL) ? 1 : -1) + default: ((a) <= 0 ? ((a) == 0 ? 1L : -1L ): 1L)) /** * @brief Calculates @p a/ @p b with arithmetic rounding */ diff --git a/tests/unittests/tests-core/tests-core-macros.c b/tests/unittests/tests-core/tests-core-macros.c index bbcb8b5090..15644d7182 100644 --- a/tests/unittests/tests-core/tests-core-macros.c +++ b/tests/unittests/tests-core/tests-core-macros.c @@ -22,7 +22,9 @@ static void test_math_signof(void) static void test_math_div_round(void) { - TEST_ASSERT_EQUAL_INT(3, DIV_ROUND(20, 7)); + size_t a = 20; + + TEST_ASSERT_EQUAL_INT(3, DIV_ROUND(a, 7)); TEST_ASSERT_EQUAL_INT(3, DIV_ROUND(21, 7)); TEST_ASSERT_EQUAL_INT(3, DIV_ROUND(22, 7)); TEST_ASSERT_EQUAL_INT(0, DIV_ROUND(1, UINT32_MAX));