1 /*
2 ** Copyright 2003-2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the MIT License.
4 */
5
6
7 #include "DoublyLinkedListTest.h"
8
9 #include <cppunit/TestCaller.h>
10 #include <cppunit/TestSuite.h>
11 #include <TestUtils.h>
12
13 #include <util/DoublyLinkedList.h>
14
15
16 // Class used for testing without offset
17 class ItemWithout {
18 public:
19 DoublyLinkedListLink<ItemWithout> fLink;
20 int32 value;
21 };
22
23 // Class used for testing with offset
24 class ItemWith {
25 public:
26 int32 value;
27 DoublyLinkedListLink<ItemWith> fLink;
28 };
29
30 // Class used for testing without offset
31 class ItemVirtualWithout {
32 public:
33 virtual int32 Value();
34
35 DoublyLinkedListLink<ItemVirtualWithout> fLink;
36 int32 value;
37 };
38
39 // Class used for testing with offset
40 class ItemVirtualWith {
41 public:
42 virtual int32 Value();
43
44 int32 value;
45 DoublyLinkedListLink<ItemVirtualWith> fLink;
46 };
47
48
49 int32
Value()50 ItemVirtualWithout::Value()
51 {
52 return value;
53 }
54
55
56 int32
Value()57 ItemVirtualWith::Value()
58 {
59 return value;
60 }
61
62
63 // #pragma mark -
64
65
DoublyLinkedListTest(std::string name)66 DoublyLinkedListTest::DoublyLinkedListTest(std::string name)
67 : BTestCase(name)
68 {
69 }
70
71
72 CppUnit::Test*
Suite()73 DoublyLinkedListTest::Suite() {
74 CppUnit::TestSuite *suite = new CppUnit::TestSuite("DLL");
75
76 suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::no offset", &DoublyLinkedListTest::WithoutOffsetTest));
77 suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::with offset", &DoublyLinkedListTest::WithOffsetTest));
78 suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::virtual no offset", &DoublyLinkedListTest::VirtualWithoutOffsetTest));
79 suite->addTest(new CppUnit::TestCaller<DoublyLinkedListTest>("DoublyLinkedList::virtual with offset", &DoublyLinkedListTest::VirtualWithOffsetTest));
80
81 return suite;
82 }
83
84
85 //! Tests the given list
86
87 template <typename Item>
88 void
TestList()89 DoublyLinkedListTest::TestList()
90 {
91 DoublyLinkedList<Item, DoublyLinkedListMemberGetLink<Item> > list;
92 int valueCount = 10;
93 Item items[valueCount];
94
95 // initialize
96
97 for (int i = 0; i < valueCount; i++) {
98 items[i].value = i;
99 list.Add(&items[i]);
100 }
101
102 // list must not be empty
103
104 CHK(!list.IsEmpty());
105
106 // count items in list
107
108 int count = 0;
109 typename DoublyLinkedList<Item,
110 DoublyLinkedListMemberGetLink<Item> >::Iterator
111 iterator = list.GetIterator();
112 while (iterator.Next() != NULL)
113 count++;
114
115 CHK(count == valueCount);
116
117 // test for equality
118
119 iterator = list.GetIterator();
120
121 int i = 0;
122 Item *item;
123 while ((item = iterator.Next()) != NULL) {
124 CHK(item->value == i);
125 CHK(item == &items[i]);
126 i++;
127 }
128
129 // remove first
130
131 Item *first = list.RemoveHead();
132 CHK(first->value == 0);
133 CHK(first == &items[0]);
134
135 // remove every second
136
137 iterator = list.GetIterator();
138 i = 0;
139 while ((item = iterator.Next()) != NULL) {
140 CHK(item->value == i + 1);
141
142 if (i % 2)
143 list.Remove(item);
144 i++;
145 }
146
147 // re-add first
148
149 list.Add(first);
150
151 // count again
152
153 count = 0;
154 iterator = list.GetIterator();
155 while (iterator.Next() != NULL)
156 count++;
157
158 CHK(count == (valueCount / 2) + 1);
159 }
160
161
162 //! Test using no offset, no virtual
163
164 void
WithoutOffsetTest()165 DoublyLinkedListTest::WithoutOffsetTest() {
166 TestList<ItemWithout>();
167 }
168
169
170 //! Test using offset, no virtual
171
172 void
WithOffsetTest()173 DoublyLinkedListTest::WithOffsetTest() {
174 TestList<ItemWith>();
175 }
176
177
178 //! Test using no offset, virtual
179
180 void
VirtualWithoutOffsetTest()181 DoublyLinkedListTest::VirtualWithoutOffsetTest() {
182 TestList<ItemVirtualWithout>();
183 }
184
185
186 //! Test using offset, virtual
187
188 void
VirtualWithOffsetTest()189 DoublyLinkedListTest::VirtualWithOffsetTest() {
190 TestList<ItemVirtualWith>();
191 }
192
193