mirror of
https://github.com/mpusz/mp-units.git
synced 2025-08-03 12:24:26 +02:00
fix: text output of 8-bit integers fixed (resolves #470)
This commit is contained in:
@@ -34,7 +34,11 @@ namespace detail {
|
|||||||
template<typename CharT, class Traits, auto R, typename Rep>
|
template<typename CharT, class Traits, auto R, typename Rep>
|
||||||
void to_stream(std::basic_ostream<CharT, Traits>& os, const quantity<R, Rep>& q)
|
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))) {
|
if constexpr (has_unit_symbol(get_unit(R))) {
|
||||||
os << " ";
|
os << " ";
|
||||||
unit_symbol_to<CharT>(std::ostream_iterator<CharT>(os), get_unit(R));
|
unit_symbol_to<CharT>(std::ostream_iterator<CharT>(os), get_unit(R));
|
||||||
|
@@ -197,7 +197,6 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SECTION("dimensionless quantity")
|
SECTION("dimensionless quantity")
|
||||||
{
|
{
|
||||||
SECTION("one with ratio == 1")
|
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("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]")
|
TEST_CASE("format string with only %Q should print quantity value only", "[text][fmt]")
|
||||||
|
Reference in New Issue
Block a user