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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1811 - (show annotations)
Thu Sep 25 23:11:13 2008 UTC (11 years, 2 months ago) by ksteube
File size: 17264 byte(s)
Copyright updated in all files

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26