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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 622 - (hide annotations)
Wed Mar 22 14:00:08 2006 UTC (13 years, 7 months ago) by robwdcock
Original Path: branches/ROBW_XPLATFORM/escript/test/Taipan/TaipanTestCase.cpp
File size: 12548 byte(s)
Creating a private branch so I can test and verify cross-platform build
1 jgs 121 /*
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 jgs 474 #include "Taipan.h"
16 jgs 468 #include "EsysException.h"
17 jgs 121
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 jgs 151 void TaipanTestCase::testN1() {
40 jgs 121
41 jgs 151 // 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 jgs 121 cout << endl;
45    
46     Taipan t;
47    
48 jgs 151 const int dim = 1;
49 jgs 121
50 jgs 151 double* array;
51     double* arraY[10];
52 jgs 121
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 jgs 151 assert(t.num_arrays() == 1);
67     assert(t.num_elements() == 1);
68 jgs 121
69 jgs 151 cout << "\tTest Taipan memTable with ten arrays of size 1 under management." << endl;
70 jgs 121
71 jgs 151 // allocate all ten arrays
72     // the number of arrays under management will increase with each allocation
73     arraY[10];
74 jgs 121 for (int i=0; i<10; i++) {
75 jgs 151 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 jgs 121 }
81    
82     for (int i=0; i<10; i++) {
83 jgs 151 arraY[i][0] = i;
84 jgs 121 }
85     for (int i=0; i<10; i++) {
86 jgs 151 assert(arraY[i][0] == i);
87 jgs 121 }
88    
89 jgs 151 // 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 jgs 121 assert(t.num_elements() == 10);
131 jgs 151 }
132 jgs 121
133 jgs 151 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 jgs 121 t.delete_array(array);
157    
158 jgs 151 assert(t.num_arrays() == 1);
159 jgs 121 assert(t.num_elements() == 0);
160 jgs 151
161 jgs 121 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 jgs 151 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 jgs 121 assert(t.num_arrays() == 0);
226     assert(t.num_elements() == 0);
227    
228 jgs 151 cout << "\tTest Taipan memTable with one array of size 10 under management." << endl;
229 jgs 121
230 jgs 151 array = t.new_array(dim,10);
231    
232 jgs 121 for (int i=0; i<10; i++) {
233 jgs 151 assert(array[i] == 0.0);
234 jgs 121 }
235    
236     for (int i=0; i<10; i++) {
237 jgs 151 array[i] = i;
238 jgs 121 }
239     for (int i=0; i<10; i++) {
240 jgs 151 assert(array[i] == i);
241 jgs 121 }
242    
243 jgs 151 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 jgs 121
248 jgs 151 t.delete_array(array);
249 jgs 121
250     assert(t.num_arrays() == 0);
251     assert(t.num_elements() == 0);
252 jgs 151
253 jgs 121 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 jgs 151 arraY[0] = t.new_array(dim,2);
342     arraY[1] = t.new_array(dim,2);
343     arraY[2] = t.new_array(dim,2);
344 jgs 121 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 jgs 151 assert(t.num_arrays(2) == 3);
354 jgs 121 assert(t.num_arrays(10) == 3);
355     assert(t.num_arrays(100) == 4);
356 jgs 151 assert(t.num_free(2) == 0);
357 jgs 121 assert(t.num_free(10) == 0);
358     assert(t.num_free(100) == 0);
359 jgs 151 assert(t.num_elements() == 436);
360 jgs 121
361 jgs 151 // delete all but the first array of each size under management
362 jgs 121 // the number of arrays under management does not change
363     // but the number of free arrays will increase with each deallocation
364 jgs 151 t.delete_array(arraY[1]);
365 jgs 121 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 jgs 151 assert(t.num_arrays(2) == 3);
374 jgs 121 assert(t.num_arrays(10) == 3);
375     assert(t.num_arrays(100) == 4);
376 jgs 151 assert(t.num_free(2) == 2);
377 jgs 121 assert(t.num_free(10) == 2);
378     assert(t.num_free(100) == 3);
379 jgs 151 assert(t.num_elements() == 436);
380 jgs 121
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 jgs 151 arraY[1] = t.new_array(dim,2);
385     arraY[2] = t.new_array(dim,2);
386 jgs 121 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 jgs 151 assert(t.num_arrays(2) == 3);
394 jgs 121 assert(t.num_arrays(10) == 3);
395     assert(t.num_arrays(100) == 4);
396 jgs 151 assert(t.num_free(2) == 0);
397 jgs 121 assert(t.num_free(10) == 0);
398     assert(t.num_free(100) == 0);
399 jgs 151 assert(t.num_elements() == 436);
400 jgs 121
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 jgs 151 t.delete_array(arraY[1]);
405 jgs 121 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 jgs 151 assert(t.num_arrays(2) == 3);
414 jgs 121 assert(t.num_arrays(10) == 3);
415     assert(t.num_arrays(100) == 4);
416 jgs 151 assert(t.num_free(2) == 2);
417 jgs 121 assert(t.num_free(10) == 2);
418     assert(t.num_free(100) == 3);
419 jgs 151 assert(t.num_elements() == 436);
420 jgs 121
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 jgs 151 testSuite->addTest (new TestCaller< TaipanTestCase>("testN1",&TaipanTestCase::testN1));
438     testSuite->addTest (new TestCaller< TaipanTestCase>("testN0",&TaipanTestCase::testN0));
439 jgs 121 return testSuite;
440     }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26