Quellcode durchsuchen

core: allow to concatenate arrays, and add unit tests for that.

legacy
Sam Hocevar sam vor 12 Jahren
Ursprung
Commit
d0dbbde159
2 geänderte Dateien mit 88 neuen und 19 gelöschten Zeilen
  1. +42
    -10
      src/array.h
  2. +46
    -9
      test/unit/array.cpp

+ 42
- 10
src/array.h Datei anzeigen

@@ -31,7 +31,7 @@ namespace lol
* m_count are allocated. The rest is uninitialised memory.
*/

template<typename T> class ArrayBase
template<typename T, typename ARRAY> 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<T> const& operator<<(T const &x)
inline ArrayBase& operator<<(T const &x)
{
if (m_count >= m_reserved)
{
@@ -236,7 +259,8 @@ public:
template<typename T1, typename T2 = void, typename T3 = void,
typename T4 = void, typename T5 = void, typename T6 = void,
typename T7 = void, typename T8 = void>
class Array : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, T8> >
class Array : public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, T8>,
Array<T1, T2, T3, T4, T5, T6, T7, T8> >
{
public:
inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
@@ -274,7 +298,8 @@ public:
template<typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6, typename T7>
class Array<T1, T2, T3, T4, T5, T6, T7, void>
: public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, void> >
: public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, T7, void>,
Array<T1, T2, T3, T4, T5, T6, T7> >
{
public:
inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
@@ -310,7 +335,8 @@ public:
template<typename T1, typename T2, typename T3, typename T4, typename T5,
typename T6>
class Array<T1, T2, T3, T4, T5, T6, void, void>
: public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, void, void> >
: public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, T6, void, void>,
Array<T1, T2, T3, T4, T5, T6> >
{
public:
inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
@@ -343,7 +369,8 @@ public:

template<typename T1, typename T2, typename T3, typename T4, typename T5>
class Array<T1, T2, T3, T4, T5, void, void, void>
: public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, void, void, void> >
: public ArrayBase<ArrayElement<T1, T2, T3, T4, T5, void, void, void>,
Array<T1, T2, T3, T4, T5> >
{
public:
inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4,
@@ -374,7 +401,8 @@ public:

template<typename T1, typename T2, typename T3, typename T4>
class Array<T1, T2, T3, T4, void, void, void, void>
: public ArrayBase<ArrayElement<T1, T2, T3, T4, void, void, void, void> >
: public ArrayBase<ArrayElement<T1, T2, T3, T4, void, void, void, void>,
Array<T1, T2, T3, T4> >
{
public:
inline void Push(T1 const &m1, T2 const &m2, T3 const &m3, T4 const &m4)
@@ -401,7 +429,8 @@ public:

template<typename T1, typename T2, typename T3>
class Array<T1, T2, T3, void, void, void, void, void>
: public ArrayBase<ArrayElement<T1, T2, T3, void, void, void, void, void> >
: public ArrayBase<ArrayElement<T1, T2, T3, void, void, void, void, void>,
Array<T1, T2, T3> >
{
public:
inline void Push(T1 const &m1, T2 const &m2, T3 const &m3)
@@ -426,7 +455,8 @@ public:

template<typename T1, typename T2>
class Array<T1, T2, void, void, void, void, void, void>
: public ArrayBase<ArrayElement<T1, T2, void, void, void, void, void, void> >
: public ArrayBase<ArrayElement<T1, T2, void, void, void, void, void, void>,
Array<T1, T2> >
{
public:
inline void Push(T1 const &m1, T2 const &m2)
@@ -448,7 +478,9 @@ public:
};

template<typename T>
class Array<T, void, void, void, void, void, void, void> : public ArrayBase<T>
class Array<T, void, void, void, void, void, void, void>
: public ArrayBase<T,
Array<T> >
{
};



+ 46
- 9
test/unit/array.cpp Datei anzeigen

@@ -24,7 +24,7 @@ LOLUNIT_FIXTURE(ArrayTest)

void TearDown() {}

LOLUNIT_TEST(ArrayFill)
LOLUNIT_TEST(ArrayPush)
{
Array<int> a;
a.Push(0);
@@ -38,13 +38,21 @@ LOLUNIT_FIXTURE(ArrayTest)
LOLUNIT_ASSERT_EQUAL(a[3], 3);
}

LOLUNIT_TEST(ArrayPushWithShift)
{
Array<int> 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<int> a;
a.Push(0);
a.Push(1);
a.Push(2);
a.Push(3);
a << 0 << 1 << 2 << 3;

Array<int> b = a;

@@ -57,10 +65,7 @@ LOLUNIT_FIXTURE(ArrayTest)
LOLUNIT_TEST(ArrayRemove)
{
Array<int> 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<int> a, b;
a << 0 << 1;
b << 2 << 3;

Array<int> 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<int> 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 */


Laden…
Abbrechen
Speichern