From 50b9050d6b97e506de6731fa87dbb1fbe9d23c62 Mon Sep 17 00:00:00 2001 From: Markus Hofbauer Date: Mon, 15 Nov 2021 12:23:28 +0100 Subject: [PATCH] Feat: Overloads for floor and ceil accepting quantities (#314) * feat: overloads for floor and ceil accepting quantities * require same type for dim and rep * be specific for msvc --- src/core/include/units/math.h | 34 ++++++++++++++++++++++++++++ test/unit_test/runtime/math_test.cpp | 8 +++++++ test/unit_test/static/math_test.cpp | 6 +++++ 3 files changed, 48 insertions(+) diff --git a/src/core/include/units/math.h b/src/core/include/units/math.h index b54e9a3f..61160e1d 100644 --- a/src/core/include/units/math.h +++ b/src/core/include/units/math.h @@ -187,6 +187,23 @@ template } } +/** + * @brief Overload of @c ::units::floor() using the unit type of To + * + * @tparam q Quantity being the base of the operation + * @return Quantity The rounded quantity with unit type of quantity To + */ +template +[[nodiscard]] constexpr quantity floor(const quantity& q) noexcept + requires std::same_as && + std::same_as && + requires { + ::units::floor(q); + } +{ + return ::units::floor(q); +} + /** * @brief Computes the smallest quantity with integer representation and unit type To with its number not less than q * @@ -227,4 +244,21 @@ template } } +/** + * @brief Overload of @c ::units::ceil() using the unit type of To + * + * @tparam q Quantity being the base of the operation + * @return Quantity The rounded quantity with unit type of quantity To + */ +template +[[nodiscard]] constexpr quantity ceil(const quantity& q) noexcept + requires std::same_as && + std::same_as && + requires { + ::units::ceil(q); + } +{ + return ::units::ceil(q); +} + } // namespace units diff --git a/test/unit_test/runtime/math_test.cpp b/test/unit_test/runtime/math_test.cpp index 56bc1d60..a3281026 100644 --- a/test/unit_test/runtime/math_test.cpp +++ b/test/unit_test/runtime/math_test.cpp @@ -148,6 +148,10 @@ TEST_CASE("floor functions", "[floor]") SECTION ("floor -999. milliseconds with target unit second should be -1 second") { REQUIRE(floor(-999._q_ms) == -1_q_s); } + SECTION ("floor 1 second with target quantity with unit type second should be 1 second") { + using showtime = si::time; + REQUIRE(floor(showtime::one()) == showtime::one()); + } } TEST_CASE("ceil functions", "[ceil]") @@ -189,6 +193,10 @@ TEST_CASE("ceil functions", "[ceil]") SECTION ("ceil -999. milliseconds with target unit second should be 0 seconds") { REQUIRE(ceil(-999._q_ms) == 0_q_s); } + SECTION ("ceil 1 second with target quantity with unit type second should be 1 second") { + using showtime = si::time; + REQUIRE(ceil(showtime::one()) == showtime::one()); + } } TEMPLATE_TEST_CASE_SIG("pow() implementation exponentiates values to power N", "[math][pow][exp]", diff --git a/test/unit_test/static/math_test.cpp b/test/unit_test/static/math_test.cpp index e6ae7841..627ba60c 100644 --- a/test/unit_test/static/math_test.cpp +++ b/test/unit_test/static/math_test.cpp @@ -78,6 +78,9 @@ static_assert(floor(1999._q_ms) == 1_q_s); static_assert(floor(-1000._q_ms) == -1_q_s); static_assert(floor(-999._q_ms) == -1_q_s); +// floor with quantity +static_assert(compare>(1_q_s)), decltype(1_q_s)>); + // ceil // integral types static_assert(compare(1_q_s)), decltype(1_q_s)>); @@ -97,6 +100,9 @@ static_assert(ceil(1001._q_ms) == 2_q_s); static_assert(ceil(1999._q_ms) == 2_q_s); static_assert(ceil(-1000._q_ms) == -1_q_s); static_assert(ceil(-999._q_ms) == 0_q_s); + +// ceil with quantity +static_assert(compare>(1_q_s)), decltype(1_q_s)>); #endif } // namespace