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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2271 - (show annotations)
Mon Feb 16 05:08:29 2009 UTC (10 years, 7 months ago) by jfenwick
File size: 17304 byte(s)
Merging version 2269 to trunk

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::const_reference
45 getRefRO(DataReady& data,int i, int j)
46 {
47 return data.getVectorRO()[getRelIndex(data.getShape(),i,j)];
48 }
49
50 ValueType::const_reference
51 getRefRO(DataReady& data,int i, int j,int k)
52 {
53 return data.getVectorRO()[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.getDataAtOffsetRO(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.getDataAtOffsetRO(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->getDataAtOffsetRO(i)==data[region[0].first+i]);
208 }
209 }
210
211 void DataExpandedTestCase::testSlicing2() {
212
213 cout << endl;
214
215 //
216 // Create a rank 2 pointData
217 DataTypes::ShapeType shape;
218 shape.push_back(3);
219 shape.push_back(3);
220 DataTypes::ValueType data(DataTypes::noValues(shape),0);
221 // DataArrayView pointData(data,shape);
222
223 //
224 // Assign an arbitrary value
225 getDRef(data,shape,0,0)=0.0;
226 getDRef(data,shape,1,0)=1.0;
227 getDRef(data,shape,2,0)=2.0;
228 getDRef(data,shape,0,1)=3.0;
229 getDRef(data,shape,1,1)=4.0;
230 getDRef(data,shape,2,1)=5.0;
231 getDRef(data,shape,0,2)=6.0;
232 getDRef(data,shape,1,2)=7.0;
233 getDRef(data,shape,2,2)=8.0;
234
235 //
236 // Create object to test
237 cout << "\tCreate rank 2 DataExpanded object." << endl;
238 DataExpanded testData(FunctionSpace(),shape,data);
239
240 cout << "\tTest slicing (part object)." << endl;
241 DataTypes::RegionType region;
242 region.push_back(DataTypes::RegionType::value_type(0,2));
243 region.push_back(DataTypes::RegionType::value_type(0,2));
244
245 DataReady_ptr testData2=resolveAndDelete(testData.getSlice(region));
246
247 //
248 // Verify data values
249 cout << "\tVerify data point values." << endl;
250 for (int j=0;j<testData2->getShape()[1];j++) {
251 for (int i=0;i<testData2->getShape()[0];i++) {
252 assert(getRefRO(*testData2,i,j)==data[getRelIndex(shape,region[0].first+i,region[1].first+j)]);
253 }
254 }
255
256 cout << "\tVerify data point attributes." << endl;
257 // DataArrayView dataView=testData2->getPointDataView();
258 assert(testData2->getRank()==region.size());
259 assert(testData2->getNoValues()==(region[0].second-region[0].first)*(region[1].second-region[1].first));
260 assert(testData2->getShape()[0]==(region[0].second-region[0].first));
261 assert(testData2->getShape()[1]==(region[1].second-region[1].first));
262
263 cout << "\tTest slicing (part object)." << endl;
264 DataTypes::RegionType region2;
265 region2.push_back(DataTypes::RegionType::value_type(1,3));
266 region2.push_back(DataTypes::RegionType::value_type(1,3));
267 DataReady_ptr testData3=resolveAndDelete(testData.getSlice(region2));
268 //
269 // Verify data values
270 cout << "\tVerify data point values." << endl;
271 for (int j=0;j<testData3->getShape()[1];j++) {
272 for (int i=0;i<testData3->getShape()[0];i++) {
273 assert(getRefRO(*testData3,i,j)==getDRef(data,shape,region2[0].first+i,region2[1].first+j));
274 }
275 }
276
277 cout << "\tVerify data point attributes." << endl;
278 // dataView=testData3->getPointDataView();
279 assert(testData3->getRank()==region2.size());
280 assert(testData3->getNoValues()==(region2[0].second-region2[0].first)*(region2[1].second-region2[1].first));
281 assert(testData3->getShape()[0]==(region2[0].second-region2[0].first));
282 assert(testData3->getShape()[1]==(region2[1].second-region2[1].first));
283
284 // delete testData2;
285 // delete testData3;
286
287 }
288
289 void DataExpandedTestCase::testSlicing3() {
290
291 cout << endl;
292
293 //
294 // Create a rank 3 pointData
295 DataTypes::ShapeType shape;
296 shape.push_back(3);
297 shape.push_back(3);
298 shape.push_back(3);
299 DataTypes::ValueType data(DataTypes::noValues(shape),0);
300 // DataArrayView pointData(data,shape);
301
302 //
303 // Assign an arbitrary value
304 getDRef(data,shape,0,0,0)=0.0;
305 getDRef(data,shape,1,0,0)=1.0;
306 getDRef(data,shape,2,0,0)=2.0;
307 getDRef(data,shape,0,1,0)=3.0;
308 getDRef(data,shape,1,1,0)=4.0;
309 getDRef(data,shape,2,1,0)=5.0;
310 getDRef(data,shape,0,2,0)=6.0;
311 getDRef(data,shape,1,2,0)=7.0;
312 getDRef(data,shape,2,2,0)=8.0;
313
314 getDRef(data,shape,0,0,1)=9.0;
315 getDRef(data,shape,1,0,1)=10.0;
316 getDRef(data,shape,2,0,1)=11.0;
317 getDRef(data,shape,0,1,1)=12.0;
318 getDRef(data,shape,1,1,1)=13.0;
319 getDRef(data,shape,2,1,1)=14.0;
320 getDRef(data,shape,0,2,1)=15.0;
321 getDRef(data,shape,1,2,1)=16.0;
322 getDRef(data,shape,2,2,1)=17.0;
323
324 getDRef(data,shape,0,0,2)=18.0;
325 getDRef(data,shape,1,0,2)=19.0;
326 getDRef(data,shape,2,0,2)=20.0;
327 getDRef(data,shape,0,1,2)=21.0;
328 getDRef(data,shape,1,1,2)=22.0;
329 getDRef(data,shape,2,1,2)=23.0;
330 getDRef(data,shape,0,2,2)=24.0;
331 getDRef(data,shape,1,2,2)=25.0;
332 getDRef(data,shape,2,2,2)=26.0;
333
334 //
335 // Create object to test
336 cout << "\tCreate rank 3 DataExpanded object." << endl;
337 DataExpanded testData(FunctionSpace(),shape,data);
338
339 cout << "\tTest slicing (part object)." << endl;
340 DataTypes::RegionType region;
341 region.push_back(DataTypes::RegionType::value_type(0,2));
342 region.push_back(DataTypes::RegionType::value_type(0,2));
343 region.push_back(DataTypes::RegionType::value_type(0,2));
344 DataReady_ptr testData2=resolveAndDelete(testData.getSlice(region));
345
346 //
347 // Verify data values
348 cout << "\tVerify data point values." << endl;
349 for (int k=0;k<testData2->getShape()[2];k++) {
350 for (int j=0;j<testData2->getShape()[1];j++) {
351 for (int i=0;i<testData2->getShape()[0];i++) {
352 assert(getRefRO(*testData2,i,j,k)==getDRef(data,shape,region[0].first+i,
353 region[1].first+j,
354 region[2].first+k));
355 }
356 }
357 }
358
359 cout << "\tVerify data point attributes." << endl;
360 // DataArrayView dataView=testData2->getPointDataView();
361 assert(testData2->getRank()==region.size());
362 assert(testData2->getNoValues()==(region[0].second-region[0].first)
363 *(region[1].second-region[1].first)
364 *(region[2].second-region[2].first));
365 assert(testData2->getShape()[0]==(region[0].second-region[0].first));
366 assert(testData2->getShape()[1]==(region[1].second-region[1].first));
367 assert(testData2->getShape()[2]==(region[2].second-region[2].first));
368
369 cout << "\tTest slicing (part object)." << endl;
370 DataTypes::RegionType region2;
371 region2.push_back(DataTypes::RegionType::value_type(1,3));
372 region2.push_back(DataTypes::RegionType::value_type(1,3));
373 region2.push_back(DataTypes::RegionType::value_type(1,3));
374 DataReady_ptr testData3=resolveAndDelete(testData.getSlice(region2));
375
376 //
377 // Verify data values
378 cout << "\tVerify data point values." << endl;
379 for (int k=0;k<testData3->getShape()[2];k++) {
380 for (int j=0;j<testData3->getShape()[1];j++) {
381 for (int i=0;i<testData3->getShape()[0];i++) {
382 assert(getRefRO(*testData3,i,j,k)==getDRef(data,shape,region2[0].first+i,
383 region2[1].first+j,
384 region2[2].first+k));
385 }
386 }
387 }
388
389 cout << "\tVerify data point attributes." << endl;
390 // dataView=testData2->getPointDataView();
391 assert(testData2->getRank()==region.size());
392 assert(testData2->getNoValues()==(region[0].second-region[0].first)
393 *(region[1].second-region[1].first)
394 *(region[2].second-region[2].first));
395 assert(testData2->getShape()[0]==(region[0].second-region[0].first));
396 assert(testData2->getShape()[1]==(region[1].second-region[1].first));
397 assert(testData2->getShape()[2]==(region[2].second-region[2].first));
398
399 // delete testData2;
400 // delete testData3;
401
402 }
403
404
405 void DataExpandedTestCase::testSliceSetting() {
406
407 cout << endl;
408
409 //
410 // Create a rank 2 pointData
411 DataTypes::ShapeType shape;
412 shape.push_back(2);
413 shape.push_back(2);
414 DataTypes::ValueType data(DataTypes::noValues(shape),0);
415 // DataArrayView pointData(data,shape);
416
417 //
418 // Assign an arbitrary value
419 data[getRelIndex(shape,0,0)]=0.0;
420 data[getRelIndex(shape,0,1)]=1.0;
421 data[getRelIndex(shape,1,0)]=2.0;
422 data[getRelIndex(shape,1,1)]=3.0;
423
424 //
425 // Create object to test
426 cout << "\tCreate rank 2 DataExpanded object." << endl;
427 DataExpanded testData(FunctionSpace(),shape,data);
428
429 //
430 // Create another rank 2 pointData
431 DataTypes::ShapeType shape2;
432 shape2.push_back(3);
433 shape2.push_back(3);
434 DataTypes::ValueType data2(DataTypes::noValues(shape2),0);
435 // DataArrayView pointData2(data2,shape2);
436
437 //
438 // Assign an arbitrary value
439 data2[getRelIndex(shape2,0,0)]=0.1;
440 data2[getRelIndex(shape2,0,1)]=1.1;
441 data2[getRelIndex(shape2,0,2)]=2.1;
442 data2[getRelIndex(shape2,1,0)]=3.1;
443 data2[getRelIndex(shape2,1,1)]=4.1;
444 data2[getRelIndex(shape2,1,2)]=5.1;
445 data2[getRelIndex(shape2,2,0)]=6.1;
446 data2[getRelIndex(shape2,2,1)]=7.1;
447 data2[getRelIndex(shape2,2,2)]=8.1;
448
449 //
450 // Create object to test
451 cout << "\tCreate second rank 2 DataExpanded object." << endl;
452 DataExpanded testData2(FunctionSpace(),shape2,data2);
453
454 cout << "\tTest slice setting (1)." << endl;
455
456 DataTypes::RegionType region;
457 region.push_back(DataTypes::RegionType::value_type(0,2));
458 region.push_back(DataTypes::RegionType::value_type(0,2));
459
460 DataTypes::RegionType region2;
461 region2.push_back(DataTypes::RegionType::value_type(1,3));
462 region2.push_back(DataTypes::RegionType::value_type(1,3));
463
464 DataAbstract* testData3=testData.getSlice(region);
465
466 testData2.setSlice(testData3,region2);
467 //
468 // Verify data values
469 cout << "\tVerify data point values." << endl;
470 for (int j=region2[1].first;j<region2[1].second;j++) {
471 for (int i=region2[0].first;i<region2[0].second;i++) {
472 assert(getRefRO(testData2,i,j)==data[getRelIndex(shape,i-(region[0].second-1),j-(region[1].second-1))]);
473 }
474 }
475
476 delete testData3;
477
478 }
479
480 void DataExpandedTestCase::testSliceSetting2() {
481
482 cout << endl;
483
484 //
485 // Create a rank 0 pointData
486 DataTypes::ShapeType shape;
487 DataTypes::ValueType data(DataTypes::noValues(shape),0);
488 // DataArrayView pointData(data,shape);
489
490 //
491 // Assign an arbitrary value
492 data[0]=0.0;
493
494 //
495 // Create object to test
496 cout << "\tCreate rank 0 DataExpanded object." << endl;
497 DataExpanded testData(FunctionSpace(),shape,data);
498
499 //
500 // Create a rank 2 pointData
501 DataTypes::ShapeType shape2;
502 shape2.push_back(3);
503 shape2.push_back(3);
504 DataTypes::ValueType data2(DataTypes::noValues(shape2),0);
505 // DataArrayView pointData2(data2,shape2);
506
507 //
508 // Assign an arbitrary value
509 data2[getRelIndex(shape2,0,0)]=0.1;
510 data2[getRelIndex(shape2,0,1)]=1.1;
511 data2[getRelIndex(shape2,0,2)]=2.1;
512 data2[getRelIndex(shape2,1,0)]=3.1;
513 data2[getRelIndex(shape2,1,1)]=4.1;
514 data2[getRelIndex(shape2,1,2)]=5.1;
515 data2[getRelIndex(shape2,2,0)]=6.1;
516 data2[getRelIndex(shape2,2,1)]=7.1;
517 data2[getRelIndex(shape2,2,2)]=8.1;
518
519 //
520 // Create object to test
521 cout << "\tCreate rank 2 DataExpanded object." << endl;
522 DataExpanded testData2(FunctionSpace(),shape2, data2);
523
524 cout << "\tTest slice setting (1)." << endl;
525
526 DataTypes::RegionType region;
527
528 DataTypes::RegionType region2;
529 region2.push_back(DataTypes::RegionType::value_type(1,1));
530 region2.push_back(DataTypes::RegionType::value_type(1,1));
531
532 DataAbstract* testData3=testData.getSlice(region);
533
534 testData2.setSlice(testData3,region2);
535
536 //
537 // Verify data values
538 cout << "\tVerify data point values." << endl;
539 for (int j=region2[1].first;j<region2[1].second;j++) {
540 for (int i=region2[0].first;i<region2[0].second;i++) {
541 assert(getRefRO(testData2,i,j)==data[0]);
542 }
543 }
544
545 delete testData3;
546
547 }
548
549
550
551
552
553 TestSuite* DataExpandedTestCase::suite ()
554 {
555 //
556 // Create the suite of tests to perform.
557 TestSuite *testSuite = new TestSuite ("DataExpandedTestCase");
558 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testAll",&DataExpandedTestCase::testAll));
559 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing",&DataExpandedTestCase::testSlicing));
560 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing2",&DataExpandedTestCase::testSlicing2));
561 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing3",&DataExpandedTestCase::testSlicing3));
562 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting",&DataExpandedTestCase::testSliceSetting));
563 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting2",&DataExpandedTestCase::testSliceSetting2));
564 return testSuite;
565 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26