diff --git a/doc/DESIGN.md b/doc/DESIGN.md index e574d55a..09754cae 100644 --- a/doc/DESIGN.md +++ b/doc/DESIGN.md @@ -246,15 +246,21 @@ concept Unit = detail::is_unit>; // exposition only ``` -Coherent derived units (units with `ratio<1>`) are created with a `coherent_derived_unit` class -template: +Coherent derived units (units with `ratio<1>`) are created with a `named_coherent_derived_unit` +or `coherent_derived_unit` class templates: ```cpp -template -struct coherent_derived_unit : downcast_child>> { +template +struct named_coherent_derived_unit : downcast_child>> { static constexpr auto symbol = Symbol; using prefix_type = PrefixType; }; + +template +struct coherent_derived_unit : downcast_child>> { + static constexpr auto symbol = /* ... */; + using prefix_type = PrefixType; +}; ``` The above exposes public `prefix_type` member type and `symbol` used to print unit symbol @@ -264,26 +270,31 @@ data). For example to define the coherent unit of `length`: ```cpp -struct metre : coherent_derived_unit {}; +struct metre : named_coherent_derived_unit {}; ``` Again, similarly to `derived_dimension`, the first class template parameter is a CRTP idiom used to provide downcasting facility (described below). -To create the rest of derived units the following class template can be used: +To create the rest of derived units the following class templates can be used: ```cpp template -struct derived_unit : downcast_child> { +struct named_derived_unit : downcast_child> { static constexpr auto symbol = Symbol; }; + +template +struct derived_unit : downcast_child> { + static constexpr auto symbol = /* ... */; +}; ``` -User has to provide a symbol name, dimension, and a ratio relative to a coherent derived unit. -For example to define `minute`: +User has to provide a symbol name (in case of a named unit), dimension, and a ratio relative +to a coherent derived unit. For example to define `minute`: ```cpp -struct minute : derived_unit> {}; +struct minute : named_derived_unit> {}; ``` The `mp-units` library provides also a few helper class templates to simplify the above process. @@ -329,16 +340,22 @@ For the cases where determining the exact ratio is not trivial another helper ca ```cpp template -struct deduced_derived_unit : downcast_child> { +struct named_deduced_derived_unit : downcast_child> { static constexpr auto symbol = Symbol; }; + +template +struct deduced_derived_unit : downcast_child> { + static constexpr auto symbol = /* ... */; +}; ``` This will deduce the ratio based on the ingredient units and their relation defined in the -dimension: +dimension and in case the symbol name is not explicitly provided it with create one based +on the provided ingredients: ```cpp -struct mile_per_hour : deduced_derived_unit {}; +struct mile_per_hour : deduced_derived_unit {}; ``` diff --git a/src/include/units/bits/format_utils.h b/src/include/units/bits/format_utils.h index 2749e87c..eb407530 100644 --- a/src/include/units/bits/format_utils.h +++ b/src/include/units/bits/format_utils.h @@ -22,7 +22,8 @@ #pragma once -#include +#include +#include #include #include @@ -61,30 +62,92 @@ namespace units { } } - template - void print_dimensions(std::basic_ostream& os, dimension) + + template + requires (0 <= Value) && (Value < 10) + inline constexpr basic_fixed_string superscript_number = "\u2070"; + +// template<> inline constexpr basic_fixed_string superscript_number<0> = "\u2070"; + template<> inline constexpr basic_fixed_string superscript_number<1> = "\u00b9"; + template<> inline constexpr basic_fixed_string superscript_number<2> = "\u00b2"; + template<> inline constexpr basic_fixed_string superscript_number<3> = "\u00b3"; + template<> inline constexpr basic_fixed_string superscript_number<4> = "\u2074"; + template<> inline constexpr basic_fixed_string superscript_number<5> = "\u2075"; + template<> inline constexpr basic_fixed_string superscript_number<6> = "\u2076"; + template<> inline constexpr basic_fixed_string superscript_number<7> = "\u2077"; + template<> inline constexpr basic_fixed_string superscript_number<8> = "\u2078"; + template<> inline constexpr basic_fixed_string superscript_number<9> = "\u2079"; + + template + requires (Value >= 0) + constexpr auto superscript() { - bool first = true; - auto ingr_printer = [&](E) { - if constexpr(E::num < 0) { - os << (first ? "1/" : "/"); + if constexpr(Value < 10) + return superscript_number; + else + return superscript() + superscript(); + } + + template + requires (Value >= 0) + constexpr auto regular() + { + if constexpr(Value < 10) + return basic_fixed_string(static_cast('0' + Value)); + else + return regular() + regular(); + } + + + template + constexpr auto operator_txt() + { + if constexpr(Idx == 0) { + if constexpr(Divide) { + return basic_fixed_string("1/"); } else { - os << (first ? "" : "⋅"); + return basic_fixed_string(""); } - os << E::dimension::symbol; - if constexpr(E::den != 1) { - os << "^(" << abs(E::num) << "/" << E::den << ")"; + } + else { + if constexpr(Divide) { + return basic_fixed_string("/"); } - else if constexpr(abs(E::num) != 1) { - // if constexpr(is_unicode) - // os << superscript; - // else - os << "^" << abs(E::num); + else { + return basic_fixed_string("⋅"); } - first = false; - }; - (ingr_printer(Es{}), ...); + } + } + + template + constexpr auto exp_txt() + { + // get calculation operator + symbol + const auto txt = operator_txt() + E::dimension::symbol; + if constexpr(E::den != 1) { + // add root part + return txt + basic_fixed_string("^(") + regular() + basic_fixed_string("/") + regular() + basic_fixed_string(")"); + } + else if constexpr(abs(E::num) != 1) { + // add exponent part + return txt + superscript(); + } + else { + return txt; + } + } + + template + constexpr auto symbol_text_impl(dimension, std::index_sequence) + { + return (exp_txt() + ...); + } + + template + constexpr auto symbol_text(dimension d) + { + return symbol_text_impl<>(d, std::index_sequence_for()); } } diff --git a/src/include/units/dimensions/acceleration.h b/src/include/units/dimensions/acceleration.h index 8a0f3510..de4f9d73 100644 --- a/src/include/units/dimensions/acceleration.h +++ b/src/include/units/dimensions/acceleration.h @@ -31,7 +31,7 @@ namespace units { template concept Acceleration = QuantityOf; - struct metre_per_second_sq : coherent_derived_unit {}; + struct metre_per_second_sq : coherent_derived_unit {}; inline namespace literals { diff --git a/src/include/units/dimensions/area.h b/src/include/units/dimensions/area.h index 89ce34ff..b5d36938 100644 --- a/src/include/units/dimensions/area.h +++ b/src/include/units/dimensions/area.h @@ -31,11 +31,11 @@ namespace units { template concept Area = QuantityOf; - struct square_metre : coherent_derived_unit {}; - struct square_millimetre : deduced_derived_unit {}; - struct square_centimetre : deduced_derived_unit {}; - struct square_kilometre : deduced_derived_unit {}; - struct square_foot : deduced_derived_unit {}; + struct square_metre : coherent_derived_unit {}; + struct square_millimetre : deduced_derived_unit {}; + struct square_centimetre : deduced_derived_unit {}; + struct square_kilometre : deduced_derived_unit {}; + struct square_foot : deduced_derived_unit {}; inline namespace literals { diff --git a/src/include/units/dimensions/capacitance.h b/src/include/units/dimensions/capacitance.h index 683af249..42558471 100644 --- a/src/include/units/dimensions/capacitance.h +++ b/src/include/units/dimensions/capacitance.h @@ -33,7 +33,7 @@ namespace units { template concept Capacitance = QuantityOf; - struct farad : coherent_derived_unit {}; + struct farad : named_coherent_derived_unit {}; inline namespace literals { diff --git a/src/include/units/dimensions/current.h b/src/include/units/dimensions/current.h index 101eb62a..bf2c608f 100644 --- a/src/include/units/dimensions/current.h +++ b/src/include/units/dimensions/current.h @@ -32,7 +32,7 @@ namespace units { template concept Current = QuantityOf; - struct ampere : coherent_derived_unit {}; + struct ampere : named_coherent_derived_unit {}; inline namespace literals { diff --git a/src/include/units/dimensions/electric_charge.h b/src/include/units/dimensions/electric_charge.h index c9eb3c27..98c0f118 100644 --- a/src/include/units/dimensions/electric_charge.h +++ b/src/include/units/dimensions/electric_charge.h @@ -33,7 +33,7 @@ namespace units { template concept ElectricCharge = QuantityOf; - struct coulomb : coherent_derived_unit {}; + struct coulomb : named_coherent_derived_unit {}; inline namespace literals { diff --git a/src/include/units/dimensions/energy.h b/src/include/units/dimensions/energy.h index 8a893383..51c7fea5 100644 --- a/src/include/units/dimensions/energy.h +++ b/src/include/units/dimensions/energy.h @@ -34,7 +34,7 @@ namespace units { template concept Energy = QuantityOf; - struct joule : coherent_derived_unit {}; + struct joule : named_coherent_derived_unit {}; struct millijoule : prefixed_derived_unit {}; struct kilojoule : prefixed_derived_unit {}; struct megajoule : prefixed_derived_unit {}; diff --git a/src/include/units/dimensions/force.h b/src/include/units/dimensions/force.h index ac0b0fc2..691f2988 100644 --- a/src/include/units/dimensions/force.h +++ b/src/include/units/dimensions/force.h @@ -33,7 +33,7 @@ namespace units { template concept Force = QuantityOf; - struct newton : coherent_derived_unit {}; + struct newton : named_coherent_derived_unit {}; inline namespace literals { diff --git a/src/include/units/dimensions/frequency.h b/src/include/units/dimensions/frequency.h index 3250ed43..39bd2ebd 100644 --- a/src/include/units/dimensions/frequency.h +++ b/src/include/units/dimensions/frequency.h @@ -33,7 +33,7 @@ namespace units { template concept Frequency = QuantityOf; - struct hertz : coherent_derived_unit {}; + struct hertz : named_coherent_derived_unit {}; struct millihertz : prefixed_derived_unit {}; struct kilohertz : prefixed_derived_unit {}; struct megahertz : prefixed_derived_unit {}; diff --git a/src/include/units/dimensions/length.h b/src/include/units/dimensions/length.h index 6f8cd454..26c9fa6b 100644 --- a/src/include/units/dimensions/length.h +++ b/src/include/units/dimensions/length.h @@ -34,7 +34,7 @@ namespace units { concept Length = QuantityOf; // SI units - struct metre : coherent_derived_unit {}; + struct metre : named_coherent_derived_unit {}; struct millimetre : prefixed_derived_unit {}; struct centimetre : prefixed_derived_unit {}; struct kilometre : prefixed_derived_unit {}; @@ -60,10 +60,10 @@ namespace units { } // namespace literals // US customary units - struct yard : derived_unit> {}; - struct foot : derived_unit, yard::ratio>> {}; - struct inch : derived_unit, foot::ratio>> {}; - struct mile : derived_unit, yard::ratio>> {}; + struct yard : named_derived_unit> {}; + struct foot : named_derived_unit, yard::ratio>> {}; + struct inch : named_derived_unit, foot::ratio>> {}; + struct mile : named_derived_unit, yard::ratio>> {}; inline namespace literals { diff --git a/src/include/units/dimensions/luminous_intensity.h b/src/include/units/dimensions/luminous_intensity.h index 4fd354a8..dcf8803d 100644 --- a/src/include/units/dimensions/luminous_intensity.h +++ b/src/include/units/dimensions/luminous_intensity.h @@ -32,7 +32,7 @@ namespace units { template concept LuminousIntensity = QuantityOf; - struct candela : coherent_derived_unit {}; + struct candela : named_coherent_derived_unit {}; inline namespace literals { diff --git a/src/include/units/dimensions/mass.h b/src/include/units/dimensions/mass.h index f48e3c43..b6e72687 100644 --- a/src/include/units/dimensions/mass.h +++ b/src/include/units/dimensions/mass.h @@ -32,8 +32,8 @@ namespace units { template concept Mass = QuantityOf; - struct kilogram : coherent_derived_unit {}; - struct gram : derived_unit> {}; + struct gram : named_derived_unit> {}; + struct kilogram : prefixed_derived_unit {}; inline namespace literals { diff --git a/src/include/units/dimensions/power.h b/src/include/units/dimensions/power.h index 76a67760..3d8ead4b 100644 --- a/src/include/units/dimensions/power.h +++ b/src/include/units/dimensions/power.h @@ -33,7 +33,7 @@ namespace units { template concept Power = QuantityOf; - struct watt : coherent_derived_unit {}; + struct watt : named_coherent_derived_unit {}; struct milliwatt : prefixed_derived_unit {}; struct kilowatt : prefixed_derived_unit {}; struct megawatt : prefixed_derived_unit {}; diff --git a/src/include/units/dimensions/pressure.h b/src/include/units/dimensions/pressure.h index 1d1b11de..85479866 100644 --- a/src/include/units/dimensions/pressure.h +++ b/src/include/units/dimensions/pressure.h @@ -33,7 +33,7 @@ namespace units { template concept Pressure = QuantityOf; - struct pascal : coherent_derived_unit {}; + struct pascal : named_coherent_derived_unit {}; inline namespace literals { diff --git a/src/include/units/dimensions/si_prefixes.h b/src/include/units/dimensions/si_prefixes.h index 406416b1..9f43bbb3 100644 --- a/src/include/units/dimensions/si_prefixes.h +++ b/src/include/units/dimensions/si_prefixes.h @@ -36,7 +36,7 @@ namespace units { struct femto : prefix, "f"> {}; struct pico : prefix, "p"> {}; struct nano : prefix, "n"> {}; - struct micro : prefix, "µ"> {}; + struct micro : prefix, "\u00b5"> {}; struct milli : prefix, "m"> {}; struct centi : prefix, "c"> {}; struct deci : prefix, "d"> {}; diff --git a/src/include/units/dimensions/substance.h b/src/include/units/dimensions/substance.h index 11e62d1f..191422aa 100644 --- a/src/include/units/dimensions/substance.h +++ b/src/include/units/dimensions/substance.h @@ -32,7 +32,7 @@ namespace units { template concept Substance = QuantityOf; - struct mole : coherent_derived_unit {}; + struct mole : named_coherent_derived_unit {}; inline namespace literals { diff --git a/src/include/units/dimensions/temperature.h b/src/include/units/dimensions/temperature.h index a5f30494..d15e9b68 100644 --- a/src/include/units/dimensions/temperature.h +++ b/src/include/units/dimensions/temperature.h @@ -32,7 +32,7 @@ namespace units { template concept ThermodynamicTemperature = QuantityOf; - struct kelvin : coherent_derived_unit {}; + struct kelvin : named_coherent_derived_unit {}; inline namespace literals { diff --git a/src/include/units/dimensions/time.h b/src/include/units/dimensions/time.h index 4aa0bbf3..4f96c1fc 100644 --- a/src/include/units/dimensions/time.h +++ b/src/include/units/dimensions/time.h @@ -33,12 +33,12 @@ namespace units { template concept Time = QuantityOf; - struct second : coherent_derived_unit {}; + struct second : named_coherent_derived_unit {}; struct nanosecond : prefixed_derived_unit {}; struct microsecond : prefixed_derived_unit {}; struct millisecond : prefixed_derived_unit {}; - struct minute : derived_unit> {}; - struct hour : derived_unit> {}; + struct minute : named_derived_unit> {}; + struct hour : named_derived_unit> {}; inline namespace literals { diff --git a/src/include/units/dimensions/velocity.h b/src/include/units/dimensions/velocity.h index 3dc04ed7..9b58a118 100644 --- a/src/include/units/dimensions/velocity.h +++ b/src/include/units/dimensions/velocity.h @@ -32,9 +32,9 @@ namespace units { template concept Velocity = QuantityOf; - struct metre_per_second : coherent_derived_unit {}; - struct kilometre_per_hour : deduced_derived_unit {}; - struct mile_per_hour : deduced_derived_unit {}; + struct metre_per_second : coherent_derived_unit {}; + struct kilometre_per_hour : deduced_derived_unit {}; + struct mile_per_hour : deduced_derived_unit {}; inline namespace literals { diff --git a/src/include/units/dimensions/voltage.h b/src/include/units/dimensions/voltage.h index 28db4cb7..688123ef 100644 --- a/src/include/units/dimensions/voltage.h +++ b/src/include/units/dimensions/voltage.h @@ -35,7 +35,7 @@ namespace units { template concept Voltage = QuantityOf; - struct volt : coherent_derived_unit {}; + struct volt : named_coherent_derived_unit {}; inline namespace literals { diff --git a/src/include/units/dimensions/volume.h b/src/include/units/dimensions/volume.h index 786aa471..3e2a55c3 100644 --- a/src/include/units/dimensions/volume.h +++ b/src/include/units/dimensions/volume.h @@ -31,11 +31,11 @@ namespace units { template concept Volume = QuantityOf; - struct cubic_metre : coherent_derived_unit {}; - struct cubic_millimetre : deduced_derived_unit {}; - struct cubic_centimetre : deduced_derived_unit {}; - struct cubic_kilometre : deduced_derived_unit {}; - struct cubic_foot : deduced_derived_unit {}; + struct cubic_metre : coherent_derived_unit {}; + struct cubic_millimetre : deduced_derived_unit {}; + struct cubic_centimetre : deduced_derived_unit {}; + struct cubic_kilometre : deduced_derived_unit {}; + struct cubic_foot : deduced_derived_unit {}; inline namespace literals { diff --git a/src/include/units/format.h b/src/include/units/format.h index 2433b5a7..c38f2fed 100644 --- a/src/include/units/format.h +++ b/src/include/units/format.h @@ -25,6 +25,7 @@ #include #include + template struct fmt::formatter, CharT> { template diff --git a/src/include/units/prefix.h b/src/include/units/prefix.h new file mode 100644 index 00000000..6a12c288 --- /dev/null +++ b/src/include/units/prefix.h @@ -0,0 +1,65 @@ +// 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. + +#pragma once + +#include +#include +#include + +namespace units { + + namespace detail { + + template + struct prefix_base : downcast_base> { + using prefix_type = PrefixType; + using ratio = R; + }; + + } + + template + struct prefix : downcast_child> { + static constexpr auto symbol = Symbol; + }; + + + // TODO gcc:92150 + // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92150 + // namespace detail { + + // template + // inline constexpr bool is_prefix = false; + + // template + // inline constexpr bool is_prefix> = true; + + // } // namespace detail + + template +// concept Prefix = detail::is_prefix; + concept Prefix = true; + + struct no_prefix; + +} // namespace units diff --git a/src/include/units/quantity.h b/src/include/units/quantity.h index 360dc654..855a3694 100644 --- a/src/include/units/quantity.h +++ b/src/include/units/quantity.h @@ -302,7 +302,7 @@ namespace units { detail::print_ratio(os); // print coherent unit dimensions and their exponents - detail::print_dimensions(os, dim{}); + os << detail::symbol_text(dim{}); } } return os; diff --git a/src/include/units/unit.h b/src/include/units/unit.h index b75aeb93..98740204 100644 --- a/src/include/units/unit.h +++ b/src/include/units/unit.h @@ -22,7 +22,9 @@ #pragma once +#include #include +#include #include #include @@ -52,38 +54,6 @@ namespace units { std::is_empty_v && detail::is_unit>; - namespace detail { - - template - struct prefix_base : downcast_base> { - using prefix_type = PrefixType; - using ratio = R; - }; - - } - - template - struct prefix : downcast_child> { - static constexpr auto symbol = Symbol; - }; - - - // TODO gcc:92150 - // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92150 - // namespace detail { - - // template - // inline constexpr bool is_prefix = false; - - // template - // inline constexpr bool is_prefix> = true; - - // } // namespace detail - - template -// concept Prefix = detail::is_prefix; - concept Prefix = true; - // make_derived_unit namespace detail { @@ -150,16 +120,27 @@ namespace units { struct no_prefix; template - struct coherent_derived_unit : downcast_child>> { + struct named_coherent_derived_unit : downcast_child>> { static constexpr auto symbol = Symbol; using prefix_type = PrefixType; }; + template + struct coherent_derived_unit : downcast_child>> { + static constexpr auto symbol = detail::symbol_text(D()); + using prefix_type = PrefixType; + }; + template - struct derived_unit : downcast_child> { + struct named_derived_unit : downcast_child> { static constexpr auto symbol = Symbol; }; + template + struct derived_unit : downcast_child> { + static constexpr auto symbol = "aaa"; + }; + template requires requires { U::symbol; } struct prefixed_derived_unit : downcast_child>> { @@ -168,8 +149,13 @@ namespace units { }; template - struct deduced_derived_unit : downcast_child> { + struct named_deduced_derived_unit : downcast_child> { static constexpr auto symbol = Symbol; }; + template + struct deduced_derived_unit : downcast_child> { + static constexpr auto symbol = "bbb"; + }; + } // namespace units diff --git a/test/unit_test/runtime/digital_information_test.cpp b/test/unit_test/runtime/digital_information_test.cpp index 0ffb12c5..822ac5c1 100644 --- a/test/unit_test/runtime/digital_information_test.cpp +++ b/test/unit_test/runtime/digital_information_test.cpp @@ -38,9 +38,9 @@ namespace data { struct kibi : units::prefix, "Ki"> {}; struct mebi : units::prefix, "Mi"> {}; - struct bit : units::coherent_derived_unit {}; + struct bit : units::named_coherent_derived_unit {}; struct kilobit : units::prefixed_derived_unit {}; - struct byte : units::derived_unit> {}; + struct byte : units::named_derived_unit> {}; struct kilobyte : units::prefixed_derived_unit {}; inline namespace literals { diff --git a/test/unit_test/runtime/text_test.cpp b/test/unit_test/runtime/text_test.cpp index 1ec9ecfa..f7da3571 100644 --- a/test/unit_test/runtime/text_test.cpp +++ b/test/unit_test/runtime/text_test.cpp @@ -46,15 +46,51 @@ TEST_CASE("operator<< on a quantity", "[text][ostream]") SECTION("floating-point representation") { - stream << 72.5kJ; - REQUIRE(stream.str() == "72.5 kJ"); + stream << 1023.5Pa; + REQUIRE(stream.str() == "1023.5 Pa"); } + } - SECTION("unit with a prefix") + SECTION("quantity with a predefined unit + prefix") + { + SECTION("in terms of base units") { stream << 125us; REQUIRE(stream.str() == "125 µs"); } + + SECTION("in terms of derived units") + { + stream << 60kJ; + REQUIRE(stream.str() == "60 kJ"); + } + } + + SECTION("quantity with a deduced unit") + { + SECTION("coherent derived unit") + { + SECTION("acceleration") + { + stream << 20.m / 2s / 1s; + REQUIRE(stream.str() == "10 m/s²"); + } + + SECTION("volume") + { + stream << 2m * 1m * 1m; + REQUIRE(stream.str() == "2 m³"); + } + } + + SECTION("deduced derived unit") + { + SECTION("velocity") + { + stream << 20.km / 2h; + REQUIRE(stream.str() == "10 km/h"); + } + } } SECTION("quantity with a predefined dimension but unknown unit") @@ -68,7 +104,7 @@ TEST_CASE("operator<< on a quantity", "[text][ostream]") SECTION("unit::ratio for a dimension without a special symbol") { stream << 2.cm * 2m * 2m; - REQUIRE(stream.str() == "8 [1/100]m^3"); + REQUIRE(stream.str() == "8 [1/100]m³"); } SECTION("unit::ratio::num != 1 && unit::ratio::den == 1") @@ -131,19 +167,19 @@ TEST_CASE("operator<< on a quantity", "[text][ostream]") SECTION("exp::num == 2 && exp::den == 1 for positive exponent") { stream << 4m * 2s * 2s; - REQUIRE(stream.str() == "16 m⋅s^2"); + REQUIRE(stream.str() == "16 m⋅s²"); } SECTION("exp::num == 2 && exp::den == 1 for negative exponent (first dimension)") { stream << 8.s / 2m / 2m; - REQUIRE(stream.str() == "2 1/m^2⋅s"); + REQUIRE(stream.str() == "2 1/m²⋅s"); } SECTION("exp::num == 2 && exp::den == 1 for negative exponent (not first dimension)") { stream << 8.m / 2kg / 2kg; - REQUIRE(stream.str() == "2 m/kg^2"); + REQUIRE(stream.str() == "2 m/kg²"); } SECTION("fractional positive exponent") diff --git a/test/unit_test/static/cgs_test.cpp b/test/unit_test/static/cgs_test.cpp index 42adada1..ab8e3140 100644 --- a/test/unit_test/static/cgs_test.cpp +++ b/test/unit_test/static/cgs_test.cpp @@ -30,12 +30,12 @@ namespace cgs { using units::centimetre; using units::gram; using units::second; - struct centimetre_per_second : units::deduced_derived_unit {}; - struct gal : units::deduced_derived_unit {}; - struct dyne : units::deduced_derived_unit {}; - struct erg : units::deduced_derived_unit {}; - struct ergps : units::deduced_derived_unit {}; - struct barye : units::deduced_derived_unit {}; + struct centimetre_per_second : units::deduced_derived_unit {}; + struct gal : units::named_deduced_derived_unit {}; + struct dyne : units::named_deduced_derived_unit {}; + struct erg : units::named_deduced_derived_unit {}; + struct ergps : units::named_deduced_derived_unit {}; // TODO make it work for erg and non-named + struct barye : units::named_deduced_derived_unit {}; inline namespace literals { diff --git a/test/unit_test/static/custom_unit_test.cpp b/test/unit_test/static/custom_unit_test.cpp index 34138a8f..95f923ec 100644 --- a/test/unit_test/static/custom_unit_test.cpp +++ b/test/unit_test/static/custom_unit_test.cpp @@ -39,10 +39,10 @@ namespace { struct kibi : units::prefix, "Ki"> {}; - struct bit : units::coherent_derived_unit {}; + struct bit : units::named_coherent_derived_unit {}; struct kilobit : units::prefixed_derived_unit {}; - struct byte : units::derived_unit> {}; + struct byte : units::named_derived_unit> {}; struct kilobyte : units::prefixed_derived_unit {}; inline namespace literals { @@ -71,11 +71,11 @@ namespace { // power spectral density struct power_spectral_density : derived_dimension, units::exp> {}; - struct sq_volt_per_hertz : coherent_derived_unit {}; + struct sq_volt_per_hertz : coherent_derived_unit {}; // amplitude spectral density struct amplitude_spectral_density : derived_dimension, units::exp> {}; - struct volt_per_sqrt_hertz : coherent_derived_unit {}; + struct volt_per_sqrt_hertz : coherent_derived_unit {}; } namespace { diff --git a/test/unit_test/static/unit_test.cpp b/test/unit_test/static/unit_test.cpp index a972bf02..fc52a059 100644 --- a/test/unit_test/static/unit_test.cpp +++ b/test/unit_test/static/unit_test.cpp @@ -48,10 +48,6 @@ namespace { /* ************** BASE DIMENSIONS **************** */ - // time - - static_assert(1h == 3600s); - // length static_assert(1km == 1000m); @@ -69,6 +65,33 @@ namespace { static_assert(5in + 8cm == 207mm); + static_assert(millimetre::symbol == "mm"); + static_assert(centimetre::symbol == "cm"); + static_assert(kilometre::symbol == "km"); + + // mass + + static_assert(1kg == 1000g); + + static_assert(kilogram::symbol == "kg"); + + // time + + static_assert(1h == 3600s); + + static_assert(nanosecond::symbol == "ns"); + static_assert(microsecond::symbol == "µs"); + static_assert(millisecond::symbol == "ms"); + + // current + + // temperature + + // substance + + // luminous intensity + + /* ************** DERIVED DIMENSIONS WITH NAMED UNITS **************** */ // frequency @@ -80,6 +103,12 @@ namespace { static_assert(3.2GHz == 3'200'000'000Hz); static_assert(10Hz * 1min == 600); + static_assert(millihertz::symbol == "mHz"); + static_assert(kilohertz::symbol == "kHz"); + static_assert(megahertz::symbol == "MHz"); + static_assert(gigahertz::symbol == "GHz"); + static_assert(terahertz::symbol == "THz"); + // force static_assert(10kg * 10mps_sq == 100N); @@ -134,6 +163,10 @@ namespace { // static_assert(2000m / 2kmph == 1h); // should not compile static_assert(quantity_cast>(2000m) / 2kmph == 1h); +// static_assert(metre_per_second::symbol == basic_fixed_string("m/s")); + // static_assert(kilometre_per_hour::symbol == basic_fixed_string("km/h")); + + // acceleration static_assert(10mps / 10s == 1mps_sq);