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

branches/ROBW_XPLATFORM/escript/test/Data/DataTestCase.cpp revision 622 by robwdcock, Wed Mar 22 14:00:08 2006 UTC trunk/escript/test/DataTestCase.cpp revision 1355 by ksteube, Thu Nov 29 00:53:31 2007 UTC
# Line 1  Line 1 
 // $Id$  
 /*  
  *****************************************************************************  
  *                                                                           *  
  *       COPYRIGHT  ACcESS  -  All Rights Reserved                           *  
  *                                                                           *  
  * This software is the property of ACcESS. No part of this code             *  
  * may be copied in any form or by any means without the expressed written   *  
  * consent of ACcESS.  Copying, use or modification of this software         *  
  * by any unauthorised person is illegal unless that person has a software   *  
  * license agreement with ACcESS.                                            *  
  *                                                                           *  
  *****************************************************************************  
 */  
1    
2  #include "DataTestCase.h"  /* $Id$ */
   
 #include "FunctionSpace.h"  
 #include "EsysException.h"  
3    
4  #include "Data.h"  /*******************************************************
5     *
6     *           Copyright 2003-2007 by ACceSS MNRF
7     *       Copyright 2007 by University of Queensland
8     *
9     *                http://esscc.uq.edu.au
10     *        Primary Business: Queensland, Australia
11     *  Licensed under the Open Software License version 3.0
12     *     http://www.opensource.org/licenses/osl-3.0.php
13     *
14     *******************************************************/
15    
16  #include <iostream>  #include <iostream>
17    #if (defined _WIN32) && (defined __INTEL_COMPILER)
18    #include <mathimf.h>
19    #else
20  #include <math.h>  #include <math.h>
21    #endif
22    
23    #include "DataTestCase.h"
24    
25    #include "escript/FunctionSpace.h"
26    #include "esysUtils/EsysException.h"
27    
28    #include "escript/Data.h"
29    
30    
31  using namespace std;  using namespace std;
32  using namespace CppUnitTest;  using namespace CppUnitTest;
# Line 541  void DataTestCase::testDataTagged() { Line 546  void DataTestCase::testDataTagged() {
546      DataArrayView defaultValue(viewData,viewShape);      DataArrayView defaultValue(viewData,viewShape);
547    
548      bool expanded=false;      bool expanded=false;
549      
550      Data myData(keys,values,defaultValue,FunctionSpace(),expanded);      Data myData(keys,values,defaultValue,FunctionSpace(),expanded);
551    
552      // cout << myData.toString() << endl;      // cout << myData.toString() << endl;
# Line 586  void DataTestCase::testDataTagged() { Line 591  void DataTestCase::testDataTagged() {
591      cout << "\tTest setting of a tag and associated value." << endl;      cout << "\tTest setting of a tag and associated value." << endl;
592    
593      // value for tag "1"      // value for tag "1"
594      DataArray eTwo(defaultValue);      DataArrayView::ValueType eTwoData(viewData);
595      for (int i=0;i<eTwo.getView().getShape()[0];i++) {      DataArrayView eTwoView(eTwoData, viewShape);
596        eTwo.getView()(i)=i+2.0;      for (int i=0;i<eTwoView.getShape()[0];i++) {
597          eTwoView(i)=i+2.0;
598      }      }
599    
600      myData.setTaggedValueFromCPP(1,eTwo.getView());      myData.setTaggedValueFromCPP(1,eTwoView);
601    
602      assert(myData.getLength()==6);      assert(myData.getLength()==6);
603    
604      myDataView = myData.getDataPoint(0,0);      myDataView = myData.getDataPoint(0,0);
605      assert(myDataView==eTwo.getView());      assert(myDataView==eTwoView);
606      assert(!myDataView.isEmpty());      assert(!myDataView.isEmpty());
607      assert(myDataView.getOffset()==3);      assert(myDataView.getOffset()==3);
608      assert(myDataView.getRank()==1);      assert(myDataView.getRank()==1);
# Line 746  void DataTestCase::testOperations() { Line 752  void DataTestCase::testOperations() {
752    
753    // test unary operations    // test unary operations
754    
755      double tmp;
756    cout << "\tTest Data::pow." << endl;    cout << "\tTest Data::pow." << endl;
757    Data power(3.0,shape,FunctionSpace(),true);    Data power(3.0,shape,FunctionSpace(),true);
758    resultEx.copy(baseEx.powD(power));    resultEx.copy(baseEx.powD(power));
# Line 753  void DataTestCase::testOperations() { Line 760  void DataTestCase::testOperations() {
760    resultTag.copy(baseTag.powD(power));    resultTag.copy(baseTag.powD(power));
761    for (int i=0;i<shape[0];i++) {    for (int i=0;i<shape[0];i++) {
762      for (int j=0;j<shape[1];j++) {      for (int j=0;j<shape[1];j++) {
763        assert(resultEx.getPointDataView()(i,j) == pow(dataView.index(i,j),3.0));        tmp=pow((double)dataView.index(i,j),(double)3.0);
764        assert(resultCon.getPointDataView()(i,j) == pow(dataView.index(i,j),3.0));        assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
765        assert(resultTag.getPointDataView()(i,j) == pow(dataView.index(i,j),3.0));        assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
766          assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
767      }      }
768    }    }
769    
# Line 763  void DataTestCase::testOperations() { Line 771  void DataTestCase::testOperations() {
771    resultEx.copy(baseEx.sin());    resultEx.copy(baseEx.sin());
772    resultCon.copy(baseCon.sin());    resultCon.copy(baseCon.sin());
773    resultTag.copy(baseTag.sin());    resultTag.copy(baseTag.sin());
774    assert(true);    for (int i=0;i<shape[0];i++) {
775        for (int j=0;j<shape[1];j++) {
776          tmp=sin((double)dataView.index(i,j));
777          assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
778          assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
779          assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
780        }
781      }
782    
783    cout << "\tTest Data::cos." << endl;    cout << "\tTest Data::cos." << endl;
784    resultEx.copy(baseEx.cos());    resultEx.copy(baseEx.cos());
785    resultCon.copy(baseCon.cos());    resultCon.copy(baseCon.cos());
786    resultTag.copy(baseTag.cos());    resultTag.copy(baseTag.cos());
787    assert(true);    for (int i=0;i<shape[0];i++) {
788        for (int j=0;j<shape[1];j++) {
789          tmp=cos((double)dataView.index(i,j));
790          assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
791          assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
792          assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
793        }
794      }
795    
796    cout << "\tTest Data::tan." << endl;    cout << "\tTest Data::tan." << endl;
797    resultEx.copy(baseEx.tan());    resultEx.copy(baseEx.tan());
798    resultCon.copy(baseCon.tan());    resultCon.copy(baseCon.tan());
799    resultTag.copy(baseTag.tan());    resultTag.copy(baseTag.tan());
800    assert(true);    for (int i=0;i<shape[0];i++) {
801        for (int j=0;j<shape[1];j++) {
802          tmp=tan((double)dataView.index(i,j));
803          assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
804          assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
805          assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
806        }
807      }
808    
809    cout << "\tTest Data::asin." << endl;    cout << "\tTest Data::asin." << endl;
810    resultEx.copy(baseEx.asin());    resultEx.copy(baseEx.asin());
# Line 793  void DataTestCase::testOperations() { Line 822  void DataTestCase::testOperations() {
822    resultEx.copy(baseEx.atan());    resultEx.copy(baseEx.atan());
823    resultCon.copy(baseCon.atan());    resultCon.copy(baseCon.atan());
824    resultTag.copy(baseTag.atan());    resultTag.copy(baseTag.atan());
825    assert(true);    for (int i=0;i<shape[0];i++) {
826        for (int j=0;j<shape[1];j++) {
827          tmp=atan((double)dataView.index(i,j));
828          assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
829          assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
830          assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
831        }
832      }
833    
834    cout << "\tTest Data::sinh." << endl;    cout << "\tTest Data::sinh." << endl;
835    resultEx.copy(baseEx.sinh());    resultEx.copy(baseEx.sinh());
836    resultCon.copy(baseCon.sinh());    resultCon.copy(baseCon.sinh());
837    resultTag.copy(baseTag.sinh());    resultTag.copy(baseTag.sinh());
838    assert(true);    for (int i=0;i<shape[0];i++) {
839        for (int j=0;j<shape[1];j++) {
840          tmp=sinh((double)dataView.index(i,j));
841          assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
842          assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
843          assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
844        }
845      }
846    
847    cout << "\tTest Data::cosh." << endl;    cout << "\tTest Data::cosh." << endl;
848    resultEx.copy(baseEx.cosh());    resultEx.copy(baseEx.cosh());
849    resultCon.copy(baseCon.cosh());    resultCon.copy(baseCon.cosh());
850    resultTag.copy(baseTag.cosh());    resultTag.copy(baseTag.cosh());
851    assert(true);    for (int i=0;i<shape[0];i++) {
852        for (int j=0;j<shape[1];j++) {
853          tmp=cosh((double)dataView.index(i,j));
854          assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
855          assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
856          assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
857        }
858      }
859    
860    cout << "\tTest Data::tanh." << endl;    cout << "\tTest Data::tanh." << endl;
861    resultEx.copy(baseEx.tanh());    resultEx.copy(baseEx.tanh());
862    resultCon.copy(baseCon.tanh());    resultCon.copy(baseCon.tanh());
863    resultTag.copy(baseTag.tanh());    resultTag.copy(baseTag.tanh());
864    assert(true);    for (int i=0;i<shape[0];i++) {
865        for (int j=0;j<shape[1];j++) {
866          tmp=tanh((double)dataView.index(i,j));
867          assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
868          assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
869          assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
870        }
871      }
872    
873    cout << "\tTest Data::asinh." << endl;    cout << "\tTest Data::asinh." << endl;
874    resultEx.copy(baseEx.asinh());    resultEx.copy(baseEx.asinh());
# Line 841  void DataTestCase::testOperations() { Line 898  void DataTestCase::testOperations() {
898    resultEx.copy(baseEx.abs());    resultEx.copy(baseEx.abs());
899    resultCon.copy(baseCon.abs());    resultCon.copy(baseCon.abs());
900    resultTag.copy(baseTag.abs());    resultTag.copy(baseTag.abs());
901    assert(true);    for (int i=0;i<shape[0];i++) {
902        for (int j=0;j<shape[1];j++) {
903          tmp=abs((double)dataView.index(i,j));
904          assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
905          assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
906          assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
907        }
908      }
909    
910    cout << "\tTest Data::sign." << endl;    cout << "\tTest Data::sign." << endl;
911    resultEx.copy(baseEx.sign());    resultEx.copy(baseEx.sign());
# Line 853  void DataTestCase::testOperations() { Line 917  void DataTestCase::testOperations() {
917    resultEx.copy(baseEx.exp());    resultEx.copy(baseEx.exp());
918    resultCon.copy(baseCon.exp());    resultCon.copy(baseCon.exp());
919    resultTag.copy(baseTag.exp());    resultTag.copy(baseTag.exp());
920    assert(true);    for (int i=0;i<shape[0];i++) {
921        for (int j=0;j<shape[1];j++) {
922          tmp=exp((double)dataView.index(i,j));
923          assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
924          assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
925          assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
926        }
927      }
928    
929    cout << "\tTest Data::sqrt." << endl;    cout << "\tTest Data::sqrt." << endl;
930    resultEx.copy(baseEx.sqrt());    resultEx.copy(baseEx.sqrt());
931    resultCon.copy(baseCon.sqrt());    resultCon.copy(baseCon.sqrt());
932    resultTag.copy(baseTag.sqrt());    resultTag.copy(baseTag.sqrt());
933    assert(true);    for (int i=0;i<shape[0];i++) {
934        for (int j=0;j<shape[1];j++) {
935          tmp=sqrt((double)dataView.index(i,j));
936          assert(std::abs(resultEx.getPointDataView()(i,j) - tmp) <= REL_TOL*std::abs(tmp));
937          assert(std::abs(resultCon.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
938          assert(std::abs(resultTag.getPointDataView()(i,j)- tmp) <= REL_TOL*std::abs(tmp));
939        }
940      }
941    
942    cout << "\tTest Data::neg." << endl;    cout << "\tTest Data::neg." << endl;
943    resultEx.copy(baseEx.neg());    resultEx.copy(baseEx.neg());
# Line 873  void DataTestCase::testOperations() { Line 951  void DataTestCase::testOperations() {
951    resultTag.copy(baseTag.pos());    resultTag.copy(baseTag.pos());
952    for (int i=0;i<shape[0];i++) {    for (int i=0;i<shape[0];i++) {
953      for (int j=0;j<shape[1];j++) {      for (int j=0;j<shape[1];j++) {
954        assert(resultEx.getPointDataView()(i,j) == dataView.index(i,j));        assert(std::abs(resultEx.getPointDataView()(i,j) - dataView.index(i,j)) <= REL_TOL*std::abs(dataView.index(i,j)));
955        assert(resultCon.getPointDataView()(i,j) == dataView.index(i,j));        assert(std::abs(resultCon.getPointDataView()(i,j) - dataView.index(i,j)) <= REL_TOL*std::abs(dataView.index(i,j)));
956        assert(resultTag.getPointDataView()(i,j) == dataView.index(i,j));        assert(std::abs(resultTag.getPointDataView()(i,j) - dataView.index(i,j)) <= REL_TOL*std::abs(dataView.index(i,j)));
957      }      }
958    }    }
959    
960    // test reduction operations    // test reduction operations
961    
962    cout << "\tTest Data::Lsup." << endl;    cout << "\tTest Data::Lsup." << endl;
963    assert(baseEx.Lsup() == 5);    assert(std::abs(baseEx.Lsup() - 5) <= REL_TOL*5);
964    assert(baseCon.Lsup() == 5);    assert(std::abs(baseCon.Lsup() - 5) <= REL_TOL*5);
965    assert(baseTag.Lsup() == 5);    assert(std::abs(baseTag.Lsup() - 5) <= REL_TOL*5);
966    
967    cout << "\tTest Data::sup." << endl;    cout << "\tTest Data::sup." << endl;
968    assert(baseEx.sup() == 5);    assert(std::abs(baseEx.sup() - 5) <= REL_TOL*5);
969    assert(baseCon.sup() == 5);    assert(std::abs(baseCon.sup() - 5) <= REL_TOL*5);
970    assert(baseTag.sup() == 5);    assert(std::abs(baseTag.sup() - 5) <= REL_TOL*5);
971    
972    cout << "\tTest Data::inf." << endl;    cout << "\tTest Data::inf." << endl;
973    assert(baseEx.inf() == 0);    assert(std::abs(baseEx.inf() - 0) <= REL_TOL*0);
974    assert(baseCon.inf() == 0);    assert(std::abs(baseCon.inf() - 0) <= REL_TOL*0);
975    assert(baseTag.inf() == 0);    assert(std::abs(baseTag.inf() - 0) <= REL_TOL*0);
976    
977    // test data-point reduction operations    // test data-point reduction operations
978    
# Line 902  void DataTestCase::testOperations() { Line 980  void DataTestCase::testOperations() {
980    resultEx.copy(baseEx.minval());    resultEx.copy(baseEx.minval());
981    resultCon.copy(baseCon.minval());    resultCon.copy(baseCon.minval());
982    resultTag.copy(baseTag.minval());    resultTag.copy(baseTag.minval());
983    assert(resultEx.getPointDataView()() == 0);    assert(std::abs(resultEx.getPointDataView()() - 0) <= REL_TOL*0);
984    assert(resultCon.getPointDataView()() == 0);    assert(std::abs(resultCon.getPointDataView()() - 0) <= REL_TOL*0);
985    assert(resultTag.getPointDataView()() == 0);    assert(std::abs(resultTag.getPointDataView()() - 0) <= REL_TOL*0);
986    
987    cout << "\tTest Data::maxval." << endl;    cout << "\tTest Data::maxval." << endl;
988    resultEx.copy(baseEx.maxval());    resultEx.copy(baseEx.maxval());
989    resultCon.copy(baseCon.maxval());    resultCon.copy(baseCon.maxval());
990    resultTag.copy(baseTag.maxval());    resultTag.copy(baseTag.maxval());
991    assert(resultEx.getPointDataView()() == 5);    assert(std::abs(resultEx.getPointDataView()() - 5) <= REL_TOL*5);
992    assert(resultCon.getPointDataView()() == 5);    assert(std::abs(resultCon.getPointDataView()() - 5) <= REL_TOL*5);
993    assert(resultTag.getPointDataView()() == 5);    assert(std::abs(resultTag.getPointDataView()() - 5) <= REL_TOL*5);
   
   cout << "\tTest Data::trace." << endl;  
   resultEx.copy(baseEx.trace());  
   resultCon.copy(baseCon.trace());  
   resultTag.copy(baseTag.trace());  
   assert(resultEx.getPointDataView()() == 15);  
   assert(resultCon.getPointDataView()() == 15);  
   assert(resultTag.getPointDataView()() == 15);  
994    
995  }  }
996    
 void DataTestCase::testRefValue() {  
   
   //  
   // Note - this test can't be run as boost::python::numeric::array  
   // objects can only be created and used from within a python thread!  
   //  
   
   cout << endl;  
   
   cout << "\tTest Data object RefValue methods." << endl;  
   
   // Create three Data object - DataExpanded, DataConstant and DataEmpty  
   DataArrayView::ShapeType viewShape;  
   viewShape.push_back(3);  
   DataArrayView::ValueType viewData(3);  
   for (int i=0;i<viewShape[0];++i) {  
     viewData[i]=i;  
   }  
   DataArrayView myView(viewData,viewShape);  
   
   bool expanded=true;  
   
   Data expandedData(myView,FunctionSpace(),expanded);  
   Data constantData(myView);  
   Data emptyData;  
   
   assert(expandedData.isExpanded());  
   assert(constantData.isConstant());  
   assert(emptyData.isEmpty());  
   
   // Check assertions are thrown for RefValue methods on DataEmpty  
   
   int ref = 0;  
   boost::python::numeric::array num_array(1.0);  
   
   try {  
       emptyData.getRefValue(ref,num_array);  
       assert(false);  
   }  
   catch (EsysException& e) {  
       assert(true);  
   }  
   try {  
       emptyData.setRefValue(ref,num_array);  
       assert(false);  
   }  
   catch (EsysException& e) {  
       assert(true);  
   }  
   
   // Check assertions are thrown for RefValue methods on DataConstant  
   try {  
       constantData.getRefValue(ref,num_array);  
       assert(false);  
   }  
   catch (EsysException& e) {  
       assert(true);  
   }  
   try {  
       constantData.setRefValue(ref,num_array);  
       assert(false);  
   }  
   catch (EsysException& e) {  
       assert(true);  
   }  
   
   // Check calls to RefValue methods on DataExpanded  
   expandedData.getRefValue(ref,num_array);  
   expandedData.setRefValue(ref,num_array);  
   
 }  
997    
998  void DataTestCase::testMemAlloc() {  void DataTestCase::testMemAlloc() {
999    

Legend:
Removed from v.622  
changed lines
  Added in v.1355

  ViewVC Help
Powered by ViewVC 1.1.26