** Copyright 2003-2004, Axel DΓΆrfler, axeld@pinc-software.de. All rights reserved.
** Distributed under the terms of the MIT License.
*/
#include "DoublyLinkedListTest.h"
#include <cppunit/TestCaller.h>
#include <cppunit/TestSuite.h>
#include <TestUtils.h>
#include <util/DoublyLinkedList.h>
class ItemWithout {
public:
DoublyLinkedListLink<ItemWithout> fLink;
int32 value;
};
class ItemWith {
public:
int32 value;
DoublyLinkedListLink<ItemWith> fLink;
};
class ItemVirtualWithout {
public:
virtual int32 Value();
DoublyLinkedListLink<ItemVirtualWithout> fLink;
int32 value;
};
class ItemVirtualWith {
public:
virtual int32 Value();
int32 value;
DoublyLinkedListLink<ItemVirtualWith> fLink;
};
int32
ItemVirtualWithout::Value()
{
return value;
}
int32
ItemVirtualWith::Value()
{
return value;
}
DoublyLinkedListTest::DoublyLinkedListTest(std::string name)
: BTestCase(name)
{
}
CppUnit::Test*
DoublyLinkedListTest::Suite() {
CppUnit::TestSuite *suite = new CppUnit::TestSuite("DLL");
suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::no offset", &DoublyLinkedListTest::WithoutOffsetTest));
suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::with offset", &DoublyLinkedListTest::WithOffsetTest));
suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::virtual no offset", &DoublyLinkedListTest::VirtualWithoutOffsetTest));
suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::virtual with offset", &DoublyLinkedListTest::VirtualWithOffsetTest));
return suite;
}
template <typename Item>
void
DoublyLinkedListTest::TestList()
{
DoublyLinkedList<Item, DoublyLinkedListMemberGetLink<Item> > list;
int valueCount = 10;
Item items[valueCount];
for (int i = 0; i < valueCount; i++) {
items[i].value = i;
list.Add(&items[i]);
}
CHK(!list.IsEmpty());
int count = 0;
typename DoublyLinkedList<Item,
DoublyLinkedListMemberGetLink<Item> >::Iterator
iterator = list.GetIterator();
while (iterator.Next() != NULL)
count++;
CHK(count == valueCount);
iterator = list.GetIterator();
int i = 0;
Item *item;
while ((item = iterator.Next()) != NULL) {
CHK(item->value == i);
CHK(item == &items[i]);
i++;
}
Item *first = list.RemoveHead();
CHK(first->value == 0);
CHK(first == &items[0]);
iterator = list.GetIterator();
i = 0;
while ((item = iterator.Next()) != NULL) {
CHK(item->value == i + 1);
if (i % 2)
list.Remove(item);
i++;
}
list.Add(first);
count = 0;
iterator = list.GetIterator();
while (iterator.Next() != NULL)
count++;
CHK(count == (valueCount / 2) + 1);
}
void
DoublyLinkedListTest::WithoutOffsetTest() {
TestList<ItemWithout>();
}
void
DoublyLinkedListTest::WithOffsetTest() {
TestList<ItemWith>();
}
void
DoublyLinkedListTest::VirtualWithoutOffsetTest() {
TestList<ItemVirtualWithout>();
}
void
DoublyLinkedListTest::VirtualWithOffsetTest() {
TestList<ItemVirtualWith>();
}