/[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 123 by jgs, Fri Jul 8 04:08:13 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    Data base(2.0,viewShape,FunctionSpace(),false);    // construct DataArrayView
391    Data power(3.0,viewShape,FunctionSpace(),false);    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    cout << "\tTest powD,sin,cos,tan,log,ln operations" << endl;    Data base(dataView);
401    
402      // 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::log." << endl;
426    result.copy(base.log());    result.copy(base.log());
427      assert(true);
428    
429      cout << "\tTest Data::ln." << endl;
430    result.copy(base.ln());    result.copy(base.ln());
431      assert(true);
432    
433      cout << "\tTest Data::abs." << endl;
434      result.copy(base.abs());
435      assert(true);
436    
437      cout << "\tTest Data::sign." << endl;
438      result.copy(base.sign());
439      assert(true);
440    
441      cout << "\tTest Data::exp." << endl;
442      result.copy(base.exp());
443      assert(true);
444    
445      cout << "\tTest Data::sqrt." << endl;
446      result.copy(base.sqrt());
447      assert(true);
448    
449      cout << "\tTest Data::neg." << endl;
450      result.copy(base.neg());
451      assert(true);
452    
453      cout << "\tTest Data::pos." << endl;
454      result.copy(base.pos());
455      for (int i=0;i<shape[0];i++) {
456        for (int j=0;j<shape[1];j++) {
457          assert(result.getPointDataView()(i,j) == dataView.index(i,j));
458        }
459      }
460    
461      // test reduction operations
462    
463      cout << "\tTest Data::Lsup." << endl;
464      assert(base.Lsup() == 5);
465    
466      cout << "\tTest Data::sup." << endl;
467      assert(base.sup() == 5);
468    
469      cout << "\tTest Data::inf." << endl;
470      assert(base.inf() == 0);
471    
472      // test data-point reduction operations
473    
474      cout << "\tTest Data::minval." << endl;
475      result.copy(base.minval());
476      assert(result.getPointDataView()() == 0);
477    
478      cout << "\tTest Data::maxval." << endl;
479      result.copy(base.maxval());
480      assert(result.getPointDataView()() == 5);
481    
482      cout << "\tTest Data::length." << endl;
483      result.copy(base.length());
484      assert(pow(result.getPointDataView()(),2.0) == 55);
485    
486      cout << "\tTest Data::trace." << endl;
487      result.copy(base.trace());
488      assert(result.getPointDataView()() == 15);
489    
490      //result.copy(base.transpose(0));
491      //assert(true);
492    
493    }
494    
495    void DataTestCase::testRefValue() {
496    
497      //
498      // Note - this test can't be run as boost::python::numeric::array
499      // objects can only be created and used from within a python thread!
500      //
501    
502      cout << endl;
503    
504      cout << "\tTest Data object RefValue methods." << endl;
505    
506      // Create three Data object - DataExpanded, DataConstant and DataEmpty
507      DataArrayView::ShapeType viewShape;
508      viewShape.push_back(3);
509      DataArrayView::ValueType viewData(3);
510      for (int i=0;i<viewShape[0];++i) {
511        viewData[i]=i;
512      }
513      DataArrayView myView(viewData,viewShape);
514    
515      bool expanded=true;
516    
517      Data expandedData(myView,FunctionSpace(),expanded);
518      Data constantData(myView);
519      Data emptyData;
520    
521      assert(expandedData.isExpanded());
522      assert(constantData.isConstant());
523      assert(emptyData.isEmpty());
524    
525      // Check assertions are thrown for RefValue methods on DataEmpty
526    
527      int ref = 0;
528      boost::python::numeric::array num_array(1.0);
529    
530      try {
531          emptyData.getRefValue(ref,num_array);
532          assert(false);
533      }
534      catch (EsysException& e) {
535          assert(true);
536      }
537      try {
538          emptyData.setRefValue(ref,num_array);
539          assert(false);
540      }
541      catch (EsysException& e) {
542          assert(true);
543      }
544    
545      // Check assertions are thrown for RefValue methods on DataConstant
546      try {
547          constantData.getRefValue(ref,num_array);
548          assert(false);
549      }
550      catch (EsysException& e) {
551          assert(true);
552      }
553      try {
554          constantData.setRefValue(ref,num_array);
555          assert(false);
556      }
557      catch (EsysException& e) {
558          assert(true);
559      }
560    
561      // Check calls to RefValue methods on DataExpanded
562      expandedData.getRefValue(ref,num_array);
563      expandedData.setRefValue(ref,num_array);
564    
565  }  }
566    
567  TestSuite* DataTestCase::suite ()  TestSuite* DataTestCase::suite ()
# Line 411  TestSuite* DataTestCase::suite () Line 578  TestSuite* DataTestCase::suite ()
578    testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));    testSuite->addTest (new TestCaller< DataTestCase>("testConstructors",&DataTestCase::testConstructors));
579    testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));    testSuite->addTest (new TestCaller< DataTestCase>("testSlicing",&DataTestCase::testSlicing));
580    testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));    testSuite->addTest (new TestCaller< DataTestCase>("testOperations",&DataTestCase::testOperations));
581      //testSuite->addTest (new TestCaller< DataTestCase>("testRefValue",&DataTestCase::testRefValue));
582    
583    return testSuite;    return testSuite;
584  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26