diff --git a/src/core-io/include/mp-units/ostream.h b/src/core-io/include/mp-units/ostream.h index d9958fae..03fb3ac4 100644 --- a/src/core-io/include/mp-units/ostream.h +++ b/src/core-io/include/mp-units/ostream.h @@ -34,7 +34,11 @@ namespace detail { template void to_stream(std::basic_ostream& os, const quantity& q) { - os << q.number(); + if constexpr (is_same_v || is_same_v) + // promote the value to int + os << +q.number(); + else + os << q.number(); if constexpr (has_unit_symbol(get_unit(R))) { os << " "; unit_symbol_to(std::ostream_iterator(os), get_unit(R)); diff --git a/test/unit_test/runtime/fmt_test.cpp b/test/unit_test/runtime/fmt_test.cpp index 73c9a89e..85e72b8a 100644 --- a/test/unit_test/runtime/fmt_test.cpp +++ b/test/unit_test/runtime/fmt_test.cpp @@ -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]")