Files
mp-units/test/unit_test/runtime/fmt_test.cpp

1209 lines
32 KiB
C++
Raw Normal View History

2019-10-16 17:04:41 +02:00
// The MIT License (MIT)
//
// Copyright (c) 2018 Mateusz Pusz
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
2020-12-28 15:18:02 +01:00
#include <units/format.h>
#include <units/math.h>
#include <units/isq/si/si.h>
#include <units/isq/si/cgs/cgs.h>
2020-12-28 15:18:02 +01:00
#include <units/quantity_io.h>
2019-10-16 17:04:41 +02:00
#include <catch2/catch.hpp>
2020-03-19 09:23:27 +01:00
#include <iomanip>
2019-10-16 17:04:41 +02:00
#include <sstream>
using namespace units;
using namespace units::isq;
using namespace units::isq::si;
2019-11-06 16:50:34 +00:00
using namespace Catch::Matchers;
2019-10-16 17:04:41 +02:00
2019-11-05 20:05:11 +00:00
TEST_CASE("operator<< on a quantity", "[text][ostream][fmt]")
2019-10-16 17:04:41 +02:00
{
2020-03-19 09:23:27 +01:00
std::ostringstream os;
2019-10-16 17:04:41 +02:00
SECTION("quantity with a predefined unit")
{
SECTION("integral representation")
{
const auto q = 60_q_W;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "60 W");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-16 17:04:41 +02:00
}
SECTION("floating-point representation")
{
const auto q = 1023.5_q_Pa;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "1023.5 Pa");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-16 17:04:41 +02:00
}
}
2019-10-18 15:34:46 +02:00
2019-11-09 21:01:37 +00:00
SECTION("quantity with a predefined prefixed unit")
{
const auto q = 125_q_us;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-09 21:01:37 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "125 µs");
2019-11-09 21:01:37 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-09 21:01:37 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-09 21:01:37 +00:00
}
}
SECTION("quantity with a predefined unit + prefix")
{
SECTION("in terms of base units")
2019-10-18 15:34:46 +02:00
{
const length<scaled_unit<ratio(1, 1, 6), metre>> q(123);
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "123 Mm");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 15:34:46 +02:00
}
SECTION("in terms of derived units")
{
const energy<scaled_unit<ratio(1, 1, -2), joule>> q(60);
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "60 cJ");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
}
}
2020-05-08 21:18:16 +02:00
SECTION("quantity with an alias unit")
{
const auto q = 2_q_l;
2020-05-08 21:18:16 +02:00
os << q;
SECTION("iostream")
{
CHECK(os.str() == "2 l");
}
SECTION("fmt with default format {} on a quantity")
{
CHECK(fmt::format("{}", q) == os.str());
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
CHECK(fmt::format("{:%Q %q}", q) == os.str());
}
}
SECTION("quantity with a prefixed alias unit")
{
const auto q = 2_q_ml;
2020-05-08 21:18:16 +02:00
os << q;
SECTION("iostream")
{
CHECK(os.str() == "2 ml");
}
SECTION("fmt with default format {} on a quantity")
{
CHECK(fmt::format("{}", q) == os.str());
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
CHECK(fmt::format("{:%Q %q}", q) == os.str());
}
}
SECTION("quantity with a deduced unit")
{
SECTION("coherent derived unit")
{
SECTION("acceleration")
{
const auto q = 20_q_m / 2_q_s / 1_q_s;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "10 m/s²");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
}
SECTION("volume")
{
const auto q = 2_q_m * 1_q_m * 1_q_m;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "2 m³");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
}
}
SECTION("surface tension")
{
const auto q = 20_q_N / 2_q_m;
2020-03-19 09:23:27 +01:00
os << q;
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "10 N/m");
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
}
SECTION("fmt with format {:%Q %q} on a quantity")
2019-11-05 20:05:11 +00:00
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
}
}
SECTION("deduced derived unit")
{
SECTION("speed")
{
const auto q = 20_q_km / 2_q_h;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "10 km/h");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
}
2019-11-04 06:46:53 +00:00
SECTION("surface tension")
{
struct newton_per_centimetre : deduced_unit<newton_per_centimetre, si::dim_surface_tension, newton, centimetre> {};
const surface_tension<newton_per_centimetre> q(123);
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "123 N/cm");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-11-04 06:46:53 +00:00
}
}
2019-10-16 17:04:41 +02:00
}
SECTION("quantity with a predefined dimension but unknown unit")
{
SECTION("unit::ratio as an SI prefix for a dimension with a special symbol")
2019-10-16 17:04:41 +02:00
{
const auto q = 4_q_N * 2_q_cm;
2020-03-19 09:23:27 +01:00
os << q;
2019-10-16 17:04:41 +02:00
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "8 cJ");
2019-11-05 20:05:11 +00:00
}
2019-10-16 17:04:41 +02:00
2019-11-05 20:05:11 +00:00
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-16 17:04:41 +02:00
2019-11-05 20:05:11 +00:00
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-16 17:04:41 +02:00
}
2019-11-05 20:05:11 +00:00
SECTION("unit::ratio for a dimension without a special symbol")
2019-10-16 17:04:41 +02:00
{
const auto q = 2_q_um * 2_q_cm * 2_q_cm;
2020-03-19 09:23:27 +01:00
os << q;
2019-10-16 17:04:41 +02:00
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
CHECK(os.str() == "8 × 10⁻¹⁰ m³");
2019-11-05 20:05:11 +00:00
}
2019-10-16 17:04:41 +02:00
2019-11-05 20:05:11 +00:00
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-16 17:04:41 +02:00
2019-11-05 20:05:11 +00:00
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-16 17:04:41 +02:00
}
2019-11-05 20:05:11 +00:00
SECTION("unit::ratio::num != 1 && unit::ratio::den == 1")
2019-10-16 17:04:41 +02:00
{
const auto q = 4 * 2_q_min / (2_q_s * 2_q_s);
2020-03-19 09:23:27 +01:00
os << q;
2019-10-16 17:04:41 +02:00
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "2 [6 × 10¹] Hz");
2019-11-05 20:05:11 +00:00
}
2019-10-16 17:04:41 +02:00
2019-11-05 20:05:11 +00:00
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-16 17:04:41 +02:00
2019-11-05 20:05:11 +00:00
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-16 17:04:41 +02:00
}
2019-10-18 23:50:43 +02:00
2019-11-05 20:05:11 +00:00
SECTION("unit::ratio::num == 1 && unit::ratio::den != 1")
2019-10-18 23:50:43 +02:00
{
const auto q = 20_q_J / 2_q_min;
2020-03-19 09:23:27 +01:00
os << q;
2019-10-18 23:50:43 +02:00
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "10 [1/6 × 10⁻¹] W");
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
2019-11-05 20:05:11 +00:00
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
2019-11-05 20:05:11 +00:00
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
}
2019-11-05 20:05:11 +00:00
SECTION("unit::ratio::num != 1 && unit::ratio::den != 1")
2019-10-18 23:50:43 +02:00
{
const auto q = 60_q_kJ / 2_q_min;
2020-03-19 09:23:27 +01:00
os << q;
2019-10-18 23:50:43 +02:00
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "30 [1/6 × 10²] W");
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
2019-11-05 20:05:11 +00:00
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
2019-11-05 20:05:11 +00:00
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
}
}
SECTION("dimensionless quantity")
{
SECTION("one with ratio == 1")
{
const auto q = 4_q_m / 2_q_m;
os << q;
SECTION("iostream")
{
CHECK(os.str() == "2");
}
SECTION("fmt with default format {} on a quantity")
{
CHECK(fmt::format("{}", q) == os.str());
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
CHECK(fmt::format("{:%Q %q}", q) == "2 ");
}
}
SECTION("one with ratio.exp != 0")
{
const auto q = 4_q_km / 2_q_m;
os << q;
SECTION("iostream")
{
CHECK(os.str() == "2 × 10³");
}
SECTION("fmt with default format {} on a quantity")
{
CHECK(fmt::format("{}", q) == os.str());
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
CHECK(fmt::format("{:%Q %q}", q) == "2 × 10³");
}
}
SECTION("percents")
{
#if UNITS_DOWNCAST_MODE == 0
const auto q = quantity_cast<dim_one, percent>(15._q_m / 100._q_m);
#else
const auto q = quantity_cast<percent>(15._q_m / 100._q_m);
#endif
os << q;
SECTION("iostream")
{
CHECK(os.str() == "15 %");
}
SECTION("fmt with default format {} on a quantity")
{
CHECK(fmt::format("{}", q) == os.str());
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
CHECK(fmt::format("{:%Q %q}", q) == os.str());
}
}
}
2019-10-18 23:50:43 +02:00
SECTION("quantity with an unkown dimension")
{
SECTION("unit::ratio::num == 1 && unit::ratio::den == 1")
{
2019-12-29 17:06:03 +01:00
SECTION("SI base units")
2019-11-05 20:05:11 +00:00
{
const auto q = 2_q_s * 2_q_m * 2_q_kg;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
2019-12-29 17:06:03 +01:00
SECTION("iostream")
{
2020-03-27 00:19:56 +02:00
CHECK(os.str() == "8 m ⋅ kg ⋅ s");
2019-12-29 17:06:03 +01:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-12-29 17:06:03 +01:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-12-29 17:06:03 +01:00
}
2019-11-05 20:05:11 +00:00
}
2019-12-29 17:06:03 +01:00
SECTION("CGS base units")
2019-11-05 20:05:11 +00:00
{
const auto q = 2._q_s * si::cgs::length<si::cgs::centimetre>(2) * si::cgs::mass<si::cgs::gram>(2);
2020-03-19 09:23:27 +01:00
os << q;
2019-12-29 17:06:03 +01:00
SECTION("iostream")
{
2020-03-27 00:19:56 +02:00
CHECK(os.str() == "8 cm ⋅ g ⋅ s");
2019-12-29 17:06:03 +01:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-12-29 17:06:03 +01:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-12-29 17:06:03 +01:00
}
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
}
SECTION("unit::ratio as an SI prefix")
{
const auto q = 4_q_km * 2_q_s;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-27 00:19:56 +02:00
CHECK(os.str() == "8 × 10³ m ⋅ s");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
}
SECTION("unit::ratio::num != 1 && unit::ratio::den == 1")
{
const auto q = 4_q_kg * 2_q_min / (2_q_s * 2_q_s);
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "2 [6 × 10¹] kg/s");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
}
SECTION("unit::ratio::num == 1 && unit::ratio::den != 1")
{
const auto q = 20_q_kg / 2_q_min;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "10 [1/6 × 10⁻¹] kg/s");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
}
SECTION("CGS base units")
{
const auto q = 2._q_s * si::cgs::length<si::metre>(2) * si::cgs::mass<si::kilogram>(2);
os << q;
2019-12-29 17:06:03 +01:00
SECTION("iostream")
{
CHECK(os.str() == "8 × 10⁵ cm ⋅ g ⋅ s");
}
2019-12-29 17:06:03 +01:00
SECTION("fmt with default format {} on a quantity")
{
CHECK(fmt::format("{}", q) == os.str());
}
2019-12-29 17:06:03 +01:00
SECTION("fmt with format {:%Q %q} on a quantity")
{
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-12-29 17:06:03 +01:00
}
}
2019-12-29 17:06:03 +01:00
2019-10-18 23:50:43 +02:00
SECTION("unit::ratio::num != 1 && unit::ratio::den != 1")
{
const auto q = 60_q_min / 2_q_km;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-27 00:19:56 +02:00
CHECK(os.str() == "30 [6 × 10⁻²] 1/m ⋅ s");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
}
SECTION("exponent::num == 1 && exponent::den == 1")
2019-10-18 23:50:43 +02:00
{
const auto q = 4_q_m * 2_q_s;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-27 00:19:56 +02:00
CHECK(os.str() == "8 m ⋅ s");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
}
SECTION("exponent::num == 2 && exponent::den == 1 for positive exponent")
2019-10-18 23:50:43 +02:00
{
const auto q = 4_q_m * 2_q_s * 2_q_s;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-27 00:19:56 +02:00
CHECK(os.str() == "16 m ⋅ s²");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
}
SECTION("exponent::num == 2 && exponent::den == 1 for negative exponent (first dimension)")
2019-10-18 23:50:43 +02:00
{
const auto q = 8_q_s / 2_q_m / 2_q_m;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-27 00:19:56 +02:00
CHECK(os.str() == "2 1/m² ⋅ s");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
}
SECTION("exponent::num == 2 && exponent::den == 1 for negative exponent (not first dimension)")
2019-10-18 23:50:43 +02:00
{
const auto q = 8_q_m / 2_q_kg / 2_q_kg;
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "2 m/kg²");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
}
SECTION("fractional positive exponent")
{
const auto q = sqrt(9_q_m);
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "3 m^(1/2)");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
}
SECTION("fractional negative exponent")
{
const auto q = sqrt(9 / 1_q_m);
2020-03-19 09:23:27 +01:00
os << q;
2019-11-05 20:05:11 +00:00
SECTION("iostream")
{
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "3 1/m^(1/2)");
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with default format {} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
SECTION("fmt with format {:%Q %q} on a quantity")
{
2020-03-19 09:23:27 +01:00
CHECK(fmt::format("{:%Q %q}", q) == os.str());
2019-11-05 20:05:11 +00:00
}
2019-10-18 23:50:43 +02:00
}
}
}
2019-10-16 17:04:41 +02:00
2019-11-06 16:50:34 +00:00
TEST_CASE("format string with only %Q should print quantity value only", "[text][fmt]")
{
SECTION("integral representation")
{
2019-11-06 22:45:45 +00:00
SECTION("positive value")
{
CHECK(fmt::format("{:%Q}", 123_q_km_per_h) == "123");
2019-11-06 22:45:45 +00:00
}
SECTION("negative value")
{
CHECK(fmt::format("{:%Q}", 5_q_m - 10_q_m) == "-5");
2019-11-06 22:45:45 +00:00
}
2019-11-06 16:50:34 +00:00
}
SECTION("floating-point representation")
{
2019-11-06 22:45:45 +00:00
SECTION("positive value")
2019-11-06 16:50:34 +00:00
{
CHECK(fmt::format("{:%Q}", 221._q_km / 2_q_h) == "110.5");
2019-11-06 16:50:34 +00:00
}
2019-11-06 22:45:45 +00:00
SECTION("negative value")
{
CHECK(fmt::format("{:%Q}", 3.14_q_m - 10_q_m) == "-6.86");
2019-11-06 22:45:45 +00:00
}
SECTION("nan")
{
CHECK(fmt::format("{:%Q}", length<metre>(std::numeric_limits<double>::quiet_NaN())) == "nan");
2019-11-06 22:45:45 +00:00
}
SECTION("inf")
{
CHECK(fmt::format("{:%Q}", length<metre>(std::numeric_limits<double>::infinity())) == "inf");
2019-11-06 22:45:45 +00:00
}
SECTION("-inf")
{
CHECK(fmt::format("{:%Q}", length<metre>(-std::numeric_limits<double>::infinity())) == "-inf");
2019-11-06 22:45:45 +00:00
}
2019-11-06 16:50:34 +00:00
}
}
TEST_CASE("format string with only %q should print quantity unit symbol only", "[text][fmt]")
{
SECTION("standard format for a unit without Unicode symbols")
{
CHECK(fmt::format("{:%q}", 123_q_km_per_h) == "km/h");
}
2019-11-06 16:50:34 +00:00
SECTION("ASCII format for a unit without Unicode symbols")
{
CHECK(fmt::format("{:%Aq}", 123_q_km_per_h) == "km/h");
}
2020-03-24 06:59:25 +02:00
SECTION("standard format for a unit with Unicode symbols")
{
SECTION("Unicode signs in a unit symbol")
{
CHECK(fmt::format("{:%q}", 123_q_kR) == "");
}
2020-03-24 06:59:25 +02:00
SECTION("Unicode signs in a unit symbol prefix")
{
CHECK(fmt::format("{:%q}", 123_q_uV) == "µV");
}
}
2020-03-24 06:59:25 +02:00
SECTION("ASCII format for a unit with Unicode symbols")
{
SECTION("Unicode signs in a unit symbol")
{
CHECK(fmt::format("{:%Aq}", 123_q_kR) == "kohm");
}
SECTION("Unicode signs in a unit symbol prefix")
{
CHECK(fmt::format("{:%Aq}", 123_q_uV) == "uV");
}
}
}
2020-03-24 06:59:25 +02:00
TEST_CASE("%q and %Q can be put anywhere in a format string", "[text][fmt]")
2019-11-06 16:50:34 +00:00
{
SECTION("no space")
{
CHECK(fmt::format("{:%Q%q}", 123_q_km_per_h) == "123km/h");
2019-11-06 16:50:34 +00:00
}
SECTION("separator")
{
CHECK(fmt::format("{:%Q###%q}", 123_q_km_per_h) == "123###km/h");
2019-11-06 16:50:34 +00:00
}
SECTION("opposite order")
{
CHECK(fmt::format("{:%q %Q}", 123_q_km_per_h) == "km/h 123");
2019-11-06 16:50:34 +00:00
}
SECTION("tabulator")
{
CHECK(fmt::format("{:%Q%t%q}", 123_q_km_per_h) == "123\tkm/h");
}
SECTION("new line")
{
CHECK(fmt::format("{:%Q%n%q}", 123_q_km_per_h) == "123\nkm/h");
}
2019-11-06 22:45:45 +00:00
SECTION("% sign")
{
CHECK(fmt::format("{:%Q%% %q}", 123_q_km_per_h) == "123% km/h");
2019-11-06 22:45:45 +00:00
}
2019-11-06 16:50:34 +00:00
}
2020-03-19 09:23:27 +01:00
TEST_CASE("fill and align specification", "[text][fmt][ostream]")
2019-11-06 16:50:34 +00:00
{
2020-03-19 09:23:27 +01:00
SECTION("ostream")
{
std::ostringstream os;
SECTION("width = 10")
{
os << "|" << std::setw(10) << 123_q_m << "|";
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "| 123 m|");
}
SECTION("width = 10, align = right")
{
os << "|" << std::setw(10) << std::right << 123_q_m << "|";
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "| 123 m|");
}
SECTION("width = 10, align = left")
{
os << "|" << std::setw(10) << std::left << 123_q_m << "|";
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "|123 m |");
}
SECTION("width = 10, fill = *")
{
os << "|" << std::setw(10) << std::setfill('*') << 123_q_m << "|";
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "|*****123 m|");
}
SECTION("width = 10, fill = *, align = right")
{
os << "|" << std::setw(10) << std::setfill('*') << std::right << 123_q_m << "|";
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "|*****123 m|");
}
SECTION("width = 10, fill = *, align = left")
{
os << "|" << std::setw(10) << std::setfill('*') << std::left << 123_q_m << "|";
2020-03-19 09:23:27 +01:00
CHECK(os.str() == "|123 m*****|");
}
}
implementing ratio<num,den,exp> which replaces ratio<num,den> https://github.com/mpusz/units/issues/14 This "works", as in it passes all static and runtime tests. However quite a few of the tests have been "modified" to make them pass. Whether this is legitimate is debatable and should be the source of some thought / discussion. 1. many of the static tests and some of the runtime tests have had the input ratios of the tests modified in the following way. eg ratio<3,1000> => ratio<3,1,-3>. ie they have been "canonicalised". There are obviously an infinite number of ratios which represent the same rational number. The way `ratio` is implemented it always moves as "many powers of 10" from the `num` and `den` into the `exp` and that makes the `canonical` ratio. Because these are all "types" and the lib uses is_same all over the place, only exact matches will be `is_same`. ie ratio<300,4,0> !is_same ratio<3,4,2> (the latter is the canonical ratio). This is perhaps fine for tests in the devlopment phase, but there may be a need for "more forgiving" comparison / concept of value equality. One such comparison which compares den,num,exp after canonicalisation is the constexpr function `same` as defined at top of `ratio_test.cpp`. We may need to expose this and perhaps add even more soft comparisions. 2. In the runtime tests it is "subjective" how some resukts should be printed. There is the question of "how exactly to format certain ratios". eg omit denominators of "1" and exponents of "0". However before even addressing these in detail a decision needs to be made about the general form of "non-floating-point-converted" ratios which do not map exactly to a "Symbol prefix". Arguably these are "relatively ugly" whatever we do, so we could just go for an easily canonicalised form. An example is: - CHECK(stream.str() == "10 [1/60]W"); + CHECK(stream.str() == "10 [1/6 x 10⁻¹]W"); Which of thses is "better"? Is there a "third", better form? It's not obvious. My opnion is: Both of 1&2 are fine for now, unless we think they go down the wrong avenue, and can be "perfected later"? ie we can expose a softer version of ratio based equality, and decide on canonical way of printing ratios (as far as that is actually a very useful output form, compared with decimal, scientific or engineering notation).
2019-12-27 18:49:43 +00:00
SECTION("default format {} on a quantity")
2019-11-06 16:50:34 +00:00
{
CHECK(fmt::format("|{:0}|", 123_q_m) == "|123 m|");
CHECK(fmt::format("|{:10}|", 123_q_m) == "| 123 m|");
CHECK(fmt::format("|{:<10}|", 123_q_m) == "|123 m |");
CHECK(fmt::format("|{:>10}|", 123_q_m) == "| 123 m|");
CHECK(fmt::format("|{:^10}|", 123_q_m) == "| 123 m |");
CHECK(fmt::format("|{:*<10}|", 123_q_m) == "|123 m*****|");
CHECK(fmt::format("|{:*>10}|", 123_q_m) == "|*****123 m|");
CHECK(fmt::format("|{:*^10}|", 123_q_m) == "|**123 m***|");
2019-11-12 16:55:11 +01:00
}
2019-11-06 16:50:34 +00:00
2019-11-12 16:55:11 +01:00
SECTION("full format {:%Q %q} on a quantity")
{
CHECK(fmt::format("|{:0%Q%q}|", 123_q_m) == "|123m|");
CHECK(fmt::format("|{:10%Q%q}|", 123_q_m) == "| 123m|");
CHECK(fmt::format("|{:<10%Q%q}|", 123_q_m) == "|123m |");
CHECK(fmt::format("|{:>10%Q%q}|", 123_q_m) == "| 123m|");
CHECK(fmt::format("|{:^10%Q%q}|", 123_q_m) == "| 123m |");
CHECK(fmt::format("|{:*<10%Q%q}|", 123_q_m) == "|123m******|");
CHECK(fmt::format("|{:*>10%Q%q}|", 123_q_m) == "|******123m|");
CHECK(fmt::format("|{:*^10%Q%q}|", 123_q_m) == "|***123m***|");
2019-11-12 16:55:11 +01:00
}
implementing ratio<num,den,exp> which replaces ratio<num,den> https://github.com/mpusz/units/issues/14 This "works", as in it passes all static and runtime tests. However quite a few of the tests have been "modified" to make them pass. Whether this is legitimate is debatable and should be the source of some thought / discussion. 1. many of the static tests and some of the runtime tests have had the input ratios of the tests modified in the following way. eg ratio<3,1000> => ratio<3,1,-3>. ie they have been "canonicalised". There are obviously an infinite number of ratios which represent the same rational number. The way `ratio` is implemented it always moves as "many powers of 10" from the `num` and `den` into the `exp` and that makes the `canonical` ratio. Because these are all "types" and the lib uses is_same all over the place, only exact matches will be `is_same`. ie ratio<300,4,0> !is_same ratio<3,4,2> (the latter is the canonical ratio). This is perhaps fine for tests in the devlopment phase, but there may be a need for "more forgiving" comparison / concept of value equality. One such comparison which compares den,num,exp after canonicalisation is the constexpr function `same` as defined at top of `ratio_test.cpp`. We may need to expose this and perhaps add even more soft comparisions. 2. In the runtime tests it is "subjective" how some resukts should be printed. There is the question of "how exactly to format certain ratios". eg omit denominators of "1" and exponents of "0". However before even addressing these in detail a decision needs to be made about the general form of "non-floating-point-converted" ratios which do not map exactly to a "Symbol prefix". Arguably these are "relatively ugly" whatever we do, so we could just go for an easily canonicalised form. An example is: - CHECK(stream.str() == "10 [1/60]W"); + CHECK(stream.str() == "10 [1/6 x 10⁻¹]W"); Which of thses is "better"? Is there a "third", better form? It's not obvious. My opnion is: Both of 1&2 are fine for now, unless we think they go down the wrong avenue, and can be "perfected later"? ie we can expose a softer version of ratio based equality, and decide on canonical way of printing ratios (as far as that is actually a very useful output form, compared with decimal, scientific or engineering notation).
2019-12-27 18:49:43 +00:00
SECTION("value only format {:%Q} on a quantity")
2019-11-12 16:55:11 +01:00
{
CHECK(fmt::format("|{:0%Q}|", 123_q_m) == "|123|");
CHECK(fmt::format("|{:10%Q}|", 123_q_m) == "| 123|");
CHECK(fmt::format("|{:<10%Q}|", 123_q_m) == "|123 |");
CHECK(fmt::format("|{:>10%Q}|", 123_q_m) == "| 123|");
CHECK(fmt::format("|{:^10%Q}|", 123_q_m) == "| 123 |");
CHECK(fmt::format("|{:*<10%Q}|", 123_q_m) == "|123*******|");
CHECK(fmt::format("|{:*>10%Q}|", 123_q_m) == "|*******123|");
CHECK(fmt::format("|{:*^10%Q}|", 123_q_m) == "|***123****|");
2019-11-12 16:55:11 +01:00
}
2019-11-06 16:50:34 +00:00
implementing ratio<num,den,exp> which replaces ratio<num,den> https://github.com/mpusz/units/issues/14 This "works", as in it passes all static and runtime tests. However quite a few of the tests have been "modified" to make them pass. Whether this is legitimate is debatable and should be the source of some thought / discussion. 1. many of the static tests and some of the runtime tests have had the input ratios of the tests modified in the following way. eg ratio<3,1000> => ratio<3,1,-3>. ie they have been "canonicalised". There are obviously an infinite number of ratios which represent the same rational number. The way `ratio` is implemented it always moves as "many powers of 10" from the `num` and `den` into the `exp` and that makes the `canonical` ratio. Because these are all "types" and the lib uses is_same all over the place, only exact matches will be `is_same`. ie ratio<300,4,0> !is_same ratio<3,4,2> (the latter is the canonical ratio). This is perhaps fine for tests in the devlopment phase, but there may be a need for "more forgiving" comparison / concept of value equality. One such comparison which compares den,num,exp after canonicalisation is the constexpr function `same` as defined at top of `ratio_test.cpp`. We may need to expose this and perhaps add even more soft comparisions. 2. In the runtime tests it is "subjective" how some resukts should be printed. There is the question of "how exactly to format certain ratios". eg omit denominators of "1" and exponents of "0". However before even addressing these in detail a decision needs to be made about the general form of "non-floating-point-converted" ratios which do not map exactly to a "Symbol prefix". Arguably these are "relatively ugly" whatever we do, so we could just go for an easily canonicalised form. An example is: - CHECK(stream.str() == "10 [1/60]W"); + CHECK(stream.str() == "10 [1/6 x 10⁻¹]W"); Which of thses is "better"? Is there a "third", better form? It's not obvious. My opnion is: Both of 1&2 are fine for now, unless we think they go down the wrong avenue, and can be "perfected later"? ie we can expose a softer version of ratio based equality, and decide on canonical way of printing ratios (as far as that is actually a very useful output form, compared with decimal, scientific or engineering notation).
2019-12-27 18:49:43 +00:00
SECTION("symbol only format {:%q} on a quantity")
2019-11-12 16:55:11 +01:00
{
CHECK(fmt::format("|{:0%q}|", 123_q_m) == "|m|");
CHECK(fmt::format("|{:10%q}|", 123_q_m) == "|m |");
CHECK(fmt::format("|{:<10%q}|", 123_q_m) == "|m |");
CHECK(fmt::format("|{:>10%q}|", 123_q_m) == "| m|");
CHECK(fmt::format("|{:^10%q}|", 123_q_m) == "| m |");
CHECK(fmt::format("|{:*<10%q}|", 123_q_m) == "|m*********|");
CHECK(fmt::format("|{:*>10%q}|", 123_q_m) == "|*********m|");
CHECK(fmt::format("|{:*^10%q}|", 123_q_m) == "|****m*****|");
2019-11-12 16:55:11 +01:00
}
}
2019-11-06 16:50:34 +00:00
2019-11-12 16:55:11 +01:00
TEST_CASE("sign specification", "[text][fmt]")
{
length<metre> inf(std::numeric_limits<double>::infinity());
length<metre> nan(std::numeric_limits<double>::quiet_NaN());
2019-11-06 16:50:34 +00:00
implementing ratio<num,den,exp> which replaces ratio<num,den> https://github.com/mpusz/units/issues/14 This "works", as in it passes all static and runtime tests. However quite a few of the tests have been "modified" to make them pass. Whether this is legitimate is debatable and should be the source of some thought / discussion. 1. many of the static tests and some of the runtime tests have had the input ratios of the tests modified in the following way. eg ratio<3,1000> => ratio<3,1,-3>. ie they have been "canonicalised". There are obviously an infinite number of ratios which represent the same rational number. The way `ratio` is implemented it always moves as "many powers of 10" from the `num` and `den` into the `exp` and that makes the `canonical` ratio. Because these are all "types" and the lib uses is_same all over the place, only exact matches will be `is_same`. ie ratio<300,4,0> !is_same ratio<3,4,2> (the latter is the canonical ratio). This is perhaps fine for tests in the devlopment phase, but there may be a need for "more forgiving" comparison / concept of value equality. One such comparison which compares den,num,exp after canonicalisation is the constexpr function `same` as defined at top of `ratio_test.cpp`. We may need to expose this and perhaps add even more soft comparisions. 2. In the runtime tests it is "subjective" how some resukts should be printed. There is the question of "how exactly to format certain ratios". eg omit denominators of "1" and exponents of "0". However before even addressing these in detail a decision needs to be made about the general form of "non-floating-point-converted" ratios which do not map exactly to a "Symbol prefix". Arguably these are "relatively ugly" whatever we do, so we could just go for an easily canonicalised form. An example is: - CHECK(stream.str() == "10 [1/60]W"); + CHECK(stream.str() == "10 [1/6 x 10⁻¹]W"); Which of thses is "better"? Is there a "third", better form? It's not obvious. My opnion is: Both of 1&2 are fine for now, unless we think they go down the wrong avenue, and can be "perfected later"? ie we can expose a softer version of ratio based equality, and decide on canonical way of printing ratios (as far as that is actually a very useful output form, compared with decimal, scientific or engineering notation).
2019-12-27 18:49:43 +00:00
SECTION("full format {:%Q %q} on a quantity")
2019-11-06 16:50:34 +00:00
{
CHECK(fmt::format("{0:%Q%q},{0:%+Q%q},{0:%-Q%q},{0:% Q%q}", 1_q_m) == "1m,+1m,1m, 1m");
CHECK(fmt::format("{0:%Q%q},{0:%+Q%q},{0:%-Q%q},{0:% Q%q}", -1_q_m) == "-1m,-1m,-1m,-1m");
2020-03-20 13:09:06 +01:00
CHECK(fmt::format("{0:%Q%q},{0:%+Q%q},{0:%-Q%q},{0:% Q%q}", inf) == "infm,+infm,infm, infm");
CHECK(fmt::format("{0:%Q%q},{0:%+Q%q},{0:%-Q%q},{0:% Q%q}", nan) == "nanm,+nanm,nanm, nanm");
2019-11-06 16:50:34 +00:00
}
implementing ratio<num,den,exp> which replaces ratio<num,den> https://github.com/mpusz/units/issues/14 This "works", as in it passes all static and runtime tests. However quite a few of the tests have been "modified" to make them pass. Whether this is legitimate is debatable and should be the source of some thought / discussion. 1. many of the static tests and some of the runtime tests have had the input ratios of the tests modified in the following way. eg ratio<3,1000> => ratio<3,1,-3>. ie they have been "canonicalised". There are obviously an infinite number of ratios which represent the same rational number. The way `ratio` is implemented it always moves as "many powers of 10" from the `num` and `den` into the `exp` and that makes the `canonical` ratio. Because these are all "types" and the lib uses is_same all over the place, only exact matches will be `is_same`. ie ratio<300,4,0> !is_same ratio<3,4,2> (the latter is the canonical ratio). This is perhaps fine for tests in the devlopment phase, but there may be a need for "more forgiving" comparison / concept of value equality. One such comparison which compares den,num,exp after canonicalisation is the constexpr function `same` as defined at top of `ratio_test.cpp`. We may need to expose this and perhaps add even more soft comparisions. 2. In the runtime tests it is "subjective" how some resukts should be printed. There is the question of "how exactly to format certain ratios". eg omit denominators of "1" and exponents of "0". However before even addressing these in detail a decision needs to be made about the general form of "non-floating-point-converted" ratios which do not map exactly to a "Symbol prefix". Arguably these are "relatively ugly" whatever we do, so we could just go for an easily canonicalised form. An example is: - CHECK(stream.str() == "10 [1/60]W"); + CHECK(stream.str() == "10 [1/6 x 10⁻¹]W"); Which of thses is "better"? Is there a "third", better form? It's not obvious. My opnion is: Both of 1&2 are fine for now, unless we think they go down the wrong avenue, and can be "perfected later"? ie we can expose a softer version of ratio based equality, and decide on canonical way of printing ratios (as far as that is actually a very useful output form, compared with decimal, scientific or engineering notation).
2019-12-27 18:49:43 +00:00
SECTION("value only format {:%Q} on a quantity")
2019-11-06 16:50:34 +00:00
{
CHECK(fmt::format("{0:%Q},{0:%+Q},{0:%-Q},{0:% Q}", 1_q_m) == "1,+1,1, 1");
CHECK(fmt::format("{0:%Q},{0:%+Q},{0:%-Q},{0:% Q}", -1_q_m) == "-1,-1,-1,-1");
2020-03-20 13:09:06 +01:00
CHECK(fmt::format("{0:%Q},{0:%+Q},{0:%-Q},{0:% Q}", inf) == "inf,+inf,inf, inf");
CHECK(fmt::format("{0:%Q},{0:%+Q},{0:%-Q},{0:% Q}", nan) == "nan,+nan,nan, nan");
2019-11-12 16:55:11 +01:00
}
}
2019-11-06 16:50:34 +00:00
2019-11-12 16:55:11 +01:00
TEST_CASE("precision specification", "[text][fmt]")
{
implementing ratio<num,den,exp> which replaces ratio<num,den> https://github.com/mpusz/units/issues/14 This "works", as in it passes all static and runtime tests. However quite a few of the tests have been "modified" to make them pass. Whether this is legitimate is debatable and should be the source of some thought / discussion. 1. many of the static tests and some of the runtime tests have had the input ratios of the tests modified in the following way. eg ratio<3,1000> => ratio<3,1,-3>. ie they have been "canonicalised". There are obviously an infinite number of ratios which represent the same rational number. The way `ratio` is implemented it always moves as "many powers of 10" from the `num` and `den` into the `exp` and that makes the `canonical` ratio. Because these are all "types" and the lib uses is_same all over the place, only exact matches will be `is_same`. ie ratio<300,4,0> !is_same ratio<3,4,2> (the latter is the canonical ratio). This is perhaps fine for tests in the devlopment phase, but there may be a need for "more forgiving" comparison / concept of value equality. One such comparison which compares den,num,exp after canonicalisation is the constexpr function `same` as defined at top of `ratio_test.cpp`. We may need to expose this and perhaps add even more soft comparisions. 2. In the runtime tests it is "subjective" how some resukts should be printed. There is the question of "how exactly to format certain ratios". eg omit denominators of "1" and exponents of "0". However before even addressing these in detail a decision needs to be made about the general form of "non-floating-point-converted" ratios which do not map exactly to a "Symbol prefix". Arguably these are "relatively ugly" whatever we do, so we could just go for an easily canonicalised form. An example is: - CHECK(stream.str() == "10 [1/60]W"); + CHECK(stream.str() == "10 [1/6 x 10⁻¹]W"); Which of thses is "better"? Is there a "third", better form? It's not obvious. My opnion is: Both of 1&2 are fine for now, unless we think they go down the wrong avenue, and can be "perfected later"? ie we can expose a softer version of ratio based equality, and decide on canonical way of printing ratios (as far as that is actually a very useful output form, compared with decimal, scientific or engineering notation).
2019-12-27 18:49:43 +00:00
SECTION("full format {:%Q %q} on a quantity")
2019-11-12 16:55:11 +01:00
{
CHECK(fmt::format("{:%.0Q %q}", 1.2345_q_m) == "1 m");
CHECK(fmt::format("{:%.1Q %q}", 1.2345_q_m) == "1.2 m");
CHECK(fmt::format("{:%.2Q %q}", 1.2345_q_m) == "1.23 m");
#ifdef UNITS_COMP_MSVC
CHECK(fmt::format("{:%.3Q %q}", 1.2345_q_m) == "1.234 m");
2020-09-05 22:54:57 +02:00
#else
CHECK(fmt::format("{:%.3Q %q}", 1.2345_q_m) == "1.235 m");
2020-09-05 22:54:57 +02:00
#endif
CHECK(fmt::format("{:%.4Q %q}", 1.2345_q_m) == "1.2345 m");
CHECK(fmt::format("{:%.5Q %q}", 1.2345_q_m) == "1.23450 m");
CHECK(fmt::format("{:%.10Q %q}", 1.2345_q_m) == "1.2345000000 m");
2019-11-12 16:55:11 +01:00
}
2019-11-06 16:50:34 +00:00
implementing ratio<num,den,exp> which replaces ratio<num,den> https://github.com/mpusz/units/issues/14 This "works", as in it passes all static and runtime tests. However quite a few of the tests have been "modified" to make them pass. Whether this is legitimate is debatable and should be the source of some thought / discussion. 1. many of the static tests and some of the runtime tests have had the input ratios of the tests modified in the following way. eg ratio<3,1000> => ratio<3,1,-3>. ie they have been "canonicalised". There are obviously an infinite number of ratios which represent the same rational number. The way `ratio` is implemented it always moves as "many powers of 10" from the `num` and `den` into the `exp` and that makes the `canonical` ratio. Because these are all "types" and the lib uses is_same all over the place, only exact matches will be `is_same`. ie ratio<300,4,0> !is_same ratio<3,4,2> (the latter is the canonical ratio). This is perhaps fine for tests in the devlopment phase, but there may be a need for "more forgiving" comparison / concept of value equality. One such comparison which compares den,num,exp after canonicalisation is the constexpr function `same` as defined at top of `ratio_test.cpp`. We may need to expose this and perhaps add even more soft comparisions. 2. In the runtime tests it is "subjective" how some resukts should be printed. There is the question of "how exactly to format certain ratios". eg omit denominators of "1" and exponents of "0". However before even addressing these in detail a decision needs to be made about the general form of "non-floating-point-converted" ratios which do not map exactly to a "Symbol prefix". Arguably these are "relatively ugly" whatever we do, so we could just go for an easily canonicalised form. An example is: - CHECK(stream.str() == "10 [1/60]W"); + CHECK(stream.str() == "10 [1/6 x 10⁻¹]W"); Which of thses is "better"? Is there a "third", better form? It's not obvious. My opnion is: Both of 1&2 are fine for now, unless we think they go down the wrong avenue, and can be "perfected later"? ie we can expose a softer version of ratio based equality, and decide on canonical way of printing ratios (as far as that is actually a very useful output form, compared with decimal, scientific or engineering notation).
2019-12-27 18:49:43 +00:00
SECTION("value only format {:%Q} on a quantity")
2019-11-12 16:55:11 +01:00
{
CHECK(fmt::format("{:%.0Q}", 1.2345_q_m) == "1");
CHECK(fmt::format("{:%.1Q}", 1.2345_q_m) == "1.2");
CHECK(fmt::format("{:%.2Q}", 1.2345_q_m) == "1.23");
#ifdef UNITS_COMP_MSVC
CHECK(fmt::format("{:%.3Q}", 1.2345_q_m) == "1.234");
2020-09-05 22:54:57 +02:00
#else
CHECK(fmt::format("{:%.3Q}", 1.2345_q_m) == "1.235");
2020-09-05 22:54:57 +02:00
#endif
CHECK(fmt::format("{:%.4Q}", 1.2345_q_m) == "1.2345");
CHECK(fmt::format("{:%.5Q}", 1.2345_q_m) == "1.23450");
CHECK(fmt::format("{:%.10Q}", 1.2345_q_m) == "1.2345000000");
2019-11-06 16:50:34 +00:00
}
}
TEST_CASE("precision specification for integral representation should throw", "[text][fmt][exception]")
{
implementing ratio<num,den,exp> which replaces ratio<num,den> https://github.com/mpusz/units/issues/14 This "works", as in it passes all static and runtime tests. However quite a few of the tests have been "modified" to make them pass. Whether this is legitimate is debatable and should be the source of some thought / discussion. 1. many of the static tests and some of the runtime tests have had the input ratios of the tests modified in the following way. eg ratio<3,1000> => ratio<3,1,-3>. ie they have been "canonicalised". There are obviously an infinite number of ratios which represent the same rational number. The way `ratio` is implemented it always moves as "many powers of 10" from the `num` and `den` into the `exp` and that makes the `canonical` ratio. Because these are all "types" and the lib uses is_same all over the place, only exact matches will be `is_same`. ie ratio<300,4,0> !is_same ratio<3,4,2> (the latter is the canonical ratio). This is perhaps fine for tests in the devlopment phase, but there may be a need for "more forgiving" comparison / concept of value equality. One such comparison which compares den,num,exp after canonicalisation is the constexpr function `same` as defined at top of `ratio_test.cpp`. We may need to expose this and perhaps add even more soft comparisions. 2. In the runtime tests it is "subjective" how some resukts should be printed. There is the question of "how exactly to format certain ratios". eg omit denominators of "1" and exponents of "0". However before even addressing these in detail a decision needs to be made about the general form of "non-floating-point-converted" ratios which do not map exactly to a "Symbol prefix". Arguably these are "relatively ugly" whatever we do, so we could just go for an easily canonicalised form. An example is: - CHECK(stream.str() == "10 [1/60]W"); + CHECK(stream.str() == "10 [1/6 x 10⁻¹]W"); Which of thses is "better"? Is there a "third", better form? It's not obvious. My opnion is: Both of 1&2 are fine for now, unless we think they go down the wrong avenue, and can be "perfected later"? ie we can expose a softer version of ratio based equality, and decide on canonical way of printing ratios (as far as that is actually a very useful output form, compared with decimal, scientific or engineering notation).
2019-12-27 18:49:43 +00:00
SECTION("full format {:%Q %q} on a quantity")
2019-11-06 16:50:34 +00:00
{
REQUIRE_THROWS_MATCHES(fmt::format("{:%.1Q %q}", 1_q_m), fmt::format_error, Message("precision not allowed for integral quantity representation"));
2019-11-06 16:50:34 +00:00
}
implementing ratio<num,den,exp> which replaces ratio<num,den> https://github.com/mpusz/units/issues/14 This "works", as in it passes all static and runtime tests. However quite a few of the tests have been "modified" to make them pass. Whether this is legitimate is debatable and should be the source of some thought / discussion. 1. many of the static tests and some of the runtime tests have had the input ratios of the tests modified in the following way. eg ratio<3,1000> => ratio<3,1,-3>. ie they have been "canonicalised". There are obviously an infinite number of ratios which represent the same rational number. The way `ratio` is implemented it always moves as "many powers of 10" from the `num` and `den` into the `exp` and that makes the `canonical` ratio. Because these are all "types" and the lib uses is_same all over the place, only exact matches will be `is_same`. ie ratio<300,4,0> !is_same ratio<3,4,2> (the latter is the canonical ratio). This is perhaps fine for tests in the devlopment phase, but there may be a need for "more forgiving" comparison / concept of value equality. One such comparison which compares den,num,exp after canonicalisation is the constexpr function `same` as defined at top of `ratio_test.cpp`. We may need to expose this and perhaps add even more soft comparisions. 2. In the runtime tests it is "subjective" how some resukts should be printed. There is the question of "how exactly to format certain ratios". eg omit denominators of "1" and exponents of "0". However before even addressing these in detail a decision needs to be made about the general form of "non-floating-point-converted" ratios which do not map exactly to a "Symbol prefix". Arguably these are "relatively ugly" whatever we do, so we could just go for an easily canonicalised form. An example is: - CHECK(stream.str() == "10 [1/60]W"); + CHECK(stream.str() == "10 [1/6 x 10⁻¹]W"); Which of thses is "better"? Is there a "third", better form? It's not obvious. My opnion is: Both of 1&2 are fine for now, unless we think they go down the wrong avenue, and can be "perfected later"? ie we can expose a softer version of ratio based equality, and decide on canonical way of printing ratios (as far as that is actually a very useful output form, compared with decimal, scientific or engineering notation).
2019-12-27 18:49:43 +00:00
SECTION("value only format {:%Q} on a quantity")
2019-11-06 16:50:34 +00:00
{
REQUIRE_THROWS_MATCHES(fmt::format("{:%.1Q}", 1_q_m), fmt::format_error, Message("precision not allowed for integral quantity representation"));
2019-11-06 16:50:34 +00:00
}
}
2020-03-18 00:59:49 +01:00
TEST_CASE("type specification", "[text][fmt]")
{
SECTION("full format {:%Q %q} on a quantity")
{
CHECK(fmt::format("{:%bQ %q}", 42_q_m) == "101010 m");
CHECK(fmt::format("{:%BQ %q}", 42_q_m) == "101010 m");
CHECK(fmt::format("{:%dQ %q}", 42_q_m) == "42 m");
CHECK(fmt::format("{:%oQ %q}", 42_q_m) == "52 m");
CHECK(fmt::format("{:%xQ %q}", 42_q_m) == "2a m");
CHECK(fmt::format("{:%XQ %q}", 42_q_m) == "2A m");
#ifdef UNITS_COMP_MSVC
CHECK(fmt::format("{:%aQ %q}", 1.2345678_q_m) == "0x1.3c0ca2a5b1d5dp+0 m");
CHECK(fmt::format("{:%.3aQ %q}", 1.2345678_q_m) == "0x1.3c1p+0 m");
CHECK(fmt::format("{:%AQ %q}", 1.2345678_q_m) == "0X1.3C0CA2A5B1D5DP+0 m");
CHECK(fmt::format("{:%.3AQ %q}", 1.2345678_q_m) == "0X1.3C1P+0 m");
2020-09-05 22:54:57 +02:00
#else
CHECK(fmt::format("{:%aQ %q}", 1.2345678_q_m) == "0x9.e065152d8eae841p-3 m");
CHECK(fmt::format("{:%.3aQ %q}", 1.2345678_q_m) == "0x9.e06p-3 m");
CHECK(fmt::format("{:%AQ %q}", 1.2345678_q_m) == "0X9.E065152D8EAE841P-3 m");
CHECK(fmt::format("{:%.3AQ %q}", 1.2345678_q_m) == "0X9.E06P-3 m");
2020-09-05 22:54:57 +02:00
#endif
CHECK(fmt::format("{:%eQ %q}", 1.2345678_q_m) == "1.234568e+00 m");
CHECK(fmt::format("{:%.3eQ %q}", 1.2345678_q_m) == "1.235e+00 m");
CHECK(fmt::format("{:%EQ %q}", 1.2345678_q_m) == "1.234568E+00 m");
CHECK(fmt::format("{:%.3EQ %q}", 1.2345678_q_m) == "1.235E+00 m");
CHECK(fmt::format("{:%gQ %q}", 1.2345678_q_m) == "1.23457 m");
CHECK(fmt::format("{:%gQ %q}", 1.2345678e8_q_m) == "1.23457e+08 m");
CHECK(fmt::format("{:%.3gQ %q}", 1.2345678_q_m) == "1.23 m");
CHECK(fmt::format("{:%.3gQ %q}", 1.2345678e8_q_m) == "1.23e+08 m");
CHECK(fmt::format("{:%GQ %q}", 1.2345678_q_m) == "1.23457 m");
CHECK(fmt::format("{:%GQ %q}", 1.2345678e8_q_m) == "1.23457E+08 m");
CHECK(fmt::format("{:%.3GQ %q}", 1.2345678_q_m) == "1.23 m");
CHECK(fmt::format("{:%.3GQ %q}", 1.2345678e8_q_m) == "1.23E+08 m");
2020-03-18 00:59:49 +01:00
}
SECTION("value only format {:%Q} on a quantity")
{
CHECK(fmt::format("{:%bQ}", 42_q_m) == "101010");
CHECK(fmt::format("{:%BQ}", 42_q_m) == "101010");
CHECK(fmt::format("{:%dQ}", 42_q_m) == "42");
CHECK(fmt::format("{:%oQ}", 42_q_m) == "52");
CHECK(fmt::format("{:%xQ}", 42_q_m) == "2a");
CHECK(fmt::format("{:%XQ}", 42_q_m) == "2A");
#ifdef UNITS_COMP_MSVC
CHECK(fmt::format("{:%aQ}", 1.2345678_q_m) == "0x1.3c0ca2a5b1d5dp+0");
CHECK(fmt::format("{:%.3aQ}", 1.2345678_q_m) == "0x1.3c1p+0");
CHECK(fmt::format("{:%AQ}", 1.2345678_q_m) == "0X1.3C0CA2A5B1D5DP+0");
CHECK(fmt::format("{:%.3AQ}", 1.2345678_q_m) == "0X1.3C1P+0");
2020-09-05 22:54:57 +02:00
#else
CHECK(fmt::format("{:%aQ}", 1.2345678_q_m) == "0x9.e065152d8eae841p-3");
CHECK(fmt::format("{:%.3aQ}", 1.2345678_q_m) == "0x9.e06p-3");
CHECK(fmt::format("{:%AQ}", 1.2345678_q_m) == "0X9.E065152D8EAE841P-3");
CHECK(fmt::format("{:%.3AQ}", 1.2345678_q_m) == "0X9.E06P-3");
2020-09-05 22:54:57 +02:00
#endif
CHECK(fmt::format("{:%eQ}", 1.2345678_q_m) == "1.234568e+00");
CHECK(fmt::format("{:%.3eQ}", 1.2345678_q_m) == "1.235e+00");
CHECK(fmt::format("{:%EQ}", 1.2345678_q_m) == "1.234568E+00");
CHECK(fmt::format("{:%.3EQ}", 1.2345678_q_m) == "1.235E+00");
CHECK(fmt::format("{:%gQ}", 1.2345678_q_m) == "1.23457");
CHECK(fmt::format("{:%gQ}", 1.2345678e8_q_m) == "1.23457e+08");
CHECK(fmt::format("{:%.3gQ}", 1.2345678_q_m) == "1.23");
CHECK(fmt::format("{:%.3gQ}", 1.2345678e8_q_m) == "1.23e+08");
CHECK(fmt::format("{:%GQ}", 1.2345678_q_m) == "1.23457");
CHECK(fmt::format("{:%GQ}", 1.2345678e8_q_m) == "1.23457E+08");
CHECK(fmt::format("{:%.3GQ}", 1.2345678_q_m) == "1.23");
CHECK(fmt::format("{:%.3GQ}", 1.2345678e8_q_m) == "1.23E+08");
2020-03-18 00:59:49 +01:00
}
}
2020-09-10 01:31:42 +02:00
TEST_CASE("different base types with the # specifier", "[text][fmt]")
2020-03-18 00:59:49 +01:00
{
SECTION("full format {:%Q %q} on a quantity")
{
CHECK(fmt::format("{:%#bQ %q}", 42_q_m) == "0b101010 m");
CHECK(fmt::format("{:%#BQ %q}", 42_q_m) == "0B101010 m");
CHECK(fmt::format("{:%#oQ %q}", 42_q_m) == "052 m");
CHECK(fmt::format("{:%#xQ %q}", 42_q_m) == "0x2a m");
CHECK(fmt::format("{:%#XQ %q}", 42_q_m) == "0X2A m");
2020-03-18 00:59:49 +01:00
}
SECTION("value only format {:%Q} on a quantity")
{
CHECK(fmt::format("{:%#bQ}", 42_q_m) == "0b101010");
CHECK(fmt::format("{:%#BQ}", 42_q_m) == "0B101010");
CHECK(fmt::format("{:%#oQ}", 42_q_m) == "052");
CHECK(fmt::format("{:%#xQ}", 42_q_m) == "0x2a");
CHECK(fmt::format("{:%#XQ}", 42_q_m) == "0X2A");
2020-03-18 00:59:49 +01:00
}
}
2019-10-18 15:34:46 +02:00
2020-09-10 01:31:42 +02:00
TEST_CASE("localization with the 'L' specifier", "[text][fmt][localization]")
{
struct group2 : std::numpunct<char>
{
2020-09-10 01:37:31 +02:00
char do_thousands_sep() const override { return '_'; }
std::string do_grouping() const override { return "\2"; }
2020-09-10 01:31:42 +02:00
};
struct group3 : std::numpunct<char>
{
2020-09-10 01:37:31 +02:00
char do_thousands_sep() const override { return '\''; }
std::string do_grouping() const override { return "\3"; }
2020-09-10 01:31:42 +02:00
};
std::locale grp2{std::locale::classic(), new group2};
std::locale grp3{std::locale::classic(), new group3};
SECTION("full format {:%LQ %q} on a quantity")
{
CHECK(fmt::format(grp2, "{:%LQ %q}", 299792458_q_m_per_s) == "2_99_79_24_58 m/s");
CHECK(fmt::format(grp3, "{:%LQ %q}", 299792458_q_m_per_s) == "299'792'458 m/s");
}
}
2019-12-23 13:22:37 +01:00
TEST_CASE("quantity_cast", "[text][ostream]")
{
2020-03-19 09:23:27 +01:00
std::ostringstream os;
2019-12-23 13:22:37 +01:00
SECTION("int to double representation")
{
const auto q = 121_q_km / 2_q_h;
2019-12-23 13:22:37 +01:00
SECTION("original")
{
2020-03-19 09:23:27 +01:00
os << q;
CHECK(os.str() == "60 km/h");
2019-12-23 13:22:37 +01:00
}
SECTION("int")
{
2020-03-19 09:23:27 +01:00
os << quantity_cast<int>(q);
CHECK(os.str() == "60 km/h");
2019-12-23 13:22:37 +01:00
}
SECTION("double")
{
2020-03-19 09:23:27 +01:00
os << quantity_cast<double>(q);
CHECK(os.str() == "60 km/h");
2019-12-23 13:22:37 +01:00
}
}
SECTION("double to int representation")
{
const auto q = 121._q_km / 2_q_h;
2019-12-23 13:22:37 +01:00
SECTION("original")
{
2020-03-19 09:23:27 +01:00
os << q;
CHECK(os.str() == "60.5 km/h");
2019-12-23 13:22:37 +01:00
}
SECTION("int")
{
2020-03-19 09:23:27 +01:00
os << quantity_cast<int>(q);
CHECK(os.str() == "60 km/h");
2019-12-23 13:22:37 +01:00
}
SECTION("double")
{
2020-03-19 09:23:27 +01:00
os << quantity_cast<double>(q);
CHECK(os.str() == "60.5 km/h");
2019-12-23 13:22:37 +01:00
}
}
}