Browse Source

Add operators "equals" and "not equals" to Vector2 class

Patrick-Christopher Mattulat 2 years ago
parent
commit
25e4385af3

+ 8 - 3
include/ls-math/vector/Vector2.hpp

@@ -24,11 +24,16 @@ namespace ls::math::vector
 
       // arithmetic operations
 
-      ls::math::vector::Vector2 operator+(Vector2 _vector);
-      ls::math::vector::Vector2 operator-(Vector2 _vector);
-      ls::math::core::type::vector_scalar operator*(Vector2 _vector);
+      ls::math::vector::Vector2 operator+(ls::math::vector::Vector2 _vector);
+      ls::math::vector::Vector2 operator-(ls::math::vector::Vector2 _vector);
+      ls::math::core::type::vector_scalar operator*(ls::math::vector::Vector2 _vector);
       ls::math::vector::Vector2 operator*(double _value);
 
+      // comparison operations
+
+      bool operator==(const Vector2& _vector);
+      bool operator!=(const Vector2& _vector);
+
       // additional functionality
 
       ls::math::core::type::vector2_component getX();

+ 11 - 1
source/ls-math/vector/Vector2.cpp

@@ -15,7 +15,7 @@ ls::math::vector::Vector2::Vector2(const ls::math::core::type::vector2_component
   this->components[1] = _y;
 }
 
-ls::math::vector::Vector2 ls::math::vector::Vector2::operator+(Vector2 _vector)
+ls::math::vector::Vector2 ls::math::vector::Vector2::operator+(ls::math::vector::Vector2 _vector)
 {
   ls::math::vector::Vector2 calculatedVector{0, 0};
   calculatedVector.setX(this->components[0] + _vector.getX());
@@ -47,6 +47,16 @@ ls::math::vector::Vector2 ls::math::vector::Vector2::operator*(double _value)
   return calculatedVector;
 }
 
+bool ls::math::vector::Vector2::operator==(const ls::math::vector::Vector2 &_vector)
+{
+  return this->components[0] == _vector.components[0] && this->components[1] == _vector.components[1];
+}
+
+bool ls::math::vector::Vector2::operator!=(const ls::math::vector::Vector2 &_vector)
+{
+  return this->components[0] != _vector.components[0] || this->components[1] != _vector.components[1];
+}
+
 ls::math::core::type::vector2_component ls::math::vector::Vector2::getX()
 {
   return this->components[0];

+ 20 - 0
test/cases/vector/Vector2Test.cpp

@@ -59,6 +59,26 @@ namespace
     ASSERT_FLOAT_EQ(6.0f, c.getY());
   }
 
+  TEST_F(Vector2Test, operator_equals)
+  {
+    ASSERT_TRUE(Vector2(3, 4) == Vector2(3, 4));
+  }
+
+  TEST_F(Vector2Test, operator_equals_not_equals)
+  {
+    ASSERT_FALSE(Vector2(3, 8) == Vector2(3, 4));
+  }
+
+  TEST_F(Vector2Test, operator_not_equals)
+  {
+    ASSERT_TRUE(Vector2(3, 5) != Vector2(3, 4));
+  }
+
+  TEST_F(Vector2Test, operator_not_equals_equals)
+  {
+    ASSERT_FALSE(Vector2(3, 4) != Vector2(3, 4));
+  }
+
   TEST_F(Vector2Test, getX)
   {
     Vector2 a{3, 4};