瀏覽代碼

base: add Array::Swap() method and the corresponding unit test.

legacy
Sam Hocevar sam 11 年之前
父節點
當前提交
5ea012c192
共有 2 個檔案被更改,包括 28 行新增0 行删除
  1. +15
    -0
      src/lol/base/array.h
  2. +13
    -0
      test/unit/array.cpp

+ 15
- 0
src/lol/base/array.h 查看文件

@@ -168,6 +168,21 @@ public:
Remove(m_count - 1, 1);
}

void Swap(int pos1, int pos2)
{
ASSERT(pos1 >= 0);
ASSERT(pos2 >= 0);
ASSERT(pos1 < m_count);
ASSERT(pos2 < m_count);

if (pos1 != pos2)
{
Element tmp = (*this)[pos1];
(*this)[pos1] = (*this)[pos2];
(*this)[pos2] = tmp;
}
}

void Remove(int pos, int todelete = 1)
{
ASSERT(pos >= 0);


+ 13
- 0
test/unit/array.cpp 查看文件

@@ -100,6 +100,19 @@ LOLUNIT_FIXTURE(ArrayTest)
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;


Loading…
取消
儲存