/[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 97 by jgs, Tue Dec 14 05:39:33 2004 UTC trunk/escript/test/Data/DataTestCase.cpp revision 537 by jgs, Mon Feb 20 02:00:18 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      // convert the data to tagged      // set a slice in target from source
242      temp.tag();  
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        // set a slice in target from source
273    
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  }  }
# Line 189  void DataTestCase::testMore() { Line 360  void DataTestCase::testMore() {
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 220  void DataTestCase::testMore() { Line 391  void DataTestCase::testMore() {
391    
392    cout << "\tExercise wherePositive method" << endl;    cout << "\tExercise wherePositive method" << endl;
393    assert(!exData.wherePositive().isEmpty());    assert(!exData.wherePositive().isEmpty());
   //assert(exData.wherePositive()==exData.wherePositive());  
394    
395    cout << "\tExercise copyWithMask method" << endl;    cout << "\tExercise copyWithMask method" << endl;
396    exData.copyWithMask(result, exData.wherePositive());    exData.copyWithMask(result, exData.wherePositive());
# Line 234  void DataTestCase::testAll() { Line 404  void DataTestCase::testAll() {
404    
405    cout << "\tCreate a Data object from a DataArrayView" << endl;    cout << "\tCreate a Data object from a DataArrayView" << endl;
406    
   DataArrayView::ValueType viewData;  
407    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
408    viewShape.push_back(3);    viewShape.push_back(3);
409      DataArrayView::ValueType viewData(3);
410    for (int i=0;i<viewShape[0];++i) {    for (int i=0;i<viewShape[0];++i) {
411      viewData.push_back(i);      viewData[i]=i;
412    }    }
413    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
414    
# Line 264  void DataTestCase::testDataConstant() { Line 434  void DataTestCase::testDataConstant() {
434    
435    cout << "\tCreate a DataConstant object from a DataArrayView" << endl;    cout << "\tCreate a DataConstant object from a DataArrayView" << endl;
436    
   DataArrayView::ValueType viewData;  
437    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
438    viewShape.push_back(2);    viewShape.push_back(2);
439    viewShape.push_back(3);    viewShape.push_back(3);
440    viewShape.push_back(4);    viewShape.push_back(4);
441      DataArrayView::ValueType viewData(2*3*4);
442    for (int i=0;i<DataArrayView::noValues(viewShape);++i) {    for (int i=0;i<DataArrayView::noValues(viewShape);++i) {
443      viewData.push_back(i);      viewData[i]=i;
444    }    }
445    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
446    
# Line 301  void DataTestCase::testDataTaggedExcepti Line 471  void DataTestCase::testDataTaggedExcepti
471    
472    cout << endl;    cout << endl;
473    
474    cout << "\tTest DataTagged operations exceptions." << endl;    cout << "\tTest DataTagged exceptions." << endl;
475    
476    Data myData;    Data myData;
477    DataArrayView myView;    DataArrayView myView;
478    
479    try {    try {
480        myData.getSampleDataByTag(0);;        myData.getSampleDataByTag(0);;
481        assert(false);        assert(false);
# Line 312  void DataTestCase::testDataTaggedExcepti Line 484  void DataTestCase::testDataTaggedExcepti
484        //cout << e.what() << endl;        //cout << e.what() << endl;
485        assert(true);        assert(true);
486    }    }
487    /*  
488    try {    try {
489        myData.setTaggedValue(0,myView);;        myData.setTaggedValueFromCPP(0,myView);;
490        assert(false);        assert(false);
491    }    }
492    catch (EsysException& e) {    catch (EsysException& e) {
493        //cout << e.what() << endl;        //cout << e.what() << endl;
494        assert(true);        assert(true);
495    }    }
   */  
496    
497  }  }
498    
# Line 329  void DataTestCase::testDataTagged() { Line 500  void DataTestCase::testDataTagged() {
500    
501    cout << endl;    cout << endl;
502    
503    cout << "\tCreate a DataTagged object from a DataArrayView" << endl;    cout << "\tCreate a DataTagged object with a default value only." << endl;
504    
505      // create tagged data with no tag values just a default
506    
507    DataTagged::TagListType keys;    DataTagged::TagListType keys;
508    
509    DataTagged::ValueListType values;    DataTagged::ValueListType values;
510    DataArrayView::ValueType viewData;  
511    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
512    viewShape.push_back(3);    viewShape.push_back(3);
513    
514      DataArrayView::ValueType viewData(3);
515    for (int i=0;i<viewShape[0];++i) {    for (int i=0;i<viewShape[0];++i) {
516      viewData.push_back(i);      viewData[i]=i;
517    }    }
518    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
519    
   // create tagged data with no tag values just a default  
520    bool expanded=false;    bool expanded=false;
521    
522    Data myData(keys,values,myView,FunctionSpace(),expanded);    Data myData(keys,values,myView,FunctionSpace(),expanded);
   assert(myData.isTagged());  
523    
524    cout << "\tTest some basic operations" << endl;    // cout << myData.toString() << endl;
525    
526    Data myDataCopy(myData);    assert(!myData.isEmpty());
527    myDataCopy.expand();    assert(myData.isTagged());
528    assert(myDataCopy.isExpanded());    assert(myData.getTagNumber(0)==1);
529      assert(myData.getDataPointRank()==1);
530      assert(myData.getLength()==3);
531    
532      DataArrayView myDataView = myData.getPointDataView();
533      assert(!myDataView.isEmpty());
534      assert(myDataView.getOffset()==0);
535      assert(myDataView.getRank()==1);
536      assert(myDataView.noValues()==3);
537      assert(myDataView.getShape().size()==1);
538      assert(myDataView(0)==0.0);
539      assert(myDataView(1)==1.0);
540      assert(myDataView(2)==2.0);
541    
542      myDataView = myData.getDataPoint(0,0);
543      assert(!myDataView.isEmpty());
544      assert(myDataView.getOffset()==0);
545      assert(myDataView.getRank()==1);
546      assert(myDataView.noValues()==3);
547      assert(myDataView.getShape().size()==1);
548      assert(myDataView(0)==0.0);
549      assert(myDataView(1)==1.0);
550      assert(myDataView(2)==2.0);
551    
552      double* sampleData=myData.getSampleData(0);
553      for (int i=0; i<myDataView.noValues(); i++) {
554        assert(sampleData[i]==i);
555      }
556      // use a non-existent tag so we get a pointer to
557      // the first element of the data array
558      sampleData=myData.getSampleDataByTag(9);
559      for (int i=0; i<myData.getLength(); i++) {
560        assert(sampleData[i]==i);
561      }
562    
563      cout << "\tTest setting of a tag and associated value." << endl;
564    
565      // value for tag "1"
566      DataArray eTwo(myView);
567      for (int i=0;i<eTwo.getView().getShape()[0];i++) {
568        eTwo.getView()(i)=i+2.0;
569      }
570    
571      myData.setTaggedValueFromCPP(1,eTwo.getView());
572    
573      assert(myData.getLength()==6);
574    
575      myDataView = myData.getDataPoint(0,0);
576      assert(myDataView==eTwo.getView());
577      assert(!myDataView.isEmpty());
578      assert(myDataView.getOffset()==3);
579      assert(myDataView.getRank()==1);
580      assert(myDataView.noValues()==3);
581      assert(myDataView.getShape().size()==1);
582      assert(myDataView(0)==2);
583      assert(myDataView(1)==3);
584      assert(myDataView(2)==4);
585    
586      sampleData=myData.getSampleDataByTag(1);
587      for (int i=0; i<myDataView.noValues(); i++) {
588        assert(sampleData[i]==i+2);
589      }
590    
591  }  }
592    
# Line 369  void DataTestCase::testConstructors() { Line 604  void DataTestCase::testConstructors() {
604      viewShape.push_back(2);      viewShape.push_back(2);
605      viewShape.push_back(3);      viewShape.push_back(3);
606      Data temp(1.3,viewShape,FunctionSpace(),false);      Data temp(1.3,viewShape,FunctionSpace(),false);
     cout << "\tDump it toString:" << endl;  
     cout << temp.toString() << endl;  
607    }    }
608  }  }
609    
610  void DataTestCase::testOperations() {  void DataTestCase::testOperations() {
611    
612    cout << endl;    cout << endl;
613    
614    cout << "\tCreate a rank 2 Data object" << endl;    // define the shape for the DataArrayView test data
615    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType shape;
616    viewShape.push_back(2);    shape.push_back(2);
617    viewShape.push_back(3);    shape.push_back(3);
618    
619      // allocate the data for the DataArrayView
620      DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
621    
622    Data base(2.0,viewShape,FunctionSpace(),false);    // construct DataArrayView
623    Data power(3.0,viewShape,FunctionSpace(),false);    DataArrayView dataView(data,shape);
624    
625      // assign values to the data
626      for (int i=0;i<shape[0];i++) {
627        for (int j=0;j<shape[1];j++) {
628          dataView(i,j)=dataView.index(i,j);
629        }
630      }
631    
632    cout << "\tPerform basic exercises of unary operations" << endl;    Data base(dataView);
633    
634      // test unary operations
635    
636      cout << "\tTest Data::pow." << endl;
637      Data power(3.0,shape,FunctionSpace(),true);
638    Data result(base.powD(power));    Data result(base.powD(power));
639    assert(result.getDataPoint(0,0)(0,0) == 8);    for (int i=0;i<shape[0];i++) {
640        for (int j=0;j<shape[1];j++) {
641          assert(result.getPointDataView()(i,j) == pow(dataView.index(i,j),3.0));
642        }
643      }
644    
645      cout << "\tTest Data::sin." << endl;
646    result.copy(base.sin());    result.copy(base.sin());
647    assert(true);    assert(true);
648    
649      cout << "\tTest Data::cos." << endl;
650    result.copy(base.cos());    result.copy(base.cos());
651    assert(true);    assert(true);
652    
653      cout << "\tTest Data::tan." << endl;
654    result.copy(base.tan());    result.copy(base.tan());
655    assert(true);    assert(true);
656    
657    result.copy(base.log());    cout << "\tTest Data::asin." << endl;
658      result.copy(base.asin());
659    assert(true);    assert(true);
660    
661    result.copy(base.ln());    cout << "\tTest Data::acos." << endl;
662      result.copy(base.acos());
663    assert(true);    assert(true);
664    
665    result.copy(base.abs());    cout << "\tTest Data::atan." << endl;
666      result.copy(base.atan());
667    assert(true);    assert(true);
668    
669    result.copy(base.maxval());    cout << "\tTest Data::sinh." << endl;
670      result.copy(base.sinh());
671    assert(true);    assert(true);
672    
673    result.copy(base.minval());    cout << "\tTest Data::cosh." << endl;
674      result.copy(base.cosh());
675    assert(true);    assert(true);
676    
677    result.copy(base.length());    cout << "\tTest Data::tanh." << endl;
678      result.copy(base.tanh());
679    assert(true);    assert(true);
680    
681    result.copy(base.sign());    cout << "\tTest Data::asinh." << endl;
682      result.copy(base.asinh());
683    assert(true);    assert(true);
684    
685    result.copy(base.transpose(0));    cout << "\tTest Data::acosh." << endl;
686      result.copy(base.acosh());
687    assert(true);    assert(true);
688    
689    result.copy(base.trace());    cout << "\tTest Data::atanh." << endl;
690      result.copy(base.atanh());
691      assert(true);
692    
693      cout << "\tTest Data::log." << endl;
694      result.copy(base.log());
695      assert(true);
696    
697      //cout << "\tTest Data::ln." << endl;
698      //result.copy(base.ln());
699      //assert(true);
700    
701      cout << "\tTest Data::abs." << endl;
702      result.copy(base.abs());
703      assert(true);
704    
705      cout << "\tTest Data::sign." << endl;
706      result.copy(base.sign());
707    assert(true);    assert(true);
708    
709      cout << "\tTest Data::exp." << endl;
710    result.copy(base.exp());    result.copy(base.exp());
711    assert(true);    assert(true);
712    
713      cout << "\tTest Data::sqrt." << endl;
714    result.copy(base.sqrt());    result.copy(base.sqrt());
715    assert(true);    assert(true);
716    
717      cout << "\tTest Data::neg." << endl;
718    result.copy(base.neg());    result.copy(base.neg());
719    assert(true);    assert(true);
720    
721      cout << "\tTest Data::pos." << endl;
722    result.copy(base.pos());    result.copy(base.pos());
723    assert(true);    for (int i=0;i<shape[0];i++) {
724        for (int j=0;j<shape[1];j++) {
725          assert(result.getPointDataView()(i,j) == dataView.index(i,j));
726        }
727      }
728    
729      // test reduction operations
730    
731      cout << "\tTest Data::Lsup." << endl;
732      assert(base.Lsup() == 5);
733    
734      cout << "\tTest Data::sup." << endl;
735      assert(base.sup() == 5);
736    
737      cout << "\tTest Data::inf." << endl;
738      assert(base.inf() == 0);
739    
740      // test data-point reduction operations
741    
742      cout << "\tTest Data::minval." << endl;
743      result.copy(base.minval());
744      assert(result.getPointDataView()() == 0);
745    
746      cout << "\tTest Data::maxval." << endl;
747      result.copy(base.maxval());
748      assert(result.getPointDataView()() == 5);
749    
750      //cout << "\tTest Data::length." << endl;
751      //result.copy(base.length());
752      //assert(pow(result.getPointDataView()(),2.0) == 55);
753    
754      cout << "\tTest Data::trace." << endl;
755      result.copy(base.trace());
756      assert(result.getPointDataView()() == 15);
757    
758      //result.copy(base.transpose(0));
759      //assert(true);
760    
761    }
762    
763    void DataTestCase::testRefValue() {
764    
765      //
766      // Note - this test can't be run as boost::python::numeric::array
767      // objects can only be created and used from within a python thread!
768      //
769    
770      cout << endl;
771    
772      cout << "\tTest Data object RefValue methods." << endl;
773    
774      // Create three Data object - DataExpanded, DataConstant and DataEmpty
775      DataArrayView::ShapeType viewShape;
776      viewShape.push_back(3);
777      DataArrayView::ValueType viewData(3);
778      for (int i=0;i<viewShape[0];++i) {
779        viewData[i]=i;
780      }
781      DataArrayView myView(viewData,viewShape);
782    
783      bool expanded=true;
784    
785      Data expandedData(myView,FunctionSpace(),expanded);
786      Data constantData(myView);
787      Data emptyData;
788    
789      assert(expandedData.isExpanded());
790      assert(constantData.isConstant());
791      assert(emptyData.isEmpty());
792    
793      // Check assertions are thrown for RefValue methods on DataEmpty
794    
795      int ref = 0;
796      boost::python::numeric::array num_array(1.0);
797    
798      try {
799          emptyData.getRefValue(ref,num_array);
800          assert(false);
801      }
802      catch (EsysException& e) {
803          assert(true);
804      }
805      try {
806          emptyData.setRefValue(ref,num_array);
807          assert(false);
808      }
809      catch (EsysException& e) {
810          assert(true);
811      }
812    
813      // Check assertions are thrown for RefValue methods on DataConstant
814      try {
815          constantData.getRefValue(ref,num_array);
816          assert(false);
817      }
818      catch (EsysException& e) {
819          assert(true);
820      }
821      try {
822          constantData.setRefValue(ref,num_array);
823          assert(false);
824      }
825      catch (EsysException& e) {
826          assert(true);
827      }
828    
829      // Check calls to RefValue methods on DataExpanded
830      expandedData.getRefValue(ref,num_array);
831      expandedData.setRefValue(ref,num_array);
832    
833    }
834    
835    void DataTestCase::testMemAlloc() {
836    
837      //
838      // Simple little sanity check for the memory allocator
839    
840      cout << endl;
841    
842      Data *testData;
843      for (int i=0; i<1000; i++) {
844        testData = new Data(0.0, DataArrayView::ShapeType(), FunctionSpace(), true);
845        delete testData;
846      }
847    
848      DataArrayView::ShapeType viewShape;
849      viewShape.push_back(10);
850      viewShape.push_back(10);
851      viewShape.push_back(10);
852    
853      Data *testData2;
854      Data *testData3 = new Data(0.0, viewShape, FunctionSpace(), true);
855      for (int i=0; i<1000; i++) {
856        testData2 = new Data(0.0, viewShape, FunctionSpace(), true);
857        delete testData2;
858      }
859      delete testData3;
860    
861  }  }
862    
863  TestSuite* DataTestCase::suite ()  TestSuite* DataTestCase::suite ()
# Line 453  TestSuite* DataTestCase::suite () Line 874  TestSuite* DataTestCase::suite ()
874    testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));    testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
875    testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));    testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
876    testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));    testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
877      //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
878      testSuite->addTest (new TestCaller< DataTestCase>("testMemAlloc",&DataTestCase::testMemAlloc));
879    
880    return testSuite;    return testSuite;
881  }  }

Legend:
Removed from v.97  
changed lines
  Added in v.537

  ViewVC Help
Powered by ViewVC 1.1.26