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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2005 - (show annotations)
Mon Nov 10 01:21:39 2008 UTC (10 years, 11 months ago) by jfenwick
File size: 17537 byte(s)
Bringing all changes across from schroedinger.
(Note this does not mean development is done, just that it will happen
on the trunk for now).
If anyone notices any problems please contact me.


1
2 /*******************************************************
3 *
4 * Copyright (c) 2003-2008 by University of Queensland
5 * Earth Systems Science Computational Center (ESSCC)
6 * http://www.uq.edu.au/esscc
7 *
8 * Primary Business: Queensland, Australia
9 * Licensed under the Open Software License version 3.0
10 * http://www.opensource.org/licenses/osl-3.0.php
11 *
12 *******************************************************/
13
14
15 #include "escript/FunctionSpace.h"
16 #include "escript/DataExpanded.h"
17 #include "esysUtils/EsysException.h"
18 #include "DataExpandedTestCase.h"
19 #include "escript/DataReady.h"
20
21 #include <iostream>
22
23 using namespace CppUnitTest;
24 using namespace escript;
25 using namespace std;
26 using namespace esysUtils;
27 using namespace escript::DataTypes;
28
29 void DataExpandedTestCase::setUp() {
30 //
31 // This is called before each test is run
32
33 }
34
35 void DataExpandedTestCase::tearDown() {
36 //
37 // This is called after each test has been run
38
39 }
40
41 namespace
42 {
43
44 ValueType::reference
45 getRef(DataReady& data,int i, int j)
46 {
47 return data.getVector()[getRelIndex(data.getShape(),i,j)];
48 }
49
50 ValueType::reference
51 getRef(DataReady& data,int i, int j,int k)
52 {
53 return data.getVector()[getRelIndex(data.getShape(),i,j,k)];
54 }
55
56 ValueType::reference
57 getDRef(ValueType& data,const ShapeType& shape,int i, int j)
58 {
59 return data[getRelIndex(shape,i,j)];
60 }
61
62 ValueType::reference
63 getDRef(ValueType& data,const ShapeType& shape,int i, int j, int k)
64 {
65 return data[getRelIndex(shape,i,j,k)];
66 }
67
68 DataReady_ptr
69 resolveAndDelete(DataAbstract* p)
70 {
71 DataReady_ptr p2=p->resolve();
72 if (p!=p2.get())
73 {
74 delete p;
75 }
76 return p2;
77 }
78
79 }
80
81
82 void DataExpandedTestCase::testAll() {
83
84 cout << endl;
85
86 //
87 // Create a rank 1 pointData
88 DataTypes::ShapeType shape;
89 shape.push_back(3);
90 DataTypes::ValueType data(DataTypes::noValues(shape),0);
91 // DataArrayView pointData(data,shape);
92
93 //
94 // Assign an arbitrary value
95 data[0]=0.0;
96 data[1]=1.0;
97 data[2]=2.0;
98
99 //
100 // Test constructor
101 cout << "\tTest DataExpanded constructor." << endl;
102 DataExpanded testData(FunctionSpace(), shape, data);
103
104 // cout << "\tTest getLength." << endl;
105 // assert(testData.getLength()==pointData.getNoValues());
106
107 cout << "\tTest getDataAtOffset." << endl;
108 for (int i=0;i<testData.getShape()[0];i++) {
109 assert(testData.getDataAtOffset(i)==data[i]);
110 }
111
112 cout << "\tVerify data point attributes." << endl;
113 // DataArrayView dataView=testData.getPointDataView();
114 assert(testData.getRank()==shape.size());
115 assert(testData.getNoValues()==shape[0]*1);
116 assert(testData.getShape()[0]==shape[0]);
117 assert(testData.getNumDPPSample()==1);
118 assert(testData.getNumSamples()==1);
119 assert(testData.validSamplePointNo(testData.getNumDPPSample()-1));
120 assert(testData.validSampleNo(testData.getNumSamples()-1));
121
122 //
123 // Test alternative constructor
124 cout << "\tTest DataExpanded alternative constructor." << endl;
125 data[0]=0.0;
126 data[1]=1.0;
127 data[2]=2.0;
128 FunctionSpace tmp_fns;
129 DataExpanded testData1(tmp_fns,shape,data);
130
131 // cout << "\tTest getLength." << endl;
132 // assert(testData1.getLength()==pointData.noValues());
133
134 // cout << "\tTest getPointDataView." << endl;
135 // for (int i=0;i<testData1.getPointDataView().getShape()[0];i++) {
136 // assert(testData1.getPointDataView()(i)==pointData(i));
137 // }
138
139 cout << "\tVerify data point attributes." << endl;
140 // dataView=testData1.getPointDataView();
141 assert(testData1.getRank()==shape.size());
142 assert(testData1.getNoValues()==shape[0]*1);
143 assert(testData1.getShape()[0]==shape[0]);
144 assert(testData1.getNumDPPSample()==1);
145 assert(testData1.getNumSamples()==1);
146 assert(testData1.validSamplePointNo(testData1.getNumDPPSample()-1));
147 assert(testData1.validSampleNo(testData1.getNumSamples()-1));
148
149 //
150 // Test copy constructor
151 cout << "\tTest DataExpanded copy constructor." << endl;
152 DataExpanded testData2(testData);
153
154 cout << "\tTest getLength." << endl;
155 assert(testData2.getLength()==data.size());
156
157 cout << "\tTest getPointDataView." << endl;
158 for (int i=0;i<testData2.getShape()[0];i++) {
159 assert(testData2.getDataAtOffset(i)==data[i]);
160 }
161
162 cout << "\tVerify data point attributes." << endl;
163 // dataView=testData2.getPointDataView();
164 assert(testData2.getRank()==shape.size());
165 assert(testData2.getNoValues()==shape[0]*1);
166 assert(testData2.getShape()[0]==shape[0]);
167 assert(testData2.getNumDPPSample()==1);
168 assert(testData2.getNumSamples()==1);
169 assert(testData2.validSamplePointNo(testData2.getNumDPPSample()-1));
170 assert(testData2.validSampleNo(testData2.getNumSamples()-1));
171
172 }
173
174
175 void DataExpandedTestCase::testSlicing() {
176
177 cout << endl;
178
179 //
180 // Create a rank 1 pointData
181 DataTypes::ShapeType shape;
182 shape.push_back(3);
183 DataTypes::ValueType data(DataTypes::noValues(shape),0);
184 // DataArrayView pointData(data,shape);
185
186 //
187 // Assign an arbitrary value
188 data[0]=0.0;
189 data[1]=1.0;
190 data[2]=2.0;
191
192 //
193 // Create object to test
194 cout << "\tCreate rank 1 DataExpanded object." << endl;
195 DataExpanded testData(FunctionSpace(),shape,data);
196
197 cout << "\tTest slicing (whole object)." << endl;
198 DataTypes::RegionType region;
199 region.push_back(DataTypes::RegionType::value_type(0,shape[0]));
200
201 DataReady_ptr testData2=resolveAndDelete(testData.getSlice(region));
202
203 //
204 // Verify data values
205 cout << "\tVerify data point values." << endl;
206 for (int i=0;i<testData2->getShape()[0];i++) {
207 assert(testData2->getDataAtOffset(i)==data[region[0].first+i]);
208 }
209
210 // cout << "\tVerify data point attributes." << endl;
211 // DataArrayView dataView=testData2->getPointDataView();
212 // assert(data.getRank()==shape.size());
213 // assert(data.getNoValues()==shape[0]*1);
214 // assert(data.getShape()[0]==shape[0]);
215
216 // delete testData2;
217 }
218
219 void DataExpandedTestCase::testSlicing2() {
220
221 cout << endl;
222
223 //
224 // Create a rank 2 pointData
225 DataTypes::ShapeType shape;
226 shape.push_back(3);
227 shape.push_back(3);
228 DataTypes::ValueType data(DataTypes::noValues(shape),0);
229 // DataArrayView pointData(data,shape);
230
231 //
232 // Assign an arbitrary value
233 getDRef(data,shape,0,0)=0.0;
234 getDRef(data,shape,1,0)=1.0;
235 getDRef(data,shape,2,0)=2.0;
236 getDRef(data,shape,0,1)=3.0;
237 getDRef(data,shape,1,1)=4.0;
238 getDRef(data,shape,2,1)=5.0;
239 getDRef(data,shape,0,2)=6.0;
240 getDRef(data,shape,1,2)=7.0;
241 getDRef(data,shape,2,2)=8.0;
242
243 //
244 // Create object to test
245 cout << "\tCreate rank 2 DataExpanded object." << endl;
246 DataExpanded testData(FunctionSpace(),shape,data);
247
248 cout << "\tTest slicing (part object)." << endl;
249 DataTypes::RegionType region;
250 region.push_back(DataTypes::RegionType::value_type(0,2));
251 region.push_back(DataTypes::RegionType::value_type(0,2));
252
253 DataReady_ptr testData2=resolveAndDelete(testData.getSlice(region));
254
255 //
256 // Verify data values
257 cout << "\tVerify data point values." << endl;
258 for (int j=0;j<testData2->getShape()[1];j++) {
259 for (int i=0;i<testData2->getShape()[0];i++) {
260 assert(getRef(*testData2,i,j)==data[getRelIndex(shape,region[0].first+i,region[1].first+j)]);
261 }
262 }
263
264 cout << "\tVerify data point attributes." << endl;
265 // DataArrayView dataView=testData2->getPointDataView();
266 assert(testData2->getRank()==region.size());
267 assert(testData2->getNoValues()==(region[0].second-region[0].first)*(region[1].second-region[1].first));
268 assert(testData2->getShape()[0]==(region[0].second-region[0].first));
269 assert(testData2->getShape()[1]==(region[1].second-region[1].first));
270
271 cout << "\tTest slicing (part object)." << endl;
272 DataTypes::RegionType region2;
273 region2.push_back(DataTypes::RegionType::value_type(1,3));
274 region2.push_back(DataTypes::RegionType::value_type(1,3));
275 DataReady_ptr testData3=resolveAndDelete(testData.getSlice(region2));
276 //
277 // Verify data values
278 cout << "\tVerify data point values." << endl;
279 for (int j=0;j<testData3->getShape()[1];j++) {
280 for (int i=0;i<testData3->getShape()[0];i++) {
281 assert(getRef(*testData3,i,j)==getDRef(data,shape,region2[0].first+i,region2[1].first+j));
282 }
283 }
284
285 cout << "\tVerify data point attributes." << endl;
286 // dataView=testData3->getPointDataView();
287 assert(testData3->getRank()==region2.size());
288 assert(testData3->getNoValues()==(region2[0].second-region2[0].first)*(region2[1].second-region2[1].first));
289 assert(testData3->getShape()[0]==(region2[0].second-region2[0].first));
290 assert(testData3->getShape()[1]==(region2[1].second-region2[1].first));
291
292 // delete testData2;
293 // delete testData3;
294
295 }
296
297 void DataExpandedTestCase::testSlicing3() {
298
299 cout << endl;
300
301 //
302 // Create a rank 3 pointData
303 DataTypes::ShapeType shape;
304 shape.push_back(3);
305 shape.push_back(3);
306 shape.push_back(3);
307 DataTypes::ValueType data(DataTypes::noValues(shape),0);
308 // DataArrayView pointData(data,shape);
309
310 //
311 // Assign an arbitrary value
312 getDRef(data,shape,0,0,0)=0.0;
313 getDRef(data,shape,1,0,0)=1.0;
314 getDRef(data,shape,2,0,0)=2.0;
315 getDRef(data,shape,0,1,0)=3.0;
316 getDRef(data,shape,1,1,0)=4.0;
317 getDRef(data,shape,2,1,0)=5.0;
318 getDRef(data,shape,0,2,0)=6.0;
319 getDRef(data,shape,1,2,0)=7.0;
320 getDRef(data,shape,2,2,0)=8.0;
321
322 getDRef(data,shape,0,0,1)=9.0;
323 getDRef(data,shape,1,0,1)=10.0;
324 getDRef(data,shape,2,0,1)=11.0;
325 getDRef(data,shape,0,1,1)=12.0;
326 getDRef(data,shape,1,1,1)=13.0;
327 getDRef(data,shape,2,1,1)=14.0;
328 getDRef(data,shape,0,2,1)=15.0;
329 getDRef(data,shape,1,2,1)=16.0;
330 getDRef(data,shape,2,2,1)=17.0;
331
332 getDRef(data,shape,0,0,2)=18.0;
333 getDRef(data,shape,1,0,2)=19.0;
334 getDRef(data,shape,2,0,2)=20.0;
335 getDRef(data,shape,0,1,2)=21.0;
336 getDRef(data,shape,1,1,2)=22.0;
337 getDRef(data,shape,2,1,2)=23.0;
338 getDRef(data,shape,0,2,2)=24.0;
339 getDRef(data,shape,1,2,2)=25.0;
340 getDRef(data,shape,2,2,2)=26.0;
341
342 //
343 // Create object to test
344 cout << "\tCreate rank 3 DataExpanded object." << endl;
345 DataExpanded testData(FunctionSpace(),shape,data);
346
347 cout << "\tTest slicing (part object)." << endl;
348 DataTypes::RegionType region;
349 region.push_back(DataTypes::RegionType::value_type(0,2));
350 region.push_back(DataTypes::RegionType::value_type(0,2));
351 region.push_back(DataTypes::RegionType::value_type(0,2));
352 DataReady_ptr testData2=resolveAndDelete(testData.getSlice(region));
353
354 //
355 // Verify data values
356 cout << "\tVerify data point values." << endl;
357 for (int k=0;k<testData2->getShape()[2];k++) {
358 for (int j=0;j<testData2->getShape()[1];j++) {
359 for (int i=0;i<testData2->getShape()[0];i++) {
360 assert(getRef(*testData2,i,j,k)==getDRef(data,shape,region[0].first+i,
361 region[1].first+j,
362 region[2].first+k));
363 }
364 }
365 }
366
367 cout << "\tVerify data point attributes." << endl;
368 // DataArrayView dataView=testData2->getPointDataView();
369 assert(testData2->getRank()==region.size());
370 assert(testData2->getNoValues()==(region[0].second-region[0].first)
371 *(region[1].second-region[1].first)
372 *(region[2].second-region[2].first));
373 assert(testData2->getShape()[0]==(region[0].second-region[0].first));
374 assert(testData2->getShape()[1]==(region[1].second-region[1].first));
375 assert(testData2->getShape()[2]==(region[2].second-region[2].first));
376
377 cout << "\tTest slicing (part object)." << endl;
378 DataTypes::RegionType region2;
379 region2.push_back(DataTypes::RegionType::value_type(1,3));
380 region2.push_back(DataTypes::RegionType::value_type(1,3));
381 region2.push_back(DataTypes::RegionType::value_type(1,3));
382 DataReady_ptr testData3=resolveAndDelete(testData.getSlice(region2));
383
384 //
385 // Verify data values
386 cout << "\tVerify data point values." << endl;
387 for (int k=0;k<testData3->getShape()[2];k++) {
388 for (int j=0;j<testData3->getShape()[1];j++) {
389 for (int i=0;i<testData3->getShape()[0];i++) {
390 assert(getRef(*testData3,i,j,k)==getDRef(data,shape,region2[0].first+i,
391 region2[1].first+j,
392 region2[2].first+k));
393 }
394 }
395 }
396
397 cout << "\tVerify data point attributes." << endl;
398 // dataView=testData2->getPointDataView();
399 assert(testData2->getRank()==region.size());
400 assert(testData2->getNoValues()==(region[0].second-region[0].first)
401 *(region[1].second-region[1].first)
402 *(region[2].second-region[2].first));
403 assert(testData2->getShape()[0]==(region[0].second-region[0].first));
404 assert(testData2->getShape()[1]==(region[1].second-region[1].first));
405 assert(testData2->getShape()[2]==(region[2].second-region[2].first));
406
407 // delete testData2;
408 // delete testData3;
409
410 }
411
412
413 void DataExpandedTestCase::testSliceSetting() {
414
415 cout << endl;
416
417 //
418 // Create a rank 2 pointData
419 DataTypes::ShapeType shape;
420 shape.push_back(2);
421 shape.push_back(2);
422 DataTypes::ValueType data(DataTypes::noValues(shape),0);
423 // DataArrayView pointData(data,shape);
424
425 //
426 // Assign an arbitrary value
427 data[getRelIndex(shape,0,0)]=0.0;
428 data[getRelIndex(shape,0,1)]=1.0;
429 data[getRelIndex(shape,1,0)]=2.0;
430 data[getRelIndex(shape,1,1)]=3.0;
431
432 //
433 // Create object to test
434 cout << "\tCreate rank 2 DataExpanded object." << endl;
435 DataExpanded testData(FunctionSpace(),shape,data);
436
437 //
438 // Create another rank 2 pointData
439 DataTypes::ShapeType shape2;
440 shape2.push_back(3);
441 shape2.push_back(3);
442 DataTypes::ValueType data2(DataTypes::noValues(shape2),0);
443 // DataArrayView pointData2(data2,shape2);
444
445 //
446 // Assign an arbitrary value
447 data2[getRelIndex(shape2,0,0)]=0.1;
448 data2[getRelIndex(shape2,0,1)]=1.1;
449 data2[getRelIndex(shape2,0,2)]=2.1;
450 data2[getRelIndex(shape2,1,0)]=3.1;
451 data2[getRelIndex(shape2,1,1)]=4.1;
452 data2[getRelIndex(shape2,1,2)]=5.1;
453 data2[getRelIndex(shape2,2,0)]=6.1;
454 data2[getRelIndex(shape2,2,1)]=7.1;
455 data2[getRelIndex(shape2,2,2)]=8.1;
456
457 //
458 // Create object to test
459 cout << "\tCreate second rank 2 DataExpanded object." << endl;
460 DataExpanded testData2(FunctionSpace(),shape2,data2);
461
462 cout << "\tTest slice setting (1)." << endl;
463
464 DataTypes::RegionType region;
465 region.push_back(DataTypes::RegionType::value_type(0,2));
466 region.push_back(DataTypes::RegionType::value_type(0,2));
467
468 DataTypes::RegionType region2;
469 region2.push_back(DataTypes::RegionType::value_type(1,3));
470 region2.push_back(DataTypes::RegionType::value_type(1,3));
471
472 DataAbstract* testData3=testData.getSlice(region);
473
474 testData2.setSlice(testData3,region2);
475 //
476 // Verify data values
477 cout << "\tVerify data point values." << endl;
478 for (int j=region2[1].first;j<region2[1].second;j++) {
479 for (int i=region2[0].first;i<region2[0].second;i++) {
480 assert(getRef(testData2,i,j)==data[getRelIndex(shape,i-(region[0].second-1),j-(region[1].second-1))]);
481 }
482 }
483
484 delete testData3;
485
486 }
487
488 void DataExpandedTestCase::testSliceSetting2() {
489
490 cout << endl;
491
492 //
493 // Create a rank 0 pointData
494 DataTypes::ShapeType shape;
495 DataTypes::ValueType data(DataTypes::noValues(shape),0);
496 // DataArrayView pointData(data,shape);
497
498 //
499 // Assign an arbitrary value
500 data[0]=0.0;
501
502 //
503 // Create object to test
504 cout << "\tCreate rank 0 DataExpanded object." << endl;
505 DataExpanded testData(FunctionSpace(),shape,data);
506
507 //
508 // Create a rank 2 pointData
509 DataTypes::ShapeType shape2;
510 shape2.push_back(3);
511 shape2.push_back(3);
512 DataTypes::ValueType data2(DataTypes::noValues(shape2),0);
513 // DataArrayView pointData2(data2,shape2);
514
515 //
516 // Assign an arbitrary value
517 data2[getRelIndex(shape2,0,0)]=0.1;
518 data2[getRelIndex(shape2,0,1)]=1.1;
519 data2[getRelIndex(shape2,0,2)]=2.1;
520 data2[getRelIndex(shape2,1,0)]=3.1;
521 data2[getRelIndex(shape2,1,1)]=4.1;
522 data2[getRelIndex(shape2,1,2)]=5.1;
523 data2[getRelIndex(shape2,2,0)]=6.1;
524 data2[getRelIndex(shape2,2,1)]=7.1;
525 data2[getRelIndex(shape2,2,2)]=8.1;
526
527 //
528 // Create object to test
529 cout << "\tCreate rank 2 DataExpanded object." << endl;
530 DataExpanded testData2(FunctionSpace(),shape2, data2);
531
532 cout << "\tTest slice setting (1)." << endl;
533
534 DataTypes::RegionType region;
535
536 DataTypes::RegionType region2;
537 region2.push_back(DataTypes::RegionType::value_type(1,1));
538 region2.push_back(DataTypes::RegionType::value_type(1,1));
539
540 DataAbstract* testData3=testData.getSlice(region);
541
542 testData2.setSlice(testData3,region2);
543
544 //
545 // Verify data values
546 cout << "\tVerify data point values." << endl;
547 for (int j=region2[1].first;j<region2[1].second;j++) {
548 for (int i=region2[0].first;i<region2[0].second;i++) {
549 assert(getRef(testData2,i,j)==data[0]);
550 }
551 }
552
553 delete testData3;
554
555 }
556
557
558
559
560
561 TestSuite* DataExpandedTestCase::suite ()
562 {
563 //
564 // Create the suite of tests to perform.
565 TestSuite *testSuite = new TestSuite ("DataExpandedTestCase");
566 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testAll",&DataExpandedTestCase::testAll));
567 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing",&DataExpandedTestCase::testSlicing));
568 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing2",&DataExpandedTestCase::testSlicing2));
569 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing3",&DataExpandedTestCase::testSlicing3));
570 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting",&DataExpandedTestCase::testSliceSetting));
571 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting2",&DataExpandedTestCase::testSliceSetting2));
572 return testSuite;
573 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26