From 079ee8dcbf4b133fdaa987c1e78dd3d1efecf02b Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Thu, 10 Jul 2014 17:26:57 +0000 Subject: [PATCH] math: some new operators for generic vectors, plus unit tests. --- src/lol/math/vector.h | 24 ++++++++++++++++++++++++ test/unit/vector.cpp | 12 ++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/lol/math/vector.h b/src/lol/math/vector.h index 7d95a612..6c3b5466 100644 --- a/src/lol/math/vector.h +++ b/src/lol/math/vector.h @@ -111,6 +111,30 @@ struct vec_t typedef T element; typedef vec_t type; + /* Default constructor, copy constructor, and destructor */ + inline constexpr vec_t() {} + inline vec_t(vec_t const &v) + { + for (int i = 0; i < N; ++i) + m_data[i] = v.m_data[i]; + } + inline ~vec_t() {} + + /* Explicit constructor for type conversion */ + template + explicit inline vec_t(vec_t const &v) + { + for (int i = 0; i < N; ++i) + m_data[i] = (T)v.m_data[i]; + } + + /* Various explicit constructors */ + explicit inline vec_t(T X) + { + for (int i = 0; i < N; ++i) + m_data[i] = X; + } + inline T& operator[](size_t n) { return m_data[n]; } inline T const& operator[](size_t n) const { return m_data[n]; } diff --git a/test/unit/vector.cpp b/test/unit/vector.cpp index 8380d8ad..c0269f5c 100644 --- a/test/unit/vector.cpp +++ b/test/unit/vector.cpp @@ -225,6 +225,18 @@ LOLUNIT_FIXTURE(VectorTest) LOLUNIT_ASSERT_DOUBLES_EQUAL(length(orthonormal(b)), 1.f, 1e-6f); LOLUNIT_ASSERT_DOUBLES_EQUAL(length(orthonormal(c)), 1.f, 1e-6f); } + + LOLUNIT_TEST(LargeVectors) + { + vec_t v0(0); + vec_t v1(1); + vec_t v2(2); + vec_t v3(3); + + auto va = v0 + v3; + auto vb = v1 + v2; + LOLUNIT_ASSERT(va == vb); + } }; } /* namespace lol */