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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3962 - (show annotations)
Tue Sep 11 09:12:07 2012 UTC (6 years, 11 months ago) by jfenwick
File size: 17945 byte(s)
Fix in brick for ambiguous typing.

Python work around for unit tests.
Passing cpp tests as well.

omp is disabled due to a weird linking error.

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26