From 5097096915e1b94419447621958d0bc79fc4afbb Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Thu, 7 Nov 2024 18:42:20 +0100 Subject: [PATCH] test: lots of tests for complex quantities --- test/static/quantity_test.cpp | 111 +++++++++++++++++++++++++++++++++- 1 file changed, 110 insertions(+), 1 deletion(-) diff --git a/test/static/quantity_test.cpp b/test/static/quantity_test.cpp index adc3cd41e..afcac7d7d 100644 --- a/test/static/quantity_test.cpp +++ b/test/static/quantity_test.cpp @@ -53,6 +53,9 @@ namespace { using namespace mp_units; using namespace mp_units::si::unit_symbols; +#if MP_UNITS_HOSTED +using namespace std::complex_literals; +#endif ////////////////////////////// // quantity class invariants @@ -70,6 +73,10 @@ concept invalid_types = requires { requires !requires { typename Q; }; // bool is not a valid representation type requires !requires { typename Q>; }; // quantity used as Rep requires !requires { typename Q; }; // vector representation expected +#if MP_UNITS_HOSTED + requires !requires { typename Q>; }; // incompatible character + requires !requires { typename Q; }; // incompatible character +#endif }; static_assert(invalid_types); @@ -176,6 +183,17 @@ static_assert(std::convertible_to, quantity, quantity>); static_assert(std::convertible_to, quantity>); +#if MP_UNITS_HOSTED +static_assert(std::constructible_from>, + quantity>>); +static_assert(std::convertible_to>, + quantity>>); +static_assert(std::constructible_from>, + quantity>>); +static_assert(std::convertible_to>, + quantity>>); +#endif + // conversion between different quantities not allowed static_assert(!std::constructible_from, quantity>); static_assert(!std::convertible_to, quantity>); @@ -235,6 +253,12 @@ static_assert(!std::convertible_to, double>); static_assert(std::constructible_from>); static_assert(!std::convertible_to, double>); static_assert(std::constructible_from>); +#if MP_UNITS_HOSTED +static_assert(!std::convertible_to>, std::complex>); +static_assert(std::constructible_from, quantity>>); +static_assert(!std::convertible_to, std::complex>); +static_assert(std::constructible_from, quantity>); +#endif /////////////////////////////////// @@ -278,7 +302,6 @@ static_assert((1. * rad + 1. * deg).in(rad) != 0 * rad); static_assert((1. * rad + 1. * deg).in(deg) != 0 * deg); #if MP_UNITS_HOSTED -using namespace std::complex_literals; static_assert(((2.f + 1if) * isq::voltage_phasor[V]).in(mV).numerical_value_in(mV) == 2000.f + 1000if); static_assert(((2.f + 1if) * isq::voltage_phasor[V]).in(mV).numerical_value_in(V) == 2.f + 1if); static_assert(((2. + 1i) * isq::voltage_phasor[V]).in(mV).numerical_value_in(mV) == 2000. + 1000i); @@ -381,6 +404,9 @@ static_assert(quantity{123}.unit == one); static_assert(quantity{123}.quantity_spec == kind_of); #if MP_UNITS_HOSTED +static_assert(quantity{123. + 1i}.unit == one); +static_assert(quantity{123. + 1i}.quantity_spec == kind_of); + using namespace std::chrono_literals; static_assert(std::is_same_v); static_assert(std::is_same_v); @@ -457,6 +483,21 @@ static_assert((1 * m *= 2 * one).numerical_value_in(m) == 2); static_assert((2 * m /= 2 * one).numerical_value_in(m) == 1); static_assert((7 * m %= 2 * m).numerical_value_in(m) == 1); +#if MP_UNITS_HOSTED +static_assert(((1. + 1i) * V += (1. + 1i) * V).numerical_value_in(V) == 2. + 2i); +static_assert(((2. + 2i) * V -= (1. + 1i) * V).numerical_value_in(V) == 1. + 1i); +static_assert(((1. + 1i) * V += 1. * V).numerical_value_in(V) == 2. + 1i); +static_assert(((2. + 2i) * V -= 1. * V).numerical_value_in(V) == 1. + 2i); +static_assert(((1. + 1i) * V *= 2.).numerical_value_in(V) == 2. + 2i); +static_assert(((2. + 2i) * V /= 2.).numerical_value_in(V) == 1. + 1i); +static_assert(((1. + 1i) * V *= 2. * one).numerical_value_in(V) == 2. + 2i); +static_assert(((2. + 2i) * V /= 2. * one).numerical_value_in(V) == 1. + 1i); +static_assert(((1. + 1i) * V *= 2. + 1i).numerical_value_in(V) == (1. + 1i) * (2. + 1i)); +static_assert(((2. + 2i) * V /= 2. + 1i).numerical_value_in(V) == (2. + 2i) / (2. + 1i)); +static_assert(((1. + 1i) * V *= (2. + 1i) * one).numerical_value_in(V) == (1. + 1i) * (2. + 1i)); +static_assert(((2. + 2i) * V /= (2. + 1i) * one).numerical_value_in(V) == (2. + 2i) / (2. + 1i)); +#endif + // different representation types static_assert((2.5 * m += 3 * m).numerical_value_in(m) == 5.5); static_assert((5.5 * m -= 3 * m).numerical_value_in(m) == 2.5); @@ -469,6 +510,10 @@ static_assert((7.5 * m /= 3 * one).numerical_value_in(m) == 2.5); static_assert((1 * m += 1 * km).numerical_value_in(m) == 1001); static_assert((2000 * m -= 1 * km).numerical_value_in(m) == 1000); static_assert((3500 * m %= 1 * km).numerical_value_in(m) == 500); +#if MP_UNITS_HOSTED +static_assert(((1000. + 1000i) * V += (1. + 1i) * kV).numerical_value_in(V) == 2000. + 2000i); +static_assert(((2000. + 2000i) * V -= (1. + 1i) * kV).numerical_value_in(V) == 1000. + 1000i); +#endif // convertible quantity types static_assert((isq::length(1 * m) += isq::height(1 * m)).numerical_value_in(m) == 2); @@ -530,6 +575,11 @@ concept invalid_compound_assignments = requires() { requires !requires(Q l) { l %= 2 * m; }; requires !requires(Q l) { l %= 2. * m; }; +#if MP_UNITS_HOSTED + // modulo operations on a complex representation type not allowed + requires !requires(Q> l) { l %= (2. + 2i) * V; }; +#endif + // no unit constants requires !requires(Q l) { l += m; }; requires !requires(Q l) { l -= m; }; @@ -558,6 +608,11 @@ concept invalid_binary_operations = requires { requires !requires(Q a, Q b) { a % b; }; requires !requires(Q a, Q b) { b % a; }; +#if MP_UNITS_HOSTED + // no complex modulo + requires !requires(Q> a) { a % (2. + 2i) * V; }; +#endif + // unit constants requires !requires { Q(1) + m; }; requires !requires { Q(1) - m; }; @@ -862,6 +917,19 @@ static_assert(4 / (2 * one) == 2 * one); static_assert(4 * one / 2 == 2 * one); static_assert(4 * one % (2 * one) == 0 * one); +#if MP_UNITS_HOSTED +static_assert(((3. + 3i) * one *= (2. + 2i) * one) == (3. + 3i) * (2. + 2i) * one); +static_assert(((6. + 6i) * one /= (2. + 2i) * one) == (6. + 6i) / (2. + 2i) * one); +static_assert((1. + 1i) * one + (1. + 1i) * one == (2. + 2i) * one); +static_assert((2. + 2i) * one - (1. + 1i) * one == (1. + 1i) * one); +static_assert((2. + 2i) * one * (2. * one) == (4. + 4i) * one); +static_assert((2. + 2i) * ((2. + 2i) * one) == (2. + 2i) * (2. + 2i) * one); +static_assert((2. + 2i) * one * (2. + 2i) == (2. + 2i) * (2. + 2i) * one); +static_assert((4. + 4i) * one / ((2. + 2i) * one) == (4. + 4i) / (2. + 2i) * one); +static_assert((4. + 4i) / ((2. + 2i) * one) == (4. + 4i) / (2. + 2i) * one); +static_assert((4. + 4i) * one / (2. + 2i) == (4. + 4i) / (2. + 2i) * one); +#endif + static_assert(1 * one + 1 == 2); static_assert(1 + 1 * one == 2); static_assert(2 * one - 1 == 1); @@ -873,6 +941,13 @@ static_assert(2.23 - 1 * one == 1.23); static_assert(4 * one % (2) == 0); static_assert(4 % (2 * one) == 0); +#if MP_UNITS_HOSTED +static_assert((1. + 1i) * one + (1. + 1i) == 2. + 2i); +static_assert((1. + 1i) + (1. + 1i) * one == 2. + 2i); +static_assert(2. * one - (1. + 1i) == (1. - 1i)); +static_assert(2. - (1. + 1i) * one == (1. - 1i)); +#endif + static_assert(2 * rad * (2 * rad) == 4 * pow<2>(rad)); // modulo arithmetics @@ -912,6 +987,36 @@ static_assert(is_same_v constexpr bool invalid_comparison = !requires { 2 * R1 == 2 * R2; } && !requires { 2 * R2 == 2 * R1; }; static_assert(invalid_comparison); +#if MP_UNITS_HOSTED +static_assert((1. + 1i) * one == 1. + 1i); +static_assert(1. + 1i != (2. + 2i) * one); +#endif /////////////////////// // ordering operators