From a7cf015a3ecdc3ff503f7dadd9462a469e342ddd Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Thu, 14 Nov 2024 20:38:50 +0100 Subject: [PATCH] feat: add compound assignment operators to `cartesian_vector` --- src/core/include/mp-units/cartesian_vector.h | 48 ++++++++++++++++++++ test/runtime/cartesian_vector_test.cpp | 38 ++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/src/core/include/mp-units/cartesian_vector.h b/src/core/include/mp-units/cartesian_vector.h index 76f8a91e..af5e7e36 100644 --- a/src/core/include/mp-units/cartesian_vector.h +++ b/src/core/include/mp-units/cartesian_vector.h @@ -119,6 +119,54 @@ public: return {-_coordinates_[0], -_coordinates_[1], -_coordinates_[2]}; } + template + requires requires(T t, U u) { + { t += u } -> std::same_as; + } + constexpr cartesian_vector& operator+=(const cartesian_vector& other) + { + _coordinates_[0] += other[0]; + _coordinates_[1] += other[1]; + _coordinates_[2] += other[2]; + return *this; + } + + template + requires requires(T t, U u) { + { t -= u } -> std::same_as; + } + constexpr cartesian_vector& operator-=(const cartesian_vector& other) + { + _coordinates_[0] -= other[0]; + _coordinates_[1] -= other[1]; + _coordinates_[2] -= other[2]; + return *this; + } + + template + requires requires(T t, U u) { + { t *= u } -> std::same_as; + } + constexpr cartesian_vector& operator*=(const U& value) + { + _coordinates_[0] *= value; + _coordinates_[1] *= value; + _coordinates_[2] *= value; + return *this; + } + + template + requires requires(T t, U u) { + { t /= u } -> std::same_as; + } + constexpr cartesian_vector& operator/=(const U& value) + { + _coordinates_[0] /= value; + _coordinates_[1] /= value; + _coordinates_[2] /= value; + return *this; + } + template U, typename V> requires requires(U u, V v) { u + v; } [[nodiscard]] friend constexpr auto operator+(const cartesian_vector& lhs, const cartesian_vector& rhs) diff --git a/test/runtime/cartesian_vector_test.cpp b/test/runtime/cartesian_vector_test.cpp index 836451a8..a7d3cb4a 100644 --- a/test/runtime/cartesian_vector_test.cpp +++ b/test/runtime/cartesian_vector_test.cpp @@ -114,6 +114,44 @@ TEST_CASE("cartesian_vector operations", "[vector]") } } + SECTION("cartesian_vector compound assignment addition") + { + cartesian_vector v1{1.0, 2.0, 3.0}; + cartesian_vector v2{4.0, 5.0, 6.0}; + v1 += v2; + REQUIRE(v1[0] == 5.0); + REQUIRE(v1[1] == 7.0); + REQUIRE(v1[2] == 9.0); + } + + SECTION("cartesian_vector compound assignment subtraction") + { + cartesian_vector v1{4.0, 5.0, 6.0}; + cartesian_vector v2{1.0, 2.0, 3.0}; + v1 -= v2; + REQUIRE(v1[0] == 3.0); + REQUIRE(v1[1] == 3.0); + REQUIRE(v1[2] == 3.0); + } + + SECTION("cartesian_vector compound assignment scalar multiplication") + { + cartesian_vector v{1.0, 2.0, 3.0}; + v *= 2.0; + REQUIRE(v[0] == 2.0); + REQUIRE(v[1] == 4.0); + REQUIRE(v[2] == 6.0); + } + + SECTION("cartesian_vector compound assignment scalar division") + { + cartesian_vector v{2.0, 4.0, 6.0}; + v /= 2.0; + REQUIRE(v[0] == 1.0); + REQUIRE(v[1] == 2.0); + REQUIRE(v[2] == 3.0); + } + SECTION("cartesian_vector addition") { SECTION("double + double")