/[escript]/trunk/escript/test/DataTagged/DataTaggedTestCase.cpp
ViewVC logotype

Diff of /trunk/escript/test/DataTagged/DataTaggedTestCase.cpp

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

trunk/esys2/escript/test/DataTagged/DataTaggedTestCase.cpp revision 82 by jgs, Tue Oct 26 06:53:54 2004 UTC trunk/escript/test/DataTagged/DataTaggedTestCase.cpp revision 497 by jgs, Tue Feb 7 01:28:01 2006 UTC
# Line 12  Line 12 
12   *                                                                           *   *                                                                           *
13   *****************************************************************************   *****************************************************************************
14  */  */
 #include "escript/Data/DataTagged.h"  
 #include "escript/Data/BinaryOp.h"  
 #include "escript/Data/UnaryOp.h"  
 #include "esysUtils/EsysException.h"  
   
 #include "finley/CPPAdapter/MeshAdapter.h"  
 #include "finley/CPPAdapter/MeshAdapterFactory.h"  
 #include "escript/Data/AbstractContinuousDomain.h"  
15    
16  #include "escript/Data/FunctionSpaceFactory.h"  #include "EsysException.h"
17  #include "escript/Data/DataFactory.h"  
18    #include "DataTagged.h"
19    
20  #include "DataTaggedTestCase.h"  #include "DataTaggedTestCase.h"
21    
22    #include "BinaryOp.h"
23    #include "UnaryOp.h"
24    #include "FunctionSpaceFactory.h"
25    #include "DataFactory.h"
26    
27  #include <iostream>  #include <iostream>
28  #include <functional>  #include <functional>
29  #include <algorithm>  #include <algorithm>
# Line 35  using namespace escript; Line 33  using namespace escript;
33  using namespace esysUtils;  using namespace esysUtils;
34  using namespace std;  using namespace std;
35    
 using namespace finley;  
   
36  void DataTaggedTestCase::setUp() {  void DataTaggedTestCase::setUp() {
37    //    //
38    // This is called before each test is run    // This is called before each test is run
# Line 91  void DataTaggedTestCase::testOperations( Line 87  void DataTaggedTestCase::testOperations(
87    cout << endl;    cout << endl;
88    
89    {    {
     cout << "\tTest default DataTagged contains only a default value." << endl;  
90      DataTagged left;      DataTagged left;
91      DataTagged right;      DataTagged right;
92    
93        cout << "\tTest default DataTagged contains only a default value." << endl;
94      binaryOp(left,right,plus<double>());      binaryOp(left,right,plus<double>());
95      assert(left.getPointDataView()()==0);      assert(left.getPointDataView()()==0);
96      assert(right.getPointDataView()()==0);      assert(right.getPointDataView()()==0);
97      cout << "\tTest addTaggedValue and binaryOp(plus)." << endl;  
98        cout << "\tTest binaryOp(plus)." << endl;
99      DataArray vOne(1.0);      DataArray vOne(1.0);
100      DataArray vTwo(2.0);      DataArray vTwo(2.0);
101      right.addTaggedValue(1,vOne.getView());      right.addTaggedValue(1,vOne.getView());
# Line 106  void DataTaggedTestCase::testOperations( Line 104  void DataTaggedTestCase::testOperations(
104      assert(left.getPointDataView()()==0);      assert(left.getPointDataView()()==0);
105      assert(left.getDataPointByTag(1)==vOne.getView());      assert(left.getDataPointByTag(1)==vOne.getView());
106      assert(left.getDataPointByTag(2)==vTwo.getView());      assert(left.getDataPointByTag(2)==vTwo.getView());
107      cout << "\tTest setTaggedValue and binaryOp(multiplies)." << endl;  
108        cout << "\tTest binaryOp(multiplies)." << endl;
109      DataArray vZero(0.0);      DataArray vZero(0.0);
110      right.setTaggedValue(1,vZero.getView());      right.setTaggedValue(1,vZero.getView());
111      right.setTaggedValue(2,vZero.getView());      right.setTaggedValue(2,vZero.getView());
# Line 116  void DataTaggedTestCase::testOperations( Line 115  void DataTaggedTestCase::testOperations(
115      assert(left.getDataPointByTag(2)==vZero.getView());      assert(left.getDataPointByTag(2)==vZero.getView());
116    }    }
117    {    {
     DataArrayView::ValueType viewData;  
118      DataArrayView::ShapeType viewShape;      DataArrayView::ShapeType viewShape;
119      viewShape.push_back(3);      viewShape.push_back(3);
120        DataArrayView::ValueType viewData(3);
121      DataTagged::TagListType keys;      DataTagged::TagListType keys;
122      DataTagged::ValueListType values;      DataTagged::ValueListType values;
123      for (int i=0;i<viewShape[0];++i) {      for (int i=0;i<viewShape[0];++i) {
124        viewData.push_back(i);        viewData[i]=i;
125      }      }
126      DataArrayView myView(viewData,viewShape);      DataArrayView myView(viewData,viewShape);
127      cout << "\tCreate tagged data with no tag values just a default." << endl;      cout << "\tCreate tagged data with no tag values just a default." << endl;
# Line 182  void DataTaggedTestCase::testOperations( Line 181  void DataTaggedTestCase::testOperations(
181      assert(tData.getDataPointByTag(2)==tTwoView);      assert(tData.getDataPointByTag(2)==tTwoView);
182      assert(tData.getDataPointByTag(3)==tThreeView);      assert(tData.getDataPointByTag(3)==tThreeView);
183    }    }
   
184  }  }
185    
186  void DataTaggedTestCase::testAll() {  void DataTaggedTestCase::testAll() {
187    
188    cout << endl;    cout << endl;
189    
190    {    {
191      cout << "\tTest default construction." << endl;  
192        cout << "\tTest default DataTagged." << endl;
193      DataTagged myData;      DataTagged myData;
194      assert(myData.getPointDataView()()==0);  
     assert(myData.getNumDPPSample()==1);  
195      assert(myData.getNumSamples()==1);      assert(myData.getNumSamples()==1);
     cout << "\tTest adding two keys with empty value list." << endl;  
     DataTagged::TagListType keys;  
     DataTagged::ValueListType values;  
     keys.push_back(1);  
     keys.push_back(2);  
     myData.addTaggedValues(keys,values);  
     for (int i=0;i<keys.size();++i) {  
       assert(myData.getPointDataView()()==0);  
     }  
   }  
   {  
     DataArrayView::ValueType viewData;  
     DataArrayView::ShapeType viewShape;  
     viewShape.push_back(3);  
     DataTagged::TagListType keys;  
     DataTagged::ValueListType values;  
     for (int i=0;i<viewShape[0];++i) {  
       viewData.push_back(0.0);  
     }  
     DataArrayView myView(viewData,viewShape);  
     cout << "\tCreate tagged data with no tag values just a default." << endl;  
     DataTagged myData(keys,values,myView,FunctionSpace());  
196      assert(myData.getNumDPPSample()==1);      assert(myData.getNumDPPSample()==1);
197      assert(myData.getNumSamples()==1);  
198      cout << "\tTest non existent tag returns the default value." << endl;      assert(myData.validSamplePointNo(0));
199      assert(myData.getDataPointByTag(1)==myView);      assert(myData.validSampleNo(0));
200      cout << "\tTest adding a single tag value." << endl;      assert(!myData.validSamplePointNo(1));
201      for (int i=0;i<myView.getShape()[0];++i) {      assert(!myData.validSampleNo(1));
202        myView(i)=i;  
203      }      assert(myData.getTagNumber(0)==1);
204      values.push_back(myView);  
205      keys.push_back(1);      assert(!myData.isCurrentTag(1));
206      myData.addTaggedValues(keys,values);  
207      assert(myData.getDataPointByTag(1)==myView);      assert(myData.getTagLookup().size()==0);
208      cout << "\tTest addition of further tags." << endl;  
209      keys.clear();      assert(myData.getLength()==1);
210      keys.push_back(3);  
211      for (int i=0;i<myView.getShape()[0];++i) {      assert(myData.getPointOffset(0,0)==0);
212        myView(i)=i+1.5;  
213      }      // cout << myData.toString() << endl;
214      myData.addTaggedValues(keys,values);  
215      assert(myData.getDataPointByTag(3)==myView);      DataArrayView myDataView = myData.getDataPoint(0,0);
216      assert(myData.getDataPointByTag(1)!=myView);      assert(!myDataView.isEmpty());
217      cout << "\tTrigger the size mismatch exception." << endl;      assert(myDataView.getOffset()==0);
218      try {      assert(myDataView.getRank()==0);
219        values.push_back(myView);      assert(myDataView.noValues()==1);
220        myData.addTaggedValues(keys,values);      assert(myDataView.getShape().size()==0);
221        assert(false);      assert(myDataView()==0.0);
222      }  
223      catch (EsysException& e) {      myDataView = myData.getDataPointByTag(1);
224        //cout << e.what() << endl;      assert(!myDataView.isEmpty());
225        assert(true);      assert(myDataView.getOffset()==0);
226      }      assert(myDataView.getRank()==0);
227        assert(myDataView.noValues()==1);
228        assert(myDataView.getShape().size()==0);
229        assert(myDataView()==0.0);
230    
231        myDataView = myData.getDefaultValue();
232        assert(!myDataView.isEmpty());
233        assert(myDataView.getOffset()==0);
234        assert(myDataView.getRank()==0);
235        assert(myDataView.noValues()==1);
236        assert(myDataView.getShape().size()==0);
237        assert(myDataView()==0.0);
238    
239        //assert(myData.getSampleDataByTag(0)[0]==0.0);
240        //assert(myData.getSampleDataByTag(3)[0]==0.0);
241        //assert(myData.getSampleDataByTag(472)[0]==0.0);
242    
243        //cout << "\tTest adding two keys with empty value list." << endl;
244        //DataTagged::TagListType keys;
245        //DataTagged::ValueListType values;
246        //keys.push_back(1);
247        //keys.push_back(2);
248        //myData.addTaggedValues(keys,values);
249        //for (int i=0;i<keys.size();++i) {
250        //  assert(myData.getPointDataView()()==0);
251        //}
252    }    }
253    
254    {    {
255      cout << "\tTest creation of tagged data with multiple tags." << endl;  //    cout << "\tCreate tagged data with no tag values just a default." << endl;
256      DataArrayView::ValueType viewData;   //   DataArrayView::ShapeType viewShape;
257      DataArrayView::ShapeType viewShape;  //    viewShape.push_back(3);
258      viewShape.push_back(3);  //    DataArrayView::ValueType viewData(3);
259      DataTagged::TagListType keys;  //    DataTagged::TagListType keys;
260      DataTagged::ValueListType values;  //    DataTagged::ValueListType values;
261      for (int i=0;i<viewShape[0];++i) {  //    for (int i=0;i<viewShape[0];i++) {
262        viewData.push_back(0.0);  //      viewData[i]=0.0;
263      }  //    }
264      DataArrayView myView(viewData,viewShape);  //    DataArrayView myView(viewData,viewShape);
265      DataArray eOne(myView);  //    DataTagged myData(keys,values,myView,FunctionSpace());
266      DataArray eTwo(myView);  //    assert(myData.getNumDPPSample()==1);
267      DataArray eThree(myView);  //    assert(myData.getNumSamples()==1);
268      for (int i=0;i<eOne.getView().getShape()[0];++i) {  //    // Test non existent tag returns the default value.
269        eOne.getView()(i)=i+1.0;  //    assert(myData.getDataPointByTag(1)==myView);
270      }  
271      for (int i=0;i<eTwo.getView().getShape()[0];++i) {      //cout << "\tTest adding a single tag value." << endl;
272        eTwo.getView()(i)=i+2.0;      //for (int i=0;i<myView.getShape()[0];++i) {
273      }      //  myView(i)=i;
274      for (int i=0;i<eThree.getView().getShape()[0];++i) {      //}
275        eThree.getView()(i)=i+3.0;      //values.push_back(myView);
276      }      //keys.push_back(1);
277      values.push_back(eOne.getView());      //myData.addTaggedValues(keys,values);
278      values.push_back(eTwo.getView());      //assert(myData.getDataPointByTag(1)==myView);
279      values.push_back(eThree.getView());      //cout << "\tTest addition of further tags." << endl;
280      keys.push_back(1);      //keys.resize(0);
281      keys.push_back(2);      //keys.push_back(3);
282      keys.push_back(3);      //for (int i=0;i<myView.getShape()[0];++i) {
283      DataTagged myData(keys,values,myView,FunctionSpace());      //  myView(i)=i+1.5;
284      assert(myData.getDataPointByTag(1)==eOne.getView());      //}
285      assert(myData.getDataPointByTag(2)==eTwo.getView());      //myData.addTaggedValues(keys,values);
286      assert(myData.getDataPointByTag(3)==eThree.getView());      //assert(myData.getDataPointByTag(3)==myView);
287      cout << "\tTest isCurrentTag function." << endl;      //assert(myData.getDataPointByTag(1)!=myView);
288      for (int i=0;i<keys.size();++i) {      //cout << "\tTrigger the size mismatch exception." << endl;
289        assert(myData.isCurrentTag(keys[i]));      //try {
290      }      //  values.push_back(myView);
291      cout << "\tCheck correct operation for key that doesn't exist." << endl;      //  myData.addTaggedValues(keys,values);
292      assert(!myData.isCurrentTag(123));      //  assert(false);
293      cout << "\tTrigger bad shape in input values exception." << endl;      //}
294      viewShape.clear();      //catch (EsysException& e) {
295      viewShape.push_back(1);      // assert(true);
296      keys.clear();      //}
     values.clear();  
     viewData.clear();  
     for (int i=0;i<viewShape[0];++i) {  
       viewData.push_back(0.0);  
     }  
     DataArrayView myView2(viewData,viewShape);  
     try {  
       myData.addTaggedValue(5,myView2);  
       assert(false);  
     }  
     catch (EsysException& e) {  
       //cout << e.what() << endl;  
       assert(true);  
     }  
     cout << "\tTest setTaggedValues." << endl;  
     DataTagged myData2;  
     myData2.reshapeDataPoint(myView.getShape());  
     keys.clear();  
     values.clear();  
     keys.push_back(1);  
     keys.push_back(2);  
     keys.push_back(3);  
     values.push_back(eOne.getView());  
     values.push_back(eTwo.getView());  
     values.push_back(eThree.getView());  
     myData2.setTaggedValues(keys,values);  
     assert(myData2.getDataPointByTag(1)==eOne.getView());  
     assert(myData2.getDataPointByTag(2)==eTwo.getView());  
     assert(myData2.getDataPointByTag(3)==eThree.getView());  
     cout << "\tTest setTaggedValue." << endl;  
     DataTagged myData3;  
     myData3.reshapeDataPoint(myView.getShape());  
     myData3.setTaggedValue(1,eOne.getView());  
     myData3.setTaggedValue(2,eTwo.getView());  
     myData3.setTaggedValue(3,eThree.getView());  
     assert(myData3.getDataPointByTag(1)==eOne.getView());  
     assert(myData3.getDataPointByTag(2)==eTwo.getView());  
     assert(myData3.getDataPointByTag(3)==eThree.getView());  
297    }    }
298    
299  }    {
300    //    cout << "\tTest creation of tagged data with multiple tags." << endl;
301  void DataTaggedTestCase::testSubtraction() {  //    DataArrayView::ShapeType viewShape;
302    //    viewShape.push_back(3);
303    // An error with FinleyMesh::getTagList used to cause binary operations  //    DataArrayView::ValueType viewData(3);
304    // between DataExpanded and DataTagged objects to seg-fault. This test   //   DataTagged::TagListType keys;
305    // case will provoke this error if it arises again.   //   DataTagged::ValueListType values;
306    //    for (int i=0;i<viewShape[0];++i) {
307    cout << endl;  //      viewData[i]=0.0;
308    //    }
309    cout << "\tCreate domain and function-space." << endl;  //    DataArrayView myView(viewData,viewShape);
310    AbstractContinuousDomain* myDomain = rectangle(10,10);  //    DataArray eOne(myView);
311    FunctionSpace f = functionOnBoundary(*myDomain);  //    DataArray eTwo(myView);
312    //    DataArray eThree(myView);
313    cout << "\tCreate two vectors, one being DataExpanded." << endl;  //    for (int i=0;i<eOne.getView().getShape()[0];++i) {
314    Data A = Vector(0,f);  //      eOne.getView()(i)=i+1.0;
315    Data B = Vector(0,f,true);  //    }
316    //    for (int i=0;i<eTwo.getView().getShape()[0];++i) {
317    cout << "\tCreate some tags and values to add to the other." << endl;  //      eTwo.getView()(i)=i+2.0;
318    DataArrayView::ValueType viewData;  //    }
319    DataArrayView::ShapeType viewShape;  //    for (int i=0;i<eThree.getView().getShape()[0];++i) {
320    viewShape.push_back(2);  //      eThree.getView()(i)=i+3.0;
321    for (int i=0;i<viewShape[0];++i) {  //    }
322      viewData.push_back(0.0);  //    values.push_back(eOne.getView());
323    }  //    values.push_back(eTwo.getView());
324    DataArrayView myView(viewData,viewShape);  //    values.push_back(eThree.getView());
325    DataArray eOne(myView);  //    keys.push_back(1);
326    for (int i=0;i<eOne.getView().getShape()[0];++i) {  //    keys.push_back(2);
327      eOne.getView()(i)=i+1.0;  //    keys.push_back(3);
328    //    DataTagged myData(keys,values,myView,FunctionSpace());
329    //    assert(myData.getDataPointByTag(1)==eOne.getView());
330    //    assert(myData.getDataPointByTag(2)==eTwo.getView());
331    //    assert(myData.getDataPointByTag(3)==eThree.getView());
332    
333        //cout << "\tTest isCurrentTag function." << endl;
334        //for (int i=0;i<keys.size();++i) {
335        //  assert(myData.isCurrentTag(keys[i]));
336        //}
337        //cout << "\tCheck correct operation for key that doesn't exist." << endl;
338        //assert(!myData.isCurrentTag(123));
339        //cout << "\tTrigger bad shape in input values exception." << endl;
340        //viewShape.clear();
341        //viewShape.push_back(1);
342        //keys.clear();
343        //values.clear();
344        //viewData.resize(1,0.0);
345        //DataArrayView myView2(viewData,viewShape);
346        //try {
347        //  myData.addTaggedValue(5,myView2);
348        //  assert(false);
349        //}
350        //catch (EsysException& e) {
351        //  assert(true);
352        //}
353        //cout << "\tTest addTaggedValues." << endl;
354        //DataTagged myData2;
355        //myData2.reshapeDataPoint(myView.getShape());
356        //keys.clear();
357        //values.clear();
358        //keys.push_back(1);
359        //keys.push_back(2);
360        //keys.push_back(3);
361        //values.push_back(eOne.getView());
362        //values.push_back(eTwo.getView());
363        //values.push_back(eThree.getView());
364        //myData2.addTaggedValues(keys,values);
365        //assert(myData2.getDataPointByTag(1)==eOne.getView());
366        //assert(myData2.getDataPointByTag(2)==eTwo.getView());
367        //assert(myData2.getDataPointByTag(3)==eThree.getView());
368        //cout << "\tTest setTaggedValue." << endl;
369        //DataTagged myData3;
370        //myData3.reshapeDataPoint(myView.getShape());
371        //myData3.addTaggedValue(1,eThree.getView());
372        //myData3.addTaggedValue(2,eOne.getView());
373        //myData3.addTaggedValue(3,eTwo.getView());
374        //myData3.setTaggedValue(1,eOne.getView());
375        //myData3.setTaggedValue(2,eTwo.getView());
376        //myData3.setTaggedValue(3,eThree.getView());
377        //assert(myData3.getDataPointByTag(1)==eOne.getView());
378        //assert(myData3.getDataPointByTag(2)==eTwo.getView());
379        //assert(myData3.getDataPointByTag(3)==eThree.getView());
380    }    }
381    
   A.setTaggedValue(2,eOne.getView());  
   
   //cout << A.toString() << endl;  
   
   cout << "\tCalculate difference." << endl;  
   Data difference = B - A;  
   
   cout << "\tCalculate other binaryOps just to be sure." << endl;  
   Data sum = B + A;  
   Data product = B * A;  
   Data dividend = B / A;  
   
   // If we get here, subtraction operation did not seg-fault.  
   assert(true);  
   
382  }  }
383    
384  TestSuite* DataTaggedTestCase::suite ()  TestSuite* DataTaggedTestCase::suite ()
# Line 384  TestSuite* DataTaggedTestCase::suite () Line 387  TestSuite* DataTaggedTestCase::suite ()
387    // create the suite of tests to perform.    // create the suite of tests to perform.
388    TestSuite *testSuite = new TestSuite ("DataTaggedTestCase");    TestSuite *testSuite = new TestSuite ("DataTaggedTestCase");
389    testSuite->addTest (new TestCaller< DataTaggedTestCase>("testAll",&DataTaggedTestCase::testAll));    testSuite->addTest (new TestCaller< DataTaggedTestCase>("testAll",&DataTaggedTestCase::testAll));
390    testSuite->addTest (new TestCaller< DataTaggedTestCase>("testOperations",&DataTaggedTestCase::testOperations));  //  testSuite->addTest (new TestCaller< DataTaggedTestCase>("testOperations",&DataTaggedTestCase::testOperations));
391    testSuite->addTest (new TestCaller< DataTaggedTestCase>("testReshape",&DataTaggedTestCase::testReshape));  //  testSuite->addTest (new TestCaller< DataTaggedTestCase>("testReshape",&DataTaggedTestCase::testReshape));
   testSuite->addTest (new TestCaller< DataTaggedTestCase>("testSubtraction",&DataTaggedTestCase::testSubtraction));  
392    return testSuite;    return testSuite;
393  }  }

Legend:
Removed from v.82  
changed lines
  Added in v.497

  ViewVC Help
Powered by ViewVC 1.1.26