// 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. #include "units/math.h" #include "units/physical/si/area.h" #include "units/physical/si/frequency.h" #include "units/physical/si/speed.h" #include "units/physical/si/volume.h" #include "units/physical/us/length.h" #include #include namespace { using namespace units; using namespace units::physical::si; // class invariants // constexpr quantity error(0); // should not compile (unit of a different dimension) // constexpr quantity> error(0); // should not compile (quantity used as Rep) // constexpr quantity error(0); // should not compile (reordered arguments) // constexpr quantity, int> error(0); // should not compile (negative unit ratio) // member types static_assert(is_same_v::rep, int>); static_assert(is_same_v::rep, double>); static_assert(is_same_v::unit, metre>); static_assert(is_same_v::unit, kilometre>); // constructors static_assert(length().count() == 0); constexpr length km{1000}; static_assert(km.count() == 1000); static_assert(length(km).count() == km.count()); static_assert(length(1).count() == 1); // static_assert(length(1.0).count() == 1); // should not compile (truncating conversion) static_assert(length(1.0).count() == 1.0); static_assert(length(1).count() == 1.0); static_assert(length(3.14).count() == 3.14); static_assert(length(km).count() == 1000); // static_assert(length(length(3.14)).count() == 3); // should not compile (truncating conversion) static_assert(length(1000.0q_m).count() == 1000.0); static_assert(length(km).count() == 1000.0); static_assert(length(1q_km).count() == 1000); // static_assert(length(1q_s).count() == 1); // should not compile (different dimensions) //static_assert(length(1010q_m).count() == 1); // should not compile (truncating conversion) // assignment operator static_assert([]() { length l1(1), l2(2); return l2 = l1; }().count() == 1); // static member functions static_assert(length::zero().count() == 0); static_assert(length::min().count() == std::numeric_limits::lowest()); static_assert(length::max().count() == std::numeric_limits::max()); static_assert(length::zero().count() == 0.0); static_assert(length::min().count() == std::numeric_limits::lowest()); static_assert(length::max().count() == std::numeric_limits::max()); // unary member operators static_assert((+km).count() == 1000); static_assert((-km).count() == -1000); static_assert((+(-km)).count() == -1000); static_assert((-(-km)).count() == 1000); // binary member operators static_assert([](auto v) { auto vv = v++; return std::make_pair(v, vv); }(km) == std::make_pair(length(1001), length(1000))); static_assert([](auto v) { auto vv = ++v; return std::make_pair(v, vv); }(km) == std::make_pair(length(1001), length(1001))); static_assert([](auto v) { auto vv = v--; return std::make_pair(v, vv); }(km) == std::make_pair(length(999), length(1000))); static_assert([](auto v) { auto vv = --v; return std::make_pair(v, vv); }(km) == std::make_pair(length(999), length(999))); // compound assignment static_assert((1q_m += 1q_m).count() == 2); static_assert((2q_m -= 1q_m).count() == 1); static_assert((1q_m *= 2).count() == 2); static_assert((2q_m /= 2).count() == 1); static_assert((7q_m %= 2).count() == 1); static_assert((7q_m %= 2q_m).count() == 1); // static_assert((7.m %= 2.).count() == 1); // should not compile (operation not allowed for floating-point types) // static_assert((7.m %= 2).count() == 1); // should not compile (operation not allowed for floating-point types) // static_assert((7q_m %= 2.).count() == 1); // should not compile (operation not allowed for floating-point types) static_assert((7q_m %= 2q_m).count() == 1); // static_assert((7.m %= 2.m).count() == 1); // should not compile (operation not allowed for floating-point types) // static_assert((7.m %= 2q_m).count() == 1); // should not compile (operation not allowed for floating-point types) // static_assert((7q_m %= 2.m).count() == 1); // should not compile (operation not allowed for floating-point types) // static_assert(2q_m += 3.5q_m); // should not compile static_assert((2.5q_m += 3q_m).count() == 5.5); static_assert((2.5q_m += 3.5q_m).count() == 6); // static_assert(2q_m *= 3.5); // should not compile static_assert((2.5q_m *= 3).count() == 7.5); static_assert((2.5q_m *= 3.5).count() == 8.75); // non-member arithmetic operators static_assert(is_same_v() + length()), length>); static_assert(is_same_v() + length()), length>); static_assert( is_same_v() + length()), length>); static_assert(is_same_v() - length()), length>); static_assert( is_same_v() - length()), length>); static_assert(is_same_v() * 1.0), length>); static_assert(is_same_v()), length>); static_assert( is_same_v() * physical::si::time()), length>); static_assert( is_same_v() * physical::si::time()), length, int>>); static_assert(is_same_v() * physical::si::time()), quantity, units::exp>, scaled_unit>>); static_assert(is_same_v()), frequency>); static_assert(is_same_v()), frequency, int>>); static_assert(is_same_v()), physical::si::time>); static_assert(is_same_v()), quantity>, scaled_unit>>); static_assert(is_same_v() / 1.0), length>); static_assert(is_same_v() / length()), double>); static_assert(is_same_v() / length()), double>); static_assert( is_same_v() / physical::si::time()), speed>); static_assert( is_same_v() / physical::si::time()), speed>>); static_assert(is_same_v() / length()), quantity, units::exp>, scaled_unit>>); static_assert(is_same_v() % short(1)), length>); static_assert(is_same_v() % length(1)), length>); static_assert((1q_m + km).count() == 1001); static_assert((1q_m + 1q_km).count() == 1001); static_assert((km - 1q_m).count() == 999); static_assert((1q_km - 1q_m).count() == 999); static_assert((2q_m * 2).count() == 4); static_assert((3 * 3q_m).count() == 9); static_assert((4q_m / 2).count() == 2); static_assert(4q_m / 2q_m == 2); static_assert(4q_km / 2000q_m == 2); static_assert((7q_m % 2).count() == 1); static_assert((7q_m % 2q_m).count() == 1); static_assert((7q_km % 2000q_m).count() == 1000); static_assert((10q_km2 * 10q_km2) / 50q_km2 == 2q_km2); // comparators static_assert(2q_m + 1q_m == 3q_m); static_assert(!(2q_m + 2q_m == 3q_m)); static_assert(2q_m + 2q_m != 3q_m); static_assert(!(2q_m + 2q_m != 4q_m)); static_assert(2q_m > 1q_m); static_assert(!(1q_m > 1q_m)); static_assert(1q_m < 2q_m); static_assert(!(2q_m < 2q_m)); static_assert(2q_m >= 1q_m); static_assert(2q_m >= 2q_m); static_assert(!(2q_m >= 3q_m)); static_assert(1q_m <= 2q_m); static_assert(2q_m <= 2q_m); static_assert(!(3q_m <= 2q_m)); static_assert(3q_m == 3.0q_m); static_assert(3q_m != 3.14q_m); static_assert(2q_m > 1.0q_m); static_assert(1.0q_m < 2q_m); static_assert(2.0q_m >= 1q_m); static_assert(1q_m <= 2.0q_m); static_assert(1000q_m == 1q_km); static_assert(1001q_m != 1q_km); static_assert(1001q_m > 1q_km); static_assert(999q_m < 1q_km); static_assert(1000q_m >= 1q_km); static_assert(1000q_m <= 1q_km); // alias units static_assert(2q_l + 2q_ml == 2002q_ml); static_assert(2q_l + 2q_ml == 2002q_cm3); static_assert(2q_l + 2q_cm3 == 2002q_ml); static_assert(2q_dm3 + 2q_cm3 == 2002q_ml); // is_quantity static_assert(Quantity>); // common_quantity static_assert(is_same_v, length>, length>); static_assert( is_same_v, length>, length>); static_assert(is_same_v, length>, length>); // common_type using namespace units::physical::us::literals; static_assert(std::equality_comparable); static_assert(std::equality_comparable_with); static_assert(0q_m == 0q_ft_us); static_assert(std::equality_comparable_with); // quantity_cast static_assert(is_same_v>(2q_km))::unit, metre>); static_assert(quantity_cast>(2q_km).count() == 2000); static_assert(quantity_cast>(2000q_m).count() == 2); static_assert(quantity_cast>(1.23q_m).count() == 1); static_assert(quantity_cast(2q_km).count() == 2000); static_assert(quantity_cast(2000q_m).count() == 2); static_assert(quantity_cast(1.23q_m).count() == 1); // time // static_assert(1q_s == 1q_m); // should not compile (different dimensions) static_assert(1q_h == 3600q_s); // length static_assert(1q_km == 1000q_m); static_assert(1q_km + 1q_m == 1001q_m); static_assert(10q_km / 5q_km == 2); static_assert(10q_km / 2 == 5q_km); // speed static_assert(10q_m / 5q_s == 2q_m_per_s); static_assert(10 / 5q_s * 1q_m == 2q_m_per_s); static_assert(1q_km / 1q_s == 1000q_m_per_s); static_assert(2q_km_per_h * 2q_h == 4q_km); static_assert(2q_km / 2q_km_per_h == 1q_h); static_assert(is_same_v(2q_m)), decltype(4q_m2)>); } // namespace