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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 149 - (show annotations)
Thu Sep 1 03:31:39 2005 UTC (15 years, 1 month ago) by jgs
File size: 20193 byte(s)
Merge of development branch dev-02 back to main trunk on 2005-09-01

1 // $Id$
2 /*
3 *****************************************************************************
4 * *
5 * COPYRIGHT ACcESS - All Rights Reserved *
6 * *
7 * This software is the property of ACcESS. No part of this code *
8 * may be copied in any form or by any means without the expressed written *
9 * consent of ACcESS. Copying, use or modification of this software *
10 * by any unauthorised person is illegal unless that person has a software *
11 * license agreement with ACcESS. *
12 * *
13 *****************************************************************************
14 */
15 #include "escript/Data/FunctionSpace.h"
16 #include "escript/Data/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
27 void DataExpandedTestCase::setUp() {
28 //
29 // This is called before each test is run
30
31 }
32
33 void DataExpandedTestCase::tearDown() {
34 //
35 // This is called after each test has been run
36
37 }
38
39 void DataExpandedTestCase::testAll() {
40
41 cout << endl;
42
43 //
44 // Create a rank 1 pointData
45 DataArrayView::ShapeType shape;
46 shape.push_back(3);
47 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
48 DataArrayView pointData(data,shape);
49
50 //
51 // Assign an arbitrary value
52 pointData(0)=0.0;
53 pointData(1)=1.0;
54 pointData(2)=2.0;
55
56 //
57 // Test constructor
58 cout << "\tTest DataExpanded constructor." << endl;
59 DataExpanded testData(pointData,FunctionSpace());
60
61 cout << "\tTest getLength." << endl;
62 assert(testData.getLength()==pointData.noValues());
63
64 cout << "\tTest getPointDataView." << endl;
65 for (int i=0;i<testData.getPointDataView().getShape()[0];i++) {
66 assert(testData.getPointDataView()(i)==pointData(i));
67 }
68
69 cout << "\tVerify data point attributes." << endl;
70 DataArrayView dataView=testData.getPointDataView();
71 assert(dataView.getRank()==shape.size());
72 assert(dataView.noValues()==shape[0]*1);
73 assert(dataView.getShape()[0]==shape[0]);
74 assert(testData.getNumDPPSample()==1);
75 assert(testData.getNumSamples()==1);
76 assert(testData.validSamplePointNo(testData.getNumDPPSample()-1));
77 assert(testData.validSampleNo(testData.getNumSamples()-1));
78
79 //
80 // Test alternative constructor
81 cout << "\tTest DataExpanded alternative constructor." << endl;
82 data[0]=0.0;
83 data[1]=1.0;
84 data[2]=2.0;
85 DataExpanded testData1(FunctionSpace(),shape,data);
86
87 cout << "\tTest getLength." << endl;
88 assert(testData1.getLength()==pointData.noValues());
89
90 cout << "\tTest getPointDataView." << endl;
91 for (int i=0;i<testData1.getPointDataView().getShape()[0];i++) {
92 assert(testData1.getPointDataView()(i)==pointData(i));
93 }
94
95 cout << "\tVerify data point attributes." << endl;
96 dataView=testData1.getPointDataView();
97 assert(dataView.getRank()==shape.size());
98 assert(dataView.noValues()==shape[0]*1);
99 assert(dataView.getShape()[0]==shape[0]);
100 assert(testData.getNumDPPSample()==1);
101 assert(testData.getNumSamples()==1);
102 assert(testData.validSamplePointNo(testData.getNumDPPSample()-1));
103 assert(testData.validSampleNo(testData.getNumSamples()-1));
104
105 //
106 // Test copy constructor
107 cout << "\tTest DataExpanded copy constructor." << endl;
108 DataExpanded testData2(testData);
109
110 cout << "\tTest getLength." << endl;
111 assert(testData2.getLength()==pointData.noValues());
112
113 cout << "\tTest getPointDataView." << endl;
114 for (int i=0;i<testData2.getPointDataView().getShape()[0];i++) {
115 assert(testData2.getPointDataView()(i)==pointData(i));
116 }
117
118 cout << "\tVerify data point attributes." << endl;
119 dataView=testData2.getPointDataView();
120 assert(dataView.getRank()==shape.size());
121 assert(dataView.noValues()==shape[0]*1);
122 assert(dataView.getShape()[0]==shape[0]);
123 assert(testData2.getNumDPPSample()==1);
124 assert(testData2.getNumSamples()==1);
125 assert(testData2.validSamplePointNo(testData2.getNumDPPSample()-1));
126 assert(testData2.validSampleNo(testData2.getNumSamples()-1));
127
128 }
129
130 void DataExpandedTestCase::testReshape() {
131
132 cout << endl;
133
134 //
135 // Create a scalar pointData
136 DataArrayView::ShapeType shape;
137 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
138 DataArrayView pointData(data,shape);
139
140 //
141 // Assign an arbitrary value
142 pointData()=1.0;
143
144 //
145 // Create object to test
146 cout << "\tCreate scalar DataExpanded object." << endl;
147 DataExpanded testData(pointData,FunctionSpace());
148
149 //
150 // Test reshape - legal
151 cout << "\tTest reshape." << endl;
152 shape.push_back(2);
153 shape.push_back(3);
154 shape.push_back(21);
155 testData.reshapeDataPoint(shape);
156 assert(testData.getPointDataView().getRank()==shape.size());
157
158 //
159 // Verify data values
160 cout << "\tVerify data point values." << endl;
161 for (int k=0;k<testData.getPointDataView().getShape()[2];k++) {
162 for (int j=0;j<testData.getPointDataView().getShape()[1];j++) {
163 for (int i=0;i<testData.getPointDataView().getShape()[0];i++) {
164 assert(testData.getPointDataView()(i,j,k)==pointData());
165 }
166 }
167 }
168
169 //
170 // Verify data attributes
171 cout << "\tVerify data point attributes." << endl;
172 DataArrayView dataView=testData.getPointDataView();
173 assert(dataView.getRank()==shape.size());
174 assert(dataView.noValues()==shape[0]*shape[1]*shape[2]);
175 assert(dataView.getShape()[0]==shape[0]);
176 assert(dataView.getShape()[1]==shape[1]);
177 assert(dataView.getShape()[2]==shape[2]);
178 assert(testData.getNumDPPSample()==1);
179 assert(testData.getNumSamples()==1);
180 assert(testData.validSamplePointNo(testData.getNumDPPSample()-1));
181 assert(testData.validSampleNo(testData.getNumSamples()-1));
182
183 //
184 // Test reshape - illegal
185 cout << "\tTest illegal reshape." << endl;
186 try {
187 testData.reshapeDataPoint(shape);
188 assert(false);
189 }
190 catch (EsysException& e) {
191 //cout << e.toString() << endl;
192 assert(true);
193 }
194
195 }
196
197 void DataExpandedTestCase::testSlicing() {
198
199 cout << endl;
200
201 //
202 // Create a rank 1 pointData
203 DataArrayView::ShapeType shape;
204 shape.push_back(3);
205 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
206 DataArrayView pointData(data,shape);
207
208 //
209 // Assign an arbitrary value
210 pointData(0)=0.0;
211 pointData(1)=1.0;
212 pointData(2)=2.0;
213
214 //
215 // Create object to test
216 cout << "\tCreate rank 1 DataExpanded object." << endl;
217 DataExpanded testData(pointData,FunctionSpace());
218
219 cout << "\tTest slicing (whole object)." << endl;
220 DataArrayView::RegionType region;
221 region.push_back(DataArrayView::RegionType::value_type(0,shape[0]));
222
223 DataAbstract* testData2=testData.getSlice(region);
224
225 //
226 // Verify data values
227 cout << "\tVerify data point values." << endl;
228 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
229 assert(testData2->getPointDataView()(i)==pointData(region[0].first+i));
230 }
231
232 cout << "\tVerify data point attributes." << endl;
233 DataArrayView dataView=testData2->getPointDataView();
234 assert(dataView.getRank()==shape.size());
235 assert(dataView.noValues()==shape[0]*1);
236 assert(dataView.getShape()[0]==shape[0]);
237
238 delete testData2;
239 }
240
241 void DataExpandedTestCase::testSlicing2() {
242
243 cout << endl;
244
245 //
246 // Create a rank 2 pointData
247 DataArrayView::ShapeType shape;
248 shape.push_back(3);
249 shape.push_back(3);
250 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
251 DataArrayView pointData(data,shape);
252
253 //
254 // Assign an arbitrary value
255 pointData(0,0)=0.0;
256 pointData(1,0)=1.0;
257 pointData(2,0)=2.0;
258 pointData(0,1)=3.0;
259 pointData(1,1)=4.0;
260 pointData(2,1)=5.0;
261 pointData(0,2)=6.0;
262 pointData(1,2)=7.0;
263 pointData(2,2)=8.0;
264
265 //
266 // Create object to test
267 cout << "\tCreate rank 2 DataExpanded object." << endl;
268 DataExpanded testData(pointData,FunctionSpace());
269
270 cout << "\tTest slicing (part object)." << endl;
271 DataArrayView::RegionType region;
272 region.push_back(DataArrayView::RegionType::value_type(0,2));
273 region.push_back(DataArrayView::RegionType::value_type(0,2));
274 DataAbstract* testData2=testData.getSlice(region);
275
276 //
277 // Verify data values
278 cout << "\tVerify data point values." << endl;
279 for (int j=0;j<testData2->getPointDataView().getShape()[1];j++) {
280 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
281 assert(testData2->getPointDataView()(i,j)==pointData(region[0].first+i,region[1].first+j));
282 }
283 }
284
285 cout << "\tVerify data point attributes." << endl;
286 DataArrayView dataView=testData2->getPointDataView();
287 assert(dataView.getRank()==region.size());
288 assert(dataView.noValues()==(region[0].second-region[0].first)*(region[1].second-region[1].first));
289 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
290 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
291
292 cout << "\tTest slicing (part object)." << endl;
293 DataArrayView::RegionType region2;
294 region2.push_back(DataArrayView::RegionType::value_type(1,3));
295 region2.push_back(DataArrayView::RegionType::value_type(1,3));
296 DataAbstract* testData3=testData.getSlice(region2);
297
298 //
299 // Verify data values
300 cout << "\tVerify data point values." << endl;
301 for (int j=0;j<testData3->getPointDataView().getShape()[1];j++) {
302 for (int i=0;i<testData3->getPointDataView().getShape()[0];i++) {
303 assert(testData3->getPointDataView()(i,j)==pointData(region2[0].first+i,region2[1].first+j));
304 }
305 }
306
307 cout << "\tVerify data point attributes." << endl;
308 dataView=testData3->getPointDataView();
309 assert(dataView.getRank()==region2.size());
310 assert(dataView.noValues()==(region2[0].second-region2[0].first)*(region2[1].second-region2[1].first));
311 assert(dataView.getShape()[0]==(region2[0].second-region2[0].first));
312 assert(dataView.getShape()[1]==(region2[1].second-region2[1].first));
313
314 delete testData2;
315 delete testData3;
316
317 }
318
319 void DataExpandedTestCase::testSlicing3() {
320
321 cout << endl;
322
323 //
324 // Create a rank 3 pointData
325 DataArrayView::ShapeType shape;
326 shape.push_back(3);
327 shape.push_back(3);
328 shape.push_back(3);
329 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
330 DataArrayView pointData(data,shape);
331
332 //
333 // Assign an arbitrary value
334 pointData(0,0,0)=0.0;
335 pointData(1,0,0)=1.0;
336 pointData(2,0,0)=2.0;
337 pointData(0,1,0)=3.0;
338 pointData(1,1,0)=4.0;
339 pointData(2,1,0)=5.0;
340 pointData(0,2,0)=6.0;
341 pointData(1,2,0)=7.0;
342 pointData(2,2,0)=8.0;
343
344 pointData(0,0,1)=9.0;
345 pointData(1,0,1)=10.0;
346 pointData(2,0,1)=11.0;
347 pointData(0,1,1)=12.0;
348 pointData(1,1,1)=13.0;
349 pointData(2,1,1)=14.0;
350 pointData(0,2,1)=15.0;
351 pointData(1,2,1)=16.0;
352 pointData(2,2,1)=17.0;
353
354 pointData(0,0,2)=18.0;
355 pointData(1,0,2)=19.0;
356 pointData(2,0,2)=20.0;
357 pointData(0,1,2)=21.0;
358 pointData(1,1,2)=22.0;
359 pointData(2,1,2)=23.0;
360 pointData(0,2,2)=24.0;
361 pointData(1,2,2)=25.0;
362 pointData(2,2,2)=26.0;
363
364 //
365 // Create object to test
366 cout << "\tCreate rank 3 DataExpanded object." << endl;
367 DataExpanded testData(pointData,FunctionSpace());
368
369 cout << "\tTest slicing (part object)." << endl;
370 DataArrayView::RegionType region;
371 region.push_back(DataArrayView::RegionType::value_type(0,2));
372 region.push_back(DataArrayView::RegionType::value_type(0,2));
373 region.push_back(DataArrayView::RegionType::value_type(0,2));
374 DataAbstract* testData2=testData.getSlice(region);
375
376 //
377 // Verify data values
378 cout << "\tVerify data point values." << endl;
379 for (int k=0;k<testData2->getPointDataView().getShape()[2];k++) {
380 for (int j=0;j<testData2->getPointDataView().getShape()[1];j++) {
381 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
382 assert(testData2->getPointDataView()(i,j,k)==pointData(region[0].first+i,
383 region[1].first+j,
384 region[2].first+k));
385 }
386 }
387 }
388
389 cout << "\tVerify data point attributes." << endl;
390 DataArrayView dataView=testData2->getPointDataView();
391 assert(dataView.getRank()==region.size());
392 assert(dataView.noValues()==(region[0].second-region[0].first)
393 *(region[1].second-region[1].first)
394 *(region[2].second-region[2].first));
395 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
396 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
397 assert(dataView.getShape()[2]==(region[2].second-region[2].first));
398
399 cout << "\tTest slicing (part object)." << endl;
400 DataArrayView::RegionType region2;
401 region2.push_back(DataArrayView::RegionType::value_type(1,3));
402 region2.push_back(DataArrayView::RegionType::value_type(1,3));
403 region2.push_back(DataArrayView::RegionType::value_type(1,3));
404 DataAbstract* testData3=testData.getSlice(region2);
405
406 //
407 // Verify data values
408 cout << "\tVerify data point values." << endl;
409 for (int k=0;k<testData3->getPointDataView().getShape()[2];k++) {
410 for (int j=0;j<testData3->getPointDataView().getShape()[1];j++) {
411 for (int i=0;i<testData3->getPointDataView().getShape()[0];i++) {
412 assert(testData3->getPointDataView()(i,j,k)==pointData(region2[0].first+i,
413 region2[1].first+j,
414 region2[2].first+k));
415 }
416 }
417 }
418
419 cout << "\tVerify data point attributes." << endl;
420 dataView=testData2->getPointDataView();
421 assert(dataView.getRank()==region.size());
422 assert(dataView.noValues()==(region[0].second-region[0].first)
423 *(region[1].second-region[1].first)
424 *(region[2].second-region[2].first));
425 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
426 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
427 assert(dataView.getShape()[2]==(region[2].second-region[2].first));
428
429 delete testData2;
430 delete testData3;
431
432 }
433
434
435 void DataExpandedTestCase::testSliceSetting() {
436
437 cout << endl;
438
439 //
440 // Create a rank 2 pointData
441 DataArrayView::ShapeType shape;
442 shape.push_back(2);
443 shape.push_back(2);
444 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
445 DataArrayView pointData(data,shape);
446
447 //
448 // Assign an arbitrary value
449 pointData(0,0)=0.0;
450 pointData(0,1)=1.0;
451 pointData(1,0)=2.0;
452 pointData(1,1)=3.0;
453
454 //
455 // Create object to test
456 cout << "\tCreate rank 2 DataExpanded object." << endl;
457 DataExpanded testData(pointData,FunctionSpace());
458
459 //
460 // Create another rank 2 pointData
461 DataArrayView::ShapeType shape2;
462 shape2.push_back(3);
463 shape2.push_back(3);
464 DataArrayView::ValueType data2(DataArrayView::noValues(shape2),0);
465 DataArrayView pointData2(data2,shape2);
466
467 //
468 // Assign an arbitrary value
469 pointData2(0,0)=0.1;
470 pointData2(0,1)=1.1;
471 pointData2(0,2)=2.1;
472 pointData2(1,0)=3.1;
473 pointData2(1,1)=4.1;
474 pointData2(1,2)=5.1;
475 pointData2(2,0)=6.1;
476 pointData2(2,1)=7.1;
477 pointData2(2,2)=8.1;
478
479 //
480 // Create object to test
481 cout << "\tCreate second rank 2 DataExpanded object." << endl;
482 DataExpanded testData2(pointData2,FunctionSpace());
483
484 cout << "\tTest slice setting (1)." << endl;
485
486 DataArrayView::RegionType region;
487 region.push_back(DataArrayView::RegionType::value_type(0,2));
488 region.push_back(DataArrayView::RegionType::value_type(0,2));
489
490 DataArrayView::RegionType region2;
491 region2.push_back(DataArrayView::RegionType::value_type(1,3));
492 region2.push_back(DataArrayView::RegionType::value_type(1,3));
493
494 DataAbstract* testData3=testData.getSlice(region);
495
496 testData2.setSlice(testData3,region2);
497
498 //
499 // Verify data values
500 cout << "\tVerify data point values." << endl;
501 for (int j=region2[1].first;j<region2[1].second;j++) {
502 for (int i=region2[0].first;i<region2[0].second;i++) {
503 assert(testData2.getPointDataView()(i,j)==pointData(i-(region[0].second-1),j-(region[1].second-1)));
504 }
505 }
506
507 delete testData3;
508
509 }
510
511 void DataExpandedTestCase::testSliceSetting2() {
512
513 cout << endl;
514
515 //
516 // Create a rank 0 pointData
517 DataArrayView::ShapeType shape;
518 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
519 DataArrayView pointData(data,shape);
520
521 //
522 // Assign an arbitrary value
523 pointData()=0.0;
524
525 //
526 // Create object to test
527 cout << "\tCreate rank 0 DataExpanded object." << endl;
528 DataExpanded testData(pointData,FunctionSpace());
529
530 //
531 // Create a rank 2 pointData
532 DataArrayView::ShapeType shape2;
533 shape2.push_back(3);
534 shape2.push_back(3);
535 DataArrayView::ValueType data2(DataArrayView::noValues(shape2),0);
536 DataArrayView pointData2(data2,shape2);
537
538 //
539 // Assign an arbitrary value
540 pointData2(0,0)=0.1;
541 pointData2(0,1)=1.1;
542 pointData2(0,2)=2.1;
543 pointData2(1,0)=3.1;
544 pointData2(1,1)=4.1;
545 pointData2(1,2)=5.1;
546 pointData2(2,0)=6.1;
547 pointData2(2,1)=7.1;
548 pointData2(2,2)=8.1;
549
550 //
551 // Create object to test
552 cout << "\tCreate rank 2 DataExpanded object." << endl;
553 DataExpanded testData2(pointData2,FunctionSpace());
554
555 cout << "\tTest slice setting (1)." << endl;
556
557 DataArrayView::RegionType region;
558
559 DataArrayView::RegionType region2;
560 region2.push_back(DataArrayView::RegionType::value_type(1,1));
561 region2.push_back(DataArrayView::RegionType::value_type(1,1));
562
563 DataAbstract* testData3=testData.getSlice(region);
564
565 testData2.setSlice(testData3,region2);
566
567 //
568 // Verify data values
569 cout << "\tVerify data point values." << endl;
570 for (int j=region2[1].first;j<region2[1].second;j++) {
571 for (int i=region2[0].first;i<region2[0].second;i++) {
572 assert(testData2.getPointDataView()(i,j)==pointData());
573 }
574 }
575
576 delete testData3;
577
578 }
579
580 void DataExpandedTestCase::testRefValue() {
581
582 cout << endl;
583
584 cout << "\tTest DataExpanded refValue methods." << endl;
585
586 //
587 // Create a rank 1 data array to create a DataExpanded object
588 DataArrayView::ShapeType shape;
589 shape.push_back(3);
590 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
591 DataArrayView dataView(data,shape);
592
593 //
594 // Create another DataArray object to pass to refValue methods
595 DataArray data2(dataView);
596
597 //
598 // Create a DataExpanded object
599 dataView(0)=0.0;
600 dataView(1)=1.0;
601 dataView(2)=2.0;
602 DataExpanded dataExp(dataView,FunctionSpace());
603
604 //
605 // Try out ref value methods
606
607 dataExp.getRefValue(1,data2);
608
609 // check the values loaded into data2 by getRefValue
610 // match the values in dataExp
611 assert(data2.getView()(0)==0.0);
612 assert(data2.getView()(1)==1.0);
613 assert(data2.getView()(2)==2.0);
614
615 data2.getView()(0)=3.0;
616 data2.getView()(1)=4.0;
617 data2.getView()(2)=5.0;
618
619 dataExp.setRefValue(1,data2);
620
621 // check the values now in dataExp match the values
622 // that were supplied in data2
623 assert(dataExp.getPointDataView()(0)==3.0);
624 assert(dataExp.getPointDataView()(1)==4.0);
625 assert(dataExp.getPointDataView()(2)==5.0);
626
627 cout << endl;
628
629 }
630
631 TestSuite* DataExpandedTestCase::suite ()
632 {
633 //
634 // Create the suite of tests to perform.
635 TestSuite *testSuite = new TestSuite ("DataExpandedTestCase");
636 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testAll",&DataExpandedTestCase::testAll));
637 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testReshape",&DataExpandedTestCase::testReshape));
638 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing",&DataExpandedTestCase::testSlicing));
639 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing2",&DataExpandedTestCase::testSlicing2));
640 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing3",&DataExpandedTestCase::testSlicing3));
641 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting",&DataExpandedTestCase::testSliceSetting));
642 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting2",&DataExpandedTestCase::testSliceSetting2));
643 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testRefValue",&DataExpandedTestCase::testRefValue));
644 return testSuite;
645 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26