From 159e2c1c0205cdc288c623ee075c7979f03ca630 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sun, 29 Dec 2019 17:19:55 +0100 Subject: [PATCH] `ratio_text` improved to omit `den == 1` for `exp != 0` --- src/include/units/bits/unit_text.h | 11 +++++++---- test/unit_test/runtime/fmt_test.cpp | 25 +++++++++---------------- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/include/units/bits/unit_text.h b/src/include/units/bits/unit_text.h index c3118fac..bb8c91a2 100644 --- a/src/include/units/bits/unit_text.h +++ b/src/include/units/bits/unit_text.h @@ -29,21 +29,24 @@ namespace units::detail { template constexpr auto ratio_text() { - if constexpr(Ratio::num != 1 || Ratio::den != 1 || Ratio::exp != 0) { + if constexpr(Ratio::num == 1 && Ratio::den == 1 && Ratio::exp != 0) { + return basic_fixed_string("\u00D7 10") + superscript() + basic_fixed_string(" "); + } + else if constexpr(Ratio::num != 1 || Ratio::den != 1 || Ratio::exp != 0) { auto txt = basic_fixed_string("[") + regular(); if constexpr(Ratio::den == 1) { if constexpr(Ratio::exp == 0) { - return txt + basic_fixed_string("]"); + return txt + basic_fixed_string("] "); } else { return txt + basic_fixed_string(" \u00D7 10") + superscript() + - basic_fixed_string("]"); + basic_fixed_string("] "); } } else { return txt + basic_fixed_string("/") + regular() + basic_fixed_string(" \u00D7 10") + superscript() + - basic_fixed_string("]"); + basic_fixed_string("] "); } } else { diff --git a/test/unit_test/runtime/fmt_test.cpp b/test/unit_test/runtime/fmt_test.cpp index 893a5d0b..b73b4ef0 100644 --- a/test/unit_test/runtime/fmt_test.cpp +++ b/test/unit_test/runtime/fmt_test.cpp @@ -298,7 +298,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]") SECTION("iostream") { - CHECK(stream.str() == "8 [1 × 10⁻²]m³"); + CHECK(stream.str() == "8 × 10⁻² m³"); } SECTION("fmt with default format {} on a quantity") @@ -319,7 +319,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]") SECTION("iostream") { - CHECK(stream.str() == "2 [6 × 10¹]Hz"); + CHECK(stream.str() == "2 [6 × 10¹] Hz"); } SECTION("fmt with default format {} on a quantity") @@ -340,7 +340,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]") SECTION("iostream") { - CHECK(stream.str() == "10 [1/6 × 10⁻¹]W"); + CHECK(stream.str() == "10 [1/6 × 10⁻¹] W"); } SECTION("fmt with default format {} on a quantity") @@ -361,7 +361,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]") SECTION("iostream") { - CHECK(stream.str() == "30 [1/6 × 10²]W"); + CHECK(stream.str() == "30 [1/6 × 10²] W"); } SECTION("fmt with default format {} on a quantity") @@ -430,7 +430,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]") SECTION("iostream") { - CHECK(stream.str() == "8 [1 × 10³]m⋅s"); + CHECK(stream.str() == "8 × 10³ m⋅s"); } SECTION("fmt with default format {} on a quantity") @@ -451,7 +451,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]") SECTION("iostream") { - CHECK(stream.str() == "2 [6 × 10¹]kg/s"); + CHECK(stream.str() == "2 [6 × 10¹] kg/s"); } SECTION("fmt with default format {} on a quantity") @@ -472,7 +472,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]") SECTION("iostream") { - CHECK(stream.str() == "10 [1/6 × 10⁻¹]kg/s"); + CHECK(stream.str() == "10 [1/6 × 10⁻¹] kg/s"); } SECTION("fmt with default format {} on a quantity") @@ -493,7 +493,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]") SECTION("iostream") { - CHECK(stream.str() == "8 [1 × 10⁵]cm⋅g⋅s"); + CHECK(stream.str() == "8 × 10⁵ cm⋅g⋅s"); } SECTION("fmt with default format {} on a quantity") @@ -514,7 +514,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]") SECTION("iostream") { - CHECK(stream.str() == "30 [6 × 10⁻²]1/m⋅s"); + CHECK(stream.str() == "30 [6 × 10⁻²] 1/m⋅s"); } SECTION("fmt with default format {} on a quantity") @@ -934,10 +934,3 @@ TEST_CASE("quantity_cast", "[text][ostream]") } } } - -// Giving a precision specification -// in the chrono-format-spec is valid only for std::chrono::duration types where the representation type Rep -// is a floating-point type. For all other Rep types, a format_error shall be thrown if the chrono-format-spec -// contains a precision specification. - - // string s = format("{:=>8}", 42ms); // value of s is "====42ms"