/[escript]/branches/arrayview_from_1695_trunk/escript/test/DataArrayViewTestCase.cpp
ViewVC logotype

Diff of /branches/arrayview_from_1695_trunk/escript/test/DataArrayViewTestCase.cpp

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

trunk/esys2/escript/test/DataArrayView/DataArrayViewTestCase.cpp revision 108 by jgs, Thu Jan 27 06:21:59 2005 UTC trunk/escript/test/DataArrayView/DataArrayViewTestCase.cpp revision 468 by jgs, Wed Jan 25 06:50:39 2006 UTC
# Line 11  Line 11 
11   *                                                                           *   *                                                                           *
12   *****************************************************************************   *****************************************************************************
13  */  */
 #include "escript/Data/DataArrayView.h"  
14  #include "escript/Data/DataArray.h"  #include "escript/Data/DataArray.h"
15    #include "escript/Data/DataArrayView.h"
16    #include "escript/Data/DataAlgorithm.h"
17    #include "EsysException.h"
18    
19  #include "DataArrayViewTestCase.h"  #include "DataArrayViewTestCase.h"
 #include "esysUtils/EsysException.h"  
20    
21  #include <iostream>  #include <iostream>
22    
# Line 1099  void DataArrayViewTestCase::testScalarVi Line 1101  void DataArrayViewTestCase::testScalarVi
1101    // Create a vector containing enough data for three scalars    // Create a vector containing enough data for three scalars
1102    // and check three scalar views return the appropriate data    // and check three scalar views return the appropriate data
1103    DataArrayView::ShapeType vShape;    DataArrayView::ShapeType vShape;
1104    DataArrayView::ValueType vData;    DataArrayView::ValueType vData(3);
1105    vData.push_back(0);    vData[0]=0;
1106    vData.push_back(1);    vData[1]=1;
1107    vData.push_back(2);    vData[2]=2;
1108    
1109    // create the three scalar views    // create the three scalar views
1110    DataArrayView zView(vData,vShape,0);    DataArrayView zView(vData,vShape,0);
# Line 1666  void DataArrayViewTestCase::testAll() Line 1668  void DataArrayViewTestCase::testAll()
1668  void DataArrayViewTestCase::testMatMult()  void DataArrayViewTestCase::testMatMult()
1669  {  {
1670    
 /*  
1671    {    {
1672      cout << endl;      cout << endl;
1673      cout << "\tTest matrix multiplication." << endl;      cout << "\tTest result shape." << endl;
1674    
1675      DataArrayView::ShapeType leftShape;      DataArrayView::ShapeType leftShape;
1676      leftShape.push_back(1);      leftShape.push_back(1);
# Line 1685  void DataArrayViewTestCase::testMatMult( Line 1686  void DataArrayViewTestCase::testMatMult(
1686    
1687      DataArrayView::ShapeType resultShape=DataArrayView::determineResultShape(leftDataView,rightDataView);      DataArrayView::ShapeType resultShape=DataArrayView::determineResultShape(leftDataView,rightDataView);
1688    
     for (int i=0;i<resultShape.size();++i) {  
       cout << "\tDimension: " << i << " size: " << resultShape[i] << endl;  
     }  
   
     DataArrayView::ValueType resultData(DataArrayView::noValues(resultShape),0);  
     DataArrayView resultDataView(resultData,resultShape);  
     
     cout << "\tTest result shape." << endl;  
   
1689      assert(resultShape.size()==2);      assert(resultShape.size()==2);
1690      assert(resultShape[0]==1);      assert(resultShape[0]==1);
1691      assert(resultShape[1]==2);      assert(resultShape[1]==2);
1692    
1693      // Assign some values      DataArrayView::ValueType resultData(DataArrayView::noValues(resultShape),0);
1694      cout << "\tAssign values." << endl;      DataArrayView resultDataView(resultData,resultShape);
1695      
1696        cout << "\tTest matrix multiplication.";
1697      double aValue=0.0;      double aValue=0.0;
1698      for (int i=0;i<leftShape[0];++i) {      for (int i=0;i<leftShape[0];i++) {
1699        for (int j=0;j<leftShape[1];++j) {        for (int j=0;j<leftShape[1];j++) {
1700      leftDataView(i,j)=++aValue;      leftDataView(i,j)=++aValue;
1701        }        }
1702      }      }
1703      aValue=0.0;      aValue=0.0;
1704      for (int i=0;i<rightShape[0];++i) {      for (int i=0;i<rightShape[0];i++) {
1705        for (int j=0;j<rightShape[1];++j) {        for (int j=0;j<rightShape[1];j++) {
1706      rightDataView(i,j)=++aValue;      rightDataView(i,j)=++aValue;
1707        }        }
1708      }      }
1709    
     cout << "\tDo matrix multiplication." << endl;  
1710      DataArrayView::matMult(leftDataView,rightDataView,resultDataView);      DataArrayView::matMult(leftDataView,rightDataView,resultDataView);
1711      }
     cout << "\tCheck result." << endl;  
     //need to hand build result matrix and compare with generated result here.  
   
     cout << "Create a vector." << endl;  
     DataArray v;  
     DataArray::ShapeType vShape;  
     vShape.push_back(3);  
     v.setShape(vShape);  
     double aValue=0.0;  
     for (int i=0;i<vShape[0];++i) {  
       v(i)=++aValue;  
     }  
   
     cout << "Create a matrix." << endl;  
     DataArray mat;  
     DataArray::ShapeType mShape;  
     mShape.push_back(3);  
     mShape.push_back(2);  
     mat.setShape(mShape);  
     aValue=0.0;  
     for (int i=0;i<mShape[0];++i) {  
       for (int j=0;j<mShape[1];++j) {  
     mat(i,j)=++aValue;  
       }  
     }  
   
     // [1,2,3] x |1, 2| = [22,28]  
     //           |3, 4|  
     //           |5, 6|  
   
     cout << "Test multiplication of matrix and vector." << endl;  
     DataArray result=DataArray::matMult(v,mat);  
     assert(fabs(result(0) - 22) < 0.1);  
     assert(fabs(result(1) - 28) < 0.1);  
   }    
 */  
1712    
1713    cout << endl;    cout << endl;
1714    
# Line 1778  void DataArrayViewTestCase::testUnaryOp( Line 1735  void DataArrayViewTestCase::testUnaryOp(
1735      // constructor      // constructor
1736      DataArrayView dataView(data,shape);      DataArrayView dataView(data,shape);
1737    
1738        double tmp;
1739      // step the view along each data point in the underlying data      // step the view along each data point in the underlying data
1740      for (int p=0;p<npoints;p++) {      for (int p=0;p<npoints;p++) {
1741    
# Line 1788  void DataArrayViewTestCase::testUnaryOp( Line 1746  void DataArrayViewTestCase::testUnaryOp(
1746        dataView.unaryOp((UnaryDFunPtr)std::sin);        dataView.unaryOp((UnaryDFunPtr)std::sin);
1747    
1748        // check the results        // check the results
1749        assert(dataView()==std::sin((double)p));        tmp = std::sin((double)p);
1750          assert(dataView()==tmp);
1751    
1752        if (p<npoints-1) {        if (p<npoints-1) {
1753          dataView.incrOffset();          dataView.incrOffset();
# Line 1909  void DataArrayViewTestCase::testBinaryOp Line 1868  void DataArrayViewTestCase::testBinaryOp
1868    
1869    {    {
1870      cout << endl;      cout << endl;
1871      cout << "\tTest BinaryOp on scalar DataArrayViews.";      cout << "\tTest binaryOp on scalar DataArrayViews.";
1872    
1873      // define the shape for the DataArrayViews      // define the shape for the DataArrayViews
1874      DataArrayView::ShapeType shape;      DataArrayView::ShapeType shape;
# Line 1947  void DataArrayViewTestCase::testBinaryOp Line 1906  void DataArrayViewTestCase::testBinaryOp
1906    
1907    {    {
1908      cout << endl;      cout << endl;
1909      cout << "\tTest BinaryOp on shape (2,3) DataArrayViews.";      cout << "\tTest binaryOp on shape (2,3) DataArrayViews.";
1910    
1911      // define the shape for the DataArrayViews      // define the shape for the DataArrayViews
1912      DataArrayView::ShapeType shape;      DataArrayView::ShapeType shape;
# Line 2053  void DataArrayViewTestCase::testBinaryOp Line 2012  void DataArrayViewTestCase::testBinaryOp
2012    
2013    {    {
2014      cout << endl;      cout << endl;
2015      cout << "\tTest BinaryOp on scalar DataArrayView and single value.";      cout << "\tTest binaryOp on scalar DataArrayView and single value.";
2016    
2017      // define the shape for the DataArrayView      // define the shape for the DataArrayView
2018      DataArrayView::ShapeType shape;      DataArrayView::ShapeType shape;
# Line 2087  void DataArrayViewTestCase::testBinaryOp Line 2046  void DataArrayViewTestCase::testBinaryOp
2046    
2047    {    {
2048      cout << endl;      cout << endl;
2049      cout << "\tTest BinaryOp on shape (2,3) DataArrayView and single value.";      cout << "\tTest binaryOp on shape (2,3) DataArrayView and single value.";
2050    
2051      // define the shape for the DataArrayView      // define the shape for the DataArrayView
2052      DataArrayView::ShapeType shape;      DataArrayView::ShapeType shape;
# Line 2114  void DataArrayViewTestCase::testBinaryOp Line 2073  void DataArrayViewTestCase::testBinaryOp
2073        // apply a binary operation to the data point        // apply a binary operation to the data point
2074        dataView.binaryOp(5.8,multiplies<double>());        dataView.binaryOp(5.8,multiplies<double>());
2075    
2076          double tmp;
2077        // check the results        // check the results
2078        for (int i=0;i<shape[0];i++) {        for (int i=0;i<shape[0];i++) {
2079          for (int j=0;j<shape[1];j++) {          for (int j=0;j<shape[1];j++) {
2080            assert(dataView(i,j)==5.8*dataView.index(i,j));            tmp=5.8*dataView.index(i,j);
2081              assert(dataView(i,j)==tmp);
2082          }          }
2083        }        }
2084    
# Line 2164  void DataArrayViewTestCase::testBinaryOp Line 2125  void DataArrayViewTestCase::testBinaryOp
2125        // apply a binary operation to the data point        // apply a binary operation to the data point
2126        dataView.binaryOp(5.4,multiplies<double>());        dataView.binaryOp(5.4,multiplies<double>());
2127    
2128          double tmp;
2129        // check the results        // check the results
2130        for (int i=0;i<shape[0];i++) {        for (int i=0;i<shape[0];i++) {
2131          for (int j=0;j<shape[1];j++) {          for (int j=0;j<shape[1];j++) {
2132            for (int k=0;k<shape[2];k++) {            for (int k=0;k<shape[2];k++) {
2133              for (int l=0;l<shape[3];l++) {              for (int l=0;l<shape[3];l++) {
2134                assert(dataView(i,j,k,l)==5.4*dataView.index(i,j,k,l));                tmp=5.4*dataView.index(i,j,k,l);
2135                  assert(dataView(i,j,k,l)==tmp);
2136                }
2137              }
2138            }
2139          }
2140    
2141          if (p<npoints-1) {
2142            dataView.incrOffset();
2143          }
2144    
2145        }
2146    
2147      }
2148    
2149      cout << endl;
2150    
2151    }
2152    
2153    void DataArrayViewTestCase::testReductionOp()
2154    {
2155    
2156      {
2157        cout << endl;
2158        cout << "\tTest reductionOp on scalar DataArrayView.";
2159    
2160        // define the shape for the DataArrayView
2161        DataArrayView::ShapeType shape;
2162    
2163        // allocate the data for the DataArrayView
2164        int npoints=4;
2165        DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0);
2166    
2167        // constructor
2168        DataArrayView dataView(data,shape);
2169    
2170        // step the view along each data point in the underlying data
2171        for (int p=0;p<npoints;p++) {
2172    
2173          // assign values to the data point
2174          dataView()=p;
2175    
2176          // apply a reduction operation to this data point and check the results
2177          FMax fmax_func;
2178          assert(dataView.reductionOp(fmax_func,numeric_limits<double>::max()*-1)==p);
2179    
2180          if (p<npoints-1) {
2181            dataView.incrOffset();
2182          }
2183    
2184        }
2185    
2186      }
2187    
2188      {
2189        cout << endl;
2190        cout << "\tTest reductionOp on shape (2,3) DataArrayView.";
2191    
2192        // define the shape for the DataArrayView
2193        DataArrayView::ShapeType shape;
2194        shape.push_back(2);
2195        shape.push_back(3);
2196    
2197        // allocate the data for the DataArrayView
2198        int npoints=4;
2199        DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0);
2200    
2201        // constructor
2202        DataArrayView dataView(data,shape);
2203    
2204        // step the view along each data point in the underlying data
2205        for (int p=0;p<npoints;p++) {
2206    
2207          // assign values to the data point
2208          for (int i=0;i<shape[0];i++) {
2209            for (int j=0;j<shape[1];j++) {
2210              dataView(i,j)=dataView.index(i,j);
2211            }
2212          }
2213    
2214          // apply a reduction operation to this data point and check the results
2215          FMin fmin_func;
2216          assert(dataView.reductionOp(fmin_func,numeric_limits<double>::max())==dataView.index(0,0));
2217    
2218          if (p<npoints-1) {
2219            dataView.incrOffset();
2220          }
2221    
2222        }
2223    
2224      }
2225    
2226      {
2227        cout << endl;
2228        cout << "\tTest reductionOp on shape (9,8,5,11) DataArrayView.";
2229    
2230        // define the shape for the DataArrayView
2231        DataArrayView::ShapeType shape;
2232        shape.push_back(9);
2233        shape.push_back(8);
2234        shape.push_back(5);
2235        shape.push_back(11);
2236    
2237        // allocate the data for the DataArrayView
2238        int npoints=4;
2239        DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0);
2240    
2241        // constructor
2242        DataArrayView dataView(data,shape);
2243    
2244        // step the view along each data point in the underlying data
2245        for (int p=0;p<npoints;p++) {
2246    
2247          // assign values to the data point
2248          for (int i=0;i<shape[0];i++) {
2249            for (int j=0;j<shape[1];j++) {
2250              for (int k=0;k<shape[2];k++) {
2251                for (int l=0;l<shape[3];l++) {
2252                  dataView(i,j,k,l)=dataView.index(i,j,k,l);
2253              }              }
2254            }            }
2255          }          }
2256        }        }
2257    
2258          // apply a reduction operation to this data point and check the results
2259          AbsMax absmax_func;
2260          assert(dataView.reductionOp(absmax_func,0)==dataView.index(8,7,4,10));
2261    
2262        if (p<npoints-1) {        if (p<npoints-1) {
2263          dataView.incrOffset();          dataView.incrOffset();
2264        }        }
# Line 2198  TestSuite* DataArrayViewTestCase::suite Line 2282  TestSuite* DataArrayViewTestCase::suite
2282    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testScalarView",&DataArrayViewTestCase::testScalarView));    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testScalarView",&DataArrayViewTestCase::testScalarView));
2283    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testResultSliceShape",&DataArrayViewTestCase::testResultSliceShape));    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testResultSliceShape",&DataArrayViewTestCase::testResultSliceShape));
2284    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testSlicing",&DataArrayViewTestCase::testSlicing));    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testSlicing",&DataArrayViewTestCase::testSlicing));
   testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testMatMult",&DataArrayViewTestCase::testMatMult));  
2285    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testUnaryOp",&DataArrayViewTestCase::testUnaryOp));    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testUnaryOp",&DataArrayViewTestCase::testUnaryOp));
2286    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testBinaryOp",&DataArrayViewTestCase::testBinaryOp));    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testBinaryOp",&DataArrayViewTestCase::testBinaryOp));
2287      testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testReductionOp",&DataArrayViewTestCase::testReductionOp));
2288      testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testMatMult",&DataArrayViewTestCase::testMatMult));
2289    return testSuite;    return testSuite;
2290  }  }

Legend:
Removed from v.108  
changed lines
  Added in v.468

  ViewVC Help
Powered by ViewVC 1.1.26