1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "ExprEvalInterpTest.hxx"
21 #include "InterpKernelExprParser.hxx"
26 using namespace INTERP_TEST;
28 void ExprEvalInterpTest::testBuildStringFromFortran()
30 char toto1[]="123456 ";
31 char result[]="123456";
33 titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto1,8);
34 CPPUNIT_ASSERT_EQUAL(6,(int)titi.length());
35 CPPUNIT_ASSERT(titi==result);
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());
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());
50 titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto4,8);
51 CPPUNIT_ASSERT_EQUAL(0,(int)titi.length());
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());
59 void ExprEvalInterpTest::testDeleteWhiteSpaces()
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());
68 char toto2[]=" jkhjkh ooooppp l ";
70 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
71 CPPUNIT_ASSERT(totoR==result);
72 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
74 char toto3[]=" jkhjkh oooo pppl ";
76 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
77 CPPUNIT_ASSERT(totoR==result);
78 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
80 char toto4[]=" jkhjkh oooo pppl";
82 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
83 CPPUNIT_ASSERT(totoR==result);
84 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
86 char toto5[]="jkhjkh oooo pppl";
88 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
89 CPPUNIT_ASSERT(totoR==result);
90 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
93 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
94 CPPUNIT_ASSERT(totoR==result);
95 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
97 char toto6[]="j k h j k h o o o o p p p l";
99 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
100 CPPUNIT_ASSERT(totoR==result);
101 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
103 char toto7[]="j k h j k h o o o o p pp l";
105 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
106 CPPUNIT_ASSERT(totoR==result);
107 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
111 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
112 CPPUNIT_ASSERT(totoR.empty());
116 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
117 CPPUNIT_ASSERT(totoR.empty());
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";
121 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
122 CPPUNIT_ASSERT(totoR==result);
123 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
126 void ExprEvalInterpTest::testInterpreter0()
128 INTERP_KERNEL::ExprParser expr1("3*-2");
130 CPPUNIT_ASSERT_DOUBLES_EQUAL(-6.,expr1.evaluate(),1e-15);
131 INTERP_KERNEL::ExprParser expr2("-2.3");
133 CPPUNIT_ASSERT_DOUBLES_EQUAL(-2.3,expr2.evaluate(),1e-15);
134 INTERP_KERNEL::ExprParser expr3("--2.3");
136 CPPUNIT_ASSERT_DOUBLES_EQUAL(2.3,expr3.evaluate(),1e-15);
137 INTERP_KERNEL::ExprParser expr4("-++2.3");
139 CPPUNIT_ASSERT_DOUBLES_EQUAL(-2.3,expr4.evaluate(),1e-15);
140 INTERP_KERNEL::ExprParser expr5("+2.3");
142 CPPUNIT_ASSERT_DOUBLES_EQUAL(2.3,expr5.evaluate(),1e-15);
143 INTERP_KERNEL::ExprParser expr6("3^-1");
145 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.33333333333333333,expr6.evaluate(),1e-15);
148 void ExprEvalInterpTest::testInterpreter1()
150 INTERP_KERNEL::ExprParser expr1("3+2*5");
152 CPPUNIT_ASSERT_DOUBLES_EQUAL(13.,expr1.evaluate(),1e-14);
153 INTERP_KERNEL::ExprParser expr2("3+2^3*5");
155 CPPUNIT_ASSERT_DOUBLES_EQUAL(43.,expr2.evaluate(),1e-14);
156 INTERP_KERNEL::ExprParser expr3("3+2^(2*5)");
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)");
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)");
164 CPPUNIT_ASSERT_DOUBLES_EQUAL(6.9355510138337619,expr5.evaluate(),1e-14);
165 INTERP_KERNEL::ExprParser expr6("3+2-4-7+4.3");
167 CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.7,expr6.evaluate(),1e-14);
168 INTERP_KERNEL::ExprParser expr7("3.2*4.5/3.3/2.2");
170 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.9834710743801653,expr7.evaluate(),1e-14);
171 INTERP_KERNEL::ExprParser expr8("3.2*4.5/3.3/2.2");
173 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.9834710743801653,expr8.evaluate(),1e-14);
174 INTERP_KERNEL::ExprParser expr9("(((1.23456789)))");
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.))");
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.)))))");
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.)))))");
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.)))))");
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.)))))");
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.)))))");
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.)))))");
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.)))))");
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.)))))");
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.)))))");
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.)))))");
209 CPPUNIT_ASSERT_DOUBLES_EQUAL(9.7179974940325309,expr20.evaluate(),1e-14);
210 INTERP_KERNEL::ExprParser expr21("max(3.2,4.5)");
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.))))))");
215 CPPUNIT_ASSERT_DOUBLES_EQUAL(10788678114.535484,expr22.evaluate(),1e-5);
216 INTERP_KERNEL::ExprParser expr23("min(3.2,4.5)");
218 CPPUNIT_ASSERT_DOUBLES_EQUAL(3.2,expr23.evaluate(),1e-14);
221 void ExprEvalInterpTest::testInterpreter2()
223 INTERP_KERNEL::ExprParser expr1("3.5*x+x*x*x/(2+x)+2*5*y");
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.};
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);
237 CPPUNIT_ASSERT_THROW(expr1.evaluateExpr(1,xyValue,&res1),INTERP_KERNEL::Exception);
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)");
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.");
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.");
262 CPPUNIT_ASSERT_THROW(expr4.prepareExprEvaluationVec(),INTERP_KERNEL::Exception);
265 void ExprEvalInterpTest::testInterpreterUnit0()
267 INTERP_KERNEL::ExprParser expr1("kg");
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");
273 CPPUNIT_ASSERT_THROW(expr2.evaluateUnit(),INTERP_KERNEL::Exception);
274 INTERP_KERNEL::ExprParser expr3("g");
276 unit=expr3.evaluateUnit();
277 CPPUNIT_ASSERT(unit.isEqual(1,0,0,0,0,0.,1.));
278 INTERP_KERNEL::ExprParser expr4("g*m");
280 unit=expr4.evaluateUnit();
281 CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,0,0.,1.));
282 INTERP_KERNEL::ExprParser expr5("g*m/K");
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");
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");
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)");
296 unit=expr8.evaluateUnit();
297 CPPUNIT_ASSERT(unit.isEqual(1,-1,0,0,-2,0.,1.));
298 INTERP_KERNEL::ExprParser expr9("km/h");
300 unit=expr9.evaluateUnit();
301 CPPUNIT_ASSERT(unit.isEqual(0,1,-1,0,0,0.,0.27777777777777779));
302 INTERP_KERNEL::ExprParser expr10("m/s");
304 unit=expr10.evaluateUnit();
305 CPPUNIT_ASSERT(unit.isEqual(0,1,-1,0,0,0.,1.));
306 INTERP_KERNEL::ExprParser expr11("m+s");
308 CPPUNIT_ASSERT_THROW(expr11.evaluateUnit(),INTERP_KERNEL::Exception);
309 INTERP_KERNEL::ExprParser expr12("m-m");
311 CPPUNIT_ASSERT_THROW(expr12.evaluateUnit(),INTERP_KERNEL::Exception);
312 const char expr13C[3]={-0x50,0x43,0x0};
313 INTERP_KERNEL::ExprParser expr13(expr13C);
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);
320 unit=expr14.evaluateUnit();
321 CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,273.15,1.));
322 INTERP_KERNEL::ExprParser expr15("kN/kg");
324 unit=expr15.evaluateUnit();
325 CPPUNIT_ASSERT(unit.isEqual(0,1,-2,0,0,0.,1000.));
326 INTERP_KERNEL::ExprParser expr16("cm");
328 unit=expr16.evaluateUnit();
329 CPPUNIT_ASSERT(unit.isEqual(0,1,0,0,0,0.,0.01));
330 INTERP_KERNEL::ExprParser expr17("m");
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);
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);
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);
347 unit=expr20.evaluateUnit();
348 CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,0.,1.));
351 void ExprEvalInterpTest::testInterpreterUnit1()
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());
385 void ExprEvalInterpTest::testInterpreter3()
387 std::set<std::string> res;
390 INTERP_KERNEL::ExprParser expr1("2.3+x>5.");
392 expr1.getSetOfVars(res);
393 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
394 CPPUNIT_ASSERT(*(res.begin())=="x");
395 expr1.prepareExprEvaluationVec();
397 expr1.evaluateExpr(1,input,res2);
398 CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
400 expr1.evaluateExpr(1,input,res2);
401 CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
403 expr1.evaluateExpr(1,input,res2);
404 CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
406 INTERP_KERNEL::ExprParser expr2("2.3+x<5.");
409 expr2.getSetOfVars(res);
410 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
411 CPPUNIT_ASSERT(*(res.begin())=="x");
412 expr2.prepareExprEvaluationVec();
414 expr2.evaluateExpr(1,input,res2);
415 CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
417 expr2.evaluateExpr(1,input,res2);
418 CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
420 expr2.evaluateExpr(1,input,res2);
421 CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
423 INTERP_KERNEL::ExprParser expr3("if(2.3+x<5.,2+3*x,3+x/2)");
426 expr3.getSetOfVars(res);
427 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
428 CPPUNIT_ASSERT(*(res.begin())=="x");
429 expr3.prepareExprEvaluationVec();
431 expr3.evaluateExpr(1,input,res2);
432 CPPUNIT_ASSERT_DOUBLES_EQUAL(2.,res2[0],1e-12);
434 expr3.evaluateExpr(1,input,res2);
435 CPPUNIT_ASSERT_DOUBLES_EQUAL(4.4,res2[0],1e-12);
437 expr3.evaluateExpr(1,input,res2);
438 CPPUNIT_ASSERT_DOUBLES_EQUAL(9.8,res2[0],1e-12);
440 INTERP_KERNEL::ExprParser expr4("if(x>1000,2*x,x/3)");
443 expr4.getSetOfVars(res);
444 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
445 CPPUNIT_ASSERT(*(res.begin())=="x");
446 expr4.prepareExprEvaluationVec();
448 expr4.evaluateExpr(1,input,res2);
449 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.9,res2[0],1e-12);
451 expr4.evaluateExpr(1,input,res2);
452 CPPUNIT_ASSERT_DOUBLES_EQUAL(333.,res2[0],1e-12);
454 expr4.evaluateExpr(1,input,res2);
455 CPPUNIT_ASSERT_DOUBLES_EQUAL(2004.,res2[0],1e-12);
457 INTERP_KERNEL::ExprParser expr5("4.4*x*log10(x)*10");
460 expr5.getSetOfVars(res);
461 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
462 CPPUNIT_ASSERT(*(res.begin())=="x");
463 expr5.prepareExprEvaluationVec();
465 expr5.evaluateExpr(1,input,res2);
466 CPPUNIT_ASSERT_DOUBLES_EQUAL(29282.131520617437,res2[0],1e-9);
468 CPPUNIT_ASSERT_THROW(expr5.evaluateExpr(1,input,res2),INTERP_KERNEL::Exception);
472 * Bug detected by Marc concerning expressions with blanks.
474 void ExprEvalInterpTest::testInterpreter4()
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);
483 expr.prepareExprEvaluation(varsV,3,1);
485 expr.evaluateExpr(1,vals, &result);
486 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.2,result,1e-12);
490 * Allowing scientific format for floats.
492 void ExprEvalInterpTest::testInterpreter5()
494 std::set<std::string> res;
497 INTERP_KERNEL::ExprParser expr1("1.85e-3*x");
499 expr1.getSetOfVars(res);
500 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
501 CPPUNIT_ASSERT(*(res.begin())=="x");
503 expr1.prepareExprEvaluationVec();
504 expr1.evaluateExpr(1,input,res2);
505 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.104895,res2[0],1e-12);
507 expr1.evaluateExpr(1,input,res2);
508 CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.121545,res2[0],1e-12);
510 INTERP_KERNEL::ExprParser expr2("x*1.85e-3");
512 expr2.getSetOfVars(res);
513 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
514 CPPUNIT_ASSERT(*(res.begin())=="x");
516 expr2.prepareExprEvaluationVec();
517 expr2.evaluateExpr(1,input,res2);
518 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.104895,res2[0],1e-12);
520 expr2.evaluateExpr(1,input,res2);
521 CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.121545,res2[0],1e-12);
523 INTERP_KERNEL::ExprParser expr3("2.6E+1+x*1.85e-3");
525 expr3.getSetOfVars(res);
526 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
527 CPPUNIT_ASSERT(*(res.begin())=="x");
529 expr3.prepareExprEvaluationVec();
530 expr3.evaluateExpr(1,input,res2);
531 CPPUNIT_ASSERT_DOUBLES_EQUAL(26.104895,res2[0],1e-12);
533 expr3.evaluateExpr(1,input,res2);
534 CPPUNIT_ASSERT_DOUBLES_EQUAL(25.878455,res2[0],1e-12);
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.))))))");
538 CPPUNIT_ASSERT_DOUBLES_EQUAL(6994207.8359543988,expr4.evaluate(),1e-5);