From d0dbbde15903050e7ef0861abef615542822757c Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sun, 6 May 2012 20:52:40 +0000 Subject: [PATCH] core: allow to concatenate arrays, and add unit tests for that. --- src/array.h | 52 +++++++++++++++++++++++++++++++++--------- test/unit/array.cpp | 55 +++++++++++++++++++++++++++++++++++++-------- 2 files changed, 88 insertions(+), 19 deletions(-) diff --git a/src/array.h b/src/array.h index b66fd659..5b23fa36 100644 --- a/src/array.h +++ b/src/array.h @@ -31,7 +31,7 @@ namespace lol * m_count are allocated. The rest is uninitialised memory. */ -template class ArrayBase +template class ArrayBase { public: typedef T Element; @@ -87,6 +87,29 @@ public: return *this; } + ArrayBase& operator+=(ARRAY const &that) + { + int todo = that.m_count; + Reserve(m_count + that.m_count); + for (int i = 0; i < todo; i++) + *this << that[i]; + return *this; + } + + ARRAY operator+(ARRAY const &that) const + { + /* FIXME: upon return, this makes a copy of the temporary object; + * use either C++11 move semantics, or add a special flag to the + * object indicating we're a temporary about to be destroyed */ + ARRAY ret; + ret.Reserve(m_count + that.m_count); + for (int i = 0; i < m_count; i++) + ret << (*this)[i]; + for (int i = 0; i < that.m_count; i++) + ret << that[i]; + return ret; + } + inline Element& operator[](int n) { return m_data[n]; @@ -107,7 +130,7 @@ public: return m_data[m_count - 1]; } - inline ArrayBase const& operator<<(T const &x) + inline ArrayBase& operator<<(T const &x) { if (m_count >= m_reserved) { @@ -236,7 +259,8 @@ public: template -class Array : public ArrayBase > +class Array : public ArrayBase, + Array > { public: inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4, @@ -274,7 +298,8 @@ public: template class Array - : public ArrayBase > + : public ArrayBase, + Array > { public: inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4, @@ -310,7 +335,8 @@ public: template class Array - : public ArrayBase > + : public ArrayBase, + Array > { public: inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4, @@ -343,7 +369,8 @@ public: template class Array - : public ArrayBase > + : public ArrayBase, + Array > { public: inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4, @@ -374,7 +401,8 @@ public: template class Array - : public ArrayBase > + : public ArrayBase, + Array > { public: inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4) @@ -401,7 +429,8 @@ public: template class Array - : public ArrayBase > + : public ArrayBase, + Array > { public: inline void Push(T1 const &m1, T2 const &m2, T3 const &m3) @@ -426,7 +455,8 @@ public: template class Array - : public ArrayBase > + : public ArrayBase, + Array > { public: inline void Push(T1 const &m1, T2 const &m2) @@ -448,7 +478,9 @@ public: }; template -class Array : public ArrayBase +class Array + : public ArrayBase > { }; diff --git a/test/unit/array.cpp b/test/unit/array.cpp index d8062c0c..0174de6f 100644 --- a/test/unit/array.cpp +++ b/test/unit/array.cpp @@ -24,7 +24,7 @@ LOLUNIT_FIXTURE(ArrayTest) void TearDown() {} - LOLUNIT_TEST(ArrayFill) + LOLUNIT_TEST(ArrayPush) { Array a; a.Push(0); @@ -38,13 +38,21 @@ LOLUNIT_FIXTURE(ArrayTest) LOLUNIT_ASSERT_EQUAL(a[3], 3); } + LOLUNIT_TEST(ArrayPushWithShift) + { + Array a; + a << 0 << 1 << 2 << 3; + + LOLUNIT_ASSERT_EQUAL(a[0], 0); + LOLUNIT_ASSERT_EQUAL(a[1], 1); + LOLUNIT_ASSERT_EQUAL(a[2], 2); + LOLUNIT_ASSERT_EQUAL(a[3], 3); + } + LOLUNIT_TEST(ArrayCopy) { Array a; - a.Push(0); - a.Push(1); - a.Push(2); - a.Push(3); + a << 0 << 1 << 2 << 3; Array b = a; @@ -57,10 +65,7 @@ LOLUNIT_FIXTURE(ArrayTest) LOLUNIT_TEST(ArrayRemove) { Array a; - a.Push(0); - a.Push(1); - a.Push(2); - a.Push(3); + a << 0 << 1 << 2 << 3; a.Remove(1); LOLUNIT_ASSERT_EQUAL(a[0], 0); @@ -82,6 +87,38 @@ LOLUNIT_FIXTURE(ArrayTest) LOLUNIT_ASSERT_EQUAL(a[0].m7, true); LOLUNIT_ASSERT_EQUAL(a[0].m8, 0); } + + LOLUNIT_TEST(ArrayConcat) + { + Array a, b; + a << 0 << 1; + b << 2 << 3; + + Array c = a + b; + LOLUNIT_ASSERT_EQUAL(c[0], 0); + LOLUNIT_ASSERT_EQUAL(c[1], 1); + LOLUNIT_ASSERT_EQUAL(c[2], 2); + LOLUNIT_ASSERT_EQUAL(c[3], 3); + } + + LOLUNIT_TEST(ArrayAppend) + { + Array a, b; + a << 0 << 1; + b << 2 << 3; + + a += b; + LOLUNIT_ASSERT_EQUAL(a[0], 0); + LOLUNIT_ASSERT_EQUAL(a[1], 1); + LOLUNIT_ASSERT_EQUAL(a[2], 2); + LOLUNIT_ASSERT_EQUAL(a[3], 3); + + b += b; + LOLUNIT_ASSERT_EQUAL(b[0], 2); + LOLUNIT_ASSERT_EQUAL(b[1], 3); + LOLUNIT_ASSERT_EQUAL(b[2], 2); + LOLUNIT_ASSERT_EQUAL(b[3], 3); + } }; } /* namespace lol */