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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1796 - (show annotations)
Wed Sep 17 01:45:46 2008 UTC (11 years, 1 month ago) by jfenwick
File size: 17299 byte(s)
Merged noarrayview branch onto trunk.


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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26