/[escript]/trunk/escript/test/TaipanTestCase.cpp
ViewVC logotype

Contents of /trunk/escript/test/TaipanTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26