177b1fd22SAxel Dörfler /*
277b1fd22SAxel Dörfler ** Copyright 2003-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3*b6f76ebeSAugustin Cavalier ** Distributed under the terms of the MIT License.
477b1fd22SAxel Dörfler */
577b1fd22SAxel Dörfler
677b1fd22SAxel Dörfler
777b1fd22SAxel Dörfler #include "DoublyLinkedListTest.h"
877b1fd22SAxel Dörfler
977b1fd22SAxel Dörfler #include <cppunit/TestCaller.h>
1077b1fd22SAxel Dörfler #include <cppunit/TestSuite.h>
1177b1fd22SAxel Dörfler #include <TestUtils.h>
1277b1fd22SAxel Dörfler
1377b1fd22SAxel Dörfler #include <util/DoublyLinkedList.h>
1477b1fd22SAxel Dörfler
1577b1fd22SAxel Dörfler
1677b1fd22SAxel Dörfler // Class used for testing without offset
1777b1fd22SAxel Dörfler class ItemWithout {
1877b1fd22SAxel Dörfler public:
19913e3765SAdrien Destugues DoublyLinkedListLink<ItemWithout> fLink;
2077b1fd22SAxel Dörfler int32 value;
2177b1fd22SAxel Dörfler };
2277b1fd22SAxel Dörfler
2377b1fd22SAxel Dörfler // Class used for testing with offset
2477b1fd22SAxel Dörfler class ItemWith {
2577b1fd22SAxel Dörfler public:
2677b1fd22SAxel Dörfler int32 value;
27913e3765SAdrien Destugues DoublyLinkedListLink<ItemWith> fLink;
2877b1fd22SAxel Dörfler };
2977b1fd22SAxel Dörfler
3077b1fd22SAxel Dörfler // Class used for testing without offset
3177b1fd22SAxel Dörfler class ItemVirtualWithout {
3277b1fd22SAxel Dörfler public:
3377b1fd22SAxel Dörfler virtual int32 Value();
3477b1fd22SAxel Dörfler
35913e3765SAdrien Destugues DoublyLinkedListLink<ItemVirtualWithout> fLink;
3677b1fd22SAxel Dörfler int32 value;
3777b1fd22SAxel Dörfler };
3877b1fd22SAxel Dörfler
3977b1fd22SAxel Dörfler // Class used for testing with offset
4077b1fd22SAxel Dörfler class ItemVirtualWith {
4177b1fd22SAxel Dörfler public:
4277b1fd22SAxel Dörfler virtual int32 Value();
4377b1fd22SAxel Dörfler
4477b1fd22SAxel Dörfler int32 value;
45913e3765SAdrien Destugues DoublyLinkedListLink<ItemVirtualWith> fLink;
4677b1fd22SAxel Dörfler };
4777b1fd22SAxel Dörfler
4877b1fd22SAxel Dörfler
4977b1fd22SAxel Dörfler int32
Value()5077b1fd22SAxel Dörfler ItemVirtualWithout::Value()
5177b1fd22SAxel Dörfler {
5277b1fd22SAxel Dörfler return value;
5377b1fd22SAxel Dörfler }
5477b1fd22SAxel Dörfler
5577b1fd22SAxel Dörfler
5677b1fd22SAxel Dörfler int32
Value()5777b1fd22SAxel Dörfler ItemVirtualWith::Value()
5877b1fd22SAxel Dörfler {
5977b1fd22SAxel Dörfler return value;
6077b1fd22SAxel Dörfler }
6177b1fd22SAxel Dörfler
6277b1fd22SAxel Dörfler
6377b1fd22SAxel Dörfler // #pragma mark -
6477b1fd22SAxel Dörfler
6577b1fd22SAxel Dörfler
DoublyLinkedListTest(std::string name)6677b1fd22SAxel Dörfler DoublyLinkedListTest::DoublyLinkedListTest(std::string name)
6777b1fd22SAxel Dörfler : BTestCase(name)
6877b1fd22SAxel Dörfler {
6977b1fd22SAxel Dörfler }
7077b1fd22SAxel Dörfler
7177b1fd22SAxel Dörfler
7277b1fd22SAxel Dörfler CppUnit::Test*
Suite()7377b1fd22SAxel Dörfler DoublyLinkedListTest::Suite() {
7477b1fd22SAxel Dörfler CppUnit::TestSuite *suite = new CppUnit::TestSuite("DLL");
7577b1fd22SAxel Dörfler
7677b1fd22SAxel Dörfler suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::no offset", &DoublyLinkedListTest::WithoutOffsetTest));
7777b1fd22SAxel Dörfler suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::with offset", &DoublyLinkedListTest::WithOffsetTest));
7877b1fd22SAxel Dörfler suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::virtual no offset", &DoublyLinkedListTest::VirtualWithoutOffsetTest));
7977b1fd22SAxel Dörfler suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::virtual with offset", &DoublyLinkedListTest::VirtualWithOffsetTest));
8077b1fd22SAxel Dörfler
8177b1fd22SAxel Dörfler return suite;
8277b1fd22SAxel Dörfler }
8377b1fd22SAxel Dörfler
8477b1fd22SAxel Dörfler
8577b1fd22SAxel Dörfler //! Tests the given list
8677b1fd22SAxel Dörfler
8777b1fd22SAxel Dörfler template <typename Item>
8877b1fd22SAxel Dörfler void
TestList()8977b1fd22SAxel Dörfler DoublyLinkedListTest::TestList()
9077b1fd22SAxel Dörfler {
91913e3765SAdrien Destugues DoublyLinkedList<Item, DoublyLinkedListMemberGetLink<Item> > list;
9277b1fd22SAxel Dörfler int valueCount = 10;
9377b1fd22SAxel Dörfler Item items[valueCount];
9477b1fd22SAxel Dörfler
9577b1fd22SAxel Dörfler // initialize
9677b1fd22SAxel Dörfler
9777b1fd22SAxel Dörfler for (int i = 0; i < valueCount; i++) {
9877b1fd22SAxel Dörfler items[i].value = i;
9977b1fd22SAxel Dörfler list.Add(&items[i]);
10077b1fd22SAxel Dörfler }
10177b1fd22SAxel Dörfler
10277b1fd22SAxel Dörfler // list must not be empty
10377b1fd22SAxel Dörfler
10477b1fd22SAxel Dörfler CHK(!list.IsEmpty());
10577b1fd22SAxel Dörfler
10677b1fd22SAxel Dörfler // count items in list
10777b1fd22SAxel Dörfler
10877b1fd22SAxel Dörfler int count = 0;
10977aa0e2aSOliver Tappe typename DoublyLinkedList<Item,
11077aa0e2aSOliver Tappe DoublyLinkedListMemberGetLink<Item> >::Iterator
111913e3765SAdrien Destugues iterator = list.GetIterator();
11277b1fd22SAxel Dörfler while (iterator.Next() != NULL)
11377b1fd22SAxel Dörfler count++;
11477b1fd22SAxel Dörfler
11577b1fd22SAxel Dörfler CHK(count == valueCount);
11677b1fd22SAxel Dörfler
11777b1fd22SAxel Dörfler // test for equality
11877b1fd22SAxel Dörfler
119913e3765SAdrien Destugues iterator = list.GetIterator();
12077b1fd22SAxel Dörfler
12177b1fd22SAxel Dörfler int i = 0;
12277b1fd22SAxel Dörfler Item *item;
12377b1fd22SAxel Dörfler while ((item = iterator.Next()) != NULL) {
12477b1fd22SAxel Dörfler CHK(item->value == i);
12577b1fd22SAxel Dörfler CHK(item == &items[i]);
12677b1fd22SAxel Dörfler i++;
12777b1fd22SAxel Dörfler }
12877b1fd22SAxel Dörfler
12977b1fd22SAxel Dörfler // remove first
13077b1fd22SAxel Dörfler
13177b1fd22SAxel Dörfler Item *first = list.RemoveHead();
13277b1fd22SAxel Dörfler CHK(first->value == 0);
13377b1fd22SAxel Dörfler CHK(first == &items[0]);
13477b1fd22SAxel Dörfler
13577b1fd22SAxel Dörfler // remove every second
13677b1fd22SAxel Dörfler
137913e3765SAdrien Destugues iterator = list.GetIterator();
13877b1fd22SAxel Dörfler i = 0;
13977b1fd22SAxel Dörfler while ((item = iterator.Next()) != NULL) {
14077b1fd22SAxel Dörfler CHK(item->value == i + 1);
14177b1fd22SAxel Dörfler
14277b1fd22SAxel Dörfler if (i % 2)
14377b1fd22SAxel Dörfler list.Remove(item);
14477b1fd22SAxel Dörfler i++;
14577b1fd22SAxel Dörfler }
14677b1fd22SAxel Dörfler
14777b1fd22SAxel Dörfler // re-add first
14877b1fd22SAxel Dörfler
14977b1fd22SAxel Dörfler list.Add(first);
15077b1fd22SAxel Dörfler
15177b1fd22SAxel Dörfler // count again
15277b1fd22SAxel Dörfler
15377b1fd22SAxel Dörfler count = 0;
154913e3765SAdrien Destugues iterator = list.GetIterator();
15577b1fd22SAxel Dörfler while (iterator.Next() != NULL)
15677b1fd22SAxel Dörfler count++;
15777b1fd22SAxel Dörfler
15877b1fd22SAxel Dörfler CHK(count == (valueCount / 2) + 1);
15977b1fd22SAxel Dörfler }
16077b1fd22SAxel Dörfler
16177b1fd22SAxel Dörfler
16277b1fd22SAxel Dörfler //! Test using no offset, no virtual
16377b1fd22SAxel Dörfler
16477b1fd22SAxel Dörfler void
WithoutOffsetTest()16577b1fd22SAxel Dörfler DoublyLinkedListTest::WithoutOffsetTest() {
16677b1fd22SAxel Dörfler TestList<ItemWithout>();
16777b1fd22SAxel Dörfler }
16877b1fd22SAxel Dörfler
16977b1fd22SAxel Dörfler
17077b1fd22SAxel Dörfler //! Test using offset, no virtual
17177b1fd22SAxel Dörfler
17277b1fd22SAxel Dörfler void
WithOffsetTest()17377b1fd22SAxel Dörfler DoublyLinkedListTest::WithOffsetTest() {
17477b1fd22SAxel Dörfler TestList<ItemWith>();
17577b1fd22SAxel Dörfler }
17677b1fd22SAxel Dörfler
17777b1fd22SAxel Dörfler
17877b1fd22SAxel Dörfler //! Test using no offset, virtual
17977b1fd22SAxel Dörfler
18077b1fd22SAxel Dörfler void
VirtualWithoutOffsetTest()18177b1fd22SAxel Dörfler DoublyLinkedListTest::VirtualWithoutOffsetTest() {
18277b1fd22SAxel Dörfler TestList<ItemVirtualWithout>();
18377b1fd22SAxel Dörfler }
18477b1fd22SAxel Dörfler
18577b1fd22SAxel Dörfler
18677b1fd22SAxel Dörfler //! Test using offset, virtual
18777b1fd22SAxel Dörfler
18877b1fd22SAxel Dörfler void
VirtualWithOffsetTest()18977b1fd22SAxel Dörfler DoublyLinkedListTest::VirtualWithOffsetTest() {
19077b1fd22SAxel Dörfler TestList<ItemVirtualWith>();
19177b1fd22SAxel Dörfler }
19277b1fd22SAxel Dörfler
193