]> SALOME platform Git repositories - modules/med.git/blob - src/INTERP_KERNELTest/ExprEvalInterpTest.cxx
Salome HOME
Merge from V6_main_20120808 08Aug12
[modules/med.git] / src / INTERP_KERNELTest / ExprEvalInterpTest.cxx
1 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "ExprEvalInterpTest.hxx"
21 #include "InterpKernelExprParser.hxx"
22
23 #include <limits>
24 #include <iterator>
25
26 using namespace INTERP_TEST;
27
28 void ExprEvalInterpTest::testBuildStringFromFortran()
29 {
30   char toto1[]="123456  ";
31   char result[]="123456";
32   std::string titi;
33   titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto1,8);
34   CPPUNIT_ASSERT_EQUAL(6,(int)titi.length());
35   CPPUNIT_ASSERT(titi==result);
36   //
37   char toto2[]=" 23456  ";
38   char result2[]=" 23456";
39   titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto2,8);
40   CPPUNIT_ASSERT(titi==result2);
41   CPPUNIT_ASSERT_EQUAL(6,(int)titi.length());
42   //
43   char toto3[]="  3456  ";
44   char result3[]="  3456";
45   titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto3,8);
46   CPPUNIT_ASSERT(titi==result3);
47   CPPUNIT_ASSERT_EQUAL(6,(int)titi.length());
48   //
49   char toto4[]="        ";
50   titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto4,8);
51   CPPUNIT_ASSERT_EQUAL(0,(int)titi.length());
52   //
53   char toto5[]="  345677";
54   titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto5,8);
55   CPPUNIT_ASSERT(titi==toto5);
56   CPPUNIT_ASSERT_EQUAL(8,(int)titi.length());
57 }
58
59 void ExprEvalInterpTest::testDeleteWhiteSpaces()
60 {
61   char toto[]=" jkhjkh ooooppp l ";
62   char result[]="jkhjkhoooopppl";
63   std::string totoS(toto);
64   std::string totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
65   CPPUNIT_ASSERT(totoR==result);
66   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
67   //
68   char toto2[]=" jkhjkh     ooooppp    l ";
69   totoS=toto2;
70   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
71   CPPUNIT_ASSERT(totoR==result);
72   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
73   //
74   char toto3[]=" jkhjkh     oooo pppl ";
75   totoS=toto3;
76   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
77   CPPUNIT_ASSERT(totoR==result);
78   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
79   //
80   char toto4[]=" jkhjkh     oooo pppl";
81   totoS=toto4;
82   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
83   CPPUNIT_ASSERT(totoR==result);
84   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
85   //
86   char toto5[]="jkhjkh     oooo pppl";
87   totoS=toto5;
88   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
89   CPPUNIT_ASSERT(totoR==result);
90   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
91   //
92   totoS=result;
93   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
94   CPPUNIT_ASSERT(totoR==result);
95   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
96   //
97   char toto6[]="j k h j k h o o o o p p p l";
98   totoS=toto6;
99   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
100   CPPUNIT_ASSERT(totoR==result);
101   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
102   //
103   char toto7[]="j  k  h j    k h   o  o  o  o  p  pp    l";
104   totoS=toto7;
105   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
106   CPPUNIT_ASSERT(totoR==result);
107   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
108   //
109   char toto8[]="           ";
110   totoS=toto8;
111   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
112   CPPUNIT_ASSERT(totoR.empty());
113   //
114   char toto9[]="";
115   totoS=toto9;
116   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
117   CPPUNIT_ASSERT(totoR.empty());
118   //
119   char toto10[]="j\n k \nh\nj \n\n  k\nh \n o \no\n o\n o \np\n\npp \n\n l";
120   totoS=toto10;
121   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
122   CPPUNIT_ASSERT(totoR==result);
123   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
124 }
125
126 void ExprEvalInterpTest::testInterpreter0()
127 {
128   INTERP_KERNEL::ExprParser expr1("3*-2");
129   expr1.parse();
130   CPPUNIT_ASSERT_DOUBLES_EQUAL(-6.,expr1.evaluate(),1e-15);
131   INTERP_KERNEL::ExprParser expr2("-2.3");
132   expr2.parse();
133   CPPUNIT_ASSERT_DOUBLES_EQUAL(-2.3,expr2.evaluate(),1e-15);
134   INTERP_KERNEL::ExprParser expr3("--2.3");
135   expr3.parse();
136   CPPUNIT_ASSERT_DOUBLES_EQUAL(2.3,expr3.evaluate(),1e-15);
137   INTERP_KERNEL::ExprParser expr4("-++2.3");
138   expr4.parse();
139   CPPUNIT_ASSERT_DOUBLES_EQUAL(-2.3,expr4.evaluate(),1e-15);
140   INTERP_KERNEL::ExprParser expr5("+2.3");
141   expr5.parse();
142   CPPUNIT_ASSERT_DOUBLES_EQUAL(2.3,expr5.evaluate(),1e-15);
143   INTERP_KERNEL::ExprParser expr6("3^-1");
144   expr6.parse();
145   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.33333333333333333,expr6.evaluate(),1e-15);
146 }
147
148 void ExprEvalInterpTest::testInterpreter1()
149 {
150   INTERP_KERNEL::ExprParser expr1("3+2*5");
151   expr1.parse();
152   CPPUNIT_ASSERT_DOUBLES_EQUAL(13.,expr1.evaluate(),1e-14);
153   INTERP_KERNEL::ExprParser expr2("3+2^3*5");
154   expr2.parse();
155   CPPUNIT_ASSERT_DOUBLES_EQUAL(43.,expr2.evaluate(),1e-14);
156   INTERP_KERNEL::ExprParser expr3("3+2^(2*5)");
157   expr3.parse();
158   CPPUNIT_ASSERT_DOUBLES_EQUAL(1027.,expr3.evaluate(),1e-14);
159   INTERP_KERNEL::ExprParser expr4("(3.2+4.3)*(1.3+2.3*7.8)");
160   expr4.parse();
161   CPPUNIT_ASSERT_DOUBLES_EQUAL(144.3,expr4.evaluate(),1e-10);
162   INTERP_KERNEL::ExprParser expr5("(3.2+4.3)*cos(1.3+2.3*7.8)");
163   expr5.parse();
164   CPPUNIT_ASSERT_DOUBLES_EQUAL(6.9355510138337619,expr5.evaluate(),1e-14);
165   INTERP_KERNEL::ExprParser expr6("3+2-4-7+4.3");
166   expr6.parse();
167   CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.7,expr6.evaluate(),1e-14);
168   INTERP_KERNEL::ExprParser expr7("3.2*4.5/3.3/2.2");
169   expr7.parse();
170   CPPUNIT_ASSERT_DOUBLES_EQUAL(1.9834710743801653,expr7.evaluate(),1e-14);
171   INTERP_KERNEL::ExprParser expr8("3.2*4.5/3.3/2.2");
172   expr8.parse();
173   CPPUNIT_ASSERT_DOUBLES_EQUAL(1.9834710743801653,expr8.evaluate(),1e-14);
174   INTERP_KERNEL::ExprParser expr9("(((1.23456789)))");
175   expr9.parse();
176   CPPUNIT_ASSERT_DOUBLES_EQUAL(1.23456789,expr9.evaluate(),1e-14);
177   INTERP_KERNEL::ExprParser expr10("3.2*((2*5.2+6.)+(1.2*1.2+3.))");
178   expr10.parse();
179   CPPUNIT_ASSERT_DOUBLES_EQUAL(66.688,expr10.evaluate(),1e-13);
180   INTERP_KERNEL::ExprParser expr11("((3.2*(((2*5.2+6.)+(1.2*1.2+3.)))))");
181   expr11.parse();
182   CPPUNIT_ASSERT_DOUBLES_EQUAL(66.688,expr11.evaluate(),1e-13);
183   INTERP_KERNEL::ExprParser expr12("((3.2*(cos((2*5.2+6.)+(1.2*1.2+3.)))))");
184   expr12.parse();
185   CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.3038041398761016,expr12.evaluate(),1e-14);
186   INTERP_KERNEL::ExprParser expr13("((3.2*(sin((2*5.2+6.)+(1.2*1.2+3.)))))");
187   expr13.parse();
188   CPPUNIT_ASSERT_DOUBLES_EQUAL(2.9223440531261784,expr13.evaluate(),1e-14);
189   INTERP_KERNEL::ExprParser expr14("((3.2*(tan((2*5.2+6.)+(1.2*1.2+3.)))))");
190   expr14.parse();
191   CPPUNIT_ASSERT_DOUBLES_EQUAL(-7.1724737512280257,expr14.evaluate(),1e-14);
192   INTERP_KERNEL::ExprParser expr15("((3.2*(sqrt((2*5.2+6.)+(1.2*1.2+3.)))))");
193   expr15.parse();
194   CPPUNIT_ASSERT_DOUBLES_EQUAL(14.608271629457059,expr15.evaluate(),1e-13);
195   INTERP_KERNEL::ExprParser expr16("-((3.2*(sqrt((2*5.2+6.)+(1.2*1.2+3.)))))");
196   expr16.parse();
197   CPPUNIT_ASSERT_DOUBLES_EQUAL(-14.608271629457059,expr16.evaluate(),1e-13);
198   INTERP_KERNEL::ExprParser expr17("(-(3.2*(sqrt((2*5.2+6.)+(1.2*1.2+3.)))))");
199   expr17.parse();
200   CPPUNIT_ASSERT_DOUBLES_EQUAL(-14.608271629457059,expr17.evaluate(),1e-13);
201   INTERP_KERNEL::ExprParser expr18("((-3.2*(sqrt((2*5.2+6.)+(1.2*1.2+3.)))))");
202   expr18.parse();
203   CPPUNIT_ASSERT_DOUBLES_EQUAL(-14.608271629457059,expr18.evaluate(),1e-13);
204   INTERP_KERNEL::ExprParser expr19("((3.2*(exp((6.+2*5.2)+(1.2*1.2+3.)))))");
205   expr19.parse();
206   CPPUNIT_ASSERT_DOUBLES_EQUAL(3596226038.1784945,expr19.evaluate(),1e-6);
207   INTERP_KERNEL::ExprParser expr20("((3.2*(ln((2*5.2+6.)+(1.2*1.2+3.)))))");
208   expr20.parse();
209   CPPUNIT_ASSERT_DOUBLES_EQUAL(9.7179974940325309,expr20.evaluate(),1e-14);
210   INTERP_KERNEL::ExprParser expr21("max(3.2,4.5)");
211   expr21.parse();
212   CPPUNIT_ASSERT_DOUBLES_EQUAL(4.5,expr21.evaluate(),1e-14);
213   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.))))))");
214   expr22.parse();
215   CPPUNIT_ASSERT_DOUBLES_EQUAL(10788678114.535484,expr22.evaluate(),1e-5);
216   INTERP_KERNEL::ExprParser expr23("min(3.2,4.5)");
217   expr23.parse();
218   CPPUNIT_ASSERT_DOUBLES_EQUAL(3.2,expr23.evaluate(),1e-14);
219 }
220
221 void ExprEvalInterpTest::testInterpreter2()
222 {
223   INTERP_KERNEL::ExprParser expr1("3.5*x+x*x*x/(2+x)+2*5*y");
224   expr1.parse();
225   std::set<std::string> res,expected;
226   expr1.getSetOfVars(res);
227   CPPUNIT_ASSERT_EQUAL(2,(int)res.size());
228   expected.insert("x"); expected.insert("y");
229   CPPUNIT_ASSERT(std::equal(res.begin(),res.end(),expected.begin()));
230   double xyValue[2]={1.,3.};
231   double res1;
232   std::vector<std::string> vars; vars.push_back("x"); vars.push_back("y");
233   expr1.prepareExprEvaluation(vars,2,1);
234   expr1.evaluateExpr(1,xyValue,&res1);
235   CPPUNIT_ASSERT_DOUBLES_EQUAL(33.833333333333336,res1,1e-13);
236   xyValue[0]=-2.;
237   CPPUNIT_ASSERT_THROW(expr1.evaluateExpr(1,xyValue,&res1),INTERP_KERNEL::Exception);
238   double res2[2];
239   xyValue[0]=1.;
240   expr1.evaluateExpr(2,xyValue,res2);
241   CPPUNIT_ASSERT_DOUBLES_EQUAL(33.833333333333336,res2[0],1e-13);
242   CPPUNIT_ASSERT_DOUBLES_EQUAL(33.833333333333336,res2[1],1e-13);
243   INTERP_KERNEL::ExprParser expr2("3.5*tan(2.3*x)*IVec+(cos(1.2+y/x)*JVec)");
244   expr2.parse();
245   res.clear(); expected.clear();
246   expr2.getSetOfVars(res);
247   CPPUNIT_ASSERT_EQUAL(4,(int)res.size());
248   expected.insert("x"); expected.insert("y"); expected.insert("IVec"); expected.insert("JVec");
249   CPPUNIT_ASSERT(std::equal(res.begin(),res.end(),expected.begin()));
250   expr2.prepareExprEvaluation(vars,2,2);
251   expr2.evaluateExpr(2,xyValue,res2);
252   CPPUNIT_ASSERT_DOUBLES_EQUAL(-3.9172477460694637,res2[0],1e-14);
253   CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.49026082134069943,res2[1],1e-14);
254   INTERP_KERNEL::ExprParser expr3("3.5*u+u^2.4+2.");
255   expr3.parse();
256   expr3.prepareExprEvaluationVec();
257   expr3.evaluateExpr(2,xyValue,res2);
258   CPPUNIT_ASSERT_DOUBLES_EQUAL(6.5,res2[0],1e-14);
259   CPPUNIT_ASSERT_DOUBLES_EQUAL(26.466610165238237,res2[1],1e-14);
260   INTERP_KERNEL::ExprParser expr4("3.5*v+u^2.4+2.");
261   expr4.parse();
262   CPPUNIT_ASSERT_THROW(expr4.prepareExprEvaluationVec(),INTERP_KERNEL::Exception);
263 }
264
265 void ExprEvalInterpTest::testInterpreterUnit0()
266 {
267   INTERP_KERNEL::ExprParser expr1("kg");
268   expr1.parse();
269   INTERP_KERNEL::DecompositionInUnitBase unit=expr1.evaluateUnit();
270   CPPUNIT_ASSERT(unit.isEqual(1,0,0,0,0,0.,1000.));
271   INTERP_KERNEL::ExprParser expr2("kgT");
272   expr2.parse();
273   CPPUNIT_ASSERT_THROW(expr2.evaluateUnit(),INTERP_KERNEL::Exception);
274   INTERP_KERNEL::ExprParser expr3("g");
275   expr3.parse();
276   unit=expr3.evaluateUnit();
277   CPPUNIT_ASSERT(unit.isEqual(1,0,0,0,0,0.,1.));
278   INTERP_KERNEL::ExprParser expr4("g*m");
279   expr4.parse();
280   unit=expr4.evaluateUnit();
281   CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,0,0.,1.));
282   INTERP_KERNEL::ExprParser expr5("g*m/K");
283   expr5.parse();
284   unit=expr5.evaluateUnit();
285   CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,-1,0.,1.));
286   INTERP_KERNEL::ExprParser expr6("g*m/K^2");
287   expr6.parse();
288   unit=expr6.evaluateUnit();
289   CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,-2,0.,1.));
290   INTERP_KERNEL::ExprParser expr7("g/K^2*m");
291   expr7.parse();
292   unit=expr7.evaluateUnit();
293   CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,-2,0.,1.));
294   INTERP_KERNEL::ExprParser expr8("g/(K^2*m)");
295   expr8.parse();
296   unit=expr8.evaluateUnit();
297   CPPUNIT_ASSERT(unit.isEqual(1,-1,0,0,-2,0.,1.));
298   INTERP_KERNEL::ExprParser expr9("km/h");
299   expr9.parse();
300   unit=expr9.evaluateUnit();
301   CPPUNIT_ASSERT(unit.isEqual(0,1,-1,0,0,0.,0.27777777777777779));
302   INTERP_KERNEL::ExprParser expr10("m/s");
303   expr10.parse();
304   unit=expr10.evaluateUnit();
305   CPPUNIT_ASSERT(unit.isEqual(0,1,-1,0,0,0.,1.));
306   INTERP_KERNEL::ExprParser expr11("m+s");
307   expr11.parse();
308   CPPUNIT_ASSERT_THROW(expr11.evaluateUnit(),INTERP_KERNEL::Exception);
309   INTERP_KERNEL::ExprParser expr12("m-m");
310   expr12.parse();
311   CPPUNIT_ASSERT_THROW(expr12.evaluateUnit(),INTERP_KERNEL::Exception);
312   const char expr13C[3]={-0x50,0x43,0x0};
313   INTERP_KERNEL::ExprParser expr13(expr13C);
314   expr13.parse();
315   unit=expr13.evaluateUnit();
316   CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,273.15,1.));
317   const char expr14C[4]={-0x3E,-0x50,0x43,0x0};
318   INTERP_KERNEL::ExprParser expr14(expr14C);
319   expr14.parse();
320   unit=expr14.evaluateUnit();
321   CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,273.15,1.));
322   INTERP_KERNEL::ExprParser expr15("kN/kg");
323   expr15.parse();
324   unit=expr15.evaluateUnit();
325   CPPUNIT_ASSERT(unit.isEqual(0,1,-2,0,0,0.,1000.));
326   INTERP_KERNEL::ExprParser expr16("cm");
327   expr16.parse();
328   unit=expr16.evaluateUnit();
329   CPPUNIT_ASSERT(unit.isEqual(0,1,0,0,0,0.,0.01));
330   INTERP_KERNEL::ExprParser expr17("m");
331   expr17.parse();
332   unit=expr17.evaluateUnit();
333   CPPUNIT_ASSERT(unit.isEqual(0,1,0,0,0,0.,1));
334   const char expr18C[3]={-0x08,0x43,0x0};
335   INTERP_KERNEL::ExprParser expr18(expr18C);
336   expr18.parse();
337   unit=expr18.evaluateUnit();
338   CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,273.15,1.));
339   const char expr19C[6]={-0x50,0x43,0x2F,-0x50,0x43,0x0};
340   INTERP_KERNEL::ExprParser expr19(expr19C);
341   expr19.parse();
342   unit=expr19.evaluateUnit();
343   CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,0,0.,1.));
344   const char expr20C[9]={-0x50,0x43,0x2A,-0x50,0x43,0x2F,-0x50,0x43,0x0};
345   INTERP_KERNEL::ExprParser expr20(expr20C);
346   expr20.parse();
347   unit=expr20.evaluateUnit();
348   CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,0.,1.));
349 }
350
351 void ExprEvalInterpTest::testInterpreterUnit1()
352 {
353   INTERP_KERNEL::Unit unit1("m/s");
354   INTERP_KERNEL::Unit unit2("km/h");
355   CPPUNIT_ASSERT(unit1.isCompatibleWith(unit2) && unit2.isCompatibleWith(unit1));
356   CPPUNIT_ASSERT_DOUBLES_EQUAL(360,unit1.convert(unit2,100.),1e-10);
357   INTERP_KERNEL::Unit unit3("J/s");
358   INTERP_KERNEL::Unit unit4("kW");
359   CPPUNIT_ASSERT(unit3.isCompatibleWith(unit4) && unit4.isCompatibleWith(unit3));
360   CPPUNIT_ASSERT_DOUBLES_EQUAL(1.,unit3.convert(unit4,1000.),1e-10);
361   CPPUNIT_ASSERT(unit4.getCoarseRepr()=="kW");
362   INTERP_KERNEL::Unit unit5("kpT");
363   CPPUNIT_ASSERT(!unit5.isInterpretationOK());
364   CPPUNIT_ASSERT(unit5.getCoarseRepr()=="kpT");
365   INTERP_KERNEL::Unit unit6("m*kpT");
366   CPPUNIT_ASSERT(!unit6.isInterpretationOK());
367   INTERP_KERNEL::Unit unit7("m*s^-1");
368   CPPUNIT_ASSERT(unit7.isCompatibleWith(unit2) && unit2.isCompatibleWith(unit7));
369   CPPUNIT_ASSERT_DOUBLES_EQUAL(360,unit7.convert(unit2,100.),1e-10);
370   const char unit8C[3]={-0x50,0x43,0x0};
371   INTERP_KERNEL::Unit unit8(unit8C);
372   INTERP_KERNEL::Unit unit9("K");
373   CPPUNIT_ASSERT(unit9.isCompatibleWith(unit8) && unit8.isCompatibleWith(unit9));
374   CPPUNIT_ASSERT_DOUBLES_EQUAL(335.15,unit8.convert(unit9,62.),1e-10);
375   CPPUNIT_ASSERT_DOUBLES_EQUAL(-16.37,unit9.convert(unit8,256.78),1e-10);
376   INTERP_KERNEL::Unit unit10("m");
377   INTERP_KERNEL::Unit unit11("cm");
378   CPPUNIT_ASSERT(unit10.isCompatibleWith(unit11) && unit11.isCompatibleWith(unit10));
379   CPPUNIT_ASSERT_DOUBLES_EQUAL(6200.,unit10.convert(unit11,62.),1e-8);
380   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.62,unit11.convert(unit10,62.),1e-15);
381   INTERP_KERNEL::Unit unit12("m-m");
382   CPPUNIT_ASSERT(!unit12.isInterpretationOK());
383 }
384
385 void ExprEvalInterpTest::testInterpreter3()
386 {
387   std::set<std::string> res;
388   double input[3];
389   double res2[3];
390   INTERP_KERNEL::ExprParser expr1("2.3+x>5.");
391   expr1.parse();
392   expr1.getSetOfVars(res);
393   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
394   CPPUNIT_ASSERT(*(res.begin())=="x");
395   expr1.prepareExprEvaluationVec();
396   input[0]=0.;
397   expr1.evaluateExpr(1,input,res2);
398   CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
399   input[0]=2.8;
400   expr1.evaluateExpr(1,input,res2);
401   CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
402   input[0]=2.6;
403   expr1.evaluateExpr(1,input,res2);
404   CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
405   //
406   INTERP_KERNEL::ExprParser expr2("2.3+x<5.");
407   expr2.parse();
408   res.clear();
409   expr2.getSetOfVars(res);
410   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
411   CPPUNIT_ASSERT(*(res.begin())=="x");
412   expr2.prepareExprEvaluationVec();
413   input[0]=0.;
414   expr2.evaluateExpr(1,input,res2);
415   CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
416   input[0]=2.8;
417   expr2.evaluateExpr(1,input,res2);
418   CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
419   input[0]=2.6;
420   expr2.evaluateExpr(1,input,res2);
421   CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
422   //
423   INTERP_KERNEL::ExprParser expr3("if(2.3+x<5.,2+3*x,3+x/2)");
424   expr3.parse();
425   res.clear();
426   expr3.getSetOfVars(res);
427   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
428   CPPUNIT_ASSERT(*(res.begin())=="x");
429   expr3.prepareExprEvaluationVec();
430   input[0]=0.;
431   expr3.evaluateExpr(1,input,res2);
432   CPPUNIT_ASSERT_DOUBLES_EQUAL(2.,res2[0],1e-12);
433   input[0]=2.8;
434   expr3.evaluateExpr(1,input,res2);
435   CPPUNIT_ASSERT_DOUBLES_EQUAL(4.4,res2[0],1e-12);
436   input[0]=2.6;
437   expr3.evaluateExpr(1,input,res2);
438   CPPUNIT_ASSERT_DOUBLES_EQUAL(9.8,res2[0],1e-12);
439   //
440   INTERP_KERNEL::ExprParser expr4("if(x>1000,2*x,x/3)");
441   expr4.parse();
442   res.clear();
443   expr4.getSetOfVars(res);
444   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
445   CPPUNIT_ASSERT(*(res.begin())=="x");
446   expr4.prepareExprEvaluationVec();
447   input[0]=2.7;
448   expr4.evaluateExpr(1,input,res2);
449   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.9,res2[0],1e-12);
450   input[0]=999.;
451   expr4.evaluateExpr(1,input,res2);
452   CPPUNIT_ASSERT_DOUBLES_EQUAL(333.,res2[0],1e-12);
453   input[0]=1002.;
454   expr4.evaluateExpr(1,input,res2);
455   CPPUNIT_ASSERT_DOUBLES_EQUAL(2004.,res2[0],1e-12);
456   //
457   INTERP_KERNEL::ExprParser expr5("4.4*x*log10(x)*10");
458   expr5.parse();
459   res.clear();
460   expr5.getSetOfVars(res);
461   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
462   CPPUNIT_ASSERT(*(res.begin())=="x");
463   expr5.prepareExprEvaluationVec();
464   input[0]=273.15;
465   expr5.evaluateExpr(1,input,res2);
466   CPPUNIT_ASSERT_DOUBLES_EQUAL(29282.131520617437,res2[0],1e-9);
467   input[0]=0.;
468   CPPUNIT_ASSERT_THROW(expr5.evaluateExpr(1,input,res2),INTERP_KERNEL::Exception);
469 }
470
471 /*!
472  * Bug detected by Marc concerning expressions with blanks.
473  */
474 void ExprEvalInterpTest::testInterpreter4()
475 {
476   INTERP_KERNEL::ExprParser expr("2*x + 1");
477   double vals[3]={0.1,0.2,0.3};
478   std::vector<std::string> varsV(3);
479   varsV[0] = "x";
480   varsV[1] = "y";
481   varsV[2] = "z";
482   expr.parse();
483   expr.prepareExprEvaluation(varsV,3,1);
484   double result;
485   expr.evaluateExpr(1,vals, &result);
486   CPPUNIT_ASSERT_DOUBLES_EQUAL(1.2,result,1e-12);
487 }
488
489 /*!
490  * Allowing scientific format for floats.
491  */
492 void ExprEvalInterpTest::testInterpreter5()
493 {
494   std::set<std::string> res;
495   double input[3];
496   double res2[3];
497   INTERP_KERNEL::ExprParser expr1("1.85e-3*x");
498   expr1.parse();
499   expr1.getSetOfVars(res);
500   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
501   CPPUNIT_ASSERT(*(res.begin())=="x");
502   input[0]=56.7;
503   expr1.prepareExprEvaluationVec();
504   expr1.evaluateExpr(1,input,res2);
505   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.104895,res2[0],1e-12);
506   input[0]=-65.7;
507   expr1.evaluateExpr(1,input,res2);
508   CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.121545,res2[0],1e-12);
509   //
510   INTERP_KERNEL::ExprParser expr2("x*1.85e-3");
511   expr2.parse();
512   expr2.getSetOfVars(res);
513   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
514   CPPUNIT_ASSERT(*(res.begin())=="x");
515   input[0]=56.7;
516   expr2.prepareExprEvaluationVec();
517   expr2.evaluateExpr(1,input,res2);
518   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.104895,res2[0],1e-12);
519   input[0]=-65.7;
520   expr2.evaluateExpr(1,input,res2);
521   CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.121545,res2[0],1e-12);
522   //
523   INTERP_KERNEL::ExprParser expr3("2.6E+1+x*1.85e-3");
524   expr3.parse();
525   expr3.getSetOfVars(res);
526   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
527   CPPUNIT_ASSERT(*(res.begin())=="x");
528   input[0]=56.7;
529   expr3.prepareExprEvaluationVec();
530   expr3.evaluateExpr(1,input,res2);
531   CPPUNIT_ASSERT_DOUBLES_EQUAL(26.104895,res2[0],1e-12);
532   input[0]=-65.7;
533   expr3.evaluateExpr(1,input,res2);
534   CPPUNIT_ASSERT_DOUBLES_EQUAL(25.878455,res2[0],1e-12);
535   //
536   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.))))))");
537   expr4.parse();
538   CPPUNIT_ASSERT_DOUBLES_EQUAL(6994207.8359543988,expr4.evaluate(),1e-5);
539 }