From 9f9397ffc285a354a136f46b013e3e3270b885d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johel=20Ernesto=20Guerrero=20Pe=C3=B1a?= Date: Sat, 5 Sep 2020 21:41:02 -0400 Subject: [PATCH] test: convert comments to code --- test/unit_test/static/quantity_point_test.cpp | 2 +- test/unit_test/static/quantity_test.cpp | 73 ++++++++++++------- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/test/unit_test/static/quantity_point_test.cpp b/test/unit_test/static/quantity_point_test.cpp index 424d785f..c03ba222 100644 --- a/test/unit_test/static/quantity_point_test.cpp +++ b/test/unit_test/static/quantity_point_test.cpp @@ -47,7 +47,7 @@ concept invalid_types = requires !requires { typename quantity; }; // reordered arguments }; -static_assert(invalid_types); +static_assert(invalid_types); // member types diff --git a/test/unit_test/static/quantity_test.cpp b/test/unit_test/static/quantity_test.cpp index 3e4b20f4..3e80b688 100644 --- a/test/unit_test/static/quantity_test.cpp +++ b/test/unit_test/static/quantity_test.cpp @@ -36,11 +36,18 @@ 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) +template +concept invalid_types = requires +{ + !requires { typename quantity; }; // unit of a different dimension + !requires { typename quantity>; }; // quantity used as Rep + !requires { typename quantity; }; // reordered arguments +}; + +static_assert(invalid_types); + // member types static_assert(is_same_v::rep, int>); @@ -56,18 +63,21 @@ 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(!std::is_constructible_v, double>); // 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(!std::is_constructible_v, + length>); // 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) +static_assert(!std::is_constructible_v, + physical::si::time>); // different dimensions +static_assert(!std::is_constructible_v, + length>); // truncating conversion // assignment operator @@ -89,24 +99,22 @@ 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))); + return std::pair(v, vv); +}(km) == std::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))); + return std::pair(v, vv); +}(km) == std::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))); + return std::pair(v, vv); +}(km) == std::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))); + return std::pair(v, vv); +}(km) == std::pair(length(999), length(999))); // compound assignment @@ -116,22 +124,29 @@ 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); +// operations not allowed for the respective quantities +template +concept invalid_compound_assignments = requires() +{ + !requires(length l) { l %= 2.; }; + !requires(length l) { l %= 2; }; + !requires(length l) { l %= 2.; }; + !requires(length l) { l %= 2.q_m; }; + !requires(length l) { l %= 2q_m; }; + !requires(length l) { l %= 2.q_m; }; + !requires(length l) { l += 3.5q_m; }; + !requires(length l) { l *= 3.5q_m; }; +}; + +static_assert(invalid_compound_assignments); + // non-member arithmetic operators static_assert(is_same_v() + length()), length>); @@ -253,9 +268,13 @@ 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); +template +constexpr bool no_crossdimensional_equality = !requires { 1q_s == length(1); }; + +static_assert(no_crossdimensional_equality); + // length static_assert(1q_km == 1000q_m);