From 05934c8b7206aaa991f2d71289cf51ddb2fba33d Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Wed, 29 Dec 2021 21:37:06 -0500 Subject: [PATCH] Add make_base_power() helper --- src/core/include/units/magnitude.h | 5 +++++ test/unit_test/runtime/magnitude_test.cpp | 24 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/core/include/units/magnitude.h b/src/core/include/units/magnitude.h index d411766a..7e766b7e 100644 --- a/src/core/include/units/magnitude.h +++ b/src/core/include/units/magnitude.h @@ -85,6 +85,11 @@ constexpr auto make_ratio() { return quotient_t, detail::prime_factorization_t>{}; } +template +constexpr auto make_base_power() { + return magnitude>{}; +} + struct pi { static inline constexpr long double value = std::numbers::pi_v; }; diff --git a/test/unit_test/runtime/magnitude_test.cpp b/test/unit_test/runtime/magnitude_test.cpp index e0f5a00f..f95c37f9 100644 --- a/test/unit_test/runtime/magnitude_test.cpp +++ b/test/unit_test/runtime/magnitude_test.cpp @@ -234,6 +234,22 @@ TEST_CASE("make_ratio performs prime factorization correctly") } } +TEST_CASE("make_magnitude handles arbitrary bases") +{ + SECTION("Equivalent to std::integral_constant for integer bases") + { + CHECK(make_base_power>() == make_ratio<2>()); + CHECK(make_base_power>() == make_ratio<7>()); + } + + SECTION("Handles non-integer bases") + { + CHECK(make_base_power() == magnitude>{}); + CHECK(make_base_power() == magnitude>{}); + CHECK(make_base_power() == magnitude>{}); + } +} + TEST_CASE("Equality works for magnitudes") { SECTION("Equivalent ratios are equal") @@ -268,6 +284,14 @@ TEST_CASE("Multiplication works for magnitudes") CHECK(make_ratio<4, 5>() * make_ratio<4, 3>() == make_ratio<16, 15>()); } + SECTION("Products handle pi correctly") + { + CHECK( + make_base_power() * make_ratio<2, 3>() * make_base_power() == + magnitude, int_base_power<3, -1>, base_power>{}); + + } + SECTION("Supports constexpr") { constexpr auto p = make_ratio<4, 5>() * make_ratio<4, 3>();