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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 117 - (hide annotations)
Fri Apr 1 05:48:57 2005 UTC (14 years, 8 months ago) by jgs
File size: 19132 byte(s)
*** empty log message ***

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

Properties

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

  ViewVC Help
Powered by ViewVC 1.1.26