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

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

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

trunk/esys2/escript/test/Data/DataTestCase.cpp revision 100 by jgs, Wed Dec 15 03:48:48 2004 UTC trunk/escript/test/Data/DataTestCase.cpp revision 511 by jgs, Fri Feb 10 05:39: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 "Data.h"
19    #include "FunctionSpace.h"
20    #include "EsysException.h"
21    
22  #include <iostream>  #include <iostream>
23    #include <math.h>
24    
25  using namespace std;  using namespace std;
26  using namespace CppUnitTest;  using namespace CppUnitTest;
# Line 150  void DataTestCase::testSlicing() { Line 152  void DataTestCase::testSlicing() {
152      DataArrayView::ShapeType viewShape;      DataArrayView::ShapeType viewShape;
153      Data source(10.0,viewShape,FunctionSpace(),true);      Data source(10.0,viewShape,FunctionSpace(),true);
154      //      //
155      // weak tests for setting a slice of DataConstant      // weak tests for setting a slice of DataExpanded
156      viewShape.push_back(2);      viewShape.push_back(2);
157      viewShape.push_back(3);      viewShape.push_back(3);
158      Data target(1.3,viewShape,FunctionSpace(),true);      Data target(1.3,viewShape,FunctionSpace(),true);
# Line 189  void DataTestCase::testMore() { Line 191  void DataTestCase::testMore() {
191    
192    cout << "\tCreate a Data object from a DataArrayView" << endl;    cout << "\tCreate a Data object from a DataArrayView" << endl;
193    
   DataArrayView::ValueType viewData;  
194    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
195    viewShape.push_back(3);    viewShape.push_back(3);
196      DataArrayView::ValueType viewData(3);
197    for (int i=0;i<viewShape[0];++i) {    for (int i=0;i<viewShape[0];++i) {
198      viewData.push_back(i);      viewData[i]=i;
199    }    }
200    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
201    
# Line 220  void DataTestCase::testMore() { Line 222  void DataTestCase::testMore() {
222    
223    cout << "\tExercise wherePositive method" << endl;    cout << "\tExercise wherePositive method" << endl;
224    assert(!exData.wherePositive().isEmpty());    assert(!exData.wherePositive().isEmpty());
   assert(exData.wherePositive()==exData.wherePositive());  
225    
226    cout << "\tExercise copyWithMask method" << endl;    cout << "\tExercise copyWithMask method" << endl;
227    exData.copyWithMask(result, exData.wherePositive());    exData.copyWithMask(result, exData.wherePositive());
# Line 234  void DataTestCase::testAll() { Line 235  void DataTestCase::testAll() {
235    
236    cout << "\tCreate a Data object from a DataArrayView" << endl;    cout << "\tCreate a Data object from a DataArrayView" << endl;
237    
   DataArrayView::ValueType viewData;  
238    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
239    viewShape.push_back(3);    viewShape.push_back(3);
240      DataArrayView::ValueType viewData(3);
241    for (int i=0;i<viewShape[0];++i) {    for (int i=0;i<viewShape[0];++i) {
242      viewData.push_back(i);      viewData[i]=i;
243    }    }
244    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
245    
# Line 264  void DataTestCase::testDataConstant() { Line 265  void DataTestCase::testDataConstant() {
265    
266    cout << "\tCreate a DataConstant object from a DataArrayView" << endl;    cout << "\tCreate a DataConstant object from a DataArrayView" << endl;
267    
   DataArrayView::ValueType viewData;  
268    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
269    viewShape.push_back(2);    viewShape.push_back(2);
270    viewShape.push_back(3);    viewShape.push_back(3);
271    viewShape.push_back(4);    viewShape.push_back(4);
272      DataArrayView::ValueType viewData(2*3*4);
273    for (int i=0;i<DataArrayView::noValues(viewShape);++i) {    for (int i=0;i<DataArrayView::noValues(viewShape);++i) {
274      viewData.push_back(i);      viewData[i]=i;
275    }    }
276    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
277    
# Line 301  void DataTestCase::testDataTaggedExcepti Line 302  void DataTestCase::testDataTaggedExcepti
302    
303    cout << endl;    cout << endl;
304    
305    cout << "\tTest DataTagged operations exceptions." << endl;    cout << "\tTest DataTagged exceptions." << endl;
306    
307    Data myData;    Data myData;
308    DataArrayView myView;    DataArrayView myView;
309    
310    try {    try {
311        myData.getSampleDataByTag(0);;        myData.getSampleDataByTag(0);;
312        assert(false);        assert(false);
# Line 312  void DataTestCase::testDataTaggedExcepti Line 315  void DataTestCase::testDataTaggedExcepti
315        //cout << e.what() << endl;        //cout << e.what() << endl;
316        assert(true);        assert(true);
317    }    }
318    /*  
319    try {    try {
320        myData.setTaggedValue(0,myView);;        myData.setTaggedValueFromCPP(0,myView);;
321        assert(false);        assert(false);
322    }    }
323    catch (EsysException& e) {    catch (EsysException& e) {
324        //cout << e.what() << endl;        //cout << e.what() << endl;
325        assert(true);        assert(true);
326    }    }
   */  
327    
328  }  }
329    
# Line 329  void DataTestCase::testDataTagged() { Line 331  void DataTestCase::testDataTagged() {
331    
332    cout << endl;    cout << endl;
333    
334    cout << "\tCreate a DataTagged object from a DataArrayView" << endl;    cout << "\tCreate a DataTagged object with a default value only." << endl;
335    
336      // create tagged data with no tag values just a default
337    
338    DataTagged::TagListType keys;    DataTagged::TagListType keys;
339    
340    DataTagged::ValueListType values;    DataTagged::ValueListType values;
341    DataArrayView::ValueType viewData;  
342    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
343    viewShape.push_back(3);    viewShape.push_back(3);
344    
345      DataArrayView::ValueType viewData(3);
346    for (int i=0;i<viewShape[0];++i) {    for (int i=0;i<viewShape[0];++i) {
347      viewData.push_back(i);      viewData[i]=i;
348    }    }
349    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
350    
   // create tagged data with no tag values just a default  
351    bool expanded=false;    bool expanded=false;
352    
353    Data myData(keys,values,myView,FunctionSpace(),expanded);    Data myData(keys,values,myView,FunctionSpace(),expanded);
   assert(myData.isTagged());  
354    
355    cout << "\tTest some basic operations" << endl;    // cout << myData.toString() << endl;
356    
357    Data myDataCopy(myData);    assert(!myData.isEmpty());
358    myDataCopy.expand();    assert(myData.isTagged());
359    assert(myDataCopy.isExpanded());    assert(myData.getTagNumber(0)==1);
360      assert(myData.getDataPointRank()==1);
361      assert(myData.getLength()==3);
362    
363      DataArrayView myDataView = myData.getPointDataView();
364      assert(!myDataView.isEmpty());
365      assert(myDataView.getOffset()==0);
366      assert(myDataView.getRank()==1);
367      assert(myDataView.noValues()==3);
368      assert(myDataView.getShape().size()==1);
369      assert(myDataView(0)==0.0);
370      assert(myDataView(1)==1.0);
371      assert(myDataView(2)==2.0);
372    
373      myDataView = myData.getDataPoint(0,0);
374      assert(!myDataView.isEmpty());
375      assert(myDataView.getOffset()==0);
376      assert(myDataView.getRank()==1);
377      assert(myDataView.noValues()==3);
378      assert(myDataView.getShape().size()==1);
379      assert(myDataView(0)==0.0);
380      assert(myDataView(1)==1.0);
381      assert(myDataView(2)==2.0);
382    
383      double* sampleData=myData.getSampleData(0);
384      for (int i=0; i<myDataView.noValues(); i++) {
385        assert(sampleData[i]==i);
386      }
387      // use a non-existent tag so we get a pointer to
388      // the first element of the data array
389      sampleData=myData.getSampleDataByTag(9);
390      for (int i=0; i<myData.getLength(); i++) {
391        assert(sampleData[i]==i);
392      }
393    
394      cout << "\tTest setting of a tag and associated value." << endl;
395    
396      // value for tag "1"
397      DataArray eTwo(myView);
398      for (int i=0;i<eTwo.getView().getShape()[0];i++) {
399        eTwo.getView()(i)=i+2.0;
400      }
401    
402      myData.setTaggedValueFromCPP(1,eTwo.getView());
403    
404      assert(myData.getLength()==6);
405    
406      myDataView = myData.getDataPoint(0,0);
407      assert(myDataView==eTwo.getView());
408      assert(!myDataView.isEmpty());
409      assert(myDataView.getOffset()==3);
410      assert(myDataView.getRank()==1);
411      assert(myDataView.noValues()==3);
412      assert(myDataView.getShape().size()==1);
413      assert(myDataView(0)==2);
414      assert(myDataView(1)==3);
415      assert(myDataView(2)==4);
416    
417      sampleData=myData.getSampleDataByTag(1);
418      for (int i=0; i<myDataView.noValues(); i++) {
419        assert(sampleData[i]==i+2);
420      }
421    
422  }  }
423    
# Line 369  void DataTestCase::testConstructors() { Line 435  void DataTestCase::testConstructors() {
435      viewShape.push_back(2);      viewShape.push_back(2);
436      viewShape.push_back(3);      viewShape.push_back(3);
437      Data temp(1.3,viewShape,FunctionSpace(),false);      Data temp(1.3,viewShape,FunctionSpace(),false);
     cout << "\tDump it toString:" << endl;  
     cout << temp.toString() << endl;  
438    }    }
439  }  }
440    
441  void DataTestCase::testOperations() {  void DataTestCase::testOperations() {
442    
443    cout << endl;    cout << endl;
444    
445    cout << "\tCreate a rank 2 Data object" << endl;    // define the shape for the DataArrayView test data
446    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType shape;
447    viewShape.push_back(2);    shape.push_back(2);
448    viewShape.push_back(3);    shape.push_back(3);
449    
450      // allocate the data for the DataArrayView
451      DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
452    
453      // construct DataArrayView
454      DataArrayView dataView(data,shape);
455    
456      // assign values to the data
457      for (int i=0;i<shape[0];i++) {
458        for (int j=0;j<shape[1];j++) {
459          dataView(i,j)=dataView.index(i,j);
460        }
461      }
462    
463    Data base(2.0,viewShape,FunctionSpace(),false);    Data base(dataView);
   Data power(3.0,viewShape,FunctionSpace(),false);  
464    
465    cout << "\tTest powD,sin,cos,tan,log,ln operations" << endl;    // test unary operations
466    
467      cout << "\tTest Data::pow." << endl;
468      Data power(3.0,shape,FunctionSpace(),true);
469    Data result(base.powD(power));    Data result(base.powD(power));
470    assert(result.getDataPoint(0,0)(0,0) == 8);    for (int i=0;i<shape[0];i++) {
471        for (int j=0;j<shape[1];j++) {
472          assert(result.getPointDataView()(i,j) == pow(dataView.index(i,j),3.0));
473        }
474      }
475    
476      cout << "\tTest Data::sin." << endl;
477    result.copy(base.sin());    result.copy(base.sin());
478      assert(true);
479    
480      cout << "\tTest Data::cos." << endl;
481    result.copy(base.cos());    result.copy(base.cos());
482      assert(true);
483    
484      cout << "\tTest Data::tan." << endl;
485    result.copy(base.tan());    result.copy(base.tan());
486      assert(true);
487    
488      cout << "\tTest Data::asin." << endl;
489      result.copy(base.asin());
490      assert(true);
491    
492      cout << "\tTest Data::acos." << endl;
493      result.copy(base.acos());
494      assert(true);
495    
496      cout << "\tTest Data::atan." << endl;
497      result.copy(base.atan());
498      assert(true);
499    
500      cout << "\tTest Data::sinh." << endl;
501      result.copy(base.sinh());
502      assert(true);
503    
504      cout << "\tTest Data::cosh." << endl;
505      result.copy(base.cosh());
506      assert(true);
507    
508      cout << "\tTest Data::tanh." << endl;
509      result.copy(base.tanh());
510      assert(true);
511    
512      cout << "\tTest Data::asinh." << endl;
513      result.copy(base.asinh());
514      assert(true);
515    
516      cout << "\tTest Data::acosh." << endl;
517      result.copy(base.acosh());
518      assert(true);
519    
520      cout << "\tTest Data::atanh." << endl;
521      result.copy(base.atanh());
522      assert(true);
523    
524      cout << "\tTest Data::log." << endl;
525    result.copy(base.log());    result.copy(base.log());
526    result.copy(base.ln());    assert(true);
527    
528      //cout << "\tTest Data::ln." << endl;
529      //result.copy(base.ln());
530      //assert(true);
531    
532      cout << "\tTest Data::abs." << endl;
533      result.copy(base.abs());
534      assert(true);
535    
536      cout << "\tTest Data::sign." << endl;
537      result.copy(base.sign());
538      assert(true);
539    
540      cout << "\tTest Data::exp." << endl;
541      result.copy(base.exp());
542      assert(true);
543    
544      cout << "\tTest Data::sqrt." << endl;
545      result.copy(base.sqrt());
546      assert(true);
547    
548      cout << "\tTest Data::neg." << endl;
549      result.copy(base.neg());
550      assert(true);
551    
552      cout << "\tTest Data::pos." << endl;
553      result.copy(base.pos());
554      for (int i=0;i<shape[0];i++) {
555        for (int j=0;j<shape[1];j++) {
556          assert(result.getPointDataView()(i,j) == dataView.index(i,j));
557        }
558      }
559    
560      // test reduction operations
561    
562      cout << "\tTest Data::Lsup." << endl;
563      assert(base.Lsup() == 5);
564    
565      cout << "\tTest Data::sup." << endl;
566      assert(base.sup() == 5);
567    
568      cout << "\tTest Data::inf." << endl;
569      assert(base.inf() == 0);
570    
571      // test data-point reduction operations
572    
573      cout << "\tTest Data::minval." << endl;
574      result.copy(base.minval());
575      assert(result.getPointDataView()() == 0);
576    
577      cout << "\tTest Data::maxval." << endl;
578      result.copy(base.maxval());
579      assert(result.getPointDataView()() == 5);
580    
581      //cout << "\tTest Data::length." << endl;
582      //result.copy(base.length());
583      //assert(pow(result.getPointDataView()(),2.0) == 55);
584    
585      cout << "\tTest Data::trace." << endl;
586      result.copy(base.trace());
587      assert(result.getPointDataView()() == 15);
588    
589      //result.copy(base.transpose(0));
590      //assert(true);
591    
592    }
593    
594    void DataTestCase::testRefValue() {
595    
596      //
597      // Note - this test can't be run as boost::python::numeric::array
598      // objects can only be created and used from within a python thread!
599      //
600    
601      cout << endl;
602    
603      cout << "\tTest Data object RefValue methods." << endl;
604    
605      // Create three Data object - DataExpanded, DataConstant and DataEmpty
606      DataArrayView::ShapeType viewShape;
607      viewShape.push_back(3);
608      DataArrayView::ValueType viewData(3);
609      for (int i=0;i<viewShape[0];++i) {
610        viewData[i]=i;
611      }
612      DataArrayView myView(viewData,viewShape);
613    
614      bool expanded=true;
615    
616      Data expandedData(myView,FunctionSpace(),expanded);
617      Data constantData(myView);
618      Data emptyData;
619    
620      assert(expandedData.isExpanded());
621      assert(constantData.isConstant());
622      assert(emptyData.isEmpty());
623    
624      // Check assertions are thrown for RefValue methods on DataEmpty
625    
626      int ref = 0;
627      boost::python::numeric::array num_array(1.0);
628    
629      try {
630          emptyData.getRefValue(ref,num_array);
631          assert(false);
632      }
633      catch (EsysException& e) {
634          assert(true);
635      }
636      try {
637          emptyData.setRefValue(ref,num_array);
638          assert(false);
639      }
640      catch (EsysException& e) {
641          assert(true);
642      }
643    
644      // Check assertions are thrown for RefValue methods on DataConstant
645      try {
646          constantData.getRefValue(ref,num_array);
647          assert(false);
648      }
649      catch (EsysException& e) {
650          assert(true);
651      }
652      try {
653          constantData.setRefValue(ref,num_array);
654          assert(false);
655      }
656      catch (EsysException& e) {
657          assert(true);
658      }
659    
660      // Check calls to RefValue methods on DataExpanded
661      expandedData.getRefValue(ref,num_array);
662      expandedData.setRefValue(ref,num_array);
663    
664    }
665    
666    void DataTestCase::testMemAlloc() {
667    
668      //
669      // Simple little sanity check for the memory allocator
670    
671      cout << endl;
672    
673      Data *testData;
674      for (int i=0; i<1000; i++) {
675        testData = new Data(0.0, DataArrayView::ShapeType(), FunctionSpace(), true);
676        delete testData;
677      }
678    
679      DataArrayView::ShapeType viewShape;
680      viewShape.push_back(10);
681      viewShape.push_back(10);
682      viewShape.push_back(10);
683    
684      Data *testData2;
685      Data *testData3 = new Data(0.0, viewShape, FunctionSpace(), true);
686      for (int i=0; i<1000; i++) {
687        testData2 = new Data(0.0, viewShape, FunctionSpace(), true);
688        delete testData2;
689      }
690      delete testData3;
691    
692  }  }
693    
694  TestSuite* DataTestCase::suite ()  TestSuite* DataTestCase::suite ()
# Line 411  TestSuite* DataTestCase::suite () Line 705  TestSuite* DataTestCase::suite ()
705    testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));    testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
706    testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));    testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
707    testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));    testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
708      //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
709      testSuite->addTest (new TestCaller< DataTestCase>("testMemAlloc",&DataTestCase::testMemAlloc));
710    
711    return testSuite;    return testSuite;
712  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26