Commits

mitsuhiko  committed 504bd5c

More tests and fixes for vectors.

  • Participants
  • Parent commits 827507d

Comments (0)

Files changed (2)

File neglect/cpplib/include/neglect/_vector2.hpp

         vector<T, 2> operator/(ScalarType scalar) const
         {
             assert(scalar);
-            ScalarType inv = static_cast<ScalarType>(1) / scalar;
-            return vector<T, 2>(x * inv, y * inv);
+            return vector<T, 2>(x / scalar, y / scalar);
         }
 
         const vector<T, 2> &operator+() const
                                       const vector<T, 2> &vec)
         {
             assert(scalar);
-            ScalarType inv = static_cast<ScalarType>(1) / scalar;
-            return vector<T, 2>(vec.x * inv, vec.y * inv);
+            return vector<T, 2>(vec.x / scalar, vec.y / scalar);
         }
 
         // -- arithmetic assignment operators
         {
             x += other.x;
             y += other.y;
+            return *this;
         }
 
         vector<T, 2> &operator-=(const vector<T, 2> &other)
         {
             x -= other.x;
             y -= other.y;
+            return *this;
         }
 
         vector<T, 2> &operator*=(const vector<T, 2> &other)
         {
             x *= other.x;
             y *= other.y;
+            return *this;
         }
 
         template <class ScalarType>
         vector<T, 2> &operator*=(ScalarType scalar)
         {
-            x *= other;
-            y *= other;
+            x *= scalar;
+            y *= scalar;
+            return *this;
         }
 
         vector<T, 2> &operator/=(const vector<T, 2> &other)
             assert(other.x && other.y);
             x /= other.x;
             y /= other.y;
+            return *this;
         }
 
         template <class ScalarType>
         vector<T, 2> &operator/=(ScalarType scalar)
         {
             assert(scalar);
-            ScalarType inv = static_cast<ScalarType>(1) / scalar;
-            x *= inv;
-            y *= inv;
+            x /= scalar;
+            y /= scalar;
+            return *this;
         }
 
         // -- helper methods

File neglect/tests/src/test_vectors.cpp

         vector<float, 2> vec2(1.0f, 2.0f);
         vector<float, 2> vec3(x);
 
+        NUT_CHECK(!vec1);
+        NUT_CHECK(vec2);
+        NUT_CHECK(static_cast<bool>(vec2));
+
         NUT_REQUIRE_EQUAL(vec1.size(), 2);
         NUT_REQUIRE_EQUAL(vec2.size(), 2);
         NUT_REQUIRE_EQUAL(vec3.size(), 2);
         NUT_CHECK_EQUAL(vec3.y, 6.0f);
     }
 
+    NUT_TESTCASE(modify_and_get)
+    {
+        vec2 v1;
+
+        v1 = vec2(1.0f, 1.0f);
+        NUT_CHECK_EQUAL(v1, vec2(1.0f, 1.0f));
+
+        v1[0] = 0.0f;
+        NUT_CHECK_EQUAL(v1, vec2(0.0f, 1.0f));
+        v1.y = 2.0f;
+        NUT_CHECK_EQUAL(v1, vec2(0.0f, 2.0f));
+        v1.s = 1.0f;
+        NUT_CHECK_EQUAL(v1, vec2(1.0f, 2.0f));
+        v1[1] = 3.0f;
+        NUT_CHECK_EQUAL(v1, vec2(1.0f, 3.0f));
+
+        NUT_CHECK_EQUAL(v1.x, 1.0f);
+        NUT_CHECK_EQUAL(v1.s, 1.0f);
+        NUT_CHECK_EQUAL(v1.y, 3.0f);
+        NUT_CHECK_EQUAL(v1.t, 3.0f);
+    }
+
     NUT_TESTCASE(basic_math)
     {
         vec2 v1(1.5f, -2.5f);
         vec2 v2(1.0f, 2.0f);
+        vec2 v;
 
         NUT_CHECK_EQUAL(v1 + v2, vec2(2.5f, -0.5f));
+        v = v1;
+        v += v2;
+        NUT_CHECK_EQUAL(v, vec2(2.5f, -0.5f));
+
         NUT_CHECK_EQUAL(v1 - v2, vec2(0.5f, -4.5f));
+        v = v1;
+        v -= v2;
+        NUT_CHECK_EQUAL(v, vec2(0.5f, -4.5f));
+
         NUT_CHECK_EQUAL(v1 * v2, vec2(1.5f, -5.0f));
+        v = v1;
+        v *= v2;
+        NUT_CHECK_EQUAL(v, vec2(1.5f, -5.0f));
+
         NUT_CHECK_EQUAL(v1 / v2, vec2(1.5f, -1.25f));
+        v = v1;
+        v /= v2;
+        NUT_CHECK_EQUAL(v, vec2(1.5f, -1.25f));
+
+        NUT_CHECK_EQUAL(v2 / 2, vec2(0.5f, 1.0f));
+        v = v2;
+        v /= 2;
+        NUT_CHECK_EQUAL(v, vec2(0.5f, 1.0f));
+
+        NUT_CHECK_EQUAL(v1 * 2, vec2(3.0f, -5.0f));
+        NUT_CHECK_EQUAL(2 * v1, vec2(3.0f, -5.0f));
+        v = v1;
+        v *= 2;
+        NUT_CHECK_EQUAL(v, vec2(3.0f, -5.0f));
+
         NUT_CHECK_EQUAL(-v1, vec2(-1.5f, 2.5f));
-
         NUT_CHECK_EQUAL(v1.norm(), 8.5f);
         NUT_CHECK_EQUAL(v1.length(), 2.9154759f);
-
         NUT_CHECK(&v1 == &+v1);
     }
 
         vector<float, 3> vec2(1.0f, 2.0f, 3.0f);
         vector<float, 3> vec3(x);
 
+        NUT_CHECK(!vec1);
+        NUT_CHECK(vec2);
+        NUT_CHECK(static_cast<bool>(vec2));
+
         NUT_REQUIRE_EQUAL(vec1.size(), 3);
         NUT_REQUIRE_EQUAL(vec2.size(), 3);
         NUT_REQUIRE_EQUAL(vec3.size(), 3);
         NUT_CHECK_EQUAL(vec3.y, 6.0f);
         NUT_CHECK_EQUAL(vec3.z, 9.0f);
     }
+
+    NUT_TESTCASE(modify_and_get)
+    {
+        vec3 v1;
+
+        v1 = vec3(1.0f, 1.0f, 1.0f);
+        NUT_CHECK_EQUAL(v1, vec3(1.0f, 1.0f, 1.0f));
+
+        v1[0] = 0.0f;
+        NUT_CHECK_EQUAL(v1, vec3(0.0f, 1.0f, 1.0f));
+        v1.y = 2.0f;
+        NUT_CHECK_EQUAL(v1, vec3(0.0f, 2.0f, 1.0f));
+        v1.s = 1.0f;
+        NUT_CHECK_EQUAL(v1, vec3(1.0f, 2.0f, 1.0f));
+        v1[1] = 3.0f;
+        v1.b = 2.0f;
+        NUT_CHECK_EQUAL(v1, vec3(1.0f, 3.0f, 2.0f));
+
+        NUT_CHECK_EQUAL(v1.x, 1.0f);
+        NUT_CHECK_EQUAL(v1.s, 1.0f);
+        NUT_CHECK_EQUAL(v1.r, 1.0f);
+        NUT_CHECK_EQUAL(v1.y, 3.0f);
+        NUT_CHECK_EQUAL(v1.t, 3.0f);
+        NUT_CHECK_EQUAL(v1.g, 3.0f);
+        NUT_CHECK_EQUAL(v1.z, 2.0f);
+        NUT_CHECK_EQUAL(v1.p, 2.0f);
+        NUT_CHECK_EQUAL(v1.b, 2.0f);
+    }
 }