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