1 |
#include "PoolTest.h" |
2 |
|
3 |
#include <iostream> |
4 |
|
5 |
#define ELEMENTS 100 |
6 |
|
7 |
CPPUNIT_TEST_SUITE_REGISTRATION(PoolTest); |
8 |
|
9 |
using namespace std; |
10 |
|
11 |
// Note: |
12 |
// we have to declare all those variables which we want to use for all |
13 |
// tests within this test suite static because there are side effects which |
14 |
// occur on transition to the next test which would change the values of our |
15 |
// variables |
16 |
static Pool<int>* pPool = NULL; |
17 |
static RTList<int>* pList = NULL; |
18 |
|
19 |
// some data structure we use as template type |
20 |
struct foo_t { |
21 |
int bar; |
22 |
}; |
23 |
|
24 |
|
25 |
// PoolTest |
26 |
|
27 |
void PoolTest::printTestSuiteName() { |
28 |
cout << "\b \nRunning Pool Tests: " << flush; |
29 |
} |
30 |
|
31 |
void PoolTest::testAllocPool() { |
32 |
//cout << "testAllocPool()" << flush; |
33 |
pPool = new Pool<int>(ELEMENTS); |
34 |
CPPUNIT_ASSERT(pPool != NULL); |
35 |
CPPUNIT_ASSERT(pPool->isEmpty()); |
36 |
CPPUNIT_ASSERT(pPool->first() == pPool->end()); |
37 |
CPPUNIT_ASSERT(pPool->last() == pPool->begin()); |
38 |
CPPUNIT_ASSERT(!pPool->poolIsEmpty()); |
39 |
} |
40 |
|
41 |
void PoolTest::testAllocAppendElements() { |
42 |
//cout << "testAllocAppendElements()" << flush; |
43 |
// allocate elements and assign a value to it |
44 |
for (int i = 0; i < ELEMENTS; i++) { |
45 |
Pool<int>::Iterator iter = pPool->allocAppend(); |
46 |
CPPUNIT_ASSERT(iter != pPool->begin()); |
47 |
CPPUNIT_ASSERT(iter != pPool->end()); |
48 |
CPPUNIT_ASSERT(iter == pPool->last()); |
49 |
*iter = i; |
50 |
} |
51 |
CPPUNIT_ASSERT(!pPool->isEmpty()); |
52 |
CPPUNIT_ASSERT(pPool->first() != pPool->end()); |
53 |
CPPUNIT_ASSERT(pPool->last() != pPool->begin()); |
54 |
CPPUNIT_ASSERT(pPool->poolIsEmpty()); |
55 |
} |
56 |
|
57 |
void PoolTest::testIterateForward() { |
58 |
//cout << "testIterateForward()" << flush; |
59 |
// iterate forward through the allocated elements and check their values |
60 |
Pool<int>::Iterator iter = pPool->first(); |
61 |
for (int i = 0; i < ELEMENTS; i++) { |
62 |
CPPUNIT_ASSERT(*iter == i); |
63 |
CPPUNIT_ASSERT(iter != pPool->begin()); |
64 |
CPPUNIT_ASSERT(iter != pPool->end()); |
65 |
iter++; |
66 |
} |
67 |
CPPUNIT_ASSERT(iter == pPool->end()); |
68 |
} |
69 |
|
70 |
void PoolTest::testIterateBackward() { |
71 |
//cout << "testIterateBackward()" << flush; |
72 |
// iterate backward through the allocated elements and check their values |
73 |
Pool<int>::Iterator iter = pPool->last(); |
74 |
for (int i = ELEMENTS - 1; i >= 0; i--) { |
75 |
CPPUNIT_ASSERT(*iter == i); |
76 |
CPPUNIT_ASSERT(iter != pPool->begin()); |
77 |
CPPUNIT_ASSERT(iter != pPool->end()); |
78 |
--iter; |
79 |
} |
80 |
CPPUNIT_ASSERT(iter == pPool->begin()); |
81 |
} |
82 |
|
83 |
void PoolTest::testFreeElements() { |
84 |
//cout << "testFreeElements()" << flush; |
85 |
// free all elements |
86 |
Pool<int>::Iterator iter = pPool->first(); |
87 |
for (int i = 0; i < ELEMENTS; i++) { |
88 |
pPool->free(iter); |
89 |
iter++; |
90 |
} |
91 |
CPPUNIT_ASSERT(pPool->isEmpty()); |
92 |
CPPUNIT_ASSERT(pPool->first() == pPool->end()); |
93 |
CPPUNIT_ASSERT(pPool->last() == pPool->begin()); |
94 |
CPPUNIT_ASSERT(!pPool->poolIsEmpty()); |
95 |
} |
96 |
|
97 |
void PoolTest::testAllocPrependElements() { |
98 |
//cout << "testAllocPrependElements()" << flush; |
99 |
for (int i = ELEMENTS - 1; i >= 0; i--) { |
100 |
Pool<int>::Iterator iter = pPool->allocPrepend(); |
101 |
CPPUNIT_ASSERT(iter != pPool->begin()); |
102 |
CPPUNIT_ASSERT(iter != pPool->end()); |
103 |
CPPUNIT_ASSERT(iter == pPool->first()); |
104 |
*iter = i; |
105 |
} |
106 |
CPPUNIT_ASSERT(!pPool->isEmpty()); |
107 |
CPPUNIT_ASSERT(pPool->first() != pPool->end()); |
108 |
CPPUNIT_ASSERT(pPool->last() != pPool->begin()); |
109 |
CPPUNIT_ASSERT(pPool->poolIsEmpty()); |
110 |
} |
111 |
|
112 |
void PoolTest::testClear() { |
113 |
//cout << "testClear()" << flush; |
114 |
pPool->clear(); |
115 |
CPPUNIT_ASSERT(pPool->isEmpty()); |
116 |
CPPUNIT_ASSERT(pPool->first() == pPool->end()); |
117 |
CPPUNIT_ASSERT(pPool->last() == pPool->begin()); |
118 |
CPPUNIT_ASSERT(!pPool->poolIsEmpty()); |
119 |
} |
120 |
|
121 |
void PoolTest::testAllocList() { |
122 |
//cout << "testAllocList()" << flush; |
123 |
pList = new RTList<int>(pPool); |
124 |
CPPUNIT_ASSERT(pList != NULL); |
125 |
CPPUNIT_ASSERT(pList->isEmpty()); |
126 |
CPPUNIT_ASSERT(pList->first() == pList->end()); |
127 |
CPPUNIT_ASSERT(pList->last() == pList->begin()); |
128 |
CPPUNIT_ASSERT(!pPool->poolIsEmpty()); |
129 |
} |
130 |
|
131 |
void PoolTest::testAllocElementsOnList() { |
132 |
//cout << "testAllocElementsOnList()" << flush; |
133 |
// allocate elements and assign a value to it |
134 |
for (int i = 0; i < ELEMENTS; i++) { |
135 |
Pool<int>::Iterator iter = pList->allocAppend(); |
136 |
CPPUNIT_ASSERT(iter != pList->begin()); |
137 |
CPPUNIT_ASSERT(iter != pList->end()); |
138 |
CPPUNIT_ASSERT(iter == pList->last()); |
139 |
*iter = i; |
140 |
} |
141 |
CPPUNIT_ASSERT(!pList->isEmpty()); |
142 |
CPPUNIT_ASSERT(pList->first() != pList->end()); |
143 |
CPPUNIT_ASSERT(pList->last() != pList->begin()); |
144 |
CPPUNIT_ASSERT(pPool->poolIsEmpty()); |
145 |
} |
146 |
|
147 |
void PoolTest::testIterateForwardOnList() { |
148 |
//cout << "testIterateForwardOnList()" << flush; |
149 |
// iterate forward through the allocated elements and check their values |
150 |
RTList<int>::Iterator iter = pList->first(); |
151 |
for (int i = 0; i < ELEMENTS; i++) { |
152 |
CPPUNIT_ASSERT(*iter == i); |
153 |
CPPUNIT_ASSERT(iter != pList->begin()); |
154 |
CPPUNIT_ASSERT(iter != pList->end()); |
155 |
iter++; |
156 |
} |
157 |
CPPUNIT_ASSERT(iter == pList->end()); |
158 |
} |
159 |
|
160 |
void PoolTest::testIterateBackwardOnList() { |
161 |
//cout << "testIterateBackwardOnList()" << flush; |
162 |
// iterate backward through the allocated elements and check their values |
163 |
RTList<int>::Iterator iter = pList->last(); |
164 |
for (int i = ELEMENTS - 1; i >= 0; i--) { |
165 |
CPPUNIT_ASSERT(*iter == i); |
166 |
CPPUNIT_ASSERT(iter != pList->begin()); |
167 |
CPPUNIT_ASSERT(iter != pList->end()); |
168 |
--iter; |
169 |
} |
170 |
CPPUNIT_ASSERT(iter == pList->begin()); |
171 |
} |
172 |
|
173 |
void PoolTest::testClearList() { |
174 |
//cout << "testClearList()" << flush; |
175 |
pList->clear(); |
176 |
CPPUNIT_ASSERT(pList->isEmpty()); |
177 |
CPPUNIT_ASSERT(pList->first() == pList->end()); |
178 |
CPPUNIT_ASSERT(pList->last() == pList->begin()); |
179 |
CPPUNIT_ASSERT(!pPool->poolIsEmpty()); |
180 |
} |
181 |
|
182 |
void PoolTest::testMoveToEnd() { |
183 |
testAllocElementsOnList(); |
184 |
CPPUNIT_ASSERT(!pList->isEmpty()); |
185 |
CPPUNIT_ASSERT(pPool->poolIsEmpty()); |
186 |
|
187 |
//cout << "testMoveToEnd()" << flush; |
188 |
RTList<int> _2ndList(pPool); |
189 |
|
190 |
{ |
191 |
RTList<int>::Iterator iter = pList->first(); |
192 |
for (int i = 0; i < ELEMENTS; i++) { |
193 |
*iter = i; |
194 |
RTList<int>::Iterator iterOn2ndList = iter.moveToEndOf(&_2ndList); |
195 |
CPPUNIT_ASSERT(*iterOn2ndList == i); |
196 |
CPPUNIT_ASSERT(iterOn2ndList == _2ndList.last()); |
197 |
iter++; |
198 |
} |
199 |
CPPUNIT_ASSERT(iter == pList->end()); |
200 |
CPPUNIT_ASSERT(pList->isEmpty()); |
201 |
CPPUNIT_ASSERT(!_2ndList.isEmpty()); |
202 |
} |
203 |
|
204 |
{ |
205 |
RTList<int>::Iterator iter = _2ndList.first(); |
206 |
for (int i = 0; i < ELEMENTS; i++) { |
207 |
CPPUNIT_ASSERT(*iter == i); |
208 |
CPPUNIT_ASSERT(iter != _2ndList.begin()); |
209 |
CPPUNIT_ASSERT(iter != _2ndList.end()); |
210 |
iter++; |
211 |
} |
212 |
CPPUNIT_ASSERT(iter == _2ndList.end()); |
213 |
} |
214 |
|
215 |
_2ndList.clear(); |
216 |
CPPUNIT_ASSERT(_2ndList.isEmpty()); |
217 |
CPPUNIT_ASSERT(!pPool->poolIsEmpty()); |
218 |
} |
219 |
|
220 |
void PoolTest::testMoveToBegin() { |
221 |
testAllocElementsOnList(); |
222 |
CPPUNIT_ASSERT(!pList->isEmpty()); |
223 |
CPPUNIT_ASSERT(pPool->poolIsEmpty()); |
224 |
|
225 |
//cout << "testMoveToBegin()" << flush; |
226 |
RTList<int> _2ndList(pPool); |
227 |
|
228 |
{ |
229 |
RTList<int>::Iterator iter = pList->first(); |
230 |
for (int i = 0; i < ELEMENTS; i++) { |
231 |
*iter = i; |
232 |
RTList<int>::Iterator iterOn2ndList = iter.moveToBeginOf(&_2ndList); |
233 |
CPPUNIT_ASSERT(*iterOn2ndList == i); |
234 |
CPPUNIT_ASSERT(iterOn2ndList == _2ndList.first()); |
235 |
iter++; |
236 |
} |
237 |
CPPUNIT_ASSERT(iter == pList->end()); |
238 |
CPPUNIT_ASSERT(pList->isEmpty()); |
239 |
CPPUNIT_ASSERT(!_2ndList.isEmpty()); |
240 |
} |
241 |
|
242 |
{ |
243 |
RTList<int>::Iterator iter = _2ndList.first(); |
244 |
for (int i = ELEMENTS - 1; i >= 0; i--) { |
245 |
CPPUNIT_ASSERT(*iter == i); |
246 |
CPPUNIT_ASSERT(iter != _2ndList.begin()); |
247 |
CPPUNIT_ASSERT(iter != _2ndList.end()); |
248 |
iter++; |
249 |
} |
250 |
CPPUNIT_ASSERT(iter == _2ndList.end()); |
251 |
} |
252 |
|
253 |
_2ndList.clear(); |
254 |
CPPUNIT_ASSERT(_2ndList.isEmpty()); |
255 |
CPPUNIT_ASSERT(!pPool->poolIsEmpty()); |
256 |
} |
257 |
|
258 |
void PoolTest::testFreeList() { |
259 |
//cout << "testFreeList()" << flush; |
260 |
if (pList) delete pList; |
261 |
} |
262 |
|
263 |
void PoolTest::testFreePool() { |
264 |
//cout << "testFreePool()" << flush; |
265 |
if (pPool) delete pPool; |
266 |
} |
267 |
|
268 |
void PoolTest::testAccessElements() { |
269 |
Pool<foo_t> pool(100); |
270 |
CPPUNIT_ASSERT(!pool.poolIsEmpty()); |
271 |
CPPUNIT_ASSERT(pool.isEmpty()); |
272 |
|
273 |
for (int i = 0; i < 100; i++) pool.allocAppend(); |
274 |
CPPUNIT_ASSERT(pool.poolIsEmpty()); |
275 |
CPPUNIT_ASSERT(!pool.isEmpty()); |
276 |
|
277 |
Pool<foo_t>::Iterator it = pool.first(); |
278 |
for (int i = 0; i < 100; i++) { |
279 |
it->bar = i; |
280 |
++it; |
281 |
} |
282 |
|
283 |
it = pool.last(); |
284 |
for (int i = 99; i >= 0; i--) { |
285 |
CPPUNIT_ASSERT(it->bar == i); |
286 |
--it; |
287 |
} |
288 |
|
289 |
pool.clear(); |
290 |
CPPUNIT_ASSERT(!pool.poolIsEmpty()); |
291 |
CPPUNIT_ASSERT(pool.isEmpty()); |
292 |
} |
293 |
|
294 |
void PoolTest::testInvalidIterators() { |
295 |
RTList<foo_t>::Iterator itNothing; |
296 |
bool passed = (itNothing) ? false : true; |
297 |
CPPUNIT_ASSERT(passed); |
298 |
CPPUNIT_ASSERT(itNothing != true); |
299 |
CPPUNIT_ASSERT(!itNothing); |
300 |
|
301 |
Pool<foo_t> pool(10); |
302 |
passed = (pool.begin()) ? false : true; |
303 |
CPPUNIT_ASSERT(passed); |
304 |
CPPUNIT_ASSERT(pool.begin() != true); |
305 |
CPPUNIT_ASSERT(!pool.begin()); |
306 |
passed = (pool.end()) ? false : true; |
307 |
CPPUNIT_ASSERT(passed); |
308 |
CPPUNIT_ASSERT(pool.end() != true); |
309 |
CPPUNIT_ASSERT(!pool.end()); |
310 |
} |