/[escript]/trunk/escript/test/Data/DataTestCase.cpp
ViewVC logotype

Diff of /trunk/escript/test/Data/DataTestCase.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

trunk/esys2/escript/test/Data/DataTestCase.cpp revision 100 by jgs, Wed Dec 15 03:48:48 2004 UTC trunk/escript/test/Data/DataTestCase.cpp revision 539 by jgs, Mon Feb 20 04:12:51 2006 UTC
# Line 12  Line 12 
12   *                                                                           *   *                                                                           *
13   *****************************************************************************   *****************************************************************************
14  */  */
 #include "esysUtils/EsysException.h"  
 #include "escript/Data/Data.h"  
 #include "escript/Data/FunctionSpace.h"  
15    
16  #include "DataTestCase.h"  #include "DataTestCase.h"
17    
18    #include "FunctionSpace.h"
19    #include "EsysException.h"
20    
21    #include "Data.h"
22    
23  #include <iostream>  #include <iostream>
24    #include <math.h>
25    
26  using namespace std;  using namespace std;
27  using namespace CppUnitTest;  using namespace CppUnitTest;
# Line 40  void DataTestCase::testSlicing() { Line 43  void DataTestCase::testSlicing() {
43    cout << endl;    cout << endl;
44    
45    {    {
46    
47        cout << "\tTest get-slicing DataConstant" << endl;
48    
49      DataArrayView::ShapeType viewShape;      DataArrayView::ShapeType viewShape;
     //  
     // weak tests for slicing DataConstant  
     cout << "\tTest slicing DataConstant" << endl;  
50      viewShape.push_back(2);      viewShape.push_back(2);
51      viewShape.push_back(3);      viewShape.push_back(3);
52      Data temp(1.3,viewShape,FunctionSpace(),false);      Data data(1.3,viewShape,FunctionSpace(),false);
53    
54        //cout << data.toString() << endl;
55    
56      DataArrayView::RegionType region;      DataArrayView::RegionType region;
57      region.push_back(DataArrayView::RegionType::value_type(0,0));      region.push_back(DataArrayView::RegionType::value_type(0,0));
58      region.push_back(DataArrayView::RegionType::value_type(0,0));      region.push_back(DataArrayView::RegionType::value_type(0,0));
59      Data slice(temp.getSlice(region));  
60      assert(slice.getDataPointRank()==0);      Data slice1(data.getSlice(region));
61      assert(slice.getDataPoint(0,0)()==1.3);  
62      //      //cout << slice1.toString() << endl;
63      // try the same but this time to produce a matrix containing one value  
64        assert(slice1.getDataPointRank()==0);
65        assert(slice1.getDataPoint(0,0)()==1.3);
66    
67      region.clear();      region.clear();
68      region.push_back(DataArrayView::RegionType::value_type(0,1));      region.push_back(DataArrayView::RegionType::value_type(0,1));
69      region.push_back(DataArrayView::RegionType::value_type(0,1));      region.push_back(DataArrayView::RegionType::value_type(0,1));
70      slice=temp.getSlice(region);  
71      assert(slice.getDataPointRank()==2);      Data slice2(data.getSlice(region));
72      assert(slice.getDataPoint(0,0)(0,0)==1.3);  
73        //cout << slice2.toString() << endl;
74    
75        assert(slice2.getDataPointRank()==2);
76        assert(slice2.getDataPoint(0,0)(0,0)==1.3);
77    
78        region.clear();
79        region.push_back(DataArrayView::RegionType::value_type(0,1));
80        region.push_back(DataArrayView::RegionType::value_type(0,2));
81    
82        Data slice3(data.getSlice(region));
83    
84        //cout << slice3.toString() << endl;
85    
86        assert(slice3.getDataPointRank()==2);
87        assert(slice3.getDataPoint(0,0)(0,0)==1.3);
88        assert(slice3.getDataPoint(0,0)(0,1)==1.3);
89    
90    }    }
91    
92    {    {
93    
94        cout << "\tTest set-slicing DataConstant" << endl;
95    
96      DataArrayView::ShapeType viewShape;      DataArrayView::ShapeType viewShape;
97        Data source(10.0,viewShape,FunctionSpace(),false);
98    
99        //cout << source.toString() << endl;
100    
101        viewShape.push_back(2);
102        viewShape.push_back(3);
103        Data target(1.3,viewShape,FunctionSpace(),false);
104    
105        //cout << target.toString() << endl;
106    
107        DataArrayView::RegionType region;
108        region.push_back(DataArrayView::RegionType::value_type(0,0));
109        region.push_back(DataArrayView::RegionType::value_type(0,0));
110    
111        target.setSlice(source,region);
112    
113        //cout << target.toString() << endl;
114    
115        assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());
116    
117      }
118    
119      {
120    
121        cout << "\tTest get-slicing DataTagged" << endl;
122    
123      //      //
124      // weak tests for slicing DataExpanded      // create a DataTagged with a default value only
125      cout << "\tTest slicing DataExpanded" << endl;  
126        DataArrayView::ShapeType viewShape;
127      viewShape.push_back(2);      viewShape.push_back(2);
128      viewShape.push_back(3);      viewShape.push_back(3);
129      Data temp(1.3,viewShape,FunctionSpace(),true);      Data data(1.3,viewShape,FunctionSpace(),false);
130      temp.getDataPoint(0,0)(0,0)=0.0;      data.tag();
131      temp.getDataPoint(0,0)(1,1)=1.0;      data.getDataPoint(0,0)(0,0)=1.0;
132        data.getDataPoint(0,0)(1,1)=2.0;
133    
134        //cout << data.toString() << endl;
135    
136        //
137        // create a scalar slice
138    
139      DataArrayView::RegionType region;      DataArrayView::RegionType region;
140      region.push_back(DataArrayView::RegionType::value_type(0,0));      region.push_back(DataArrayView::RegionType::value_type(0,0));
141      region.push_back(DataArrayView::RegionType::value_type(0,0));      region.push_back(DataArrayView::RegionType::value_type(0,0));
142      Data slice(temp.getSlice(region));  
143      assert(slice.getDataPointRank()==0);      Data slice1(data.getSlice(region));
144      assert(slice.getDataPoint(0,0)()==0.0);  
145        //cout << slice1.toString() << endl;
146    
147        assert(slice1.isTagged());
148        assert(slice1.getDataPointRank()==0);
149        assert(slice1.getDataPoint(0,0)()==1.0);
150    
151      //      //
152      // try the same but this time to produce a matrix containing one value      // create a rank 2 slice with one value
153    
154      region.clear();      region.clear();
155      region.push_back(DataArrayView::RegionType::value_type(0,1));      region.push_back(DataArrayView::RegionType::value_type(0,1));
156      region.push_back(DataArrayView::RegionType::value_type(0,1));      region.push_back(DataArrayView::RegionType::value_type(0,1));
157      slice=temp.getSlice(region);  
158      assert(slice.getDataPointRank()==2);      Data slice2(data.getSlice(region));
159      assert(slice.getDataPoint(0,0)(0,0)==0.0);  
160        //cout << slice2.toString() << endl;
161    
162        assert(slice2.isTagged());
163        assert(slice2.getDataPointRank()==2);
164        assert(slice2.getDataPoint(0,0)(0,0)==1.0);
165    
166        //
167        // create a rank 2 slice with four values
168    
169      region.clear();      region.clear();
170      region.push_back(DataArrayView::RegionType::value_type(0,2));      region.push_back(DataArrayView::RegionType::value_type(0,2));
171      region.push_back(DataArrayView::RegionType::value_type(0,2));      region.push_back(DataArrayView::RegionType::value_type(0,2));
172      slice=temp.getSlice(region);  
173      assert(slice.getDataPoint(0,0)(0,0)==0.0);      Data slice3(data.getSlice(region));
174      assert(slice.getDataPoint(0,0)(1,1)==1.0);  
175        //cout << slice3.toString() << endl;
176    
177        assert(slice3.isTagged());
178        assert(slice3.getDataPointRank()==2);
179        assert(slice3.getDataPoint(0,0)(0,0)==1.0);
180        assert(slice3.getDataPoint(0,0)(0,1)==1.3);
181        assert(slice3.getDataPoint(0,0)(1,0)==1.3);
182        assert(slice3.getDataPoint(0,0)(1,1)==2.0);
183    
184        //
185        // add a value for tag "1"
186    
187        DataArrayView::ValueType viewData(6);
188        for (int i=0;i<viewData.size();i++) {
189          viewData[i]=i;
190        }
191        DataArrayView dataView(viewData,viewShape);
192    
193        data.setTaggedValueFromCPP(1, dataView);
194    
195        //
196        // create a full slice
197    
198        region.clear();
199        region.push_back(DataArrayView::RegionType::value_type(0,2));
200        region.push_back(DataArrayView::RegionType::value_type(0,3));
201    
202        Data slice4(data.getSlice(region));
203    
204        //cout << slice4.toString() << endl;
205    
206        assert(slice4.isTagged());
207        assert(slice4.getDataPointRank()==2);
208        assert(slice4.getDataPoint(0,0)(0,0)==0);
209        assert(slice4.getDataPoint(0,0)(0,1)==2);
210        assert(slice4.getDataPoint(0,0)(0,2)==4);
211        assert(slice4.getDataPoint(0,0)(1,0)==1);
212        assert(slice4.getDataPoint(0,0)(1,1)==3);
213        assert(slice4.getDataPoint(0,0)(1,2)==5);
214    
215    }    }
216    
217    {    {
218    
219        cout << "\tTest set-slicing DataTagged" << endl;
220    
221        //
222        // create a DataTagged with a scalar default value only
223    
224      DataArrayView::ShapeType viewShape;      DataArrayView::ShapeType viewShape;
225        Data source(10.0,viewShape,FunctionSpace(),false);
226        source.tag();
227    
228        //cout << source.toString() << endl;
229    
230      //      //
231      // weak tests for slicing DataTagged      // create a DataTagged with a rank 2 default value only
232      cout << "\tTest slicing DataTagged" << endl;  
233      viewShape.push_back(2);      viewShape.push_back(2);
234      viewShape.push_back(3);      viewShape.push_back(3);
235      Data temp(1.3,viewShape,FunctionSpace(),false);      Data target(1.3,viewShape,FunctionSpace(),false);
236        target.tag();
237    
238        //cout << target.toString() << endl;
239    
240        //
241        // set a slice in target from source
242    
243        DataArrayView::RegionType region;
244        region.push_back(DataArrayView::RegionType::value_type(1,1));
245        region.push_back(DataArrayView::RegionType::value_type(1,1));
246    
247        target.setSlice(source,region);
248    
249        //cout << target.toString() << endl;
250    
251        assert(target.isTagged());
252        assert(target.getDataPointRank()==2);
253        assert(target.getDataPoint(0,0)(0,0)==1.3);
254        assert(target.getDataPoint(0,0)(0,1)==1.3);
255        assert(target.getDataPoint(0,0)(0,2)==1.3);
256        assert(target.getDataPoint(0,0)(1,0)==1.3);
257        assert(target.getDataPoint(0,0)(1,1)==source.getDataPoint(0,0)());
258        assert(target.getDataPoint(0,0)(1,2)==1.3);
259    
260        //
261        // add a value for tag "1"
262    
263        DataArrayView::ValueType viewData(6);
264        for (int i=0;i<viewData.size();i++) {
265          viewData[i]=i;
266        }
267        DataArrayView dataView(viewData,viewShape);
268    
269        target.setTaggedValueFromCPP(1, dataView);
270    
271      //      //
272      // convert the data to tagged      // set a slice in target from source
273      temp.tag();  
274        region.clear();
275        region.push_back(DataArrayView::RegionType::value_type(0,0));
276        region.push_back(DataArrayView::RegionType::value_type(1,1));
277    
278        target.setSlice(source,region);
279    
280        //cout << target.toString() << endl;
281    
282        assert(target.isTagged());
283        assert(target.getDataPointRank()==2);
284        assert(target.getDataPoint(0,0)(0,0)==0);
285        assert(target.getDataPoint(0,0)(0,1)==source.getDataPoint(0,0)());
286        assert(target.getDataPoint(0,0)(0,2)==4);
287        assert(target.getDataPoint(0,0)(1,0)==1);
288        assert(target.getDataPoint(0,0)(1,1)==3);
289        assert(target.getDataPoint(0,0)(1,2)==5);
290    
291      }
292    
293      {
294    
295        cout << "\tTest get-slicing DataExpanded" << endl;
296    
297        DataArrayView::ShapeType viewShape;
298        viewShape.push_back(2);
299        viewShape.push_back(3);
300        Data temp(1.3,viewShape,FunctionSpace(),true);
301    
302      temp.getDataPoint(0,0)(0,0)=0.0;      temp.getDataPoint(0,0)(0,0)=0.0;
303      temp.getDataPoint(0,0)(1,1)=1.0;      temp.getDataPoint(0,0)(1,1)=1.0;
304    
305      DataArrayView::RegionType region;      DataArrayView::RegionType region;
306      region.push_back(DataArrayView::RegionType::value_type(0,0));      region.push_back(DataArrayView::RegionType::value_type(0,0));
307      region.push_back(DataArrayView::RegionType::value_type(0,0));      region.push_back(DataArrayView::RegionType::value_type(0,0));
308    
309      Data slice(temp.getSlice(region));      Data slice(temp.getSlice(region));
310    
311      assert(slice.getDataPointRank()==0);      assert(slice.getDataPointRank()==0);
312      assert(slice.getDataPoint(0,0)()==0.0);      assert(slice.getDataPoint(0,0)()==0.0);
313      //  
     // try the same but this time to produce a matrix containing one value  
314      region.clear();      region.clear();
315      region.push_back(DataArrayView::RegionType::value_type(0,1));      region.push_back(DataArrayView::RegionType::value_type(0,1));
316      region.push_back(DataArrayView::RegionType::value_type(0,1));      region.push_back(DataArrayView::RegionType::value_type(0,1));
317    
318      slice=temp.getSlice(region);      slice=temp.getSlice(region);
319    
320      assert(slice.getDataPointRank()==2);      assert(slice.getDataPointRank()==2);
321      assert(slice.getDataPoint(0,0)(0,0)==0.0);      assert(slice.getDataPoint(0,0)(0,0)==0.0);
322    
323      region.clear();      region.clear();
324      region.push_back(DataArrayView::RegionType::value_type(0,2));      region.push_back(DataArrayView::RegionType::value_type(0,2));
325      region.push_back(DataArrayView::RegionType::value_type(0,2));      region.push_back(DataArrayView::RegionType::value_type(0,2));
326    
327      slice=temp.getSlice(region);      slice=temp.getSlice(region);
328    
329      assert(slice.getDataPoint(0,0)(0,0)==0.0);      assert(slice.getDataPoint(0,0)(0,0)==0.0);
330      assert(slice.getDataPoint(0,0)(1,1)==1.0);      assert(slice.getDataPoint(0,0)(1,1)==1.0);
   }  
331    
   {  
     DataArrayView::ShapeType viewShape;  
     Data source(10.0,viewShape,FunctionSpace(),false);  
     //  
     // weak tests for setting a slice of DataConstant  
     cout << "\tTest slicing DataConstant" << endl;  
     viewShape.push_back(2);  
     viewShape.push_back(3);  
     Data target(1.3,viewShape,FunctionSpace(),false);  
     DataArrayView::RegionType region;  
     region.push_back(DataArrayView::RegionType::value_type(0,0));  
     region.push_back(DataArrayView::RegionType::value_type(0,0));  
     target.setSlice(source,region);  
     assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());  
332    }    }
333    
334    {    {
335    
336        cout << "\tTest set-slicing DataExpanded" << endl;
337    
338      DataArrayView::ShapeType viewShape;      DataArrayView::ShapeType viewShape;
339      Data source(10.0,viewShape,FunctionSpace(),true);      Data source(10.0,viewShape,FunctionSpace(),true);
340      //  
     // weak tests for setting a slice of DataConstant  
341      viewShape.push_back(2);      viewShape.push_back(2);
342      viewShape.push_back(3);      viewShape.push_back(3);
343      Data target(1.3,viewShape,FunctionSpace(),true);      Data target(1.3,viewShape,FunctionSpace(),true);
     DataArrayView::RegionType region;  
     region.push_back(DataArrayView::RegionType::value_type(0,0));  
     region.push_back(DataArrayView::RegionType::value_type(0,0));  
     target.setSlice(source,region);  
     assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());  
   }  
344    
   {  
     DataArrayView::ShapeType viewShape;  
     Data source(10.0,viewShape,FunctionSpace(),false);  
     source.tag();  
     //  
     // weak tests for slicing DataTagged  
     cout << "\tTest slicing DataTagged" << endl;  
     viewShape.push_back(2);  
     viewShape.push_back(3);  
     Data target(1.3,viewShape,FunctionSpace(),false);  
     //  
     // convert the data to tagged  
     target.tag();  
345      DataArrayView::RegionType region;      DataArrayView::RegionType region;
346      region.push_back(DataArrayView::RegionType::value_type(0,0));      region.push_back(DataArrayView::RegionType::value_type(0,0));
347      region.push_back(DataArrayView::RegionType::value_type(0,0));      region.push_back(DataArrayView::RegionType::value_type(0,0));
348    
349      target.setSlice(source,region);      target.setSlice(source,region);
350    
351      assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());      assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());
352    
353    }    }
354    
355  }  }
356    
357  void DataTestCase::testMore() {  void DataTestCase::testAll() {
358    
359    cout << endl;    cout << endl;
360    
361    cout << "\tCreate a Data object from a DataArrayView" << endl;    cout << "\tCreate a Data object from a DataArrayView" << endl;
362    
   DataArrayView::ValueType viewData;  
363    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
364    viewShape.push_back(3);    viewShape.push_back(3);
365      DataArrayView::ValueType viewData(3);
366    for (int i=0;i<viewShape[0];++i) {    for (int i=0;i<viewShape[0];++i) {
367      viewData.push_back(i);      viewData[i]=i;
368    }    }
369    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
370    
# Line 210  void DataTestCase::testMore() { Line 381  void DataTestCase::testMore() {
381    result=exData*cData;    result=exData*cData;
382    assert(result.isExpanded());    assert(result.isExpanded());
383    
   assert(result.Lsup()==4);  
   assert(result.sup()==4);  
   assert(result.inf()==0);  
   
   result=exData+cData;  
   result=exData-cData;  
   result=exData/cData;  
   
   cout << "\tExercise wherePositive method" << endl;  
   assert(!exData.wherePositive().isEmpty());  
   assert(exData.wherePositive()==exData.wherePositive());  
   
   cout << "\tExercise copyWithMask method" << endl;  
   exData.copyWithMask(result, exData.wherePositive());  
   assert(!exData.wherePositive().isEmpty());  
   
384  }  }
385    
386  void DataTestCase::testAll() {  void DataTestCase::testMore() {
387    
388    cout << endl;    cout << endl;
389    
390    cout << "\tCreate a Data object from a DataArrayView" << endl;    cout << "\tCreate a Data object from a DataArrayView" << endl;
391    
   DataArrayView::ValueType viewData;  
392    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
393    viewShape.push_back(3);    viewShape.push_back(3);
394      DataArrayView::ValueType viewData(3);
395    for (int i=0;i<viewShape[0];++i) {    for (int i=0;i<viewShape[0];++i) {
396      viewData.push_back(i);      viewData[i]=i;
397    }    }
398    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
399    
400    bool expanded=true;    bool expanded=true;
   
401    Data exData(myView,FunctionSpace(),expanded);    Data exData(myView,FunctionSpace(),expanded);
402    Data cData(myView);    Data cData(myView);
403    Data result;    Data result;
# Line 256  void DataTestCase::testAll() { Line 410  void DataTestCase::testAll() {
410    result=exData*cData;    result=exData*cData;
411    assert(result.isExpanded());    assert(result.isExpanded());
412    
413      assert(result.Lsup()==4);
414      assert(result.sup()==4);
415      assert(result.inf()==0);
416    
417      result=exData+cData;
418      result=exData-cData;
419      result=exData/cData;
420    
421      cout << "\tExercise wherePositive method" << endl;
422      assert(!exData.wherePositive().isEmpty());
423    
424      cout << "\tExercise copyWithMask method" << endl;
425      exData.copyWithMask(result, exData.wherePositive());
426      assert(!exData.wherePositive().isEmpty());
427    
428  }  }
429    
430  void DataTestCase::testDataConstant() {  void DataTestCase::testDataConstant() {
# Line 264  void DataTestCase::testDataConstant() { Line 433  void DataTestCase::testDataConstant() {
433    
434    cout << "\tCreate a DataConstant object from a DataArrayView" << endl;    cout << "\tCreate a DataConstant object from a DataArrayView" << endl;
435    
   DataArrayView::ValueType viewData;  
436    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
437    viewShape.push_back(2);    viewShape.push_back(2);
438    viewShape.push_back(3);    viewShape.push_back(3);
439    viewShape.push_back(4);    viewShape.push_back(4);
440      DataArrayView::ValueType viewData(2*3*4);
441    for (int i=0;i<DataArrayView::noValues(viewShape);++i) {    for (int i=0;i<DataArrayView::noValues(viewShape);++i) {
442      viewData.push_back(i);      viewData[i]=i;
443    }    }
444    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
445    
# Line 297  void DataTestCase::testDataConstant() { Line 466  void DataTestCase::testDataConstant() {
466    
467  }  }
468    
469    void DataTestCase::testDataTagged() {
470    
471      cout << endl;
472    
473      {
474    
475        cout << "\tCreate a DataTagged object with a default value only." << endl;
476    
477        DataTagged::TagListType keys;
478    
479        DataTagged::ValueListType values;
480    
481        DataArrayView::ShapeType viewShape;
482        viewShape.push_back(3);
483    
484        DataArrayView::ValueType viewData(3);
485        for (int i=0;i<viewShape[0];i++) {
486          viewData[i]=i;
487        }
488        DataArrayView defaultValue(viewData,viewShape);
489    
490        bool expanded=false;
491      
492        Data myData(keys,values,defaultValue,FunctionSpace(),expanded);
493    
494        // cout << myData.toString() << endl;
495    
496        assert(!myData.isEmpty());
497        assert(myData.isTagged());
498        assert(myData.getTagNumber(0)==1);
499        assert(myData.getDataPointRank()==1);
500        assert(myData.getLength()==3);
501    
502        DataArrayView myDataView = myData.getPointDataView();
503        assert(!myDataView.isEmpty());
504        assert(myDataView.getOffset()==0);
505        assert(myDataView.getRank()==1);
506        assert(myDataView.noValues()==3);
507        assert(myDataView.getShape().size()==1);
508        assert(myDataView(0)==0.0);
509        assert(myDataView(1)==1.0);
510        assert(myDataView(2)==2.0);
511    
512        myDataView = myData.getDataPoint(0,0);
513        assert(!myDataView.isEmpty());
514        assert(myDataView.getOffset()==0);
515        assert(myDataView.getRank()==1);
516        assert(myDataView.noValues()==3);
517        assert(myDataView.getShape().size()==1);
518        assert(myDataView(0)==0.0);
519        assert(myDataView(1)==1.0);
520        assert(myDataView(2)==2.0);
521    
522        double* sampleData=myData.getSampleData(0);
523        for (int i=0; i<myDataView.noValues(); i++) {
524          assert(sampleData[i]==i);
525        }
526        // use a non-existent tag so we get a pointer to
527        // the first element of the data array
528        sampleData=myData.getSampleDataByTag(9);
529        for (int i=0; i<myData.getLength(); i++) {
530          assert(sampleData[i]==i);
531        }
532    
533        cout << "\tTest setting of a tag and associated value." << endl;
534    
535        // value for tag "1"
536        DataArray eTwo(defaultValue);
537        for (int i=0;i<eTwo.getView().getShape()[0];i++) {
538          eTwo.getView()(i)=i+2.0;
539        }
540    
541        myData.setTaggedValueFromCPP(1,eTwo.getView());
542    
543        assert(myData.getLength()==6);
544    
545        myDataView = myData.getDataPoint(0,0);
546        assert(myDataView==eTwo.getView());
547        assert(!myDataView.isEmpty());
548        assert(myDataView.getOffset()==3);
549        assert(myDataView.getRank()==1);
550        assert(myDataView.noValues()==3);
551        assert(myDataView.getShape().size()==1);
552        assert(myDataView(0)==2);
553        assert(myDataView(1)==3);
554        assert(myDataView(2)==4);
555    
556        sampleData=myData.getSampleDataByTag(1);
557        for (int i=0; i<myDataView.noValues(); i++) {
558          assert(sampleData[i]==i+2);
559        }
560    
561      }
562    
563      {
564    
565        cout << "\tCreate a DataTagged object via tag() method." << endl;
566    
567        DataArrayView::ShapeType viewShape;
568        viewShape.push_back(2);
569        viewShape.push_back(3);
570        Data myData(1.3,viewShape,FunctionSpace(),false);
571        myData.tag();
572    
573        //cout << myData.toString() << endl;
574    
575        assert(!myData.isEmpty());
576        assert(myData.isTagged());
577        assert(myData.getTagNumber(0)==1);
578        assert(myData.getDataPointRank()==2);
579        assert(myData.getLength()==6);
580    
581        // check default value
582        DataArrayView myDataView = myData.getPointDataView();
583        assert(!myDataView.isEmpty());
584        assert(myDataView.getOffset()==0);
585        assert(myDataView.getRank()==2);
586        assert(myDataView.noValues()==6);
587        assert(myDataView.getShape().size()==2);
588        assert(myDataView(0,0)==1.3);
589        assert(myDataView(0,1)==1.3);
590        assert(myDataView(0,2)==1.3);
591        assert(myDataView(1,0)==1.3);
592        assert(myDataView(1,1)==1.3);
593        assert(myDataView(1,2)==1.3);
594    
595        // check value for data-point (0,0).
596        myDataView = myData.getDataPoint(0,0);
597        assert(!myDataView.isEmpty());
598        assert(myDataView.getOffset()==0);
599        assert(myDataView.getRank()==2);
600        assert(myDataView.noValues()==6);
601        assert(myDataView.getShape().size()==2);
602        assert(myDataView(0,0)==1.3);
603        assert(myDataView(0,1)==1.3);
604        assert(myDataView(0,2)==1.3);
605        assert(myDataView(1,0)==1.3);
606        assert(myDataView(1,1)==1.3);
607        assert(myDataView(1,2)==1.3);
608    
609      }
610    
611    }
612    
613  void DataTestCase::testDataTaggedExceptions() {  void DataTestCase::testDataTaggedExceptions() {
614    
615    cout << endl;    cout << endl;
616    
617    cout << "\tTest DataTagged operations exceptions." << endl;    cout << "\tTest DataTagged exceptions." << endl;
618    
619    Data myData;    Data myData;
620    DataArrayView myView;    DataArrayView myView;
621    
622    try {    try {
623        myData.getSampleDataByTag(0);;        myData.getSampleDataByTag(0);;
624        assert(false);        assert(false);
# Line 312  void DataTestCase::testDataTaggedExcepti Line 627  void DataTestCase::testDataTaggedExcepti
627        //cout << e.what() << endl;        //cout << e.what() << endl;
628        assert(true);        assert(true);
629    }    }
630    /*  
631    try {    try {
632        myData.setTaggedValue(0,myView);;        myData.setTaggedValueFromCPP(0,myView);;
633        assert(false);        assert(false);
634    }    }
635    catch (EsysException& e) {    catch (EsysException& e) {
636        //cout << e.what() << endl;        //cout << e.what() << endl;
637        assert(true);        assert(true);
638    }    }
   */  
   
 }  
   
 void DataTestCase::testDataTagged() {  
   
   cout << endl;  
   
   cout << "\tCreate a DataTagged object from a DataArrayView" << endl;  
   
   DataTagged::TagListType keys;  
   DataTagged::ValueListType values;  
   DataArrayView::ValueType viewData;  
   DataArrayView::ShapeType viewShape;  
   viewShape.push_back(3);  
   for (int i=0;i<viewShape[0];++i) {  
     viewData.push_back(i);  
   }  
   DataArrayView myView(viewData,viewShape);  
   
   // create tagged data with no tag values just a default  
   bool expanded=false;  
   
   Data myData(keys,values,myView,FunctionSpace(),expanded);  
   assert(myData.isTagged());  
   
   cout << "\tTest some basic operations" << endl;  
   
   Data myDataCopy(myData);  
   myDataCopy.expand();  
   assert(myDataCopy.isExpanded());  
639    
640  }  }
641    
# Line 369  void DataTestCase::testConstructors() { Line 653  void DataTestCase::testConstructors() {
653      viewShape.push_back(2);      viewShape.push_back(2);
654      viewShape.push_back(3);      viewShape.push_back(3);
655      Data temp(1.3,viewShape,FunctionSpace(),false);      Data temp(1.3,viewShape,FunctionSpace(),false);
     cout << "\tDump it toString:" << endl;  
     cout << temp.toString() << endl;  
656    }    }
657  }  }
658    
659  void DataTestCase::testOperations() {  void DataTestCase::testOperations() {
660    
661    cout << endl;    cout << endl;
662    
663    cout << "\tCreate a rank 2 Data object" << endl;    // define the shape for the DataArrayView test data
664    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType shape;
665    viewShape.push_back(2);    shape.push_back(2);
666    viewShape.push_back(3);    shape.push_back(3);
667    
668      // allocate the data for the DataArrayView
669      DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
670    
671    Data base(2.0,viewShape,FunctionSpace(),false);    // construct DataArrayView
672    Data power(3.0,viewShape,FunctionSpace(),false);    DataArrayView dataView(data,shape);
673    
674      // assign values to the data
675      for (int i=0;i<shape[0];i++) {
676        for (int j=0;j<shape[1];j++) {
677          dataView(i,j)=dataView.index(i,j);
678        }
679      }
680    
681    cout << "\tTest powD,sin,cos,tan,log,ln operations" << endl;    Data base(dataView);
682    
683      // test unary operations
684    
685      cout << "\tTest Data::pow." << endl;
686      Data power(3.0,shape,FunctionSpace(),true);
687    Data result(base.powD(power));    Data result(base.powD(power));
688    assert(result.getDataPoint(0,0)(0,0) == 8);    for (int i=0;i<shape[0];i++) {
689        for (int j=0;j<shape[1];j++) {
690          assert(result.getPointDataView()(i,j) == pow(dataView.index(i,j),3.0));
691        }
692      }
693    
694      cout << "\tTest Data::sin." << endl;
695    result.copy(base.sin());    result.copy(base.sin());
696      assert(true);
697    
698      cout << "\tTest Data::cos." << endl;
699    result.copy(base.cos());    result.copy(base.cos());
700      assert(true);
701    
702      cout << "\tTest Data::tan." << endl;
703    result.copy(base.tan());    result.copy(base.tan());
704      assert(true);
705    
706      cout << "\tTest Data::asin." << endl;
707      result.copy(base.asin());
708      assert(true);
709    
710      cout << "\tTest Data::acos." << endl;
711      result.copy(base.acos());
712      assert(true);
713    
714      cout << "\tTest Data::atan." << endl;
715      result.copy(base.atan());
716      assert(true);
717    
718      cout << "\tTest Data::sinh." << endl;
719      result.copy(base.sinh());
720      assert(true);
721    
722      cout << "\tTest Data::cosh." << endl;
723      result.copy(base.cosh());
724      assert(true);
725    
726      cout << "\tTest Data::tanh." << endl;
727      result.copy(base.tanh());
728      assert(true);
729    
730      cout << "\tTest Data::asinh." << endl;
731      result.copy(base.asinh());
732      assert(true);
733    
734      cout << "\tTest Data::acosh." << endl;
735      result.copy(base.acosh());
736      assert(true);
737    
738      cout << "\tTest Data::atanh." << endl;
739      result.copy(base.atanh());
740      assert(true);
741    
742      cout << "\tTest Data::log." << endl;
743    result.copy(base.log());    result.copy(base.log());
744    result.copy(base.ln());    assert(true);
745    
746      //cout << "\tTest Data::ln." << endl;
747      //result.copy(base.ln());
748      //assert(true);
749    
750      cout << "\tTest Data::abs." << endl;
751      result.copy(base.abs());
752      assert(true);
753    
754      cout << "\tTest Data::sign." << endl;
755      result.copy(base.sign());
756      assert(true);
757    
758      cout << "\tTest Data::exp." << endl;
759      result.copy(base.exp());
760      assert(true);
761    
762      cout << "\tTest Data::sqrt." << endl;
763      result.copy(base.sqrt());
764      assert(true);
765    
766      cout << "\tTest Data::neg." << endl;
767      result.copy(base.neg());
768      assert(true);
769    
770      cout << "\tTest Data::pos." << endl;
771      result.copy(base.pos());
772      for (int i=0;i<shape[0];i++) {
773        for (int j=0;j<shape[1];j++) {
774          assert(result.getPointDataView()(i,j) == dataView.index(i,j));
775        }
776      }
777    
778      // test reduction operations
779    
780      cout << "\tTest Data::Lsup." << endl;
781      assert(base.Lsup() == 5);
782    
783      cout << "\tTest Data::sup." << endl;
784      assert(base.sup() == 5);
785    
786      cout << "\tTest Data::inf." << endl;
787      assert(base.inf() == 0);
788    
789      // test data-point reduction operations
790    
791      cout << "\tTest Data::minval." << endl;
792      result.copy(base.minval());
793      assert(result.getPointDataView()() == 0);
794    
795      cout << "\tTest Data::maxval." << endl;
796      result.copy(base.maxval());
797      assert(result.getPointDataView()() == 5);
798    
799      //cout << "\tTest Data::length." << endl;
800      //result.copy(base.length());
801      //assert(pow(result.getPointDataView()(),2.0) == 55);
802    
803      cout << "\tTest Data::trace." << endl;
804      result.copy(base.trace());
805      assert(result.getPointDataView()() == 15);
806    
807      //result.copy(base.transpose(0));
808      //assert(true);
809    
810    }
811    
812    void DataTestCase::testRefValue() {
813    
814      //
815      // Note - this test can't be run as boost::python::numeric::array
816      // objects can only be created and used from within a python thread!
817      //
818    
819      cout << endl;
820    
821      cout << "\tTest Data object RefValue methods." << endl;
822    
823      // Create three Data object - DataExpanded, DataConstant and DataEmpty
824      DataArrayView::ShapeType viewShape;
825      viewShape.push_back(3);
826      DataArrayView::ValueType viewData(3);
827      for (int i=0;i<viewShape[0];++i) {
828        viewData[i]=i;
829      }
830      DataArrayView myView(viewData,viewShape);
831    
832      bool expanded=true;
833    
834      Data expandedData(myView,FunctionSpace(),expanded);
835      Data constantData(myView);
836      Data emptyData;
837    
838      assert(expandedData.isExpanded());
839      assert(constantData.isConstant());
840      assert(emptyData.isEmpty());
841    
842      // Check assertions are thrown for RefValue methods on DataEmpty
843    
844      int ref = 0;
845      boost::python::numeric::array num_array(1.0);
846    
847      try {
848          emptyData.getRefValue(ref,num_array);
849          assert(false);
850      }
851      catch (EsysException& e) {
852          assert(true);
853      }
854      try {
855          emptyData.setRefValue(ref,num_array);
856          assert(false);
857      }
858      catch (EsysException& e) {
859          assert(true);
860      }
861    
862      // Check assertions are thrown for RefValue methods on DataConstant
863      try {
864          constantData.getRefValue(ref,num_array);
865          assert(false);
866      }
867      catch (EsysException& e) {
868          assert(true);
869      }
870      try {
871          constantData.setRefValue(ref,num_array);
872          assert(false);
873      }
874      catch (EsysException& e) {
875          assert(true);
876      }
877    
878      // Check calls to RefValue methods on DataExpanded
879      expandedData.getRefValue(ref,num_array);
880      expandedData.setRefValue(ref,num_array);
881    
882    }
883    
884    void DataTestCase::testMemAlloc() {
885    
886      //
887      // Simple little sanity check for the memory allocator
888    
889      cout << endl;
890    
891      Data *testData;
892      for (int i=0; i<1000; i++) {
893        testData = new Data(0.0, DataArrayView::ShapeType(), FunctionSpace(), true);
894        delete testData;
895      }
896    
897      DataArrayView::ShapeType viewShape;
898      viewShape.push_back(10);
899      viewShape.push_back(10);
900      viewShape.push_back(10);
901    
902      Data *testData2;
903      Data *testData3 = new Data(0.0, viewShape, FunctionSpace(), true);
904      for (int i=0; i<1000; i++) {
905        testData2 = new Data(0.0, viewShape, FunctionSpace(), true);
906        delete testData2;
907      }
908      delete testData3;
909    
910  }  }
911    
912  TestSuite* DataTestCase::suite ()  TestSuite* DataTestCase::suite ()
# Line 411  TestSuite* DataTestCase::suite () Line 923  TestSuite* DataTestCase::suite ()
923    testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));    testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
924    testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));    testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
925    testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));    testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
926      //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
927      testSuite->addTest (new TestCaller< DataTestCase>("testMemAlloc",&DataTestCase::testMemAlloc));
928    
929    return testSuite;    return testSuite;
930  }  }

Legend:
Removed from v.100  
changed lines
  Added in v.539

  ViewVC Help
Powered by ViewVC 1.1.26