|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- //
- // Lol Engine
- //
- // Copyright: (c) 2010-2013 Sam Hocevar <sam@hocevar.net>
- // This program is free software; you can redistribute it and/or
- // modify it under the terms of the Do What The Fuck You Want To
- // Public License, Version 2, as published by Sam Hocevar. See
- // http://www.wtfpl.net/ for more details.
- //
-
- #if defined HAVE_CONFIG_H
- # include "config.h"
- #endif
-
- #include "core.h"
- #include "lol/unit.h"
-
- namespace lol
- {
-
- struct TrackedObj
- {
- static int m_ctor, m_dtor;
-
- TrackedObj() { m_ctor++; }
- TrackedObj(TrackedObj const &) { m_ctor++; }
- ~TrackedObj() { m_dtor++; }
- };
-
- int TrackedObj::m_ctor = 0;
- int TrackedObj::m_dtor = 0;
-
- LOLUNIT_FIXTURE(ArrayTest)
- {
- void SetUp() {}
-
- void TearDown() {}
-
- /* HACK: we disable these tests because they fail with the
- * Xcode iPhone compiler. */
- #if !defined __clang__ || !defined __arm__
- LOLUNIT_TEST(ArrayPush)
- {
- Array<int> a;
- a.Push(0);
- a.Push(1);
- a.Push(2);
- a.Push(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(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 << 0 << 1 << 2 << 3;
-
- Array<int> b = a;
-
- LOLUNIT_ASSERT_EQUAL(b[0], 0);
- LOLUNIT_ASSERT_EQUAL(b[1], 1);
- LOLUNIT_ASSERT_EQUAL(b[2], 2);
- LOLUNIT_ASSERT_EQUAL(b[3], 3);
- }
-
- LOLUNIT_TEST(ArrayRemove)
- {
- Array<int> a;
- a << 0 << 1 << 2 << 3;
- a.Remove(1);
-
- LOLUNIT_ASSERT_EQUAL(a.Count(), 3);
- LOLUNIT_ASSERT_EQUAL(a[0], 0);
- LOLUNIT_ASSERT_EQUAL(a[1], 2);
- LOLUNIT_ASSERT_EQUAL(a[2], 3);
-
- Array<int> b;
- b << 0 << 1 << 2 << 3;
- b.Remove(-2);
-
- LOLUNIT_ASSERT_EQUAL(b.Count(), 3);
- LOLUNIT_ASSERT_EQUAL(b[0], 0);
- LOLUNIT_ASSERT_EQUAL(b[1], 1);
- LOLUNIT_ASSERT_EQUAL(b[2], 3);
- }
-
- LOLUNIT_TEST(ArrayRemoveSwap)
- {
- Array<int> a;
- a << 0 << 1 << 2 << 3;
- a.RemoveSwap(1);
-
- LOLUNIT_ASSERT_EQUAL(a.Count(), 3);
- LOLUNIT_ASSERT_EQUAL(a[0], 0);
- LOLUNIT_ASSERT_EQUAL(a[1], 3);
- LOLUNIT_ASSERT_EQUAL(a[2], 2);
-
- Array<int> b;
- b << 0 << 1 << 2 << 3;
- b.Remove(1, 2);
-
- LOLUNIT_ASSERT_EQUAL(b.Count(), 2);
- LOLUNIT_ASSERT_EQUAL(b[0], 0);
- LOLUNIT_ASSERT_EQUAL(b[1], 3);
- }
- #endif
-
- LOLUNIT_TEST(EightElements)
- {
- Array<int, long, float, double, unsigned, char, bool, void *> a;
- a.Push(1, 2, 3.f, 4.0, 5, 'a', true, 0);
-
- LOLUNIT_ASSERT_EQUAL(a[0].m1, 1);
- LOLUNIT_ASSERT_EQUAL(a[0].m2, 2);
- LOLUNIT_ASSERT_EQUAL(a[0].m3, 3.f);
- LOLUNIT_ASSERT_EQUAL(a[0].m4, 4.0);
- LOLUNIT_ASSERT_EQUAL(a[0].m5, 5);
- LOLUNIT_ASSERT_EQUAL(a[0].m6, 'a');
- LOLUNIT_ASSERT_EQUAL(a[0].m7, true);
- LOLUNIT_ASSERT_EQUAL(a[0].m8, 0);
- }
-
- LOLUNIT_TEST(ArraySwap)
- {
- Array<int, int> a;
- a.Push(10, 20);
- a.Push(30, 40);
- a.Swap(0, 1);
-
- LOLUNIT_ASSERT_EQUAL(30, a[0].m1);
- LOLUNIT_ASSERT_EQUAL(40, a[0].m2);
- LOLUNIT_ASSERT_EQUAL(10, a[1].m1);
- LOLUNIT_ASSERT_EQUAL(20, a[1].m2);
- }
-
- 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);
- }
-
- LOLUNIT_TEST(ElementCtorDtor)
- {
- /* Ensure array elements get created and destroyed the proper
- * number of times. */
- TrackedObj::m_ctor = 0;
- TrackedObj::m_dtor = 0;
- {
- Array<TrackedObj> a;
-
- a.Push(TrackedObj());
- }
- LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
-
- TrackedObj::m_ctor = 0;
- TrackedObj::m_dtor = 0;
- {
- Array<TrackedObj> a;
-
- a.Resize(2);
- a.Resize(4);
- a.Resize(1);
- }
- LOLUNIT_ASSERT_EQUAL(TrackedObj::m_ctor, TrackedObj::m_dtor);
- }
- };
-
- } /* namespace lol */
-
|