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/derived_dimension.h>
#include <units/prefix.h>
#include <units/unit.h>
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()
{
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
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
return prefix::symbol;
} else {
@@ -138,19 +139,16 @@ constexpr auto unit_text()
// print as a prefix or ratio of a coherent unit
using coherent_unit = dimension_unit<Dim>;
if constexpr (has_symbol<coherent_unit>) {
// use predefined coherent unit symbol
constexpr auto symbol_text = coherent_unit::symbol;
constexpr auto prefix_txt =
prefix_or_ratio_text<U::ratio / coherent_unit::ratio, symbol_text.standard().size()>();
return prefix_txt + symbol_text;
} else {
// use derived dimension ingredients to create a unit symbol
constexpr auto symbol_text = derived_dimension_unit_text<Dim>();
constexpr auto prefix_txt =
prefix_or_ratio_text<U::ratio / coherent_unit::ratio, symbol_text.standard().size()>();
return prefix_txt + symbol_text;
}
constexpr auto symbol_text = []() {
if constexpr (has_symbol<coherent_unit>)
return coherent_unit::symbol;
else
return derived_dimension_unit_text<Dim>();
}();
constexpr auto prefix_txt =
prefix_or_ratio_text<U, U::ratio / coherent_unit::ratio, symbol_text.standard().size()>();
return prefix_txt + symbol_text;
}
}

View File

@@ -30,7 +30,7 @@
namespace units {
struct one : named_unit<one, ""> {};
struct one : derived_unit<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>
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 units