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

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

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

revision 108 by jgs, Thu Jan 27 06:21:59 2005 UTC revision 113 by jgs, Mon Feb 28 07:06:33 2005 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 "DataArrayViewTestCase.h"  #include "escript/Data/DataArrayView.h"
16    #include "escript/Data/DataAlgorithm.h"
17  #include "esysUtils/EsysException.h"  #include "esysUtils/EsysException.h"
18    
19    #include "DataArrayViewTestCase.h"
20    
21  #include <iostream>  #include <iostream>
22    
23  using namespace CppUnitTest;  using namespace CppUnitTest;
# 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 1909  void DataArrayViewTestCase::testBinaryOp Line 1866  void DataArrayViewTestCase::testBinaryOp
1866    
1867    {    {
1868      cout << endl;      cout << endl;
1869      cout << "\tTest BinaryOp on scalar DataArrayViews.";      cout << "\tTest binaryOp on scalar DataArrayViews.";
1870    
1871      // define the shape for the DataArrayViews      // define the shape for the DataArrayViews
1872      DataArrayView::ShapeType shape;      DataArrayView::ShapeType shape;
# Line 1947  void DataArrayViewTestCase::testBinaryOp Line 1904  void DataArrayViewTestCase::testBinaryOp
1904    
1905    {    {
1906      cout << endl;      cout << endl;
1907      cout << "\tTest BinaryOp on shape (2,3) DataArrayViews.";      cout << "\tTest binaryOp on shape (2,3) DataArrayViews.";
1908    
1909      // define the shape for the DataArrayViews      // define the shape for the DataArrayViews
1910      DataArrayView::ShapeType shape;      DataArrayView::ShapeType shape;
# Line 2053  void DataArrayViewTestCase::testBinaryOp Line 2010  void DataArrayViewTestCase::testBinaryOp
2010    
2011    {    {
2012      cout << endl;      cout << endl;
2013      cout << "\tTest BinaryOp on scalar DataArrayView and single value.";      cout << "\tTest binaryOp on scalar DataArrayView and single value.";
2014    
2015      // define the shape for the DataArrayView      // define the shape for the DataArrayView
2016      DataArrayView::ShapeType shape;      DataArrayView::ShapeType shape;
# Line 2087  void DataArrayViewTestCase::testBinaryOp Line 2044  void DataArrayViewTestCase::testBinaryOp
2044    
2045    {    {
2046      cout << endl;      cout << endl;
2047      cout << "\tTest BinaryOp on shape (2,3) DataArrayView and single value.";      cout << "\tTest binaryOp on shape (2,3) DataArrayView and single value.";
2048    
2049      // define the shape for the DataArrayView      // define the shape for the DataArrayView
2050      DataArrayView::ShapeType shape;      DataArrayView::ShapeType shape;
# Line 2187  void DataArrayViewTestCase::testBinaryOp Line 2144  void DataArrayViewTestCase::testBinaryOp
2144    
2145  }  }
2146    
2147    void DataArrayViewTestCase::testReductionOp()
2148    {
2149    
2150      {
2151        cout << endl;
2152        cout << "\tTest reductionOp on scalar DataArrayView.";
2153    
2154        // define the shape for the DataArrayView
2155        DataArrayView::ShapeType shape;
2156    
2157        // allocate the data for the DataArrayView
2158        int npoints=4;
2159        DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0);
2160    
2161        // constructor
2162        DataArrayView dataView(data,shape);
2163    
2164        // step the view along each data point in the underlying data
2165        for (int p=0;p<npoints;p++) {
2166    
2167          // assign values to the data point
2168          dataView()=p;
2169    
2170          // apply a reduction operation to this data point and check the results
2171          assert(dataView.reductionOp(DataAlgorithmAdapter<FMax>(numeric_limits<double>::max()*-1))==p);
2172    
2173          if (p<npoints-1) {
2174            dataView.incrOffset();
2175          }
2176    
2177        }
2178    
2179      }
2180    
2181      {
2182        cout << endl;
2183        cout << "\tTest reductionOp on shape (2,3) DataArrayView.";
2184    
2185        // define the shape for the DataArrayView
2186        DataArrayView::ShapeType shape;
2187        shape.push_back(2);
2188        shape.push_back(3);
2189    
2190        // allocate the data for the DataArrayView
2191        int npoints=4;
2192        DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0);
2193    
2194        // constructor
2195        DataArrayView dataView(data,shape);
2196    
2197        // step the view along each data point in the underlying data
2198        for (int p=0;p<npoints;p++) {
2199    
2200          // assign values to the data point
2201          for (int i=0;i<shape[0];i++) {
2202            for (int j=0;j<shape[1];j++) {
2203              dataView(i,j)=dataView.index(i,j);
2204            }
2205          }
2206    
2207          // apply a reduction operation to this data point and check the results
2208          assert(dataView.reductionOp(DataAlgorithmAdapter<FMin>(numeric_limits<double>::max()))==dataView.index(0,0));
2209    
2210          if (p<npoints-1) {
2211            dataView.incrOffset();
2212          }
2213    
2214        }
2215    
2216      }
2217    
2218      {
2219        cout << endl;
2220        cout << "\tTest reductionOp on shape (9,8,5,11) DataArrayView.";
2221    
2222        // define the shape for the DataArrayView
2223        DataArrayView::ShapeType shape;
2224        shape.push_back(9);
2225        shape.push_back(8);
2226        shape.push_back(5);
2227        shape.push_back(11);
2228    
2229        // allocate the data for the DataArrayView
2230        int npoints=4;
2231        DataArrayView::ValueType data(DataArrayView::noValues(shape)*npoints,0);
2232    
2233        // constructor
2234        DataArrayView dataView(data,shape);
2235    
2236        // step the view along each data point in the underlying data
2237        for (int p=0;p<npoints;p++) {
2238    
2239          // assign values to the data point
2240          for (int i=0;i<shape[0];i++) {
2241            for (int j=0;j<shape[1];j++) {
2242              for (int k=0;k<shape[2];k++) {
2243                for (int l=0;l<shape[3];l++) {
2244                  dataView(i,j,k,l)=dataView.index(i,j,k,l);
2245                }
2246              }
2247            }
2248          }
2249    
2250          // apply a reduction operation to this data point and check the results
2251          assert(dataView.reductionOp(DataAlgorithmAdapter<AbsMax>(0))==dataView.index(8,7,4,10));
2252    
2253          if (p<npoints-1) {
2254            dataView.incrOffset();
2255          }
2256    
2257        }
2258    
2259      }
2260    
2261      cout << endl;
2262    
2263    }
2264    
2265  TestSuite* DataArrayViewTestCase::suite ()  TestSuite* DataArrayViewTestCase::suite ()
2266  {  {
2267    //    //
# Line 2198  TestSuite* DataArrayViewTestCase::suite Line 2273  TestSuite* DataArrayViewTestCase::suite
2273    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testScalarView",&DataArrayViewTestCase::testScalarView));    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testScalarView",&DataArrayViewTestCase::testScalarView));
2274    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testResultSliceShape",&DataArrayViewTestCase::testResultSliceShape));    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testResultSliceShape",&DataArrayViewTestCase::testResultSliceShape));
2275    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testSlicing",&DataArrayViewTestCase::testSlicing));    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testSlicing",&DataArrayViewTestCase::testSlicing));
   testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testMatMult",&DataArrayViewTestCase::testMatMult));  
2276    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testUnaryOp",&DataArrayViewTestCase::testUnaryOp));    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testUnaryOp",&DataArrayViewTestCase::testUnaryOp));
2277    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testBinaryOp",&DataArrayViewTestCase::testBinaryOp));    testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testBinaryOp",&DataArrayViewTestCase::testBinaryOp));
2278      testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testReductionOp",&DataArrayViewTestCase::testReductionOp));
2279      testSuite->addTest (new TestCaller< DataArrayViewTestCase>("testMatMult",&DataArrayViewTestCase::testMatMult));
2280    return testSuite;    return testSuite;
2281  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.26