fix: text output of 8-bit integers fixed (resolves #470)

This commit is contained in:
Mateusz Pusz
2023-07-05 11:24:53 +02:00
parent dfaf293774
commit 7b47b2fe57
2 changed files with 44 additions and 2 deletions

View File

@@ -34,7 +34,11 @@ namespace detail {
template<typename CharT, class Traits, auto R, typename Rep>
void to_stream(std::basic_ostream<CharT, Traits>& os, const quantity<R, Rep>& q)
{
os << q.number();
if constexpr (is_same_v<Rep, std::uint8_t> || is_same_v<Rep, std::int8_t>)
// promote the value to int
os << +q.number();
else
os << q.number();
if constexpr (has_unit_symbol(get_unit(R))) {
os << " ";
unit_symbol_to<CharT>(std::ostream_iterator<CharT>(os), get_unit(R));

View File

@@ -197,7 +197,6 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
}
}
SECTION("dimensionless quantity")
{
SECTION("one with ratio == 1")
@@ -236,6 +235,45 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
SECTION("fmt with format {:%Q %q} on a quantity") { CHECK(MP_UNITS_STD_FMT::format("{:%Q %q}", q) == os.str()); }
}
}
SECTION("8-bit integers")
{
SECTION("signed positive")
{
const auto q = std::int8_t{42} * si::second;
os << q;
SECTION("iostream") { CHECK(os.str() == "42 s"); }
SECTION("fmt with default format {} on a quantity") { CHECK(MP_UNITS_STD_FMT::format("{}", q) == os.str()); }
SECTION("fmt with format {:%Q %q} on a quantity") { CHECK(MP_UNITS_STD_FMT::format("{:%Q %q}", q) == os.str()); }
}
SECTION("signed negative")
{
const auto q = std::int8_t{-42} * si::second;
os << q;
SECTION("iostream") { CHECK(os.str() == "-42 s"); }
SECTION("fmt with default format {} on a quantity") { CHECK(MP_UNITS_STD_FMT::format("{}", q) == os.str()); }
SECTION("fmt with format {:%Q %q} on a quantity") { CHECK(MP_UNITS_STD_FMT::format("{:%Q %q}", q) == os.str()); }
}
SECTION("unsigned")
{
const auto q = std::uint8_t{42} * si::second;
os << q;
SECTION("iostream") { CHECK(os.str() == "42 s"); }
SECTION("fmt with default format {} on a quantity") { CHECK(MP_UNITS_STD_FMT::format("{}", q) == os.str()); }
SECTION("fmt with format {:%Q %q} on a quantity") { CHECK(MP_UNITS_STD_FMT::format("{:%Q %q}", q) == os.str()); }
}
}
}
TEST_CASE("format string with only %Q should print quantity value only", "[text][fmt]")