Back to index

salome-med  6.5.0
ExprEvalInterpTest.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
00002 //
00003 // This library is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public
00005 // License as published by the Free Software Foundation; either
00006 // version 2.1 of the License.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // Lesser General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU Lesser General Public
00014 // License along with this library; if not, write to the Free Software
00015 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00016 //
00017 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00018 //
00019 
00020 #include "ExprEvalInterpTest.hxx"
00021 #include "InterpKernelExprParser.hxx"
00022 
00023 #include <limits>
00024 #include <iterator>
00025 
00026 using namespace INTERP_TEST;
00027 
00028 void ExprEvalInterpTest::testBuildStringFromFortran()
00029 {
00030   char toto1[]="123456  ";
00031   char result[]="123456";
00032   std::string titi;
00033   titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto1,8);
00034   CPPUNIT_ASSERT_EQUAL(6,(int)titi.length());
00035   CPPUNIT_ASSERT(titi==result);
00036   //
00037   char toto2[]=" 23456  ";
00038   char result2[]=" 23456";
00039   titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto2,8);
00040   CPPUNIT_ASSERT(titi==result2);
00041   CPPUNIT_ASSERT_EQUAL(6,(int)titi.length());
00042   //
00043   char toto3[]="  3456  ";
00044   char result3[]="  3456";
00045   titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto3,8);
00046   CPPUNIT_ASSERT(titi==result3);
00047   CPPUNIT_ASSERT_EQUAL(6,(int)titi.length());
00048   //
00049   char toto4[]="        ";
00050   titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto4,8);
00051   CPPUNIT_ASSERT_EQUAL(0,(int)titi.length());
00052   //
00053   char toto5[]="  345677";
00054   titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto5,8);
00055   CPPUNIT_ASSERT(titi==toto5);
00056   CPPUNIT_ASSERT_EQUAL(8,(int)titi.length());
00057 }
00058 
00059 void ExprEvalInterpTest::testDeleteWhiteSpaces()
00060 {
00061   char toto[]=" jkhjkh ooooppp l ";
00062   char result[]="jkhjkhoooopppl";
00063   std::string totoS(toto);
00064   std::string totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
00065   CPPUNIT_ASSERT(totoR==result);
00066   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
00067   //
00068   char toto2[]=" jkhjkh     ooooppp    l ";
00069   totoS=toto2;
00070   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
00071   CPPUNIT_ASSERT(totoR==result);
00072   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
00073   //
00074   char toto3[]=" jkhjkh     oooo pppl ";
00075   totoS=toto3;
00076   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
00077   CPPUNIT_ASSERT(totoR==result);
00078   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
00079   //
00080   char toto4[]=" jkhjkh     oooo pppl";
00081   totoS=toto4;
00082   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
00083   CPPUNIT_ASSERT(totoR==result);
00084   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
00085   //
00086   char toto5[]="jkhjkh     oooo pppl";
00087   totoS=toto5;
00088   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
00089   CPPUNIT_ASSERT(totoR==result);
00090   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
00091   //
00092   totoS=result;
00093   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
00094   CPPUNIT_ASSERT(totoR==result);
00095   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
00096   //
00097   char toto6[]="j k h j k h o o o o p p p l";
00098   totoS=toto6;
00099   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
00100   CPPUNIT_ASSERT(totoR==result);
00101   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
00102   //
00103   char toto7[]="j  k  h j    k h   o  o  o  o  p  pp    l";
00104   totoS=toto7;
00105   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
00106   CPPUNIT_ASSERT(totoR==result);
00107   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
00108   //
00109   char toto8[]="           ";
00110   totoS=toto8;
00111   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
00112   CPPUNIT_ASSERT(totoR.empty());
00113   //
00114   char toto9[]="";
00115   totoS=toto9;
00116   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
00117   CPPUNIT_ASSERT(totoR.empty());
00118   //
00119   char toto10[]="j\n k \nh\nj \n\n  k\nh \n o \no\n o\n o \np\n\npp \n\n l";
00120   totoS=toto10;
00121   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
00122   CPPUNIT_ASSERT(totoR==result);
00123   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
00124 }
00125 
00126 void ExprEvalInterpTest::testInterpreter0()
00127 {
00128   INTERP_KERNEL::ExprParser expr1("3*-2");
00129   expr1.parse();
00130   CPPUNIT_ASSERT_DOUBLES_EQUAL(-6.,expr1.evaluate(),1e-15);
00131   INTERP_KERNEL::ExprParser expr2("-2.3");
00132   expr2.parse();
00133   CPPUNIT_ASSERT_DOUBLES_EQUAL(-2.3,expr2.evaluate(),1e-15);
00134   INTERP_KERNEL::ExprParser expr3("--2.3");
00135   expr3.parse();
00136   CPPUNIT_ASSERT_DOUBLES_EQUAL(2.3,expr3.evaluate(),1e-15);
00137   INTERP_KERNEL::ExprParser expr4("-++2.3");
00138   expr4.parse();
00139   CPPUNIT_ASSERT_DOUBLES_EQUAL(-2.3,expr4.evaluate(),1e-15);
00140   INTERP_KERNEL::ExprParser expr5("+2.3");
00141   expr5.parse();
00142   CPPUNIT_ASSERT_DOUBLES_EQUAL(2.3,expr5.evaluate(),1e-15);
00143   INTERP_KERNEL::ExprParser expr6("3^-1");
00144   expr6.parse();
00145   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.33333333333333333,expr6.evaluate(),1e-15);
00146 }
00147 
00148 void ExprEvalInterpTest::testInterpreter1()
00149 {
00150   INTERP_KERNEL::ExprParser expr1("3+2*5");
00151   expr1.parse();
00152   CPPUNIT_ASSERT_DOUBLES_EQUAL(13.,expr1.evaluate(),1e-14);
00153   INTERP_KERNEL::ExprParser expr2("3+2^3*5");
00154   expr2.parse();
00155   CPPUNIT_ASSERT_DOUBLES_EQUAL(43.,expr2.evaluate(),1e-14);
00156   INTERP_KERNEL::ExprParser expr3("3+2^(2*5)");
00157   expr3.parse();
00158   CPPUNIT_ASSERT_DOUBLES_EQUAL(1027.,expr3.evaluate(),1e-14);
00159   INTERP_KERNEL::ExprParser expr4("(3.2+4.3)*(1.3+2.3*7.8)");
00160   expr4.parse();
00161   CPPUNIT_ASSERT_DOUBLES_EQUAL(144.3,expr4.evaluate(),1e-10);
00162   INTERP_KERNEL::ExprParser expr5("(3.2+4.3)*cos(1.3+2.3*7.8)");
00163   expr5.parse();
00164   CPPUNIT_ASSERT_DOUBLES_EQUAL(6.9355510138337619,expr5.evaluate(),1e-14);
00165   INTERP_KERNEL::ExprParser expr6("3+2-4-7+4.3");
00166   expr6.parse();
00167   CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.7,expr6.evaluate(),1e-14);
00168   INTERP_KERNEL::ExprParser expr7("3.2*4.5/3.3/2.2");
00169   expr7.parse();
00170   CPPUNIT_ASSERT_DOUBLES_EQUAL(1.9834710743801653,expr7.evaluate(),1e-14);
00171   INTERP_KERNEL::ExprParser expr8("3.2*4.5/3.3/2.2");
00172   expr8.parse();
00173   CPPUNIT_ASSERT_DOUBLES_EQUAL(1.9834710743801653,expr8.evaluate(),1e-14);
00174   INTERP_KERNEL::ExprParser expr9("(((1.23456789)))");
00175   expr9.parse();
00176   CPPUNIT_ASSERT_DOUBLES_EQUAL(1.23456789,expr9.evaluate(),1e-14);
00177   INTERP_KERNEL::ExprParser expr10("3.2*((2*5.2+6.)+(1.2*1.2+3.))");
00178   expr10.parse();
00179   CPPUNIT_ASSERT_DOUBLES_EQUAL(66.688,expr10.evaluate(),1e-13);
00180   INTERP_KERNEL::ExprParser expr11("((3.2*(((2*5.2+6.)+(1.2*1.2+3.)))))");
00181   expr11.parse();
00182   CPPUNIT_ASSERT_DOUBLES_EQUAL(66.688,expr11.evaluate(),1e-13);
00183   INTERP_KERNEL::ExprParser expr12("((3.2*(cos((2*5.2+6.)+(1.2*1.2+3.)))))");
00184   expr12.parse();
00185   CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.3038041398761016,expr12.evaluate(),1e-14);
00186   INTERP_KERNEL::ExprParser expr13("((3.2*(sin((2*5.2+6.)+(1.2*1.2+3.)))))");
00187   expr13.parse();
00188   CPPUNIT_ASSERT_DOUBLES_EQUAL(2.9223440531261784,expr13.evaluate(),1e-14);
00189   INTERP_KERNEL::ExprParser expr14("((3.2*(tan((2*5.2+6.)+(1.2*1.2+3.)))))");
00190   expr14.parse();
00191   CPPUNIT_ASSERT_DOUBLES_EQUAL(-7.1724737512280257,expr14.evaluate(),1e-14);
00192   INTERP_KERNEL::ExprParser expr15("((3.2*(sqrt((2*5.2+6.)+(1.2*1.2+3.)))))");
00193   expr15.parse();
00194   CPPUNIT_ASSERT_DOUBLES_EQUAL(14.608271629457059,expr15.evaluate(),1e-13);
00195   INTERP_KERNEL::ExprParser expr16("-((3.2*(sqrt((2*5.2+6.)+(1.2*1.2+3.)))))");
00196   expr16.parse();
00197   CPPUNIT_ASSERT_DOUBLES_EQUAL(-14.608271629457059,expr16.evaluate(),1e-13);
00198   INTERP_KERNEL::ExprParser expr17("(-(3.2*(sqrt((2*5.2+6.)+(1.2*1.2+3.)))))");
00199   expr17.parse();
00200   CPPUNIT_ASSERT_DOUBLES_EQUAL(-14.608271629457059,expr17.evaluate(),1e-13);
00201   INTERP_KERNEL::ExprParser expr18("((-3.2*(sqrt((2*5.2+6.)+(1.2*1.2+3.)))))");
00202   expr18.parse();
00203   CPPUNIT_ASSERT_DOUBLES_EQUAL(-14.608271629457059,expr18.evaluate(),1e-13);
00204   INTERP_KERNEL::ExprParser expr19("((3.2*(exp((6.+2*5.2)+(1.2*1.2+3.)))))");
00205   expr19.parse();
00206   CPPUNIT_ASSERT_DOUBLES_EQUAL(3596226038.1784945,expr19.evaluate(),1e-6);
00207   INTERP_KERNEL::ExprParser expr20("((3.2*(ln((2*5.2+6.)+(1.2*1.2+3.)))))");
00208   expr20.parse();
00209   CPPUNIT_ASSERT_DOUBLES_EQUAL(9.7179974940325309,expr20.evaluate(),1e-14);
00210   INTERP_KERNEL::ExprParser expr21("max(3.2,4.5)");
00211   expr21.parse();
00212   CPPUNIT_ASSERT_DOUBLES_EQUAL(4.5,expr21.evaluate(),1e-14);
00213   INTERP_KERNEL::ExprParser expr22("3.*max(((3.2*(ln((2*5.2+6.)+(1.2*1.2+3.))))),((3.2*(exp((6.+2*5.2)+(1.2*1.2+3.))))))");
00214   expr22.parse();
00215   CPPUNIT_ASSERT_DOUBLES_EQUAL(10788678114.535484,expr22.evaluate(),1e-5);
00216   INTERP_KERNEL::ExprParser expr23("min(3.2,4.5)");
00217   expr23.parse();
00218   CPPUNIT_ASSERT_DOUBLES_EQUAL(3.2,expr23.evaluate(),1e-14);
00219 }
00220 
00221 void ExprEvalInterpTest::testInterpreter2()
00222 {
00223   INTERP_KERNEL::ExprParser expr1("3.5*x+x*x*x/(2+x)+2*5*y");
00224   expr1.parse();
00225   std::set<std::string> res,expected;
00226   expr1.getSetOfVars(res);
00227   CPPUNIT_ASSERT_EQUAL(2,(int)res.size());
00228   expected.insert("x"); expected.insert("y");
00229   CPPUNIT_ASSERT(std::equal(res.begin(),res.end(),expected.begin()));
00230   double xyValue[2]={1.,3.};
00231   double res1;
00232   std::vector<std::string> vars; vars.push_back("x"); vars.push_back("y");
00233   expr1.prepareExprEvaluation(vars);
00234   expr1.evaluateExpr(1,xyValue,&res1);
00235   CPPUNIT_ASSERT_DOUBLES_EQUAL(33.833333333333336,res1,1e-13);
00236   xyValue[0]=-2.;
00237   CPPUNIT_ASSERT_THROW(expr1.evaluateExpr(1,xyValue,&res1),INTERP_KERNEL::Exception);
00238   double res2[2];
00239   xyValue[0]=1.;
00240   expr1.evaluateExpr(2,xyValue,res2);
00241   CPPUNIT_ASSERT_DOUBLES_EQUAL(33.833333333333336,res2[0],1e-13);
00242   CPPUNIT_ASSERT_DOUBLES_EQUAL(33.833333333333336,res2[1],1e-13);
00243   INTERP_KERNEL::ExprParser expr2("3.5*tan(2.3*x)*IVec+(cos(1.2+y/x)*JVec)");
00244   expr2.parse();
00245   res.clear(); expected.clear();
00246   expr2.getSetOfVars(res);
00247   CPPUNIT_ASSERT_EQUAL(4,(int)res.size());
00248   expected.insert("x"); expected.insert("y"); expected.insert("IVec"); expected.insert("JVec");
00249   CPPUNIT_ASSERT(std::equal(res.begin(),res.end(),expected.begin()));
00250   expr2.prepareExprEvaluation(vars);
00251   expr2.evaluateExpr(2,xyValue,res2);
00252   CPPUNIT_ASSERT_DOUBLES_EQUAL(-3.9172477460694637,res2[0],1e-14);
00253   CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.49026082134069943,res2[1],1e-14);
00254   INTERP_KERNEL::ExprParser expr3("3.5*u+u^2.4+2.");
00255   expr3.parse();
00256   expr3.prepareExprEvaluationVec();
00257   expr3.evaluateExpr(2,xyValue,res2);
00258   CPPUNIT_ASSERT_DOUBLES_EQUAL(6.5,res2[0],1e-14);
00259   CPPUNIT_ASSERT_DOUBLES_EQUAL(26.466610165238237,res2[1],1e-14);
00260   INTERP_KERNEL::ExprParser expr4("3.5*v+u^2.4+2.");
00261   expr4.parse();
00262   CPPUNIT_ASSERT_THROW(expr4.prepareExprEvaluationVec(),INTERP_KERNEL::Exception);
00263 }
00264 
00265 void ExprEvalInterpTest::testInterpreterUnit0()
00266 {
00267   INTERP_KERNEL::ExprParser expr1("kg");
00268   expr1.parse();
00269   INTERP_KERNEL::DecompositionInUnitBase unit=expr1.evaluateUnit();
00270   CPPUNIT_ASSERT(unit.isEqual(1,0,0,0,0,0.,1000.));
00271   INTERP_KERNEL::ExprParser expr2("kgT");
00272   expr2.parse();
00273   CPPUNIT_ASSERT_THROW(expr2.evaluateUnit(),INTERP_KERNEL::Exception);
00274   INTERP_KERNEL::ExprParser expr3("g");
00275   expr3.parse();
00276   unit=expr3.evaluateUnit();
00277   CPPUNIT_ASSERT(unit.isEqual(1,0,0,0,0,0.,1.));
00278   INTERP_KERNEL::ExprParser expr4("g*m");
00279   expr4.parse();
00280   unit=expr4.evaluateUnit();
00281   CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,0,0.,1.));
00282   INTERP_KERNEL::ExprParser expr5("g*m/K");
00283   expr5.parse();
00284   unit=expr5.evaluateUnit();
00285   CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,-1,0.,1.));
00286   INTERP_KERNEL::ExprParser expr6("g*m/K^2");
00287   expr6.parse();
00288   unit=expr6.evaluateUnit();
00289   CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,-2,0.,1.));
00290   INTERP_KERNEL::ExprParser expr7("g/K^2*m");
00291   expr7.parse();
00292   unit=expr7.evaluateUnit();
00293   CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,-2,0.,1.));
00294   INTERP_KERNEL::ExprParser expr8("g/(K^2*m)");
00295   expr8.parse();
00296   unit=expr8.evaluateUnit();
00297   CPPUNIT_ASSERT(unit.isEqual(1,-1,0,0,-2,0.,1.));
00298   INTERP_KERNEL::ExprParser expr9("km/h");
00299   expr9.parse();
00300   unit=expr9.evaluateUnit();
00301   CPPUNIT_ASSERT(unit.isEqual(0,1,-1,0,0,0.,0.27777777777777779));
00302   INTERP_KERNEL::ExprParser expr10("m/s");
00303   expr10.parse();
00304   unit=expr10.evaluateUnit();
00305   CPPUNIT_ASSERT(unit.isEqual(0,1,-1,0,0,0.,1.));
00306   INTERP_KERNEL::ExprParser expr11("m+s");
00307   expr11.parse();
00308   CPPUNIT_ASSERT_THROW(expr11.evaluateUnit(),INTERP_KERNEL::Exception);
00309   INTERP_KERNEL::ExprParser expr12("m-m");
00310   expr12.parse();
00311   CPPUNIT_ASSERT_THROW(expr12.evaluateUnit(),INTERP_KERNEL::Exception);
00312   const char expr13C[3]={-0x50,0x43,0x0};
00313   INTERP_KERNEL::ExprParser expr13(expr13C);
00314   expr13.parse();
00315   unit=expr13.evaluateUnit();
00316   CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,273.15,1.));
00317   const char expr14C[4]={-0x3E,-0x50,0x43,0x0};
00318   INTERP_KERNEL::ExprParser expr14(expr14C);
00319   expr14.parse();
00320   unit=expr14.evaluateUnit();
00321   CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,273.15,1.));
00322   INTERP_KERNEL::ExprParser expr15("kN/kg");
00323   expr15.parse();
00324   unit=expr15.evaluateUnit();
00325   CPPUNIT_ASSERT(unit.isEqual(0,1,-2,0,0,0.,1000.));
00326   INTERP_KERNEL::ExprParser expr16("cm");
00327   expr16.parse();
00328   unit=expr16.evaluateUnit();
00329   CPPUNIT_ASSERT(unit.isEqual(0,1,0,0,0,0.,0.01));
00330   INTERP_KERNEL::ExprParser expr17("m");
00331   expr17.parse();
00332   unit=expr17.evaluateUnit();
00333   CPPUNIT_ASSERT(unit.isEqual(0,1,0,0,0,0.,1));
00334   const char expr18C[3]={-0x08,0x43,0x0};
00335   INTERP_KERNEL::ExprParser expr18(expr18C);
00336   expr18.parse();
00337   unit=expr18.evaluateUnit();
00338   CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,273.15,1.));
00339   const char expr19C[6]={-0x50,0x43,0x2F,-0x50,0x43,0x0};
00340   INTERP_KERNEL::ExprParser expr19(expr19C);
00341   expr19.parse();
00342   unit=expr19.evaluateUnit();
00343   CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,0,0.,1.));
00344   const char expr20C[9]={-0x50,0x43,0x2A,-0x50,0x43,0x2F,-0x50,0x43,0x0};
00345   INTERP_KERNEL::ExprParser expr20(expr20C);
00346   expr20.parse();
00347   unit=expr20.evaluateUnit();
00348   CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,0.,1.));
00349 }
00350 
00351 void ExprEvalInterpTest::testInterpreterUnit1()
00352 {
00353   INTERP_KERNEL::Unit unit1("m/s");
00354   INTERP_KERNEL::Unit unit2("km/h");
00355   CPPUNIT_ASSERT(unit1.isCompatibleWith(unit2) && unit2.isCompatibleWith(unit1));
00356   CPPUNIT_ASSERT_DOUBLES_EQUAL(360,unit1.convert(unit2,100.),1e-10);
00357   INTERP_KERNEL::Unit unit3("J/s");
00358   INTERP_KERNEL::Unit unit4("kW");
00359   CPPUNIT_ASSERT(unit3.isCompatibleWith(unit4) && unit4.isCompatibleWith(unit3));
00360   CPPUNIT_ASSERT_DOUBLES_EQUAL(1.,unit3.convert(unit4,1000.),1e-10);
00361   CPPUNIT_ASSERT(unit4.getCoarseRepr()=="kW");
00362   INTERP_KERNEL::Unit unit5("kpT");
00363   CPPUNIT_ASSERT(!unit5.isInterpretationOK());
00364   CPPUNIT_ASSERT(unit5.getCoarseRepr()=="kpT");
00365   INTERP_KERNEL::Unit unit6("m*kpT");
00366   CPPUNIT_ASSERT(!unit6.isInterpretationOK());
00367   INTERP_KERNEL::Unit unit7("m*s^-1");
00368   CPPUNIT_ASSERT(unit7.isCompatibleWith(unit2) && unit2.isCompatibleWith(unit7));
00369   CPPUNIT_ASSERT_DOUBLES_EQUAL(360,unit7.convert(unit2,100.),1e-10);
00370   const char unit8C[3]={-0x50,0x43,0x0};
00371   INTERP_KERNEL::Unit unit8(unit8C);
00372   INTERP_KERNEL::Unit unit9("K");
00373   CPPUNIT_ASSERT(unit9.isCompatibleWith(unit8) && unit8.isCompatibleWith(unit9));
00374   CPPUNIT_ASSERT_DOUBLES_EQUAL(335.15,unit8.convert(unit9,62.),1e-10);
00375   CPPUNIT_ASSERT_DOUBLES_EQUAL(-16.37,unit9.convert(unit8,256.78),1e-10);
00376   INTERP_KERNEL::Unit unit10("m");
00377   INTERP_KERNEL::Unit unit11("cm");
00378   CPPUNIT_ASSERT(unit10.isCompatibleWith(unit11) && unit11.isCompatibleWith(unit10));
00379   CPPUNIT_ASSERT_DOUBLES_EQUAL(6200.,unit10.convert(unit11,62.),1e-8);
00380   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.62,unit11.convert(unit10,62.),1e-15);
00381   INTERP_KERNEL::Unit unit12("m-m");
00382   CPPUNIT_ASSERT(!unit12.isInterpretationOK());
00383 }
00384 
00385 void ExprEvalInterpTest::testInterpreter3()
00386 {
00387   std::set<std::string> res;
00388   double input[3];
00389   double res2[3];
00390   INTERP_KERNEL::ExprParser expr1("2.3+x>5.");
00391   expr1.parse();
00392   expr1.getSetOfVars(res);
00393   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
00394   CPPUNIT_ASSERT(*(res.begin())=="x");
00395   expr1.prepareExprEvaluationVec();
00396   input[0]=0.;
00397   expr1.evaluateExpr(1,input,res2);
00398   CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
00399   input[0]=2.8;
00400   expr1.evaluateExpr(1,input,res2);
00401   CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
00402   input[0]=2.6;
00403   expr1.evaluateExpr(1,input,res2);
00404   CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
00405   //
00406   INTERP_KERNEL::ExprParser expr2("2.3+x<5.");
00407   expr2.parse();
00408   res.clear();
00409   expr2.getSetOfVars(res);
00410   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
00411   CPPUNIT_ASSERT(*(res.begin())=="x");
00412   expr2.prepareExprEvaluationVec();
00413   input[0]=0.;
00414   expr2.evaluateExpr(1,input,res2);
00415   CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
00416   input[0]=2.8;
00417   expr2.evaluateExpr(1,input,res2);
00418   CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
00419   input[0]=2.6;
00420   expr2.evaluateExpr(1,input,res2);
00421   CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
00422   //
00423   INTERP_KERNEL::ExprParser expr3("if(2.3+x<5.,2+3*x,3+x/2)");
00424   expr3.parse();
00425   res.clear();
00426   expr3.getSetOfVars(res);
00427   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
00428   CPPUNIT_ASSERT(*(res.begin())=="x");
00429   expr3.prepareExprEvaluationVec();
00430   input[0]=0.;
00431   expr3.evaluateExpr(1,input,res2);
00432   CPPUNIT_ASSERT_DOUBLES_EQUAL(2.,res2[0],1e-12);
00433   input[0]=2.8;
00434   expr3.evaluateExpr(1,input,res2);
00435   CPPUNIT_ASSERT_DOUBLES_EQUAL(4.4,res2[0],1e-12);
00436   input[0]=2.6;
00437   expr3.evaluateExpr(1,input,res2);
00438   CPPUNIT_ASSERT_DOUBLES_EQUAL(9.8,res2[0],1e-12);
00439   //
00440   INTERP_KERNEL::ExprParser expr4("if(x>1000,2*x,x/3)");
00441   expr4.parse();
00442   res.clear();
00443   expr4.getSetOfVars(res);
00444   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
00445   CPPUNIT_ASSERT(*(res.begin())=="x");
00446   expr4.prepareExprEvaluationVec();
00447   input[0]=2.7;
00448   expr4.evaluateExpr(1,input,res2);
00449   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.9,res2[0],1e-12);
00450   input[0]=999.;
00451   expr4.evaluateExpr(1,input,res2);
00452   CPPUNIT_ASSERT_DOUBLES_EQUAL(333.,res2[0],1e-12);
00453   input[0]=1002.;
00454   expr4.evaluateExpr(1,input,res2);
00455   CPPUNIT_ASSERT_DOUBLES_EQUAL(2004.,res2[0],1e-12);
00456   //
00457   INTERP_KERNEL::ExprParser expr5("4.4*x*log10(x)*10");
00458   expr5.parse();
00459   res.clear();
00460   expr5.getSetOfVars(res);
00461   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
00462   CPPUNIT_ASSERT(*(res.begin())=="x");
00463   expr5.prepareExprEvaluationVec();
00464   input[0]=273.15;
00465   expr5.evaluateExpr(1,input,res2);
00466   CPPUNIT_ASSERT_DOUBLES_EQUAL(29282.131520617437,res2[0],1e-9);
00467   input[0]=0.;
00468   CPPUNIT_ASSERT_THROW(expr5.evaluateExpr(1,input,res2),INTERP_KERNEL::Exception);
00469 }
00470 
00474 void ExprEvalInterpTest::testInterpreter4()
00475 {
00476   INTERP_KERNEL::ExprParser expr("2*x + 1");
00477   double vals[3]={0.1,0.2,0.3};
00478   std::vector<std::string> varsV(3);
00479   varsV[0] = "x";
00480   varsV[1] = "y";
00481   varsV[2] = "z";
00482   expr.parse();
00483   expr.prepareExprEvaluation(varsV);
00484   double result;
00485   expr.evaluateExpr(1,vals, &result);
00486   CPPUNIT_ASSERT_DOUBLES_EQUAL(1.2,result,1e-12);
00487 }
00488 
00492 void ExprEvalInterpTest::testInterpreter5()
00493 {
00494   std::set<std::string> res;
00495   double input[3];
00496   double res2[3];
00497   INTERP_KERNEL::ExprParser expr1("1.85e-3*x");
00498   expr1.parse();
00499   expr1.getSetOfVars(res);
00500   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
00501   CPPUNIT_ASSERT(*(res.begin())=="x");
00502   input[0]=56.7;
00503   expr1.prepareExprEvaluationVec();
00504   expr1.evaluateExpr(1,input,res2);
00505   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.104895,res2[0],1e-12);
00506   input[0]=-65.7;
00507   expr1.evaluateExpr(1,input,res2);
00508   CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.121545,res2[0],1e-12);
00509   //
00510   INTERP_KERNEL::ExprParser expr2("x*1.85e-3");
00511   expr2.parse();
00512   expr2.getSetOfVars(res);
00513   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
00514   CPPUNIT_ASSERT(*(res.begin())=="x");
00515   input[0]=56.7;
00516   expr2.prepareExprEvaluationVec();
00517   expr2.evaluateExpr(1,input,res2);
00518   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.104895,res2[0],1e-12);
00519   input[0]=-65.7;
00520   expr2.evaluateExpr(1,input,res2);
00521   CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.121545,res2[0],1e-12);
00522   //
00523   INTERP_KERNEL::ExprParser expr3("2.6E+1+x*1.85e-3");
00524   expr3.parse();
00525   expr3.getSetOfVars(res);
00526   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
00527   CPPUNIT_ASSERT(*(res.begin())=="x");
00528   input[0]=56.7;
00529   expr3.prepareExprEvaluationVec();
00530   expr3.evaluateExpr(1,input,res2);
00531   CPPUNIT_ASSERT_DOUBLES_EQUAL(26.104895,res2[0],1e-12);
00532   input[0]=-65.7;
00533   expr3.evaluateExpr(1,input,res2);
00534   CPPUNIT_ASSERT_DOUBLES_EQUAL(25.878455,res2[0],1e-12);
00535   //
00536   INTERP_KERNEL::ExprParser expr4("3.*max(((3.2e+1*(ln((2*5.2E-02+6.)+(1.2E-001*1.2E+2+3e-4))))),((3.2E-2*(exp((6e-1+2*5.2e-2)+(1.2E001*1.2+3.))))))");
00537   expr4.parse();
00538   CPPUNIT_ASSERT_DOUBLES_EQUAL(6994207.8359543988,expr4.evaluate(),1e-5);
00539 }