From 72c2d064097a34ba589b18873df09078af953157 Mon Sep 17 00:00:00 2001 From: Chip Hogg Date: Mon, 2 Dec 2024 15:41:44 -0500 Subject: [PATCH] Support scaling a unit symbol by a magnitude (#341) I found this useful in handling some "liters per 100 km" test cases. --- au/code/au/unit_symbol.hh | 3 ++- au/code/au/unit_symbol_test.cc | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/au/code/au/unit_symbol.hh b/au/code/au/unit_symbol.hh index 07fa991e..72fc46b6 100644 --- a/au/code/au/unit_symbol.hh +++ b/au/code/au/unit_symbol.hh @@ -30,7 +30,8 @@ namespace au { template struct SymbolFor : detail::MakesQuantityFromNumber, detail::ScalesQuantity, - detail::ComposesWith {}; + detail::ComposesWith, + detail::CanScaleByMagnitude {}; // // Create a unit symbol using the more fluent APIs that unit slots make possible. For example: diff --git a/au/code/au/unit_symbol_test.cc b/au/code/au/unit_symbol_test.cc index e9b892e0..7e387fd6 100644 --- a/au/code/au/unit_symbol_test.cc +++ b/au/code/au/unit_symbol_test.cc @@ -43,4 +43,12 @@ TEST(SymbolFor, ScalesUnitsOfExistingQuantity) { TEST(SymbolFor, CompatibleWithUnitSlot) { EXPECT_THAT(meters(35u).in(m), SameTypeAndValue(35u)); } +TEST(SymbolFor, CanScaleByMagnitude) { + // Yes, the identifier name is a little awkward for these symbols for anonymous scaled units. + // But it's still important to have this functionality. + constexpr auto u100_m = mag<100>() * m; + + EXPECT_THAT(3.5f / u100_m, SameTypeAndValue(inverse(meters * mag<100>())(3.5f))); +} + } // namespace au