From 78a1a18683ea1d5101824fca9a31d9d72335de61 Mon Sep 17 00:00:00 2001 From: Joshua DeWeese Date: Wed, 7 Feb 2024 09:50:13 -0500 Subject: [PATCH 1/5] macros/utils: add LIMIT() macro This patch adds a macro that can be used to limit a value to a given range. --- core/lib/include/macros/utils.h | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/core/lib/include/macros/utils.h b/core/lib/include/macros/utils.h index 2b6b6f728209..1e25245ff522 100644 --- a/core/lib/include/macros/utils.h +++ b/core/lib/include/macros/utils.h @@ -63,6 +63,22 @@ extern "C" { #define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif +/** + * @brief Limit a value to an inclusive range + * + * If @p val is > @p high, @p high is returned. If @p val is < @p low, @p low + * is returned. Otherwise, @p val is returned. + * + * @note This macro evaluate its arguments more than once. + * + * @param[in] val value to limit + * @param[in] low minimum limit + * @param[in] high maximum limit + * + * @return range limited value + */ +#define LIMIT(val, low, high) ((val < low) ? low : (val > high) ? high : val) + #ifdef __cplusplus } #endif From 45c1441e4d8b6537d980087204d516c12604011d Mon Sep 17 00:00:00 2001 From: Joshua DeWeese Date: Wed, 7 Feb 2024 11:18:08 -0500 Subject: [PATCH 2/5] macros/utils: add ABS() macro This patch adds the classic ABS() macro to exist along side of the MAX/MIN macros. --- core/lib/include/macros/utils.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/lib/include/macros/utils.h b/core/lib/include/macros/utils.h index 1e25245ff522..d3ec5027ac88 100644 --- a/core/lib/include/macros/utils.h +++ b/core/lib/include/macros/utils.h @@ -63,6 +63,16 @@ extern "C" { #define MIN(a, b) ((a) < (b) ? (a) : (b)) #endif +#ifndef ABS +/** + * @brief Returns the absolute value of @p x + * + * @note This is the trivial implementation that does evaluate the arguments + * more than once + */ +#define ABS(x) ((x) > 0 ? (x) : -(x)) +#endif + /** * @brief Limit a value to an inclusive range * From 8e3bbca8b55a47cdde5fa72fa209cacd317ea6de Mon Sep 17 00:00:00 2001 From: Joshua DeWeese Date: Thu, 8 Feb 2024 10:51:39 -0500 Subject: [PATCH 3/5] tests/unittests: add tests for MAX()/MIN() macros --- .../unittests/tests-core/tests-core-macros.c | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tests/unittests/tests-core/tests-core-macros.c b/tests/unittests/tests-core/tests-core-macros.c index 76b5b4554f98..9cbcd6708551 100644 --- a/tests/unittests/tests-core/tests-core-macros.c +++ b/tests/unittests/tests-core/tests-core-macros.c @@ -12,6 +12,25 @@ #include "embUnit.h" #include "tests-core.h" #include "macros/math.h" +#include "macros/utils.h" + +static void test_max(void) +{ + TEST_ASSERT_EQUAL_INT(10, MAX(5, 10)); + TEST_ASSERT_EQUAL_INT(10, MAX(10, 5)); + + // prove it works with non-integer types + TEST_ASSERT(22.1 == MAX(22.1, 5.5)); +} + +static void test_min(void) +{ + TEST_ASSERT_EQUAL_INT(5, MIN(5, 10)); + TEST_ASSERT_EQUAL_INT(5, MIN(10, 5)); + + // prove it works with non-integer types + TEST_ASSERT(5.5 == MIN(22.1, 5.5)); +} static void test_math_signof(void) { @@ -81,6 +100,8 @@ static void test_math_div_round_inf(void) Test *tests_core_macros_tests(void) { EMB_UNIT_TESTFIXTURES(fixtures) { + new_TestFixture(test_max), + new_TestFixture(test_min), new_TestFixture(test_math_signof), new_TestFixture(test_math_div_round), new_TestFixture(test_math_div_round_up), From b1cb2435d812690f3d9775600fe0013b01212aea Mon Sep 17 00:00:00 2001 From: Joshua DeWeese Date: Thu, 8 Feb 2024 10:53:50 -0500 Subject: [PATCH 4/5] tests/unittests: add unit test for LIMIT() macro --- tests/unittests/tests-core/tests-core-macros.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/unittests/tests-core/tests-core-macros.c b/tests/unittests/tests-core/tests-core-macros.c index 9cbcd6708551..92b4c68fdce2 100644 --- a/tests/unittests/tests-core/tests-core-macros.c +++ b/tests/unittests/tests-core/tests-core-macros.c @@ -32,6 +32,18 @@ static void test_min(void) TEST_ASSERT(5.5 == MIN(22.1, 5.5)); } +static void test_limit(void) +{ + TEST_ASSERT_EQUAL_INT(5, LIMIT(5, -10, 10)); + TEST_ASSERT_EQUAL_INT(10, LIMIT(10, -10, 10)); + TEST_ASSERT_EQUAL_INT(10, LIMIT(22, -10, 10)); + TEST_ASSERT_EQUAL_INT(-10, LIMIT(-10, -10, 10)); + TEST_ASSERT_EQUAL_INT(-10, LIMIT(-22, -10, 10)); + + // prove it works with non-integer types + TEST_ASSERT(10.2 == LIMIT(22.2, -10.1, 10.2)); +} + static void test_math_signof(void) { TEST_ASSERT_EQUAL_INT(1, SIGNOF(3)); @@ -102,6 +114,7 @@ Test *tests_core_macros_tests(void) EMB_UNIT_TESTFIXTURES(fixtures) { new_TestFixture(test_max), new_TestFixture(test_min), + new_TestFixture(test_limit), new_TestFixture(test_math_signof), new_TestFixture(test_math_div_round), new_TestFixture(test_math_div_round_up), From fa7a57772834f695f0c6445f78354a40096eeea7 Mon Sep 17 00:00:00 2001 From: Joshua DeWeese Date: Thu, 8 Feb 2024 10:54:25 -0500 Subject: [PATCH 5/5] tests/unittests: add test for ABS() macro --- tests/unittests/tests-core/tests-core-macros.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/unittests/tests-core/tests-core-macros.c b/tests/unittests/tests-core/tests-core-macros.c index 92b4c68fdce2..bd77d0e87c15 100644 --- a/tests/unittests/tests-core/tests-core-macros.c +++ b/tests/unittests/tests-core/tests-core-macros.c @@ -32,6 +32,15 @@ static void test_min(void) TEST_ASSERT(5.5 == MIN(22.1, 5.5)); } +static void test_abs(void) +{ + TEST_ASSERT_EQUAL_INT(22, ABS(22)); + TEST_ASSERT_EQUAL_INT(22, ABS(-22)); + + // prove it works with non-integer types + TEST_ASSERT(300.7 == ABS(-300.7)); +} + static void test_limit(void) { TEST_ASSERT_EQUAL_INT(5, LIMIT(5, -10, 10)); @@ -114,6 +123,7 @@ Test *tests_core_macros_tests(void) EMB_UNIT_TESTFIXTURES(fixtures) { new_TestFixture(test_max), new_TestFixture(test_min), + new_TestFixture(test_abs), new_TestFixture(test_limit), new_TestFixture(test_math_signof), new_TestFixture(test_math_div_round),