mirror of
https://github.com/mpusz/mp-units.git
synced 2025-06-25 01:01:33 +02:00
feat: add compound assignment operators to cartesian_vector
This commit is contained in:
@ -119,6 +119,54 @@ public:
|
||||
return {-_coordinates_[0], -_coordinates_[1], -_coordinates_[2]};
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
requires requires(T t, U u) {
|
||||
{ t += u } -> std::same_as<T&>;
|
||||
}
|
||||
constexpr cartesian_vector& operator+=(const cartesian_vector<U>& other)
|
||||
{
|
||||
_coordinates_[0] += other[0];
|
||||
_coordinates_[1] += other[1];
|
||||
_coordinates_[2] += other[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
requires requires(T t, U u) {
|
||||
{ t -= u } -> std::same_as<T&>;
|
||||
}
|
||||
constexpr cartesian_vector& operator-=(const cartesian_vector<U>& other)
|
||||
{
|
||||
_coordinates_[0] -= other[0];
|
||||
_coordinates_[1] -= other[1];
|
||||
_coordinates_[2] -= other[2];
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
requires requires(T t, U u) {
|
||||
{ t *= u } -> std::same_as<T&>;
|
||||
}
|
||||
constexpr cartesian_vector& operator*=(const U& value)
|
||||
{
|
||||
_coordinates_[0] *= value;
|
||||
_coordinates_[1] *= value;
|
||||
_coordinates_[2] *= value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename U>
|
||||
requires requires(T t, U u) {
|
||||
{ t /= u } -> std::same_as<T&>;
|
||||
}
|
||||
constexpr cartesian_vector& operator/=(const U& value)
|
||||
{
|
||||
_coordinates_[0] /= value;
|
||||
_coordinates_[1] /= value;
|
||||
_coordinates_[2] /= value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<std::same_as<T> U, typename V>
|
||||
requires requires(U u, V v) { u + v; }
|
||||
[[nodiscard]] friend constexpr auto operator+(const cartesian_vector<U>& lhs, const cartesian_vector<V>& rhs)
|
||||
|
@ -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")
|
||||
|
Reference in New Issue
Block a user