/[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

revision 100 by jgs, Wed Dec 15 03:48:48 2004 UTC revision 150 by jgs, Thu Sep 15 03:44:45 2005 UTC
# Line 19  Line 19 
19  #include "DataTestCase.h"  #include "DataTestCase.h"
20    
21  #include <iostream>  #include <iostream>
22    #include <math.h>
23    
24  using namespace std;  using namespace std;
25  using namespace CppUnitTest;  using namespace CppUnitTest;
# Line 150  void DataTestCase::testSlicing() { Line 151  void DataTestCase::testSlicing() {
151      DataArrayView::ShapeType viewShape;      DataArrayView::ShapeType viewShape;
152      Data source(10.0,viewShape,FunctionSpace(),true);      Data source(10.0,viewShape,FunctionSpace(),true);
153      //      //
154      // weak tests for setting a slice of DataConstant      // weak tests for setting a slice of DataExpanded
155      viewShape.push_back(2);      viewShape.push_back(2);
156      viewShape.push_back(3);      viewShape.push_back(3);
157      Data target(1.3,viewShape,FunctionSpace(),true);      Data target(1.3,viewShape,FunctionSpace(),true);
# Line 189  void DataTestCase::testMore() { Line 190  void DataTestCase::testMore() {
190    
191    cout << "\tCreate a Data object from a DataArrayView" << endl;    cout << "\tCreate a Data object from a DataArrayView" << endl;
192    
   DataArrayView::ValueType viewData;  
193    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
194    viewShape.push_back(3);    viewShape.push_back(3);
195      DataArrayView::ValueType viewData(3);
196    for (int i=0;i<viewShape[0];++i) {    for (int i=0;i<viewShape[0];++i) {
197      viewData.push_back(i);      viewData[i]=i;
198    }    }
199    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
200    
# Line 220  void DataTestCase::testMore() { Line 221  void DataTestCase::testMore() {
221    
222    cout << "\tExercise wherePositive method" << endl;    cout << "\tExercise wherePositive method" << endl;
223    assert(!exData.wherePositive().isEmpty());    assert(!exData.wherePositive().isEmpty());
   assert(exData.wherePositive()==exData.wherePositive());  
224    
225    cout << "\tExercise copyWithMask method" << endl;    cout << "\tExercise copyWithMask method" << endl;
226    exData.copyWithMask(result, exData.wherePositive());    exData.copyWithMask(result, exData.wherePositive());
# Line 234  void DataTestCase::testAll() { Line 234  void DataTestCase::testAll() {
234    
235    cout << "\tCreate a Data object from a DataArrayView" << endl;    cout << "\tCreate a Data object from a DataArrayView" << endl;
236    
   DataArrayView::ValueType viewData;  
237    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
238    viewShape.push_back(3);    viewShape.push_back(3);
239      DataArrayView::ValueType viewData(3);
240    for (int i=0;i<viewShape[0];++i) {    for (int i=0;i<viewShape[0];++i) {
241      viewData.push_back(i);      viewData[i]=i;
242    }    }
243    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
244    
# Line 264  void DataTestCase::testDataConstant() { Line 264  void DataTestCase::testDataConstant() {
264    
265    cout << "\tCreate a DataConstant object from a DataArrayView" << endl;    cout << "\tCreate a DataConstant object from a DataArrayView" << endl;
266    
   DataArrayView::ValueType viewData;  
267    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
268    viewShape.push_back(2);    viewShape.push_back(2);
269    viewShape.push_back(3);    viewShape.push_back(3);
270    viewShape.push_back(4);    viewShape.push_back(4);
271      DataArrayView::ValueType viewData(2*3*4);
272    for (int i=0;i<DataArrayView::noValues(viewShape);++i) {    for (int i=0;i<DataArrayView::noValues(viewShape);++i) {
273      viewData.push_back(i);      viewData[i]=i;
274    }    }
275    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
276    
# Line 302  void DataTestCase::testDataTaggedExcepti Line 302  void DataTestCase::testDataTaggedExcepti
302    cout << endl;    cout << endl;
303    
304    cout << "\tTest DataTagged operations exceptions." << endl;    cout << "\tTest DataTagged operations exceptions." << endl;
305    
306    Data myData;    Data myData;
307    DataArrayView myView;    DataArrayView myView;
308    
309    try {    try {
310        myData.getSampleDataByTag(0);;        myData.getSampleDataByTag(0);;
311        assert(false);        assert(false);
# Line 312  void DataTestCase::testDataTaggedExcepti Line 314  void DataTestCase::testDataTaggedExcepti
314        //cout << e.what() << endl;        //cout << e.what() << endl;
315        assert(true);        assert(true);
316    }    }
317    
318    /*    /*
319    try {    try {
320        myData.setTaggedValue(0,myView);;        myData.setTaggedValue(0,myView);;
# Line 333  void DataTestCase::testDataTagged() { Line 336  void DataTestCase::testDataTagged() {
336    
337    DataTagged::TagListType keys;    DataTagged::TagListType keys;
338    DataTagged::ValueListType values;    DataTagged::ValueListType values;
   DataArrayView::ValueType viewData;  
339    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType viewShape;
340    viewShape.push_back(3);    viewShape.push_back(3);
341      DataArrayView::ValueType viewData(3);
342    for (int i=0;i<viewShape[0];++i) {    for (int i=0;i<viewShape[0];++i) {
343      viewData.push_back(i);      viewData[i]=i;
344    }    }
345    DataArrayView myView(viewData,viewShape);    DataArrayView myView(viewData,viewShape);
346    
# Line 369  void DataTestCase::testConstructors() { Line 372  void DataTestCase::testConstructors() {
372      viewShape.push_back(2);      viewShape.push_back(2);
373      viewShape.push_back(3);      viewShape.push_back(3);
374      Data temp(1.3,viewShape,FunctionSpace(),false);      Data temp(1.3,viewShape,FunctionSpace(),false);
     cout << "\tDump it toString:" << endl;  
     cout << temp.toString() << endl;  
375    }    }
376  }  }
377    
378  void DataTestCase::testOperations() {  void DataTestCase::testOperations() {
379    
380    cout << endl;    cout << endl;
381    
382    cout << "\tCreate a rank 2 Data object" << endl;    // define the shape for the DataArrayView test data
383    DataArrayView::ShapeType viewShape;    DataArrayView::ShapeType shape;
384    viewShape.push_back(2);    shape.push_back(2);
385    viewShape.push_back(3);    shape.push_back(3);
386    
387      // allocate the data for the DataArrayView
388      DataArrayView::ValueType data(DataArrayView::noValues(shape),0);
389    
390      // construct DataArrayView
391      DataArrayView dataView(data,shape);
392    
393      // assign values to the data
394      for (int i=0;i<shape[0];i++) {
395        for (int j=0;j<shape[1];j++) {
396          dataView(i,j)=dataView.index(i,j);
397        }
398      }
399    
400    Data base(2.0,viewShape,FunctionSpace(),false);    Data base(dataView);
   Data power(3.0,viewShape,FunctionSpace(),false);  
401    
402    cout << "\tTest powD,sin,cos,tan,log,ln operations" << endl;    // test unary operations
403    
404      cout << "\tTest Data::pow." << endl;
405      Data power(3.0,shape,FunctionSpace(),true);
406    Data result(base.powD(power));    Data result(base.powD(power));
407    assert(result.getDataPoint(0,0)(0,0) == 8);    for (int i=0;i<shape[0];i++) {
408        for (int j=0;j<shape[1];j++) {
409          assert(result.getPointDataView()(i,j) == pow(dataView.index(i,j),3.0));
410        }
411      }
412    
413      cout << "\tTest Data::sin." << endl;
414    result.copy(base.sin());    result.copy(base.sin());
415      assert(true);
416    
417      cout << "\tTest Data::cos." << endl;
418    result.copy(base.cos());    result.copy(base.cos());
419      assert(true);
420    
421      cout << "\tTest Data::tan." << endl;
422    result.copy(base.tan());    result.copy(base.tan());
423      assert(true);
424    
425      cout << "\tTest Data::asin." << endl;
426      result.copy(base.asin());
427      assert(true);
428    
429      cout << "\tTest Data::acos." << endl;
430      result.copy(base.acos());
431      assert(true);
432    
433      cout << "\tTest Data::atan." << endl;
434      result.copy(base.atan());
435      assert(true);
436    
437      cout << "\tTest Data::sinh." << endl;
438      result.copy(base.sinh());
439      assert(true);
440    
441      cout << "\tTest Data::cosh." << endl;
442      result.copy(base.cosh());
443      assert(true);
444    
445      cout << "\tTest Data::tanh." << endl;
446      result.copy(base.tanh());
447      assert(true);
448    
449      cout << "\tTest Data::asinh." << endl;
450      result.copy(base.asinh());
451      assert(true);
452    
453      cout << "\tTest Data::acosh." << endl;
454      result.copy(base.acosh());
455      assert(true);
456    
457      cout << "\tTest Data::atanh." << endl;
458      result.copy(base.atanh());
459      assert(true);
460    
461      cout << "\tTest Data::log." << endl;
462    result.copy(base.log());    result.copy(base.log());
463      assert(true);
464    
465      cout << "\tTest Data::ln." << endl;
466    result.copy(base.ln());    result.copy(base.ln());
467      assert(true);
468    
469      cout << "\tTest Data::abs." << endl;
470      result.copy(base.abs());
471      assert(true);
472    
473      cout << "\tTest Data::sign." << endl;
474      result.copy(base.sign());
475      assert(true);
476    
477      cout << "\tTest Data::exp." << endl;
478      result.copy(base.exp());
479      assert(true);
480    
481      cout << "\tTest Data::sqrt." << endl;
482      result.copy(base.sqrt());
483      assert(true);
484    
485      cout << "\tTest Data::neg." << endl;
486      result.copy(base.neg());
487      assert(true);
488    
489      cout << "\tTest Data::pos." << endl;
490      result.copy(base.pos());
491      for (int i=0;i<shape[0];i++) {
492        for (int j=0;j<shape[1];j++) {
493          assert(result.getPointDataView()(i,j) == dataView.index(i,j));
494        }
495      }
496    
497      // test reduction operations
498    
499      cout << "\tTest Data::Lsup." << endl;
500      assert(base.Lsup() == 5);
501    
502      cout << "\tTest Data::sup." << endl;
503      assert(base.sup() == 5);
504    
505      cout << "\tTest Data::inf." << endl;
506      assert(base.inf() == 0);
507    
508      // test data-point reduction operations
509    
510      cout << "\tTest Data::minval." << endl;
511      result.copy(base.minval());
512      assert(result.getPointDataView()() == 0);
513    
514      cout << "\tTest Data::maxval." << endl;
515      result.copy(base.maxval());
516      assert(result.getPointDataView()() == 5);
517    
518      cout << "\tTest Data::length." << endl;
519      result.copy(base.length());
520      assert(pow(result.getPointDataView()(),2.0) == 55);
521    
522      cout << "\tTest Data::trace." << endl;
523      result.copy(base.trace());
524      assert(result.getPointDataView()() == 15);
525    
526      //result.copy(base.transpose(0));
527      //assert(true);
528    
529    }
530    
531    void DataTestCase::testRefValue() {
532    
533      //
534      // Note - this test can't be run as boost::python::numeric::array
535      // objects can only be created and used from within a python thread!
536      //
537    
538      cout << endl;
539    
540      cout << "\tTest Data object RefValue methods." << endl;
541    
542      // Create three Data object - DataExpanded, DataConstant and DataEmpty
543      DataArrayView::ShapeType viewShape;
544      viewShape.push_back(3);
545      DataArrayView::ValueType viewData(3);
546      for (int i=0;i<viewShape[0];++i) {
547        viewData[i]=i;
548      }
549      DataArrayView myView(viewData,viewShape);
550    
551      bool expanded=true;
552    
553      Data expandedData(myView,FunctionSpace(),expanded);
554      Data constantData(myView);
555      Data emptyData;
556    
557      assert(expandedData.isExpanded());
558      assert(constantData.isConstant());
559      assert(emptyData.isEmpty());
560    
561      // Check assertions are thrown for RefValue methods on DataEmpty
562    
563      int ref = 0;
564      boost::python::numeric::array num_array(1.0);
565    
566      try {
567          emptyData.getRefValue(ref,num_array);
568          assert(false);
569      }
570      catch (EsysException& e) {
571          assert(true);
572      }
573      try {
574          emptyData.setRefValue(ref,num_array);
575          assert(false);
576      }
577      catch (EsysException& e) {
578          assert(true);
579      }
580    
581      // Check assertions are thrown for RefValue methods on DataConstant
582      try {
583          constantData.getRefValue(ref,num_array);
584          assert(false);
585      }
586      catch (EsysException& e) {
587          assert(true);
588      }
589      try {
590          constantData.setRefValue(ref,num_array);
591          assert(false);
592      }
593      catch (EsysException& e) {
594          assert(true);
595      }
596    
597      // Check calls to RefValue methods on DataExpanded
598      expandedData.getRefValue(ref,num_array);
599      expandedData.setRefValue(ref,num_array);
600    
601  }  }
602    
603  TestSuite* DataTestCase::suite ()  TestSuite* DataTestCase::suite ()
# Line 411  TestSuite* DataTestCase::suite () Line 614  TestSuite* DataTestCase::suite ()
614    testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));    testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
615    testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));    testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
616    testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));    testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
617      //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
618    
619    return testSuite;    return testSuite;
620  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26