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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1387 - (hide annotations)
Fri Jan 11 07:45:26 2008 UTC (11 years, 10 months ago) by trankine
Original Path: temp/escript/test/TaipanTestCase.cpp
File size: 12129 byte(s)
Restore the trunk that existed before the windows changes were committed to the (now moved to branches) old trunk.
1 jgs 121
2 ksteube 1312 /* $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 robwdcock 670 #include "escript/Taipan.h"
17 robwdcock 638 #include "esysUtils/EsysException.h"
18 jgs 121
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 jgs 151 void TaipanTestCase::testN1() {
41 jgs 121
42 jgs 151 // 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 jgs 121 cout << endl;
46    
47     Taipan t;
48    
49 jgs 151 const int dim = 1;
50 jgs 121
51 jgs 151 double* array;
52     double* arraY[10];
53 jgs 121
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 jgs 151 assert(t.num_arrays() == 1);
68     assert(t.num_elements() == 1);
69 jgs 121
70 jgs 151 cout << "\tTest Taipan memTable with ten arrays of size 1 under management." << endl;
71 jgs 121
72 jgs 151 // allocate all ten arrays
73     // the number of arrays under management will increase with each allocation
74     arraY[10];
75 jgs 121 for (int i=0; i<10; i++) {
76 jgs 151 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 jgs 121 }
82    
83     for (int i=0; i<10; i++) {
84 jgs 151 arraY[i][0] = i;
85 jgs 121 }
86     for (int i=0; i<10; i++) {
87 jgs 151 assert(arraY[i][0] == i);
88 jgs 121 }
89    
90 jgs 151 // 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 jgs 121 assert(t.num_elements() == 10);
132 jgs 151 }
133 jgs 121
134 jgs 151 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 jgs 121 t.delete_array(array);
158    
159 jgs 151 assert(t.num_arrays() == 1);
160 jgs 121 assert(t.num_elements() == 0);
161 jgs 151
162 jgs 121 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 jgs 151 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 jgs 121 assert(t.num_arrays() == 0);
227     assert(t.num_elements() == 0);
228    
229 jgs 151 cout << "\tTest Taipan memTable with one array of size 10 under management." << endl;
230 jgs 121
231 jgs 151 array = t.new_array(dim,10);
232    
233 jgs 121 for (int i=0; i<10; i++) {
234 jgs 151 assert(array[i] == 0.0);
235 jgs 121 }
236    
237     for (int i=0; i<10; i++) {
238 jgs 151 array[i] = i;
239 jgs 121 }
240     for (int i=0; i<10; i++) {
241 jgs 151 assert(array[i] == i);
242 jgs 121 }
243    
244 jgs 151 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 jgs 121
249 jgs 151 t.delete_array(array);
250 jgs 121
251     assert(t.num_arrays() == 0);
252     assert(t.num_elements() == 0);
253 jgs 151
254 jgs 121 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 jgs 151 arraY[0] = t.new_array(dim,2);
343     arraY[1] = t.new_array(dim,2);
344     arraY[2] = t.new_array(dim,2);
345 jgs 121 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 jgs 151 assert(t.num_arrays(2) == 3);
355 jgs 121 assert(t.num_arrays(10) == 3);
356     assert(t.num_arrays(100) == 4);
357 jgs 151 assert(t.num_free(2) == 0);
358 jgs 121 assert(t.num_free(10) == 0);
359     assert(t.num_free(100) == 0);
360 jgs 151 assert(t.num_elements() == 436);
361 jgs 121
362 jgs 151 // delete all but the first array of each size under management
363 jgs 121 // the number of arrays under management does not change
364     // but the number of free arrays will increase with each deallocation
365 jgs 151 t.delete_array(arraY[1]);
366 jgs 121 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 jgs 151 assert(t.num_arrays(2) == 3);
375 jgs 121 assert(t.num_arrays(10) == 3);
376     assert(t.num_arrays(100) == 4);
377 jgs 151 assert(t.num_free(2) == 2);
378 jgs 121 assert(t.num_free(10) == 2);
379     assert(t.num_free(100) == 3);
380 jgs 151 assert(t.num_elements() == 436);
381 jgs 121
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 jgs 151 arraY[1] = t.new_array(dim,2);
386     arraY[2] = t.new_array(dim,2);
387 jgs 121 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 jgs 151 assert(t.num_arrays(2) == 3);
395 jgs 121 assert(t.num_arrays(10) == 3);
396     assert(t.num_arrays(100) == 4);
397 jgs 151 assert(t.num_free(2) == 0);
398 jgs 121 assert(t.num_free(10) == 0);
399     assert(t.num_free(100) == 0);
400 jgs 151 assert(t.num_elements() == 436);
401 jgs 121
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 jgs 151 t.delete_array(arraY[1]);
406 jgs 121 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 jgs 151 assert(t.num_arrays(2) == 3);
415 jgs 121 assert(t.num_arrays(10) == 3);
416     assert(t.num_arrays(100) == 4);
417 jgs 151 assert(t.num_free(2) == 2);
418 jgs 121 assert(t.num_free(10) == 2);
419     assert(t.num_free(100) == 3);
420 jgs 151 assert(t.num_elements() == 436);
421 jgs 121
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 jgs 151 testSuite->addTest (new TestCaller< TaipanTestCase>("testN1",&TaipanTestCase::testN1));
439     testSuite->addTest (new TestCaller< TaipanTestCase>("testN0",&TaipanTestCase::testN0));
440 jgs 121 return testSuite;
441     }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26