1 |
/* |
2 |
***************************************************************************** |
3 |
* * |
4 |
* COPYRIGHT ACcESS - All Rights Reserved * |
5 |
* * |
6 |
* This software is the property of ACcESS. No part of this code * |
7 |
* may be copied in any form or by any means without the expressed written * |
8 |
* consent of ACcESS. Copying, use or modification of this software * |
9 |
* by any unauthorised person is illegal unless that person has a software * |
10 |
* license agreement with ACcESS. * |
11 |
* * |
12 |
***************************************************************************** |
13 |
*/ |
14 |
|
15 |
#include "Taipan.h" |
16 |
#include "EsysException.h" |
17 |
|
18 |
#include "TaipanTestCase.h" |
19 |
|
20 |
#include <iostream> |
21 |
|
22 |
using namespace std; |
23 |
using namespace escript; |
24 |
using namespace esysUtils; |
25 |
using namespace CppUnitTest; |
26 |
|
27 |
void TaipanTestCase::setUp() { |
28 |
// |
29 |
// This is called before each test is run |
30 |
|
31 |
} |
32 |
|
33 |
void TaipanTestCase::tearDown() { |
34 |
// |
35 |
// This is called after each test has been run |
36 |
|
37 |
} |
38 |
|
39 |
void TaipanTestCase::testN1() { |
40 |
|
41 |
// Arrays with N=1 are handled differently by the Taipan memory manager, these |
42 |
// are never disposed of to maximise reusability, so they are tested seperately. |
43 |
|
44 |
cout << endl; |
45 |
|
46 |
Taipan t; |
47 |
|
48 |
const int dim = 1; |
49 |
|
50 |
double* array; |
51 |
double* arraY[10]; |
52 |
|
53 |
cout << "\tTest Taipan memTable with one array of size 1 under management." << endl; |
54 |
|
55 |
array = t.new_array(dim,1); |
56 |
|
57 |
assert(array[0] == 0.0); |
58 |
|
59 |
assert(t.num_arrays() == 1); |
60 |
assert(t.num_arrays(1) == 1); |
61 |
assert(t.num_free(1) == 0); |
62 |
assert(t.num_elements() == 1); |
63 |
|
64 |
t.delete_array(array); |
65 |
|
66 |
assert(t.num_arrays() == 1); |
67 |
assert(t.num_elements() == 1); |
68 |
|
69 |
cout << "\tTest Taipan memTable with ten arrays of size 1 under management." << endl; |
70 |
|
71 |
// allocate all ten arrays |
72 |
// the number of arrays under management will increase with each allocation |
73 |
arraY[10]; |
74 |
for (int i=0; i<10; i++) { |
75 |
arraY[i] = t.new_array(dim,1); |
76 |
assert(t.num_arrays() == i+1); |
77 |
assert(t.num_arrays(1) == i+1); |
78 |
assert(t.num_free(1) == 0); |
79 |
assert(t.num_elements() == i+1); |
80 |
} |
81 |
|
82 |
for (int i=0; i<10; i++) { |
83 |
arraY[i][0] = i; |
84 |
} |
85 |
for (int i=0; i<10; i++) { |
86 |
assert(arraY[i][0] == i); |
87 |
} |
88 |
|
89 |
// delete all but the last array under management |
90 |
// the number of arrays under management does not change |
91 |
// but the number of free arrays will increase with each deallocation |
92 |
for (int i=0; i<9; i++) { |
93 |
t.delete_array(arraY[i]); |
94 |
assert(t.num_arrays() == 10); |
95 |
assert(t.num_arrays(1) == 10); |
96 |
assert(t.num_free(1) == i+1); |
97 |
assert(t.num_elements() == 10); |
98 |
} |
99 |
assert(arraY[9][0] == 9); |
100 |
|
101 |
// now reallocate the 9 free arrays |
102 |
// the preserved arrays will be reused |
103 |
// and the number of free arrays will decrease with each allocation |
104 |
for (int i=0; i<9; i++) { |
105 |
arraY[i] = t.new_array(dim,1); |
106 |
assert(t.num_arrays() == 10); |
107 |
assert(t.num_arrays(1) == 10); |
108 |
assert(t.num_free(1) == 8-i); |
109 |
assert(t.num_elements() == 10); |
110 |
} |
111 |
for (int i=0; i<10; i++) { |
112 |
assert(arraY[i][0] == i); |
113 |
} |
114 |
|
115 |
// delete all but the last array under management |
116 |
// the number of arrays under management does not change |
117 |
// but the number of free arrays will increase with each deallocation |
118 |
for (int i=0; i<9; i++) { |
119 |
t.delete_array(arraY[i]); |
120 |
assert(t.num_arrays() == 10); |
121 |
assert(t.num_arrays(1) == 10); |
122 |
assert(t.num_free(1) == i+1); |
123 |
assert(t.num_elements() == 10); |
124 |
} |
125 |
assert(arraY[9][0] == 9); |
126 |
|
127 |
// finally delete the last array |
128 |
t.delete_array(arraY[9]); |
129 |
assert(t.num_arrays() == 10); |
130 |
assert(t.num_elements() == 10); |
131 |
} |
132 |
|
133 |
void TaipanTestCase::testN0() { |
134 |
|
135 |
// Arrays with N=0 are handled differently by the Taipan memory manager, these |
136 |
// are never disposed of to maximise reusability, so they are tested seperately. |
137 |
|
138 |
cout << endl; |
139 |
|
140 |
Taipan t; |
141 |
|
142 |
const int dim = 1; |
143 |
|
144 |
double* array; |
145 |
double* arraY[10]; |
146 |
|
147 |
cout << "\tTest Taipan memTable with one array of size 0 under management." << endl; |
148 |
|
149 |
array = t.new_array(dim,0); |
150 |
|
151 |
assert(t.num_arrays() == 1); |
152 |
assert(t.num_arrays(0) == 1); |
153 |
assert(t.num_free(0) == 0); |
154 |
assert(t.num_elements() == 0); |
155 |
|
156 |
t.delete_array(array); |
157 |
|
158 |
assert(t.num_arrays() == 1); |
159 |
assert(t.num_elements() == 0); |
160 |
|
161 |
cout << "\tTest Taipan memTable with ten arrays of size 0 under management." << endl; |
162 |
|
163 |
// allocate all ten arrays |
164 |
// the number of arrays under management will increase with each allocation |
165 |
for (int i=0; i<10; i++) { |
166 |
arraY[i] = t.new_array(dim,0); |
167 |
assert(t.num_arrays() == i+1); |
168 |
assert(t.num_arrays(0) == i+1); |
169 |
assert(t.num_free(0) == 0); |
170 |
assert(t.num_elements() == 0); |
171 |
} |
172 |
|
173 |
// delete all but the last array under management |
174 |
// the number of arrays under management does not change |
175 |
// but the number of free arrays will increase with each deallocation |
176 |
for (int i=0; i<9; i++) { |
177 |
t.delete_array(arraY[i]); |
178 |
assert(t.num_arrays() == 10); |
179 |
assert(t.num_arrays(0) == 10); |
180 |
assert(t.num_free(0) == i+1); |
181 |
assert(t.num_elements() == 0); |
182 |
} |
183 |
|
184 |
// now reallocate the 9 free arrays |
185 |
// the preserved arrays will be reused |
186 |
// and the number of free arrays will decrease with each allocation |
187 |
for (int i=0; i<9; i++) { |
188 |
arraY[i] = t.new_array(dim,0); |
189 |
assert(t.num_arrays() == 10); |
190 |
assert(t.num_arrays(0) == 10); |
191 |
assert(t.num_free(0) == 8-i); |
192 |
assert(t.num_elements() == 0); |
193 |
} |
194 |
|
195 |
// delete all but the last array under management |
196 |
// the number of arrays under management does not change |
197 |
// but the number of free arrays will increase with each deallocation |
198 |
for (int i=0; i<9; i++) { |
199 |
t.delete_array(arraY[i]); |
200 |
assert(t.num_arrays() == 10); |
201 |
assert(t.num_arrays(0) == 10); |
202 |
assert(t.num_free(0) == i+1); |
203 |
assert(t.num_elements() == 0); |
204 |
} |
205 |
|
206 |
// finally delete the last array |
207 |
t.delete_array(arraY[9]); |
208 |
assert(t.num_arrays() == 10); |
209 |
assert(t.num_elements() == 0); |
210 |
} |
211 |
|
212 |
void TaipanTestCase::testAll() { |
213 |
|
214 |
cout << endl; |
215 |
|
216 |
Taipan t; |
217 |
|
218 |
double* array; |
219 |
double* arraY[10]; |
220 |
|
221 |
const int dim = 1; |
222 |
|
223 |
cout << "\tTest empty Taipan memTable." << endl; |
224 |
|
225 |
assert(t.num_arrays() == 0); |
226 |
assert(t.num_elements() == 0); |
227 |
|
228 |
cout << "\tTest Taipan memTable with one array of size 10 under management." << endl; |
229 |
|
230 |
array = t.new_array(dim,10); |
231 |
|
232 |
for (int i=0; i<10; i++) { |
233 |
assert(array[i] == 0.0); |
234 |
} |
235 |
|
236 |
for (int i=0; i<10; i++) { |
237 |
array[i] = i; |
238 |
} |
239 |
for (int i=0; i<10; i++) { |
240 |
assert(array[i] == i); |
241 |
} |
242 |
|
243 |
assert(t.num_arrays() == 1); |
244 |
assert(t.num_arrays(10) == 1); |
245 |
assert(t.num_free(10) == 0); |
246 |
assert(t.num_elements() == 10); |
247 |
|
248 |
t.delete_array(array); |
249 |
|
250 |
assert(t.num_arrays() == 0); |
251 |
assert(t.num_elements() == 0); |
252 |
|
253 |
cout << "\tTest Taipan memTable with ten arrays of size 10 under management." << endl; |
254 |
|
255 |
// allocate all ten arrays |
256 |
// the number of arrays under management will increase with each allocation |
257 |
arraY[10]; |
258 |
for (int i=0; i<10; i++) { |
259 |
arraY[i] = t.new_array(dim,10); |
260 |
assert(t.num_arrays() == i+1); |
261 |
assert(t.num_arrays(10) == i+1); |
262 |
assert(t.num_free(10) == 0); |
263 |
assert(t.num_elements() == (i+1)*10); |
264 |
} |
265 |
|
266 |
int val = 0; |
267 |
for (int i=0; i<10; i++) { |
268 |
for (int j=0; j<10; j++) { |
269 |
arraY[i][j] = val; |
270 |
val++; |
271 |
} |
272 |
} |
273 |
val = 0; |
274 |
for (int i=0; i<10; i++) { |
275 |
for (int j=0; j<10; j++) { |
276 |
assert(arraY[i][j] == val); |
277 |
val++; |
278 |
} |
279 |
} |
280 |
|
281 |
// delete all but the last array under management |
282 |
// the number of arrays under management does not change |
283 |
// but the number of free arrays will increase with each deallocation |
284 |
for (int i=0; i<9; i++) { |
285 |
t.delete_array(arraY[i]); |
286 |
assert(t.num_arrays() == 10); |
287 |
assert(t.num_arrays(10) == 10); |
288 |
assert(t.num_free(10) == i+1); |
289 |
assert(t.num_elements() == 100); |
290 |
} |
291 |
val = 90; |
292 |
for (int j=0; j<10; j++) { |
293 |
assert(arraY[9][j] == val); |
294 |
val++; |
295 |
} |
296 |
|
297 |
// now reallocate the 9 free arrays |
298 |
// the preserved arrays will be reused |
299 |
// and the number of free arrays will decrease with each allocation |
300 |
for (int i=0; i<9; i++) { |
301 |
arraY[i] = t.new_array(dim,10); |
302 |
assert(t.num_arrays() == 10); |
303 |
assert(t.num_arrays(10) == 10); |
304 |
assert(t.num_free(10) == 8-i); |
305 |
assert(t.num_elements() == 100); |
306 |
} |
307 |
val = 0; |
308 |
for (int i=0; i<10; i++) { |
309 |
for (int j=0; j<10; j++) { |
310 |
assert(arraY[i][j] == val); |
311 |
val++; |
312 |
} |
313 |
} |
314 |
|
315 |
// delete all but the last array under management |
316 |
// the number of arrays under management does not change |
317 |
// but the number of free arrays will increase with each deallocation |
318 |
for (int i=0; i<9; i++) { |
319 |
t.delete_array(arraY[i]); |
320 |
assert(t.num_arrays() == 10); |
321 |
assert(t.num_arrays(10) == 10); |
322 |
assert(t.num_free(10) == i+1); |
323 |
assert(t.num_elements() == 100); |
324 |
} |
325 |
val = 90; |
326 |
for (int j=0; j<10; j++) { |
327 |
assert(arraY[9][j] == val); |
328 |
val++; |
329 |
} |
330 |
|
331 |
// finally delete the last array |
332 |
// all arrays under management are deleted |
333 |
t.delete_array(arraY[9]); |
334 |
assert(t.num_arrays() == 0); |
335 |
assert(t.num_elements() == 0); |
336 |
|
337 |
cout << "\tTest Taipan memTable with ten arrays of various sizes under management." << endl; |
338 |
|
339 |
// allocate all ten arrays |
340 |
// the number of arrays under management will increase with each allocation |
341 |
arraY[0] = t.new_array(dim,2); |
342 |
arraY[1] = t.new_array(dim,2); |
343 |
arraY[2] = t.new_array(dim,2); |
344 |
arraY[3] = t.new_array(dim,10); |
345 |
arraY[4] = t.new_array(dim,10); |
346 |
arraY[5] = t.new_array(dim,10); |
347 |
arraY[6] = t.new_array(dim,100); |
348 |
arraY[7] = t.new_array(dim,100); |
349 |
arraY[8] = t.new_array(dim,100); |
350 |
arraY[9] = t.new_array(dim,100); |
351 |
|
352 |
assert(t.num_arrays() == 10); |
353 |
assert(t.num_arrays(2) == 3); |
354 |
assert(t.num_arrays(10) == 3); |
355 |
assert(t.num_arrays(100) == 4); |
356 |
assert(t.num_free(2) == 0); |
357 |
assert(t.num_free(10) == 0); |
358 |
assert(t.num_free(100) == 0); |
359 |
assert(t.num_elements() == 436); |
360 |
|
361 |
// delete all but the first array of each size under management |
362 |
// the number of arrays under management does not change |
363 |
// but the number of free arrays will increase with each deallocation |
364 |
t.delete_array(arraY[1]); |
365 |
t.delete_array(arraY[2]); |
366 |
t.delete_array(arraY[4]); |
367 |
t.delete_array(arraY[5]); |
368 |
t.delete_array(arraY[7]); |
369 |
t.delete_array(arraY[8]); |
370 |
t.delete_array(arraY[9]); |
371 |
|
372 |
assert(t.num_arrays() == 10); |
373 |
assert(t.num_arrays(2) == 3); |
374 |
assert(t.num_arrays(10) == 3); |
375 |
assert(t.num_arrays(100) == 4); |
376 |
assert(t.num_free(2) == 2); |
377 |
assert(t.num_free(10) == 2); |
378 |
assert(t.num_free(100) == 3); |
379 |
assert(t.num_elements() == 436); |
380 |
|
381 |
// now reallocate the free arrays |
382 |
// the preserved arrays will be reused |
383 |
// and the number of free arrays will decrease with each allocation |
384 |
arraY[1] = t.new_array(dim,2); |
385 |
arraY[2] = t.new_array(dim,2); |
386 |
arraY[4] = t.new_array(dim,10); |
387 |
arraY[5] = t.new_array(dim,10); |
388 |
arraY[7] = t.new_array(dim,100); |
389 |
arraY[8] = t.new_array(dim,100); |
390 |
arraY[9] = t.new_array(dim,100); |
391 |
|
392 |
assert(t.num_arrays() == 10); |
393 |
assert(t.num_arrays(2) == 3); |
394 |
assert(t.num_arrays(10) == 3); |
395 |
assert(t.num_arrays(100) == 4); |
396 |
assert(t.num_free(2) == 0); |
397 |
assert(t.num_free(10) == 0); |
398 |
assert(t.num_free(100) == 0); |
399 |
assert(t.num_elements() == 436); |
400 |
|
401 |
// delete all but the last array of each size under management |
402 |
// the number of arrays under management does not change |
403 |
// but the number of free arrays will increase with each deallocation |
404 |
t.delete_array(arraY[1]); |
405 |
t.delete_array(arraY[2]); |
406 |
t.delete_array(arraY[4]); |
407 |
t.delete_array(arraY[5]); |
408 |
t.delete_array(arraY[7]); |
409 |
t.delete_array(arraY[8]); |
410 |
t.delete_array(arraY[9]); |
411 |
|
412 |
assert(t.num_arrays() == 10); |
413 |
assert(t.num_arrays(2) == 3); |
414 |
assert(t.num_arrays(10) == 3); |
415 |
assert(t.num_arrays(100) == 4); |
416 |
assert(t.num_free(2) == 2); |
417 |
assert(t.num_free(10) == 2); |
418 |
assert(t.num_free(100) == 3); |
419 |
assert(t.num_elements() == 436); |
420 |
|
421 |
// deallocate the last of the arrays |
422 |
t.delete_array(arraY[0]); |
423 |
t.delete_array(arraY[3]); |
424 |
t.delete_array(arraY[6]); |
425 |
|
426 |
assert(t.num_arrays() == 0); |
427 |
assert(t.num_elements() == 0); |
428 |
} |
429 |
|
430 |
TestSuite* TaipanTestCase::suite () |
431 |
{ |
432 |
// |
433 |
// create the suite of tests to perform. |
434 |
TestSuite *testSuite = new TestSuite ("TaipanTestCase"); |
435 |
|
436 |
testSuite->addTest (new TestCaller< TaipanTestCase>("testAll",&TaipanTestCase::testAll)); |
437 |
testSuite->addTest (new TestCaller< TaipanTestCase>("testN1",&TaipanTestCase::testN1)); |
438 |
testSuite->addTest (new TestCaller< TaipanTestCase>("testN0",&TaipanTestCase::testN0)); |
439 |
return testSuite; |
440 |
} |