/[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 119 - (show annotations)
Tue Apr 12 04:45:05 2005 UTC (14 years, 6 months ago) by jgs
File size: 20048 byte(s)
*** empty log message ***

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 DataAbstract* testData2=testData.getSlice(region);
223
224 //
225 // Verify data values
226 cout << "\tVerify data point values." << endl;
227 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
228 assert(testData2->getPointDataView()(i)==pointData(region[0].first+i));
229 }
230
231 cout << "\tVerify data point attributes." << endl;
232 DataArrayView dataView=testData2->getPointDataView();
233 assert(dataView.getRank()==shape.size());
234 assert(dataView.noValues()==shape[0]*1);
235 assert(dataView.getShape()[0]==shape[0]);
236
237 }
238
239 void DataExpandedTestCase::testSlicing2() {
240
241 cout << endl;
242
243 //
244 // Create a rank 2 pointData
245 DataArrayView::ShapeType shape;
246 shape.push_back(3);
247 shape.push_back(3);
248 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
249 DataArrayView pointData(data,shape);
250
251 //
252 // Assign an arbitrary value
253 pointData(0,0)=0.0;
254 pointData(1,0)=1.0;
255 pointData(2,0)=2.0;
256 pointData(0,1)=3.0;
257 pointData(1,1)=4.0;
258 pointData(2,1)=5.0;
259 pointData(0,2)=6.0;
260 pointData(1,2)=7.0;
261 pointData(2,2)=8.0;
262
263 //
264 // Create object to test
265 cout << "\tCreate rank 2 DataExpanded object." << endl;
266 DataExpanded testData(pointData,FunctionSpace());
267
268 cout << "\tTest slicing (part object)." << endl;
269 DataArrayView::RegionType region;
270 region.push_back(DataArrayView::RegionType::value_type(0,2));
271 region.push_back(DataArrayView::RegionType::value_type(0,2));
272 DataAbstract* testData2=testData.getSlice(region);
273
274 //
275 // Verify data values
276 cout << "\tVerify data point values." << endl;
277 for (int j=0;j<testData2->getPointDataView().getShape()[1];j++) {
278 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
279 assert(testData2->getPointDataView()(i,j)==pointData(region[0].first+i,region[1].first+j));
280 }
281 }
282
283 cout << "\tVerify data point attributes." << endl;
284 DataArrayView dataView=testData2->getPointDataView();
285 assert(dataView.getRank()==region.size());
286 assert(dataView.noValues()==(region[0].second-region[0].first)*(region[1].second-region[1].first));
287 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
288 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
289
290 cout << "\tTest slicing (part object)." << endl;
291 DataArrayView::RegionType region2;
292 region2.push_back(DataArrayView::RegionType::value_type(1,3));
293 region2.push_back(DataArrayView::RegionType::value_type(1,3));
294 DataAbstract* testData3=testData.getSlice(region2);
295
296 //
297 // Verify data values
298 cout << "\tVerify data point values." << endl;
299 for (int j=0;j<testData3->getPointDataView().getShape()[1];j++) {
300 for (int i=0;i<testData3->getPointDataView().getShape()[0];i++) {
301 assert(testData3->getPointDataView()(i,j)==pointData(region2[0].first+i,region2[1].first+j));
302 }
303 }
304
305 cout << "\tVerify data point attributes." << endl;
306 dataView=testData3->getPointDataView();
307 assert(dataView.getRank()==region2.size());
308 assert(dataView.noValues()==(region2[0].second-region2[0].first)*(region2[1].second-region2[1].first));
309 assert(dataView.getShape()[0]==(region2[0].second-region2[0].first));
310 assert(dataView.getShape()[1]==(region2[1].second-region2[1].first));
311
312 }
313
314 void DataExpandedTestCase::testSlicing3() {
315
316 cout << endl;
317
318 //
319 // Create a rank 3 pointData
320 DataArrayView::ShapeType shape;
321 shape.push_back(3);
322 shape.push_back(3);
323 shape.push_back(3);
324 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
325 DataArrayView pointData(data,shape);
326
327 //
328 // Assign an arbitrary value
329 pointData(0,0,0)=0.0;
330 pointData(1,0,0)=1.0;
331 pointData(2,0,0)=2.0;
332 pointData(0,1,0)=3.0;
333 pointData(1,1,0)=4.0;
334 pointData(2,1,0)=5.0;
335 pointData(0,2,0)=6.0;
336 pointData(1,2,0)=7.0;
337 pointData(2,2,0)=8.0;
338
339 pointData(0,0,1)=9.0;
340 pointData(1,0,1)=10.0;
341 pointData(2,0,1)=11.0;
342 pointData(0,1,1)=12.0;
343 pointData(1,1,1)=13.0;
344 pointData(2,1,1)=14.0;
345 pointData(0,2,1)=15.0;
346 pointData(1,2,1)=16.0;
347 pointData(2,2,1)=17.0;
348
349 pointData(0,0,2)=18.0;
350 pointData(1,0,2)=19.0;
351 pointData(2,0,2)=20.0;
352 pointData(0,1,2)=21.0;
353 pointData(1,1,2)=22.0;
354 pointData(2,1,2)=23.0;
355 pointData(0,2,2)=24.0;
356 pointData(1,2,2)=25.0;
357 pointData(2,2,2)=26.0;
358
359 //
360 // Create object to test
361 cout << "\tCreate rank 3 DataExpanded object." << endl;
362 DataExpanded testData(pointData,FunctionSpace());
363
364 cout << "\tTest slicing (part object)." << endl;
365 DataArrayView::RegionType region;
366 region.push_back(DataArrayView::RegionType::value_type(0,2));
367 region.push_back(DataArrayView::RegionType::value_type(0,2));
368 region.push_back(DataArrayView::RegionType::value_type(0,2));
369 DataAbstract* testData2=testData.getSlice(region);
370
371 //
372 // Verify data values
373 cout << "\tVerify data point values." << endl;
374 for (int k=0;k<testData2->getPointDataView().getShape()[2];k++) {
375 for (int j=0;j<testData2->getPointDataView().getShape()[1];j++) {
376 for (int i=0;i<testData2->getPointDataView().getShape()[0];i++) {
377 assert(testData2->getPointDataView()(i,j,k)==pointData(region[0].first+i,
378 region[1].first+j,
379 region[2].first+k));
380 }
381 }
382 }
383
384 cout << "\tVerify data point attributes." << endl;
385 DataArrayView dataView=testData2->getPointDataView();
386 assert(dataView.getRank()==region.size());
387 assert(dataView.noValues()==(region[0].second-region[0].first)
388 *(region[1].second-region[1].first)
389 *(region[2].second-region[2].first));
390 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
391 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
392 assert(dataView.getShape()[2]==(region[2].second-region[2].first));
393
394 cout << "\tTest slicing (part object)." << endl;
395 DataArrayView::RegionType region2;
396 region2.push_back(DataArrayView::RegionType::value_type(1,3));
397 region2.push_back(DataArrayView::RegionType::value_type(1,3));
398 region2.push_back(DataArrayView::RegionType::value_type(1,3));
399 DataAbstract* testData3=testData.getSlice(region2);
400
401 //
402 // Verify data values
403 cout << "\tVerify data point values." << endl;
404 for (int k=0;k<testData3->getPointDataView().getShape()[2];k++) {
405 for (int j=0;j<testData3->getPointDataView().getShape()[1];j++) {
406 for (int i=0;i<testData3->getPointDataView().getShape()[0];i++) {
407 assert(testData3->getPointDataView()(i,j,k)==pointData(region2[0].first+i,
408 region2[1].first+j,
409 region2[2].first+k));
410 }
411 }
412 }
413
414 cout << "\tVerify data point attributes." << endl;
415 dataView=testData2->getPointDataView();
416 assert(dataView.getRank()==region.size());
417 assert(dataView.noValues()==(region[0].second-region[0].first)
418 *(region[1].second-region[1].first)
419 *(region[2].second-region[2].first));
420 assert(dataView.getShape()[0]==(region[0].second-region[0].first));
421 assert(dataView.getShape()[1]==(region[1].second-region[1].first));
422 assert(dataView.getShape()[2]==(region[2].second-region[2].first));
423
424 }
425
426
427 void DataExpandedTestCase::testSliceSetting() {
428
429 cout << endl;
430
431 //
432 // Create a rank 2 pointData
433 DataArrayView::ShapeType shape;
434 shape.push_back(2);
435 shape.push_back(2);
436 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
437 DataArrayView pointData(data,shape);
438
439 //
440 // Assign an arbitrary value
441 pointData(0,0)=0.0;
442 pointData(0,1)=1.0;
443 pointData(1,0)=2.0;
444 pointData(1,1)=3.0;
445
446 //
447 // Create object to test
448 cout << "\tCreate rank 2 DataExpanded object." << endl;
449 DataExpanded testData(pointData,FunctionSpace());
450
451 //
452 // Create another rank 2 pointData
453 DataArrayView::ShapeType shape2;
454 shape2.push_back(3);
455 shape2.push_back(3);
456 DataArrayView::ValueType data2(DataArrayView::noValues(shape2),0);
457 DataArrayView pointData2(data2,shape2);
458
459 //
460 // Assign an arbitrary value
461 pointData2(0,0)=0.1;
462 pointData2(0,1)=1.1;
463 pointData2(0,2)=2.1;
464 pointData2(1,0)=3.1;
465 pointData2(1,1)=4.1;
466 pointData2(1,2)=5.1;
467 pointData2(2,0)=6.1;
468 pointData2(2,1)=7.1;
469 pointData2(2,2)=8.1;
470
471 //
472 // Create object to test
473 cout << "\tCreate second rank 2 DataExpanded object." << endl;
474 DataExpanded testData2(pointData2,FunctionSpace());
475
476 cout << "\tTest slice setting (1)." << endl;
477
478 DataArrayView::RegionType region;
479 region.push_back(DataArrayView::RegionType::value_type(0,2));
480 region.push_back(DataArrayView::RegionType::value_type(0,2));
481
482 DataArrayView::RegionType region2;
483 region2.push_back(DataArrayView::RegionType::value_type(1,3));
484 region2.push_back(DataArrayView::RegionType::value_type(1,3));
485
486 DataAbstract* testData3=testData.getSlice(region);
487
488 testData2.setSlice(testData3,region2);
489
490 //
491 // Verify data values
492 cout << "\tVerify data point values." << endl;
493 for (int j=region2[1].first;j<region2[1].second;j++) {
494 for (int i=region2[0].first;i<region2[0].second;i++) {
495 assert(testData2.getPointDataView()(i,j)==pointData(i-(region[0].second-1),j-(region[1].second-1)));
496 }
497 }
498
499 }
500
501 void DataExpandedTestCase::testSliceSetting2() {
502
503 cout << endl;
504
505 //
506 // Create a rank 0 pointData
507 DataArrayView::ShapeType shape;
508 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
509 DataArrayView pointData(data,shape);
510
511 //
512 // Assign an arbitrary value
513 pointData()=0.0;
514
515 //
516 // Create object to test
517 cout << "\tCreate rank 0 DataExpanded object." << endl;
518 DataExpanded testData(pointData,FunctionSpace());
519
520 //
521 // Create a rank 2 pointData
522 DataArrayView::ShapeType shape2;
523 shape2.push_back(3);
524 shape2.push_back(3);
525 DataArrayView::ValueType data2(DataArrayView::noValues(shape2),0);
526 DataArrayView pointData2(data2,shape2);
527
528 //
529 // Assign an arbitrary value
530 pointData2(0,0)=0.1;
531 pointData2(0,1)=1.1;
532 pointData2(0,2)=2.1;
533 pointData2(1,0)=3.1;
534 pointData2(1,1)=4.1;
535 pointData2(1,2)=5.1;
536 pointData2(2,0)=6.1;
537 pointData2(2,1)=7.1;
538 pointData2(2,2)=8.1;
539
540 //
541 // Create object to test
542 cout << "\tCreate rank 2 DataExpanded object." << endl;
543 DataExpanded testData2(pointData2,FunctionSpace());
544
545 cout << "\tTest slice setting (1)." << endl;
546
547 DataArrayView::RegionType region;
548
549 DataArrayView::RegionType region2;
550 region2.push_back(DataArrayView::RegionType::value_type(1,1));
551 region2.push_back(DataArrayView::RegionType::value_type(1,1));
552
553 DataAbstract* testData3=testData.getSlice(region);
554
555 testData2.setSlice(testData3,region2);
556
557 //
558 // Verify data values
559 cout << "\tVerify data point values." << endl;
560 for (int j=region2[1].first;j<region2[1].second;j++) {
561 for (int i=region2[0].first;i<region2[0].second;i++) {
562 assert(testData2.getPointDataView()(i,j)==pointData());
563 }
564 }
565
566 }
567
568 void DataExpandedTestCase::testRefValue() {
569
570 cout << endl;
571
572 cout << "\tTest DataExpanded refValue methods." << endl;
573
574 //
575 // Create a rank 1 data array to create a DataExpanded object
576 DataArrayView::ShapeType shape;
577 shape.push_back(3);
578 DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
579 DataArrayView dataView(data,shape);
580
581 //
582 // Create another DataArray object to pass to refValue methods
583 DataArray data2(dataView);
584
585 //
586 // Create a DataExpanded object
587 dataView(0)=0.0;
588 dataView(1)=1.0;
589 dataView(2)=2.0;
590 DataExpanded dataExp(dataView,FunctionSpace());
591
592 //
593 // Try out ref value methods
594
595 dataExp.getRefValue(1,data2);
596
597 // check the values loaded into data2 by getRefValue
598 // match the values in dataExp
599 assert(data2.getView()(0)==0.0);
600 assert(data2.getView()(1)==1.0);
601 assert(data2.getView()(2)==2.0);
602
603 data2.getView()(0)=3.0;
604 data2.getView()(1)=4.0;
605 data2.getView()(2)=5.0;
606
607 dataExp.setRefValue(1,data2);
608
609 // check the values now in dataExp match the values
610 // that were supplied in data2
611 assert(dataExp.getPointDataView()(0)==3.0);
612 assert(dataExp.getPointDataView()(1)==4.0);
613 assert(dataExp.getPointDataView()(2)==5.0);
614
615 cout << endl;
616
617 }
618
619 TestSuite* DataExpandedTestCase::suite ()
620 {
621 //
622 // Create the suite of tests to perform.
623 TestSuite *testSuite = new TestSuite ("DataExpandedTestCase");
624 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testAll",&DataExpandedTestCase::testAll));
625 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testReshape",&DataExpandedTestCase::testReshape));
626 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing",&DataExpandedTestCase::testSlicing));
627 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing2",&DataExpandedTestCase::testSlicing2));
628 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSlicing3",&DataExpandedTestCase::testSlicing3));
629 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting",&DataExpandedTestCase::testSliceSetting));
630 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testSliceSetting2",&DataExpandedTestCase::testSliceSetting2));
631 testSuite->addTest (new TestCaller< DataExpandedTestCase>("testRefValue",&DataExpandedTestCase::testRefValue));
632 return testSuite;
633 }

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26