Feat: Round (#313)

* feat: round

* add more constraints for round

* overload round for quantity

* fix clang tidy

* Validate types before return
This commit is contained in:
Markus Hofbauer authored and GitHub committed 2021-11-15 18:53:15 +01:00
1 parent 0d16427116
commit 4cd90674d5
3 files changed
+173 -15

No files matched your search

+71
View File
@@ -199,6 +199,77 @@ TEST_CASE("ceil functions", "[ceil]")
}
}
TEST_CASE("round functions", "[round]")
{
SECTION ("round 1 second with target unit second should be 1 second") {
REQUIRE(round<si::second>(1_q_s) == 1_q_s);
}
SECTION ("round 1000 milliseconds with target unit second should be 1 second") {
REQUIRE(round<si::second>(1000_q_ms) == 1_q_s);
}
SECTION ("round 1001 milliseconds with target unit second should be 1 second") {
REQUIRE(round<si::second>(1001_q_ms) == 1_q_s);
}
SECTION ("round 1499 milliseconds with target unit second should be 1 second") {
REQUIRE(round<si::second>(1499_q_ms) == 1_q_s);
}
SECTION ("round 1500 milliseconds with target unit second should be 2 seconds") {
REQUIRE(round<si::second>(1500_q_ms) == 2_q_s);
}
SECTION ("round 1999 milliseconds with target unit second should be 2 seconds") {
REQUIRE(round<si::second>(1999_q_ms) == 2_q_s);
}
SECTION ("round -1000 milliseconds with target unit second should be -1 second") {
REQUIRE(round<si::second>(-1000_q_ms) == -1_q_s);
}
SECTION ("round -1001 milliseconds with target unit second should be -1 second") {
REQUIRE(round<si::second>(-1001_q_ms) == -1_q_s);
}
SECTION ("round -1499 milliseconds with target unit second should be -1 second") {
REQUIRE(round<si::second>(-1499_q_ms) == -1_q_s);
}
SECTION ("round -1500 milliseconds with target unit second should be -2 seconds") {
REQUIRE(round<si::second>(-1500_q_ms) == -2_q_s);
}
SECTION ("round -1999 milliseconds with target unit second should be -2 seconds") {
REQUIRE(round<si::second>(-1999_q_ms) == -2_q_s);
}
SECTION ("round 1000. milliseconds with target unit second should be 1 second") {
REQUIRE(round<si::second>(1000._q_ms) == 1_q_s);
}
SECTION ("round 1001. milliseconds with target unit second should be 1 second") {
REQUIRE(round<si::second>(1001._q_ms) == 1_q_s);
}
SECTION ("round 1499. milliseconds with target unit second should be 1 second") {
REQUIRE(round<si::second>(1499._q_ms) == 1_q_s);
}
SECTION ("round 1500. milliseconds with target unit second should be 2 seconds") {
REQUIRE(round<si::second>(1500._q_ms) == 2_q_s);
}
SECTION ("round 1999. milliseconds with target unit second should be 2 seconds") {
REQUIRE(round<si::second>(1999._q_ms) == 2_q_s);
}
SECTION ("round -1000. milliseconds with target unit second should be -1 second") {
REQUIRE(round<si::second>(-1000._q_ms) == -1_q_s);
}
SECTION ("round -1001. milliseconds with target unit second should be -1 second") {
REQUIRE(round<si::second>(-1001._q_ms) == -1_q_s);
}
SECTION ("round -1499. milliseconds with target unit second should be -1 second") {
REQUIRE(round<si::second>(-1499._q_ms) == -1_q_s);
}
SECTION ("round -1500. milliseconds with target unit second should be -2 seconds") {
REQUIRE(round<si::second>(-1500._q_ms) == -2_q_s);
}
SECTION ("round -1999. milliseconds with target unit second should be -2 seconds") {
REQUIRE(round<si::second>(-1999._q_ms) == -2_q_s);
}
SECTION ("round 1 second with target quantity with unit type second should be 1 second") {
using showtime = si::time<si::second, int>;
REQUIRE(round<showtime>(showtime::one()) == showtime::one());
}
}
TEMPLATE_TEST_CASE_SIG("pow<N>() implementation exponentiates values to power N", "[math][pow][exp]",
(std::intmax_t N, N), 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 20, 25)
{
+36 -1
View File
@@ -57,7 +57,7 @@ static_assert(compare<decltype(pow<1, 4>(4_q_m2)), decltype(sqrt(2_q_m))>);
static_assert(compare<decltype(pow<1, 4>(4_q_km2)), decltype(sqrt(2_q_km))>);
static_assert(compare<decltype(pow<1, 4>(4_q_ft2)), decltype(sqrt(2_q_ft))>);
#if __cpp_lib_constexpr_cmath // TODO remove once std::floor is constexpr for all compilers
#if __cpp_lib_constexpr_cmath // TODO remove once std::floor, std::ceil, and std::round is constexpr for all compilers
// floor
// integral types
static_assert(compare<decltype(floor<si::second>(1_q_s)), decltype(1_q_s)>);
@@ -103,6 +103,41 @@ static_assert(ceil<si::second>(-999._q_ms) == 0_q_s);
// ceil with quantity
static_assert(compare<decltype(ceil<si::time<si::second>>(1_q_s)), decltype(1_q_s)>);
// round
// integral types
static_assert(compare<decltype(round<si::second>(1_q_s)), decltype(1_q_s)>);
static_assert(compare<decltype(round<si::second>(1000_q_ms)), decltype(1_q_s)>);
static_assert(compare<decltype(round<si::second>(1001_q_ms)), decltype(1_q_s)>);
static_assert(compare<decltype(round<si::second>(1499_q_ms)), decltype(1_q_s)>);
static_assert(compare<decltype(round<si::second>(1500_q_ms)), decltype(2_q_s)>);
static_assert(compare<decltype(round<si::second>(1999_q_ms)), decltype(2_q_s)>);
static_assert(compare<decltype(round<si::second>(-1000_q_ms)), decltype(-1_q_s)>);
static_assert(compare<decltype(round<si::second>(-1001_q_ms)), decltype(-1_q_s)>);
static_assert(compare<decltype(round<si::second>(-1499_q_ms)), decltype(-1_q_s)>);
static_assert(compare<decltype(round<si::second>(-1500_q_ms)), decltype(-2_q_s)>);
static_assert(compare<decltype(round<si::second>(-1999_q_ms)), decltype(-2_q_s)>);
// floating-point
static_assert(round<si::second>(1.3_q_s) == 1_q_s);
static_assert(round<si::second>(-1.3_q_s) == -1_q_s);
static_assert(compare<decltype(round<si::second>(1000._q_ms)), decltype(1_q_s)>);
static_assert(compare<decltype(round<si::second>(1001._q_ms)), decltype(1_q_s)>);
static_assert(compare<decltype(round<si::second>(1499._q_ms)), decltype(1_q_s)>);
static_assert(compare<decltype(round<si::second>(1500._q_ms)), decltype(2_q_s)>);
static_assert(compare<decltype(round<si::second>(1999._q_ms)), decltype(2_q_s)>);
static_assert(compare<decltype(round<si::second>(-1000._q_ms)), decltype(-1_q_s)>);
static_assert(compare<decltype(round<si::second>(-1001._q_ms)), decltype(-1_q_s)>);
static_assert(compare<decltype(round<si::second>(-1499._q_ms)), decltype(-1_q_s)>);
static_assert(compare<decltype(round<si::second>(-1500._q_ms)), decltype(-2_q_s)>);
static_assert(compare<decltype(round<si::second>(-1999._q_ms)), decltype(-2_q_s)>);
// round with quantity
static_assert(compare<decltype(round<si::time<si::second>>(1_q_s)), decltype(1_q_s)>);
#endif
} // namespace