fix: unit symbol text output unit tests fixed

This commit is contained in:
Mateusz Pusz
2022-05-11 18:10:51 +02:00
parent 9a022da919
commit 7737167d1d
3 changed files with 17 additions and 16 deletions

View File

@@ -26,6 +26,7 @@
#include <units/bits/external/text_tools.h> #include <units/bits/external/text_tools.h>
#include <units/derived_dimension.h> #include <units/derived_dimension.h>
#include <units/prefix.h> #include <units/prefix.h>
#include <units/unit.h>
namespace units::detail { namespace units::detail {
@@ -57,7 +58,7 @@ constexpr auto ratio_text()
} }
} }
template<ratio R, std::size_t SymbolLen> template<Unit U, ratio R, std::size_t SymbolLen>
constexpr auto prefix_or_ratio_text() constexpr auto prefix_or_ratio_text()
{ {
if constexpr (R.num == 1 && R.den == 1 && R.exp == 0) { if constexpr (R.num == 1 && R.den == 1 && R.exp == 0) {
@@ -67,7 +68,7 @@ constexpr auto prefix_or_ratio_text()
// try to form a prefix // try to form a prefix
using prefix = downcast<detail::prefix_base<R>>; using prefix = downcast<detail::prefix_base<R>>;
if constexpr (!is_same_v<prefix, prefix_base<R>>) { if constexpr (can_be_prefixed<U> && !is_same_v<prefix, prefix_base<R>>) {
// print as a prefixed unit // print as a prefixed unit
return prefix::symbol; return prefix::symbol;
} else { } else {
@@ -138,19 +139,16 @@ constexpr auto unit_text()
// print as a prefix or ratio of a coherent unit // print as a prefix or ratio of a coherent unit
using coherent_unit = dimension_unit<Dim>; using coherent_unit = dimension_unit<Dim>;
if constexpr (has_symbol<coherent_unit>) { constexpr auto symbol_text = []() {
// use predefined coherent unit symbol if constexpr (has_symbol<coherent_unit>)
constexpr auto symbol_text = coherent_unit::symbol; return coherent_unit::symbol;
constexpr auto prefix_txt = else
prefix_or_ratio_text<U::ratio / coherent_unit::ratio, symbol_text.standard().size()>(); return derived_dimension_unit_text<Dim>();
return prefix_txt + symbol_text; }();
} else {
// use derived dimension ingredients to create a unit symbol constexpr auto prefix_txt =
constexpr auto symbol_text = derived_dimension_unit_text<Dim>(); prefix_or_ratio_text<U, U::ratio / coherent_unit::ratio, symbol_text.standard().size()>();
constexpr auto prefix_txt = return prefix_txt + symbol_text;
prefix_or_ratio_text<U::ratio / coherent_unit::ratio, symbol_text.standard().size()>();
return prefix_txt + symbol_text;
}
} }
} }

View File

@@ -30,7 +30,7 @@
namespace units { namespace units {
struct one : named_unit<one, ""> {}; struct one : derived_unit<one> {};
struct percent : named_scaled_unit<percent, "%", ratio(1, 100), one> {}; struct percent : named_scaled_unit<percent, "%", ratio(1, 100), one> {};
/** /**

View File

@@ -225,6 +225,9 @@ void can_be_prefixed_impl(const volatile alias_unit<U, Symbol>*);
template<Unit U> template<Unit U>
inline constexpr bool can_be_prefixed<U> = requires(U * u) { can_be_prefixed_impl(u); }; inline constexpr bool can_be_prefixed<U> = requires(U * u) { can_be_prefixed_impl(u); };
template<ratio R, typename U>
inline constexpr bool can_be_prefixed<scaled_unit<R, U>> = can_be_prefixed<typename U::reference>;
} // namespace detail } // namespace detail
} // namespace units } // namespace units