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

Diff of /trunk/escript/test/DataMathsTestCase.cpp

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

revision 3596 by jfenwick, Thu Jan 28 02:03:15 2010 UTC revision 3597 by caltinay, Thu Sep 15 06:03:17 2011 UTC
# Line 12  Line 12 
12  *******************************************************/  *******************************************************/
13    
14    
15    #include "DataMathsTestCase.h"
16  #include "escript/DataAlgorithm.h"  #include "escript/DataAlgorithm.h"
17    #include "escript/DataTypes.h"
18  #include "escript/DataVector.h"  #include "escript/DataVector.h"
19  #include "esysUtils/EsysException.h"  #include "esysUtils/EsysException.h"
20    
21  #include "DataMathsTestCase.h"  #include <cppunit/TestCaller.h>
 #include "escript/DataTypes.h"  
   
22  #include <iostream>  #include <iostream>
23    
24  using namespace CppUnitTest;  using namespace CppUnit;
25  using namespace esysUtils;  using namespace esysUtils;
26  using namespace escript;  using namespace escript;
27  using namespace std;  using namespace std;
28  using namespace escript::DataTypes;  using namespace escript::DataTypes;
29  using namespace escript::DataMaths;  using namespace escript::DataMaths;
30    
 void DataMathsTestCase::setUp() {  
   //  
   // This is called before each test is run  
   
 }  
   
 void DataMathsTestCase::tearDown() {  
   //  
   // This is called after each test has been run  
   
 }  
   
31    
32  void DataMathsTestCase::testMatMult()  void DataMathsTestCase::testMatMult()
33  {  {
   
   {  
34      cout << endl;      cout << endl;
35      cout << "\tTest result shape." << endl;      cout << "\tTest result shape." << endl;
36    
# Line 62  void DataMathsTestCase::testMatMult() Line 48  void DataMathsTestCase::testMatMult()
48    
49      DataTypes::ShapeType resultShape=DataMaths::determineResultShape(leftShape,rightShape);      DataTypes::ShapeType resultShape=DataMaths::determineResultShape(leftShape,rightShape);
50    
51      assert(resultShape.size()==2);      CPPUNIT_ASSERT(resultShape.size()==2);
52      assert(resultShape[0]==1);      CPPUNIT_ASSERT(resultShape[0]==1);
53      assert(resultShape[1]==2);      CPPUNIT_ASSERT(resultShape[1]==2);
54    
55      DataTypes::ValueType resultData(DataTypes::noValues(resultShape),0);      DataTypes::ValueType resultData(DataTypes::noValues(resultShape),0);
56    
# Line 83  void DataMathsTestCase::testMatMult() Line 69  void DataMathsTestCase::testMatMult()
69      }      }
70    
71      DataMaths::matMult(leftData,leftShape,0,rightData,rightShape,0,resultData, resultShape);      DataMaths::matMult(leftData,leftShape,0,rightData,rightShape,0,resultData, resultShape);
72      assert((resultData[0]==22) && (resultData[1]==28));      CPPUNIT_ASSERT((resultData[0]==22) && (resultData[1]==28));
   }  
   
   cout << endl;  
73    
74        cout << endl;
75  }  }
76    
77  void DataMathsTestCase::testUnaryOp()  void DataMathsTestCase::testUnaryOp()
# Line 121  void DataMathsTestCase::testUnaryOp() Line 105  void DataMathsTestCase::testUnaryOp()
105    
106        // check the results        // check the results
107        tmp = std::sin((double)p);        tmp = std::sin((double)p);
108        assert(std::abs(data[offset]-tmp)<=REL_TOL*std::abs(tmp));        CPPUNIT_ASSERT(std::abs(data[offset]-tmp)<=REL_TOL*std::abs(tmp));
109    
110        if (p<npoints-1) {        if (p<npoints-1) {
111          offset++;          offset++;
# Line 163  void DataMathsTestCase::testUnaryOp() Line 147  void DataMathsTestCase::testUnaryOp()
147        // check the results        // check the results
148        for (int i=0;i<shape[0];i++) {        for (int i=0;i<shape[0];i++) {
149          for (int j=0;j<shape[1];j++) {          for (int j=0;j<shape[1];j++) {
150  //           assert(std::abs(dataView(i,j)-std::sqrt((double)dataView.index(i,j)))<=REL_TOL*std::sqrt((double)dataView.index(i,j)));  //           CPPUNIT_ASSERT(std::abs(dataView(i,j)-std::sqrt((double)dataView.index(i,j)))<=REL_TOL*std::sqrt((double)dataView.index(i,j)));
151            assert(std::abs(data[offset+getRelIndex(shape,i,j)]-std::sqrt((double)offset+getRelIndex(shape,i,j)))<=REL_TOL*std::sqrt((double)offset+getRelIndex(shape,i,j)));            CPPUNIT_ASSERT(std::abs(data[offset+getRelIndex(shape,i,j)]-std::sqrt((double)offset+getRelIndex(shape,i,j)))<=REL_TOL*std::sqrt((double)offset+getRelIndex(shape,i,j)));
152          }          }
153        }        }
154    
# Line 215  void DataMathsTestCase::testUnaryOp() Line 199  void DataMathsTestCase::testUnaryOp()
199          for (int j=0;j<shape[1];j++) {          for (int j=0;j<shape[1];j++) {
200            for (int k=0;k<shape[2];k++) {            for (int k=0;k<shape[2];k++) {
201              for (int l=0;l<shape[3];l++) {              for (int l=0;l<shape[3];l++) {
202                assert(std::abs(data[offset+getRelIndex(shape,i,j,k,l)]-std::log(1+(double)data[offset+getRelIndex(shape,i,j,k,l)]))<=REL_TOL*std::abs(std::log(1+(double)data[offset+getRelIndex(shape,i,j,k,l)])));                CPPUNIT_ASSERT(std::abs(data[offset+getRelIndex(shape,i,j,k,l)]-std::log(1+(double)data[offset+getRelIndex(shape,i,j,k,l)]))<=REL_TOL*std::abs(std::log(1+(double)data[offset+getRelIndex(shape,i,j,k,l)])));
203              }              }
204            }            }
205          }          }
# Line 264  void DataMathsTestCase::testBinaryOp() Line 248  void DataMathsTestCase::testBinaryOp()
248        binaryOp(data1,scalarShape,offset,data2,scalarShape,offset, plus<double>());        binaryOp(data1,scalarShape,offset,data2,scalarShape,offset, plus<double>());
249    
250        // check the results        // check the results
251        assert(data1[offset]==p+p);        CPPUNIT_ASSERT(data1[offset]==p+p);
252    
253        if (p<npoints-1) {        if (p<npoints-1) {
254      ++offset;      ++offset;
# Line 307  void DataMathsTestCase::testBinaryOp() Line 291  void DataMathsTestCase::testBinaryOp()
291        // check the results        // check the results
292        for (int i=0;i<shape[0];i++) {        for (int i=0;i<shape[0];i++) {
293          for (int j=0;j<shape[1];j++) {          for (int j=0;j<shape[1];j++) {
294            assert(data1[offset+getRelIndex(shape,i,j)]==(offset+getRelIndex(shape,i,j))*(offset+getRelIndex(shape,i,j)));            CPPUNIT_ASSERT(data1[offset+getRelIndex(shape,i,j)]==(offset+getRelIndex(shape,i,j))*(offset+getRelIndex(shape,i,j)));
295          }          }
296        }        }
297    
# Line 360  void DataMathsTestCase::testBinaryOp() Line 344  void DataMathsTestCase::testBinaryOp()
344          for (int j=0;j<shape[1];j++) {          for (int j=0;j<shape[1];j++) {
345            for (int k=0;k<shape[2];k++) {            for (int k=0;k<shape[2];k++) {
346              for (int l=0;l<shape[3];l++) {              for (int l=0;l<shape[3];l++) {
347                assert(data1[offset+getRelIndex(shape,i,j,k,l)]==(offset+getRelIndex(shape,i,j,k,l))*(offset+getRelIndex(shape,i,j,k,l)));                CPPUNIT_ASSERT(data1[offset+getRelIndex(shape,i,j,k,l)]==(offset+getRelIndex(shape,i,j,k,l))*(offset+getRelIndex(shape,i,j,k,l)));
348              }              }
349            }            }
350          }          }
# Line 397  void DataMathsTestCase::testBinaryOp() Line 381  void DataMathsTestCase::testBinaryOp()
381        binaryOp(data,shape,offset,4.9,plus<double>());        binaryOp(data,shape,offset,4.9,plus<double>());
382    
383        // check the results        // check the results
384        assert(data[offset]==4.9+p);        CPPUNIT_ASSERT(data[offset]==4.9+p);
385    
386        if (p<npoints-1) {        if (p<npoints-1) {
387          ++offset;          ++offset;
# Line 440  void DataMathsTestCase::testBinaryOp() Line 424  void DataMathsTestCase::testBinaryOp()
424        for (int i=0;i<shape[0];i++) {        for (int i=0;i<shape[0];i++) {
425          for (int j=0;j<shape[1];j++) {          for (int j=0;j<shape[1];j++) {
426            tmp=5.8*(offset+getRelIndex(shape,i,j));            tmp=5.8*(offset+getRelIndex(shape,i,j));
427            assert(std::abs(data[offset+getRelIndex(shape,i,j)]-tmp)<=REL_TOL*std::abs(tmp));            CPPUNIT_ASSERT(std::abs(data[offset+getRelIndex(shape,i,j)]-tmp)<=REL_TOL*std::abs(tmp));
428          }          }
429        }        }
430    
# Line 493  void DataMathsTestCase::testBinaryOp() Line 477  void DataMathsTestCase::testBinaryOp()
477            for (int k=0;k<shape[2];k++) {            for (int k=0;k<shape[2];k++) {
478              for (int l=0;l<shape[3];l++) {              for (int l=0;l<shape[3];l++) {
479                tmp=5.4*(offset+getRelIndex(shape,i,j,k,l));                tmp=5.4*(offset+getRelIndex(shape,i,j,k,l));
480                assert(std::abs(data[offset+getRelIndex(shape,i,j,k,l)]-tmp)<=REL_TOL*std::abs(tmp));                CPPUNIT_ASSERT(std::abs(data[offset+getRelIndex(shape,i,j,k,l)]-tmp)<=REL_TOL*std::abs(tmp));
481              }              }
482            }            }
483          }          }
# Line 534  void DataMathsTestCase::testReductionOp( Line 518  void DataMathsTestCase::testReductionOp(
518    
519        // apply a reduction operation to this data point and check the results        // apply a reduction operation to this data point and check the results
520        FMax fmax_func;        FMax fmax_func;
521  //       assert(std::abs(dataView.reductionOp(fmax_func,numeric_limits<double>::max()*-1)-p)<=REL_TOL*p);  //       CPPUNIT_ASSERT(std::abs(dataView.reductionOp(fmax_func,numeric_limits<double>::max()*-1)-p)<=REL_TOL*p);
522        assert(std::abs(reductionOp(data,shape,offset,fmax_func,numeric_limits<double>::max()*-1)-p)<=REL_TOL*p);        CPPUNIT_ASSERT(std::abs(reductionOp(data,shape,offset,fmax_func,numeric_limits<double>::max()*-1)-p)<=REL_TOL*p);
523    
524    
525    
# Line 573  void DataMathsTestCase::testReductionOp( Line 557  void DataMathsTestCase::testReductionOp(
557    
558        // apply a reduction operation to this data point and check the results        // apply a reduction operation to this data point and check the results
559        FMin fmin_func;        FMin fmin_func;
560        assert(std::abs(reductionOp(data,shape,offset,fmin_func,numeric_limits<double>::max())-offset)<=REL_TOL*std::abs(offset));        CPPUNIT_ASSERT(std::abs(reductionOp(data,shape,offset,fmin_func,numeric_limits<double>::max())-offset)<=REL_TOL*std::abs(offset));
561    
562        if (p<npoints-1) {        if (p<npoints-1) {
563          offset+=noValues(shape);          offset+=noValues(shape);
# Line 615  void DataMathsTestCase::testReductionOp( Line 599  void DataMathsTestCase::testReductionOp(
599    
600        // apply a reduction operation to this data point and check the results        // apply a reduction operation to this data point and check the results
601        AbsMax absmax_func;        AbsMax absmax_func;
602        assert(reductionOp(data,shape,offset,absmax_func,0)==offset+getRelIndex(shape,8,7,4,10));        CPPUNIT_ASSERT(reductionOp(data,shape,offset,absmax_func,0)==offset+getRelIndex(shape,8,7,4,10));
603    
604        if (p<npoints-1) {        if (p<npoints-1) {
605          offset+=noValues(shape);          offset+=noValues(shape);
# Line 629  void DataMathsTestCase::testReductionOp( Line 613  void DataMathsTestCase::testReductionOp(
613    
614  }  }
615    
616  TestSuite* DataMathsTestCase::suite ()  TestSuite* DataMathsTestCase::suite()
617  {  {
   //  
618    // create the suite of tests to perform.    // create the suite of tests to perform.
619    TestSuite *testSuite = new TestSuite ("DataMathsTestCase");    TestSuite *testSuite = new TestSuite("DataMathsTestCase");
620    
621    testSuite->addTest (new TestCaller< DataMathsTestCase>("testUnaryOp",&DataMathsTestCase::testUnaryOp));    testSuite->addTest(new TestCaller<DataMathsTestCase>(
622    testSuite->addTest (new TestCaller< DataMathsTestCase>("testBinaryOp",&DataMathsTestCase::testBinaryOp));                "testUnaryOp",&DataMathsTestCase::testUnaryOp));
623    testSuite->addTest (new TestCaller< DataMathsTestCase>("testReductionOp",&DataMathsTestCase::testReductionOp));    testSuite->addTest(new TestCaller<DataMathsTestCase>(
624    testSuite->addTest (new TestCaller< DataMathsTestCase>("testMatMult",&DataMathsTestCase::testMatMult));                "testBinaryOp",&DataMathsTestCase::testBinaryOp));
625      testSuite->addTest(new TestCaller<DataMathsTestCase>(
626                  "testReductionOp",&DataMathsTestCase::testReductionOp));
627      testSuite->addTest(new TestCaller<DataMathsTestCase>(
628                  "testMatMult",&DataMathsTestCase::testMatMult));
629    return testSuite;    return testSuite;
630  }  }
631    

Legend:
Removed from v.3596  
changed lines
  Added in v.3597

  ViewVC Help
Powered by ViewVC 1.1.26