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

Diff of /trunk/escript/test/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/DataTestCase.cpp revision 682 by robwdcock, Mon Mar 27 02:43:09 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 "escript/FunctionSpace.h"
19    #include "esysUtils/EsysException.h"
20    
21    #include "escript/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 source 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:\n" << source.toString() << endl;
229    
230      //      //
231      // weak tests for slicing DataTagged      // create a target 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:\n" << 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:\n" << 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" to target
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        //cout << "target:\n" << target.toString() << endl;
272    
273        //
274        // set a slice in target from source
275    
276        region.clear();
277        region.push_back(DataArrayView::RegionType::value_type(0,0));
278        region.push_back(DataArrayView::RegionType::value_type(1,1));
279    
280        target.setSlice(source,region);
281    
282        //cout << "target:\n" << target.toString() << endl;
283    
284        assert(target.isTagged());
285        assert(target.getDataPointRank()==2);
286        assert(target.getDataPoint(0,0)(0,0)==0);
287        assert(target.getDataPoint(0,0)(0,1)==source.getDataPoint(0,0)());
288        assert(target.getDataPoint(0,0)(0,2)==4);
289        assert(target.getDataPoint(0,0)(1,0)==1);
290        assert(target.getDataPoint(0,0)(1,1)==3);
291        assert(target.getDataPoint(0,0)(1,2)==5);
292    
293      //      //
294      // convert the data to tagged      // add a value for tag "2" to source
295      temp.tag();  
296        DataArrayView::ShapeType viewShape2;
297        DataArrayView::ValueType viewData2(1);
298        viewData2[0]=6;
299        DataArrayView dataView2(viewData2,viewShape2);
300    
301        source.setTaggedValueFromCPP(2, dataView2);
302    
303        //cout << "source:\n" << source.toString() << endl;
304    
305        //
306        // set a slice in target from source
307    
308        region.clear();
309        region.push_back(DataArrayView::RegionType::value_type(0,0));
310        region.push_back(DataArrayView::RegionType::value_type(1,1));
311    
312        target.setSlice(source,region);
313    
314        //cout << "target:\n" << target.toString() << endl;
315    
316        assert(target.isTagged());
317        assert(target.getDataPointRank()==2);
318    
319        // use a non-existant tag so we get a pointer to the default value
320        // ie: the first element in the data array
321        DataAbstract::ValueType::value_type* targetData=target.getSampleDataByTag(9);
322        for (int i=0; i<target.getLength(); i++) {
323          assert(targetData[i]>=0);
324        }
325        assert(targetData[0]==1.3);
326        assert(targetData[1]==1.3);
327        assert(targetData[2]==10);
328        assert(targetData[3]==10);
329        assert(targetData[4]==1.3);
330        assert(targetData[5]==1.3);
331        assert(targetData[6]==0);
332        assert(targetData[7]==1);
333        assert(targetData[8]==10);
334        assert(targetData[9]==3);
335        assert(targetData[10]==4);
336        assert(targetData[11]==5);
337        assert(targetData[12]==1.3);
338        assert(targetData[13]==1.3);
339        assert(targetData[14]==6);
340        assert(targetData[15]==10);
341        assert(targetData[16]==1.3);
342        assert(targetData[17]==1.3);
343    
344      }
345    
346      {
347    
348        cout << "\tTest get-slicing DataExpanded" << endl;
349    
350        DataArrayView::ShapeType viewShape;
351        viewShape.push_back(2);
352        viewShape.push_back(3);
353        Data temp(1.3,viewShape,FunctionSpace(),true);
354    
355      temp.getDataPoint(0,0)(0,0)=0.0;      temp.getDataPoint(0,0)(0,0)=0.0;
356      temp.getDataPoint(0,0)(1,1)=1.0;      temp.getDataPoint(0,0)(1,1)=1.0;
357    
358      DataArrayView::RegionType region;      DataArrayView::RegionType region;
359      region.push_back(DataArrayView::RegionType::value_type(0,0));      region.push_back(DataArrayView::RegionType::value_type(0,0));
360      region.push_back(DataArrayView::RegionType::value_type(0,0));      region.push_back(DataArrayView::RegionType::value_type(0,0));
361    
362      Data slice(temp.getSlice(region));      Data slice(temp.getSlice(region));
363    
364      assert(slice.getDataPointRank()==0);      assert(slice.getDataPointRank()==0);
365      assert(slice.getDataPoint(0,0)()==0.0);      assert(slice.getDataPoint(0,0)()==0.0);
366      //  
     // try the same but this time to produce a matrix containing one value  
367      region.clear();      region.clear();
368      region.push_back(DataArrayView::RegionType::value_type(0,1));      region.push_back(DataArrayView::RegionType::value_type(0,1));
369      region.push_back(DataArrayView::RegionType::value_type(0,1));      region.push_back(DataArrayView::RegionType::value_type(0,1));
370    
371      slice=temp.getSlice(region);      slice=temp.getSlice(region);
372    
373      assert(slice.getDataPointRank()==2);      assert(slice.getDataPointRank()==2);
374      assert(slice.getDataPoint(0,0)(0,0)==0.0);      assert(slice.getDataPoint(0,0)(0,0)==0.0);
375    
376      region.clear();      region.clear();
377      region.push_back(DataArrayView::RegionType::value_type(0,2));      region.push_back(DataArrayView::RegionType::value_type(0,2));
378      region.push_back(DataArrayView::RegionType::value_type(0,2));      region.push_back(DataArrayView::RegionType::value_type(0,2));
379    
380      slice=temp.getSlice(region);      slice=temp.getSlice(region);
381    
382      assert(slice.getDataPoint(0,0)(0,0)==0.0);      assert(slice.getDataPoint(0,0)(0,0)==0.0);
383      assert(slice.getDataPoint(0,0)(1,1)==1.0);      assert(slice.getDataPoint(0,0)(1,1)==1.0);
   }  
384    
   {  
     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)());  
385    }    }
386    
387    {    {
388    
389        cout << "\tTest set-slicing DataExpanded" << endl;
390    
391      DataArrayView::ShapeType viewShape;      DataArrayView::ShapeType viewShape;
392      Data source(10.0,viewShape,FunctionSpace(),true);      Data source(10.0,viewShape,FunctionSpace(),true);
393      //  
     // weak tests for setting a slice of DataConstant  
394      viewShape.push_back(2);      viewShape.push_back(2);
395      viewShape.push_back(3);      viewShape.push_back(3);
396      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)());  
   }  
397    
   {  
     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();  
398      DataArrayView::RegionType region;      DataArrayView::RegionType region;
399      region.push_back(DataArrayView::RegionType::value_type(0,0));      region.push_back(DataArrayView::RegionType::value_type(0,0));
400      region.push_back(DataArrayView::RegionType::value_type(0,0));      region.push_back(DataArrayView::RegionType::value_type(0,0));
401    
402      target.setSlice(source,region);      target.setSlice(source,region);
403    
404      assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());      assert(target.getDataPoint(0,0)(0,0)==source.getDataPoint(0,0)());
405    
406    }    }
407    
408  }  }
409    
410  void DataTestCase::testMore() {  void DataTestCase::testAll() {
411    
412    cout << endl;    cout << endl;
413    
414    cout << "\tCreate a Data object from a DataArrayView" << endl;    cout << "\tCreate a Data object from a DataArrayView" << endl;
415    
   DataArrayView::ValueType viewData;  
416    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
417    viewShape.push_back(3);    viewShape.push_back(3);
418      DataArrayView::ValueType viewData(3);
419    for (int i=0;i<viewShape[0];++i) {    for (int i=0;i<viewShape[0];++i) {
420      viewData.push_back(i);      viewData[i]=i;
421    }    }
422    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
423    
# Line 210  void DataTestCase::testMore() { Line 434  void DataTestCase::testMore() {
434    result=exData*cData;    result=exData*cData;
435    assert(result.isExpanded());    assert(result.isExpanded());
436    
   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());  
   
437  }  }
438    
439  void DataTestCase::testAll() {  void DataTestCase::testMore() {
440    
441    cout << endl;    cout << endl;
442    
443    cout << "\tCreate a Data object from a DataArrayView" << endl;    cout << "\tCreate a Data object from a DataArrayView" << endl;
444    
   DataArrayView::ValueType viewData;  
445    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
446    viewShape.push_back(3);    viewShape.push_back(3);
447      DataArrayView::ValueType viewData(3);
448    for (int i=0;i<viewShape[0];++i) {    for (int i=0;i<viewShape[0];++i) {
449      viewData.push_back(i);      viewData[i]=i;
450    }    }
451    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
452    
453    bool expanded=true;    bool expanded=true;
   
454    Data exData(myView,FunctionSpace(),expanded);    Data exData(myView,FunctionSpace(),expanded);
455    Data cData(myView);    Data cData(myView);
456    Data result;    Data result;
# Line 256  void DataTestCase::testAll() { Line 463  void DataTestCase::testAll() {
463    result=exData*cData;    result=exData*cData;
464    assert(result.isExpanded());    assert(result.isExpanded());
465    
466      assert(result.Lsup()==4);
467      assert(result.sup()==4);
468      assert(result.inf()==0);
469    
470      result=exData+cData;
471      result=exData-cData;
472      result=exData/cData;
473    
474      cout << "\tExercise wherePositive method" << endl;
475      assert(!exData.wherePositive().isEmpty());
476    
477      cout << "\tExercise copyWithMask method" << endl;
478      exData.copyWithMask(result, exData.wherePositive());
479      assert(!exData.wherePositive().isEmpty());
480    
481  }  }
482    
483  void DataTestCase::testDataConstant() {  void DataTestCase::testDataConstant() {
# Line 264  void DataTestCase::testDataConstant() { Line 486  void DataTestCase::testDataConstant() {
486    
487    cout << "\tCreate a DataConstant object from a DataArrayView" << endl;    cout << "\tCreate a DataConstant object from a DataArrayView" << endl;
488    
   DataArrayView::ValueType viewData;  
489    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
490    viewShape.push_back(2);    viewShape.push_back(2);
491    viewShape.push_back(3);    viewShape.push_back(3);
492    viewShape.push_back(4);    viewShape.push_back(4);
493      DataArrayView::ValueType viewData(2*3*4);
494    for (int i=0;i<DataArrayView::noValues(viewShape);++i) {    for (int i=0;i<DataArrayView::noValues(viewShape);++i) {
495      viewData.push_back(i);      viewData[i]=i;
496    }    }
497    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
498    
# Line 297  void DataTestCase::testDataConstant() { Line 519  void DataTestCase::testDataConstant() {
519    
520  }  }
521    
522    void DataTestCase::testDataTagged() {
523    
524      cout << endl;
525    
526      {
527    
528        cout << "\tCreate a DataTagged object with a default value only." << endl;
529    
530        DataTagged::TagListType keys;
531    
532        DataTagged::ValueListType values;
533    
534        DataArrayView::ShapeType viewShape;
535        viewShape.push_back(3);
536    
537        DataArrayView::ValueType viewData(3);
538        for (int i=0;i<viewShape[0];i++) {
539          viewData[i]=i;
540        }
541        DataArrayView defaultValue(viewData,viewShape);
542    
543        bool expanded=false;
544      
545        Data myData(keys,values,defaultValue,FunctionSpace(),expanded);
546    
547        // cout << myData.toString() << endl;
548    
549        assert(!myData.isEmpty());
550        assert(myData.isTagged());
551        assert(myData.getTagNumber(0)==1);
552        assert(myData.getDataPointRank()==1);
553        assert(myData.getLength()==3);
554    
555        DataArrayView myDataView = myData.getPointDataView();
556        assert(!myDataView.isEmpty());
557        assert(myDataView.getOffset()==0);
558        assert(myDataView.getRank()==1);
559        assert(myDataView.noValues()==3);
560        assert(myDataView.getShape().size()==1);
561        assert(myDataView(0)==0.0);
562        assert(myDataView(1)==1.0);
563        assert(myDataView(2)==2.0);
564    
565        myDataView = myData.getDataPoint(0,0);
566        assert(!myDataView.isEmpty());
567        assert(myDataView.getOffset()==0);
568        assert(myDataView.getRank()==1);
569        assert(myDataView.noValues()==3);
570        assert(myDataView.getShape().size()==1);
571        assert(myDataView(0)==0.0);
572        assert(myDataView(1)==1.0);
573        assert(myDataView(2)==2.0);
574    
575        double* sampleData=myData.getSampleData(0);
576        for (int i=0; i<myDataView.noValues(); i++) {
577          assert(sampleData[i]==i);
578        }
579        // use a non-existent tag so we get a pointer to
580        // the first element of the data array
581        sampleData=myData.getSampleDataByTag(9);
582        for (int i=0; i<myData.getLength(); i++) {
583          assert(sampleData[i]==i);
584        }
585    
586        cout << "\tTest setting of a tag and associated value." << endl;
587    
588        // value for tag "1"
589        DataArray eTwo(defaultValue);
590        for (int i=0;i<eTwo.getView().getShape()[0];i++) {
591          eTwo.getView()(i)=i+2.0;
592        }
593    
594        myData.setTaggedValueFromCPP(1,eTwo.getView());
595    
596        assert(myData.getLength()==6);
597    
598        myDataView = myData.getDataPoint(0,0);
599        assert(myDataView==eTwo.getView());
600        assert(!myDataView.isEmpty());
601        assert(myDataView.getOffset()==3);
602        assert(myDataView.getRank()==1);
603        assert(myDataView.noValues()==3);
604        assert(myDataView.getShape().size()==1);
605        assert(myDataView(0)==2);
606        assert(myDataView(1)==3);
607        assert(myDataView(2)==4);
608    
609        sampleData=myData.getSampleDataByTag(1);
610        for (int i=0; i<myDataView.noValues(); i++) {
611          assert(sampleData[i]==i+2);
612        }
613    
614      }
615    
616      {
617    
618        cout << "\tCreate a DataTagged object via tag() method." << endl;
619    
620        DataArrayView::ShapeType viewShape;
621        viewShape.push_back(2);
622        viewShape.push_back(3);
623        Data myData(1.3,viewShape,FunctionSpace(),false);
624        myData.tag();
625    
626        //cout << myData.toString() << endl;
627    
628        assert(!myData.isEmpty());
629        assert(myData.isTagged());
630        assert(myData.getTagNumber(0)==1);
631        assert(myData.getDataPointRank()==2);
632        assert(myData.getLength()==6);
633    
634        // check default value
635        DataArrayView myDataView = myData.getPointDataView();
636        assert(!myDataView.isEmpty());
637        assert(myDataView.getOffset()==0);
638        assert(myDataView.getRank()==2);
639        assert(myDataView.noValues()==6);
640        assert(myDataView.getShape().size()==2);
641        assert(myDataView(0,0)==1.3);
642        assert(myDataView(0,1)==1.3);
643        assert(myDataView(0,2)==1.3);
644        assert(myDataView(1,0)==1.3);
645        assert(myDataView(1,1)==1.3);
646        assert(myDataView(1,2)==1.3);
647    
648        // check value for data-point (0,0).
649        myDataView = myData.getDataPoint(0,0);
650        assert(!myDataView.isEmpty());
651        assert(myDataView.getOffset()==0);
652        assert(myDataView.getRank()==2);
653        assert(myDataView.noValues()==6);
654        assert(myDataView.getShape().size()==2);
655        assert(myDataView(0,0)==1.3);
656        assert(myDataView(0,1)==1.3);
657        assert(myDataView(0,2)==1.3);
658        assert(myDataView(1,0)==1.3);
659        assert(myDataView(1,1)==1.3);
660        assert(myDataView(1,2)==1.3);
661    
662      }
663    
664    }
665    
666  void DataTestCase::testDataTaggedExceptions() {  void DataTestCase::testDataTaggedExceptions() {
667    
668    cout << endl;    cout << endl;
669    
670    cout << "\tTest DataTagged operations exceptions." << endl;    cout << "\tTest DataTagged exceptions." << endl;
671    
672    Data myData;    Data myData;
673    DataArrayView myView;    DataArrayView myView;
674    
675    try {    try {
676        myData.getSampleDataByTag(0);;        myData.getSampleDataByTag(0);;
677        assert(false);        assert(false);
# Line 312  void DataTestCase::testDataTaggedExcepti Line 680  void DataTestCase::testDataTaggedExcepti
680        //cout << e.what() << endl;        //cout << e.what() << endl;
681        assert(true);        assert(true);
682    }    }
683    /*  
684    try {    try {
685        myData.setTaggedValue(0,myView);;        myData.setTaggedValueFromCPP(0,myView);;
686        assert(false);        assert(false);
687    }    }
688    catch (EsysException& e) {    catch (EsysException& e) {
689        //cout << e.what() << endl;        //cout << e.what() << endl;
690        assert(true);        assert(true);
691    }    }
   */  
   
 }  
   
 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());  
692    
693  }  }
694    
# Line 369  void DataTestCase::testConstructors() { Line 706  void DataTestCase::testConstructors() {
706      viewShape.push_back(2);      viewShape.push_back(2);
707      viewShape.push_back(3);      viewShape.push_back(3);
708      Data temp(1.3,viewShape,FunctionSpace(),false);      Data temp(1.3,viewShape,FunctionSpace(),false);
     cout << "\tDump it toString:" << endl;  
     cout << temp.toString() << endl;  
709    }    }
710  }  }
711    
712  void DataTestCase::testOperations() {  void DataTestCase::testOperations() {
713    
714      cout << endl;
715    
716      // define the shape for the DataArrayView test data
717      DataArrayView::ShapeType shape;
718      shape.push_back(2);
719      shape.push_back(3);
720    
721      // allocate the data for the DataArrayView
722      DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
723    
724      // construct DataArrayView
725      DataArrayView dataView(data,shape);
726    
727      // assign values to the data
728      for (int i=0;i<shape[0];i++) {
729        for (int j=0;j<shape[1];j++) {
730          dataView(i,j)=dataView.index(i,j);
731        }
732      }
733    
734      Data baseEx(dataView,FunctionSpace(),true);
735      Data baseCon(dataView,FunctionSpace(),false);
736      Data baseTag(dataView,FunctionSpace(),false);
737      baseTag.tag();
738    
739      assert(baseEx.isExpanded());
740      assert(baseCon.isConstant());
741      assert(baseTag.isTagged());
742    
743      Data resultEx;
744      Data resultCon;
745      Data resultTag;
746    
747      // test unary operations
748    
749      cout << "\tTest Data::pow." << endl;
750      Data power(3.0,shape,FunctionSpace(),true);
751      resultEx.copy(baseEx.powD(power));
752      resultCon.copy(baseCon.powD(power));
753      resultTag.copy(baseTag.powD(power));
754      for (int i=0;i<shape[0];i++) {
755        for (int j=0;j<shape[1];j++) {
756          assert(resultEx.getPointDataView()(i,j) == pow(dataView.index(i,j),3.0));
757          assert(resultCon.getPointDataView()(i,j) == pow(dataView.index(i,j),3.0));
758          assert(resultTag.getPointDataView()(i,j) == pow(dataView.index(i,j),3.0));
759        }
760      }
761    
762      cout << "\tTest Data::sin." << endl;
763      resultEx.copy(baseEx.sin());
764      resultCon.copy(baseCon.sin());
765      resultTag.copy(baseTag.sin());
766      assert(true);
767    
768      cout << "\tTest Data::cos." << endl;
769      resultEx.copy(baseEx.cos());
770      resultCon.copy(baseCon.cos());
771      resultTag.copy(baseTag.cos());
772      assert(true);
773    
774      cout << "\tTest Data::tan." << endl;
775      resultEx.copy(baseEx.tan());
776      resultCon.copy(baseCon.tan());
777      resultTag.copy(baseTag.tan());
778      assert(true);
779    
780      cout << "\tTest Data::asin." << endl;
781      resultEx.copy(baseEx.asin());
782      resultCon.copy(baseCon.asin());
783      resultTag.copy(baseTag.asin());
784      assert(true);
785    
786      cout << "\tTest Data::acos." << endl;
787      resultEx.copy(baseEx.acos());
788      resultCon.copy(baseCon.acos());
789      resultTag.copy(baseTag.acos());
790      assert(true);
791    
792      cout << "\tTest Data::atan." << endl;
793      resultEx.copy(baseEx.atan());
794      resultCon.copy(baseCon.atan());
795      resultTag.copy(baseTag.atan());
796      assert(true);
797    
798      cout << "\tTest Data::sinh." << endl;
799      resultEx.copy(baseEx.sinh());
800      resultCon.copy(baseCon.sinh());
801      resultTag.copy(baseTag.sinh());
802      assert(true);
803    
804      cout << "\tTest Data::cosh." << endl;
805      resultEx.copy(baseEx.cosh());
806      resultCon.copy(baseCon.cosh());
807      resultTag.copy(baseTag.cosh());
808      assert(true);
809    
810      cout << "\tTest Data::tanh." << endl;
811      resultEx.copy(baseEx.tanh());
812      resultCon.copy(baseCon.tanh());
813      resultTag.copy(baseTag.tanh());
814      assert(true);
815    
816      cout << "\tTest Data::asinh." << endl;
817      resultEx.copy(baseEx.asinh());
818      resultCon.copy(baseCon.asinh());
819      resultTag.copy(baseTag.asinh());
820      assert(true);
821    
822      cout << "\tTest Data::acosh." << endl;
823      resultEx.copy(baseEx.acosh());
824      resultCon.copy(baseCon.acosh());
825      resultTag.copy(baseTag.acosh());
826      assert(true);
827    
828      cout << "\tTest Data::atanh." << endl;
829      resultEx.copy(baseEx.atanh());
830      resultCon.copy(baseCon.atanh());
831      resultTag.copy(baseTag.atanh());
832      assert(true);
833    
834      cout << "\tTest Data::log." << endl;
835      resultEx.copy(baseEx.log());
836      resultCon.copy(baseCon.log());
837      resultTag.copy(baseTag.log());
838      assert(true);
839    
840      cout << "\tTest Data::abs." << endl;
841      resultEx.copy(baseEx.abs());
842      resultCon.copy(baseCon.abs());
843      resultTag.copy(baseTag.abs());
844      assert(true);
845    
846      cout << "\tTest Data::sign." << endl;
847      resultEx.copy(baseEx.sign());
848      resultCon.copy(baseCon.sign());
849      resultTag.copy(baseTag.sign());
850      assert(true);
851    
852      cout << "\tTest Data::exp." << endl;
853      resultEx.copy(baseEx.exp());
854      resultCon.copy(baseCon.exp());
855      resultTag.copy(baseTag.exp());
856      assert(true);
857    
858      cout << "\tTest Data::sqrt." << endl;
859      resultEx.copy(baseEx.sqrt());
860      resultCon.copy(baseCon.sqrt());
861      resultTag.copy(baseTag.sqrt());
862      assert(true);
863    
864      cout << "\tTest Data::neg." << endl;
865      resultEx.copy(baseEx.neg());
866      resultCon.copy(baseCon.neg());
867      resultTag.copy(baseTag.neg());
868      assert(true);
869    
870      cout << "\tTest Data::pos." << endl;
871      resultEx.copy(baseEx.pos());
872      resultCon.copy(baseCon.pos());
873      resultTag.copy(baseTag.pos());
874      for (int i=0;i<shape[0];i++) {
875        for (int j=0;j<shape[1];j++) {
876          assert(resultEx.getPointDataView()(i,j) == dataView.index(i,j));
877          assert(resultCon.getPointDataView()(i,j) == dataView.index(i,j));
878          assert(resultTag.getPointDataView()(i,j) == dataView.index(i,j));
879        }
880      }
881    
882      // test reduction operations
883    
884      cout << "\tTest Data::Lsup." << endl;
885      assert(baseEx.Lsup() == 5);
886      assert(baseCon.Lsup() == 5);
887      assert(baseTag.Lsup() == 5);
888    
889      cout << "\tTest Data::sup." << endl;
890      assert(baseEx.sup() == 5);
891      assert(baseCon.sup() == 5);
892      assert(baseTag.sup() == 5);
893    
894      cout << "\tTest Data::inf." << endl;
895      assert(baseEx.inf() == 0);
896      assert(baseCon.inf() == 0);
897      assert(baseTag.inf() == 0);
898    
899      // test data-point reduction operations
900    
901      cout << "\tTest Data::minval." << endl;
902      resultEx.copy(baseEx.minval());
903      resultCon.copy(baseCon.minval());
904      resultTag.copy(baseTag.minval());
905      assert(resultEx.getPointDataView()() == 0);
906      assert(resultCon.getPointDataView()() == 0);
907      assert(resultTag.getPointDataView()() == 0);
908    
909      cout << "\tTest Data::maxval." << endl;
910      resultEx.copy(baseEx.maxval());
911      resultCon.copy(baseCon.maxval());
912      resultTag.copy(baseTag.maxval());
913      assert(resultEx.getPointDataView()() == 5);
914      assert(resultCon.getPointDataView()() == 5);
915      assert(resultTag.getPointDataView()() == 5);
916    
917      cout << "\tTest Data::trace." << endl;
918      resultEx.copy(baseEx.trace());
919      resultCon.copy(baseCon.trace());
920      resultTag.copy(baseTag.trace());
921      assert(resultEx.getPointDataView()() == 15);
922      assert(resultCon.getPointDataView()() == 15);
923      assert(resultTag.getPointDataView()() == 15);
924    
925    }
926    
927    void DataTestCase::testRefValue() {
928    
929      //
930      // Note - this test can't be run as boost::python::numeric::array
931      // objects can only be created and used from within a python thread!
932      //
933    
934    cout << endl;    cout << endl;
935    
936    cout << "\tCreate a rank 2 Data object" << endl;    cout << "\tTest Data object RefValue methods." << endl;
937    
938      // Create three Data object - DataExpanded, DataConstant and DataEmpty
939    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
   viewShape.push_back(2);  
940    viewShape.push_back(3);    viewShape.push_back(3);
941      DataArrayView::ValueType viewData(3);
942      for (int i=0;i<viewShape[0];++i) {
943        viewData[i]=i;
944      }
945      DataArrayView myView(viewData,viewShape);
946    
947      bool expanded=true;
948    
949      Data expandedData(myView,FunctionSpace(),expanded);
950      Data constantData(myView);
951      Data emptyData;
952    
953      assert(expandedData.isExpanded());
954      assert(constantData.isConstant());
955      assert(emptyData.isEmpty());
956    
957      // Check assertions are thrown for RefValue methods on DataEmpty
958    
959      int ref = 0;
960      boost::python::numeric::array num_array(1.0);
961    
962      try {
963          emptyData.getRefValue(ref,num_array);
964          assert(false);
965      }
966      catch (EsysException& e) {
967          assert(true);
968      }
969      try {
970          emptyData.setRefValue(ref,num_array);
971          assert(false);
972      }
973      catch (EsysException& e) {
974          assert(true);
975      }
976    
977      // Check assertions are thrown for RefValue methods on DataConstant
978      try {
979          constantData.getRefValue(ref,num_array);
980          assert(false);
981      }
982      catch (EsysException& e) {
983          assert(true);
984      }
985      try {
986          constantData.setRefValue(ref,num_array);
987          assert(false);
988      }
989      catch (EsysException& e) {
990          assert(true);
991      }
992    
993      // Check calls to RefValue methods on DataExpanded
994      expandedData.getRefValue(ref,num_array);
995      expandedData.setRefValue(ref,num_array);
996    
997    }
998    
999    Data base(2.0,viewShape,FunctionSpace(),false);  void DataTestCase::testMemAlloc() {
   Data power(3.0,viewShape,FunctionSpace(),false);  
1000    
1001    cout << "\tTest powD,sin,cos,tan,log,ln operations" << endl;    //
1002      // Simple little sanity check for the memory allocator
1003    
1004    Data result(base.powD(power));    cout << endl;
1005    assert(result.getDataPoint(0,0)(0,0) == 8);  
1006      Data *testData;
1007      for (int i=0; i<1000; i++) {
1008        testData = new Data(0.0, DataArrayView::ShapeType(), FunctionSpace(), true);
1009        delete testData;
1010      }
1011    
1012      DataArrayView::ShapeType viewShape;
1013      viewShape.push_back(10);
1014      viewShape.push_back(10);
1015      viewShape.push_back(10);
1016    
1017      Data *testData2;
1018      Data *testData3 = new Data(0.0, viewShape, FunctionSpace(), true);
1019      for (int i=0; i<1000; i++) {
1020        testData2 = new Data(0.0, viewShape, FunctionSpace(), true);
1021        delete testData2;
1022      }
1023      delete testData3;
1024    
   result.copy(base.sin());  
   result.copy(base.cos());  
   result.copy(base.tan());  
   result.copy(base.log());  
   result.copy(base.ln());  
1025  }  }
1026    
1027  TestSuite* DataTestCase::suite ()  TestSuite* DataTestCase::suite ()
# Line 411  TestSuite* DataTestCase::suite () Line 1038  TestSuite* DataTestCase::suite ()
1038    testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));    testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
1039    testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));    testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
1040    testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));    testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
1041      //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
1042      testSuite->addTest (new TestCaller< DataTestCase>("testMemAlloc",&DataTestCase::testMemAlloc));
1043    
1044    return testSuite;    return testSuite;
1045  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26