1 // Copyright (C) 2007-2015 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, or (at your option) any later version.
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
19 // Author : Anthony Geay (CEA/DEN)
21 #include "ExprEvalInterpTest.hxx"
22 #include "InterpKernelExprParser.hxx"
27 using namespace INTERP_TEST;
29 void ExprEvalInterpTest::testBuildStringFromFortran()
31 char toto1[]="123456 ";
32 char result[]="123456";
34 titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto1,8);
35 CPPUNIT_ASSERT_EQUAL(6,(int)titi.length());
36 CPPUNIT_ASSERT(titi==result);
38 char toto2[]=" 23456 ";
39 char result2[]=" 23456";
40 titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto2,8);
41 CPPUNIT_ASSERT(titi==result2);
42 CPPUNIT_ASSERT_EQUAL(6,(int)titi.length());
44 char toto3[]=" 3456 ";
45 char result3[]=" 3456";
46 titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto3,8);
47 CPPUNIT_ASSERT(titi==result3);
48 CPPUNIT_ASSERT_EQUAL(6,(int)titi.length());
51 titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto4,8);
52 CPPUNIT_ASSERT_EQUAL(0,(int)titi.length());
54 char toto5[]=" 345677";
55 titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto5,8);
56 CPPUNIT_ASSERT(titi==toto5);
57 CPPUNIT_ASSERT_EQUAL(8,(int)titi.length());
60 void ExprEvalInterpTest::testDeleteWhiteSpaces()
62 char toto[]=" jkhjkh ooooppp l ";
63 char result[]="jkhjkhoooopppl";
64 std::string totoS(toto);
65 std::string totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
66 CPPUNIT_ASSERT(totoR==result);
67 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
69 char toto2[]=" jkhjkh ooooppp l ";
71 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
72 CPPUNIT_ASSERT(totoR==result);
73 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
75 char toto3[]=" jkhjkh oooo pppl ";
77 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
78 CPPUNIT_ASSERT(totoR==result);
79 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
81 char toto4[]=" jkhjkh oooo pppl";
83 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
84 CPPUNIT_ASSERT(totoR==result);
85 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
87 char toto5[]="jkhjkh oooo pppl";
89 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
90 CPPUNIT_ASSERT(totoR==result);
91 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
94 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
95 CPPUNIT_ASSERT(totoR==result);
96 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
98 char toto6[]="j k h j k h o o o o p p p l";
100 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
101 CPPUNIT_ASSERT(totoR==result);
102 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
104 char toto7[]="j k h j k h o o o o p pp l";
106 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
107 CPPUNIT_ASSERT(totoR==result);
108 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
112 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
113 CPPUNIT_ASSERT(totoR.empty());
117 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
118 CPPUNIT_ASSERT(totoR.empty());
120 char toto10[]="j\n k \nh\nj \n\n k\nh \n o \no\n o\n o \np\n\npp \n\n l";
122 totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
123 CPPUNIT_ASSERT(totoR==result);
124 CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
127 void ExprEvalInterpTest::testInterpreter0()
129 INTERP_KERNEL::ExprParser expr1("3*-2");
131 CPPUNIT_ASSERT_DOUBLES_EQUAL(-6.,expr1.evaluate(),1e-15);
132 INTERP_KERNEL::ExprParser expr2("-2.3");
134 CPPUNIT_ASSERT_DOUBLES_EQUAL(-2.3,expr2.evaluate(),1e-15);
135 INTERP_KERNEL::ExprParser expr3("--2.3");
137 CPPUNIT_ASSERT_DOUBLES_EQUAL(2.3,expr3.evaluate(),1e-15);
138 INTERP_KERNEL::ExprParser expr4("-++2.3");
140 CPPUNIT_ASSERT_DOUBLES_EQUAL(-2.3,expr4.evaluate(),1e-15);
141 INTERP_KERNEL::ExprParser expr5("+2.3");
143 CPPUNIT_ASSERT_DOUBLES_EQUAL(2.3,expr5.evaluate(),1e-15);
144 INTERP_KERNEL::ExprParser expr6("3^-1");
146 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.33333333333333333,expr6.evaluate(),1e-15);
149 void ExprEvalInterpTest::testInterpreter1()
151 INTERP_KERNEL::ExprParser expr1("3+2*5");
153 CPPUNIT_ASSERT_DOUBLES_EQUAL(13.,expr1.evaluate(),1e-14);
154 INTERP_KERNEL::ExprParser expr2("3+2^3*5");
156 CPPUNIT_ASSERT_DOUBLES_EQUAL(43.,expr2.evaluate(),1e-14);
157 INTERP_KERNEL::ExprParser expr3("3+2^(2*5)");
159 CPPUNIT_ASSERT_DOUBLES_EQUAL(1027.,expr3.evaluate(),1e-14);
160 INTERP_KERNEL::ExprParser expr4("(3.2+4.3)*(1.3+2.3*7.8)");
162 CPPUNIT_ASSERT_DOUBLES_EQUAL(144.3,expr4.evaluate(),1e-10);
163 INTERP_KERNEL::ExprParser expr5("(3.2+4.3)*cos(1.3+2.3*7.8)");
165 CPPUNIT_ASSERT_DOUBLES_EQUAL(6.9355510138337619,expr5.evaluate(),1e-14);
166 INTERP_KERNEL::ExprParser expr6("3+2-4-7+4.3");
168 CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.7,expr6.evaluate(),1e-14);
169 INTERP_KERNEL::ExprParser expr7("3.2*4.5/3.3/2.2");
171 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.9834710743801653,expr7.evaluate(),1e-14);
172 INTERP_KERNEL::ExprParser expr8("3.2*4.5/3.3/2.2");
174 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.9834710743801653,expr8.evaluate(),1e-14);
175 INTERP_KERNEL::ExprParser expr9("(((1.23456789)))");
177 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.23456789,expr9.evaluate(),1e-14);
178 INTERP_KERNEL::ExprParser expr10("3.2*((2*5.2+6.)+(1.2*1.2+3.))");
180 CPPUNIT_ASSERT_DOUBLES_EQUAL(66.688,expr10.evaluate(),1e-13);
181 INTERP_KERNEL::ExprParser expr11("((3.2*(((2*5.2+6.)+(1.2*1.2+3.)))))");
183 CPPUNIT_ASSERT_DOUBLES_EQUAL(66.688,expr11.evaluate(),1e-13);
184 INTERP_KERNEL::ExprParser expr12("((3.2*(cos((2*5.2+6.)+(1.2*1.2+3.)))))");
186 CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.3038041398761016,expr12.evaluate(),1e-14);
187 INTERP_KERNEL::ExprParser expr13("((3.2*(sin((2*5.2+6.)+(1.2*1.2+3.)))))");
189 CPPUNIT_ASSERT_DOUBLES_EQUAL(2.9223440531261784,expr13.evaluate(),1e-14);
190 INTERP_KERNEL::ExprParser expr14("((3.2*(tan((2*5.2+6.)+(1.2*1.2+3.)))))");
192 CPPUNIT_ASSERT_DOUBLES_EQUAL(-7.1724737512280257,expr14.evaluate(),1e-14);
193 INTERP_KERNEL::ExprParser expr15("((3.2*(sqrt((2*5.2+6.)+(1.2*1.2+3.)))))");
195 CPPUNIT_ASSERT_DOUBLES_EQUAL(14.608271629457059,expr15.evaluate(),1e-13);
196 INTERP_KERNEL::ExprParser expr16("-((3.2*(sqrt((2*5.2+6.)+(1.2*1.2+3.)))))");
198 CPPUNIT_ASSERT_DOUBLES_EQUAL(-14.608271629457059,expr16.evaluate(),1e-13);
199 INTERP_KERNEL::ExprParser expr17("(-(3.2*(sqrt((2*5.2+6.)+(1.2*1.2+3.)))))");
201 CPPUNIT_ASSERT_DOUBLES_EQUAL(-14.608271629457059,expr17.evaluate(),1e-13);
202 INTERP_KERNEL::ExprParser expr18("((-3.2*(sqrt((2*5.2+6.)+(1.2*1.2+3.)))))");
204 CPPUNIT_ASSERT_DOUBLES_EQUAL(-14.608271629457059,expr18.evaluate(),1e-13);
205 INTERP_KERNEL::ExprParser expr19("((3.2*(exp((6.+2*5.2)+(1.2*1.2+3.)))))");
207 CPPUNIT_ASSERT_DOUBLES_EQUAL(3596226038.1784945,expr19.evaluate(),1e-6);
208 INTERP_KERNEL::ExprParser expr20("((3.2*(ln((2*5.2+6.)+(1.2*1.2+3.)))))");
210 CPPUNIT_ASSERT_DOUBLES_EQUAL(9.7179974940325309,expr20.evaluate(),1e-14);
211 INTERP_KERNEL::ExprParser expr21("max(3.2,4.5)");
213 CPPUNIT_ASSERT_DOUBLES_EQUAL(4.5,expr21.evaluate(),1e-14);
214 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.))))))");
216 CPPUNIT_ASSERT_DOUBLES_EQUAL(10788678114.535484,expr22.evaluate(),1e-5);
217 INTERP_KERNEL::ExprParser expr23("min(3.2,4.5)");
219 CPPUNIT_ASSERT_DOUBLES_EQUAL(3.2,expr23.evaluate(),1e-14);
222 void ExprEvalInterpTest::testInterpreter2()
224 INTERP_KERNEL::ExprParser expr1("3.5*x+x*x*x/(2+x)+2*5*y");
226 std::set<std::string> res,expected;
227 expr1.getSetOfVars(res);
228 CPPUNIT_ASSERT_EQUAL(2,(int)res.size());
229 expected.insert("x"); expected.insert("y");
230 CPPUNIT_ASSERT(std::equal(res.begin(),res.end(),expected.begin()));
231 double xyValue[2]={1.,3.};
233 std::vector<std::string> vars; vars.push_back("x"); vars.push_back("y");
234 expr1.prepareExprEvaluation(vars,2,1);
235 expr1.evaluateExpr(1,xyValue,&res1);
236 CPPUNIT_ASSERT_DOUBLES_EQUAL(33.833333333333336,res1,1e-13);
238 CPPUNIT_ASSERT_THROW(expr1.evaluateExpr(1,xyValue,&res1),INTERP_KERNEL::Exception);
241 expr1.evaluateExpr(2,xyValue,res2);
242 CPPUNIT_ASSERT_DOUBLES_EQUAL(33.833333333333336,res2[0],1e-13);
243 CPPUNIT_ASSERT_DOUBLES_EQUAL(33.833333333333336,res2[1],1e-13);
244 INTERP_KERNEL::ExprParser expr2("3.5*tan(2.3*x)*IVec+(cos(1.2+y/x)*JVec)");
246 res.clear(); expected.clear();
247 expr2.getSetOfVars(res);
248 CPPUNIT_ASSERT_EQUAL(4,(int)res.size());
249 expected.insert("x"); expected.insert("y"); expected.insert("IVec"); expected.insert("JVec");
250 CPPUNIT_ASSERT(std::equal(res.begin(),res.end(),expected.begin()));
251 expr2.prepareExprEvaluation(vars,2,2);
252 expr2.evaluateExpr(2,xyValue,res2);
253 CPPUNIT_ASSERT_DOUBLES_EQUAL(-3.9172477460694637,res2[0],1e-14);
254 CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.49026082134069943,res2[1],1e-14);
255 INTERP_KERNEL::ExprParser expr3("3.5*u+u^2.4+2.");
257 expr3.prepareExprEvaluationVec();
258 expr3.evaluateExpr(2,xyValue,res2);
259 CPPUNIT_ASSERT_DOUBLES_EQUAL(6.5,res2[0],1e-14);
260 CPPUNIT_ASSERT_DOUBLES_EQUAL(26.466610165238237,res2[1],1e-14);
261 INTERP_KERNEL::ExprParser expr4("3.5*v+u^2.4+2.");
263 CPPUNIT_ASSERT_THROW(expr4.prepareExprEvaluationVec(),INTERP_KERNEL::Exception);
266 void ExprEvalInterpTest::testInterpreterUnit0()
268 INTERP_KERNEL::ExprParser expr1("kg");
270 INTERP_KERNEL::DecompositionInUnitBase unit=expr1.evaluateUnit();
271 CPPUNIT_ASSERT(unit.isEqual(1,0,0,0,0,0.,1000.));
272 INTERP_KERNEL::ExprParser expr2("kgT");
274 CPPUNIT_ASSERT_THROW(expr2.evaluateUnit(),INTERP_KERNEL::Exception);
275 INTERP_KERNEL::ExprParser expr3("g");
277 unit=expr3.evaluateUnit();
278 CPPUNIT_ASSERT(unit.isEqual(1,0,0,0,0,0.,1.));
279 INTERP_KERNEL::ExprParser expr4("g*m");
281 unit=expr4.evaluateUnit();
282 CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,0,0.,1.));
283 INTERP_KERNEL::ExprParser expr5("g*m/K");
285 unit=expr5.evaluateUnit();
286 CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,-1,0.,1.));
287 INTERP_KERNEL::ExprParser expr6("g*m/K^2");
289 unit=expr6.evaluateUnit();
290 CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,-2,0.,1.));
291 INTERP_KERNEL::ExprParser expr7("g/K^2*m");
293 unit=expr7.evaluateUnit();
294 CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,-2,0.,1.));
295 INTERP_KERNEL::ExprParser expr8("g/(K^2*m)");
297 unit=expr8.evaluateUnit();
298 CPPUNIT_ASSERT(unit.isEqual(1,-1,0,0,-2,0.,1.));
299 INTERP_KERNEL::ExprParser expr9("km/h");
301 unit=expr9.evaluateUnit();
302 CPPUNIT_ASSERT(unit.isEqual(0,1,-1,0,0,0.,0.27777777777777779));
303 INTERP_KERNEL::ExprParser expr10("m/s");
305 unit=expr10.evaluateUnit();
306 CPPUNIT_ASSERT(unit.isEqual(0,1,-1,0,0,0.,1.));
307 INTERP_KERNEL::ExprParser expr11("m+s");
309 CPPUNIT_ASSERT_THROW(expr11.evaluateUnit(),INTERP_KERNEL::Exception);
310 INTERP_KERNEL::ExprParser expr12("m-m");
312 CPPUNIT_ASSERT_THROW(expr12.evaluateUnit(),INTERP_KERNEL::Exception);
313 const char expr13C[3]={-0x50,0x43,0x0};
314 INTERP_KERNEL::ExprParser expr13(expr13C);
316 unit=expr13.evaluateUnit();
317 CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,273.15,1.));
318 const char expr14C[4]={-0x3E,-0x50,0x43,0x0};
319 INTERP_KERNEL::ExprParser expr14(expr14C);
321 unit=expr14.evaluateUnit();
322 CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,273.15,1.));
323 INTERP_KERNEL::ExprParser expr15("kN/kg");
325 unit=expr15.evaluateUnit();
326 CPPUNIT_ASSERT(unit.isEqual(0,1,-2,0,0,0.,1000.));
327 INTERP_KERNEL::ExprParser expr16("cm");
329 unit=expr16.evaluateUnit();
330 CPPUNIT_ASSERT(unit.isEqual(0,1,0,0,0,0.,0.01));
331 INTERP_KERNEL::ExprParser expr17("m");
333 unit=expr17.evaluateUnit();
334 CPPUNIT_ASSERT(unit.isEqual(0,1,0,0,0,0.,1));
335 const char expr18C[3]={-0x08,0x43,0x0};
336 INTERP_KERNEL::ExprParser expr18(expr18C);
338 unit=expr18.evaluateUnit();
339 CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,273.15,1.));
340 const char expr19C[6]={-0x50,0x43,0x2F,-0x50,0x43,0x0};
341 INTERP_KERNEL::ExprParser expr19(expr19C);
343 unit=expr19.evaluateUnit();
344 CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,0,0.,1.));
345 const char expr20C[9]={-0x50,0x43,0x2A,-0x50,0x43,0x2F,-0x50,0x43,0x0};
346 INTERP_KERNEL::ExprParser expr20(expr20C);
348 unit=expr20.evaluateUnit();
349 CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,0.,1.));
352 void ExprEvalInterpTest::testInterpreterUnit1()
354 INTERP_KERNEL::Unit unit1("m/s");
355 INTERP_KERNEL::Unit unit2("km/h");
356 CPPUNIT_ASSERT(unit1.isCompatibleWith(unit2) && unit2.isCompatibleWith(unit1));
357 CPPUNIT_ASSERT_DOUBLES_EQUAL(360,unit1.convert(unit2,100.),1e-10);
358 INTERP_KERNEL::Unit unit3("J/s");
359 INTERP_KERNEL::Unit unit4("kW");
360 CPPUNIT_ASSERT(unit3.isCompatibleWith(unit4) && unit4.isCompatibleWith(unit3));
361 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.,unit3.convert(unit4,1000.),1e-10);
362 CPPUNIT_ASSERT(unit4.getCoarseRepr()=="kW");
363 INTERP_KERNEL::Unit unit5("kpT");
364 CPPUNIT_ASSERT(!unit5.isInterpretationOK());
365 CPPUNIT_ASSERT(unit5.getCoarseRepr()=="kpT");
366 INTERP_KERNEL::Unit unit6("m*kpT");
367 CPPUNIT_ASSERT(!unit6.isInterpretationOK());
368 INTERP_KERNEL::Unit unit7("m*s^-1");
369 CPPUNIT_ASSERT(unit7.isCompatibleWith(unit2) && unit2.isCompatibleWith(unit7));
370 CPPUNIT_ASSERT_DOUBLES_EQUAL(360,unit7.convert(unit2,100.),1e-10);
371 const char unit8C[3]={-0x50,0x43,0x0};
372 INTERP_KERNEL::Unit unit8(unit8C);
373 INTERP_KERNEL::Unit unit9("K");
374 CPPUNIT_ASSERT(unit9.isCompatibleWith(unit8) && unit8.isCompatibleWith(unit9));
375 CPPUNIT_ASSERT_DOUBLES_EQUAL(335.15,unit8.convert(unit9,62.),1e-10);
376 CPPUNIT_ASSERT_DOUBLES_EQUAL(-16.37,unit9.convert(unit8,256.78),1e-10);
377 INTERP_KERNEL::Unit unit10("m");
378 INTERP_KERNEL::Unit unit11("cm");
379 CPPUNIT_ASSERT(unit10.isCompatibleWith(unit11) && unit11.isCompatibleWith(unit10));
380 CPPUNIT_ASSERT_DOUBLES_EQUAL(6200.,unit10.convert(unit11,62.),1e-8);
381 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.62,unit11.convert(unit10,62.),1e-15);
382 INTERP_KERNEL::Unit unit12("m-m");
383 CPPUNIT_ASSERT(!unit12.isInterpretationOK());
386 void ExprEvalInterpTest::testInterpreter3()
388 std::set<std::string> res;
391 INTERP_KERNEL::ExprParser expr1("2.3+x>5.");
393 expr1.getSetOfVars(res);
394 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
395 CPPUNIT_ASSERT(*(res.begin())=="x");
396 expr1.prepareExprEvaluationVec();
398 expr1.evaluateExpr(1,input,res2);
399 CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
401 expr1.evaluateExpr(1,input,res2);
402 CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
404 expr1.evaluateExpr(1,input,res2);
405 CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
407 INTERP_KERNEL::ExprParser expr2("2.3+x<5.");
410 expr2.getSetOfVars(res);
411 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
412 CPPUNIT_ASSERT(*(res.begin())=="x");
413 expr2.prepareExprEvaluationVec();
415 expr2.evaluateExpr(1,input,res2);
416 CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
418 expr2.evaluateExpr(1,input,res2);
419 CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
421 expr2.evaluateExpr(1,input,res2);
422 CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
424 INTERP_KERNEL::ExprParser expr3("if(2.3+x<5.,2+3*x,3+x/2)");
427 expr3.getSetOfVars(res);
428 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
429 CPPUNIT_ASSERT(*(res.begin())=="x");
430 expr3.prepareExprEvaluationVec();
432 expr3.evaluateExpr(1,input,res2);
433 CPPUNIT_ASSERT_DOUBLES_EQUAL(2.,res2[0],1e-12);
435 expr3.evaluateExpr(1,input,res2);
436 CPPUNIT_ASSERT_DOUBLES_EQUAL(4.4,res2[0],1e-12);
438 expr3.evaluateExpr(1,input,res2);
439 CPPUNIT_ASSERT_DOUBLES_EQUAL(9.8,res2[0],1e-12);
441 INTERP_KERNEL::ExprParser expr4("if(x>1000,2*x,x/3)");
444 expr4.getSetOfVars(res);
445 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
446 CPPUNIT_ASSERT(*(res.begin())=="x");
447 expr4.prepareExprEvaluationVec();
449 expr4.evaluateExpr(1,input,res2);
450 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.9,res2[0],1e-12);
452 expr4.evaluateExpr(1,input,res2);
453 CPPUNIT_ASSERT_DOUBLES_EQUAL(333.,res2[0],1e-12);
455 expr4.evaluateExpr(1,input,res2);
456 CPPUNIT_ASSERT_DOUBLES_EQUAL(2004.,res2[0],1e-12);
458 INTERP_KERNEL::ExprParser expr5("4.4*x*log10(x)*10");
461 expr5.getSetOfVars(res);
462 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
463 CPPUNIT_ASSERT(*(res.begin())=="x");
464 expr5.prepareExprEvaluationVec();
466 expr5.evaluateExpr(1,input,res2);
467 CPPUNIT_ASSERT_DOUBLES_EQUAL(29282.131520617437,res2[0],1e-9);
469 CPPUNIT_ASSERT_THROW(expr5.evaluateExpr(1,input,res2),INTERP_KERNEL::Exception);
473 * Bug detected by Marc concerning expressions with blanks.
475 void ExprEvalInterpTest::testInterpreter4()
477 INTERP_KERNEL::ExprParser expr("2*x + 1");
478 double vals[3]={0.1,0.2,0.3};
479 std::vector<std::string> varsV(3);
484 expr.prepareExprEvaluation(varsV,3,1);
486 expr.evaluateExpr(1,vals, &result);
487 CPPUNIT_ASSERT_DOUBLES_EQUAL(1.2,result,1e-12);
491 * Allowing scientific format for floats.
493 void ExprEvalInterpTest::testInterpreter5()
495 std::set<std::string> res;
498 INTERP_KERNEL::ExprParser expr1("1.85e-3*x");
500 expr1.getSetOfVars(res);
501 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
502 CPPUNIT_ASSERT(*(res.begin())=="x");
504 expr1.prepareExprEvaluationVec();
505 expr1.evaluateExpr(1,input,res2);
506 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.104895,res2[0],1e-12);
508 expr1.evaluateExpr(1,input,res2);
509 CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.121545,res2[0],1e-12);
511 INTERP_KERNEL::ExprParser expr2("x*1.85e-3");
513 expr2.getSetOfVars(res);
514 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
515 CPPUNIT_ASSERT(*(res.begin())=="x");
517 expr2.prepareExprEvaluationVec();
518 expr2.evaluateExpr(1,input,res2);
519 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.104895,res2[0],1e-12);
521 expr2.evaluateExpr(1,input,res2);
522 CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.121545,res2[0],1e-12);
524 INTERP_KERNEL::ExprParser expr3("2.6E+1+x*1.85e-3");
526 expr3.getSetOfVars(res);
527 CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
528 CPPUNIT_ASSERT(*(res.begin())=="x");
530 expr3.prepareExprEvaluationVec();
531 expr3.evaluateExpr(1,input,res2);
532 CPPUNIT_ASSERT_DOUBLES_EQUAL(26.104895,res2[0],1e-12);
534 expr3.evaluateExpr(1,input,res2);
535 CPPUNIT_ASSERT_DOUBLES_EQUAL(25.878455,res2[0],1e-12);
537 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.))))))");
539 CPPUNIT_ASSERT_DOUBLES_EQUAL(6994207.8359543988,expr4.evaluate(),1e-5);
543 * Test focusing on fast evaluator.
545 void ExprEvalInterpTest::testInterpreter6()
547 std::vector<double> stackOfVal;
550 INTERP_KERNEL::ExprParser expr1("1.-2./3.");
552 expr1.prepareFastEvaluator();
553 expr1.evaluateDoubleInternal(stackOfVal);
554 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
555 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.33333333333333333,stackOfVal.back(),1e-14);
558 INTERP_KERNEL::ExprParser expr2("1.-2.^3.");
560 expr2.prepareFastEvaluator();
561 expr2.evaluateDoubleInternal(stackOfVal);
562 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
563 CPPUNIT_ASSERT_DOUBLES_EQUAL(-7.,stackOfVal.back(),1e-14);
566 INTERP_KERNEL::ExprParser expr3("(7.-2.)^3.");
568 expr3.prepareFastEvaluator();
569 expr3.evaluateDoubleInternal(stackOfVal);
570 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
571 CPPUNIT_ASSERT_DOUBLES_EQUAL(125.,stackOfVal.back(),1e-12);
572 // now playing with one parameter - calling several times
574 INTERP_KERNEL::ExprParser expr4("1.2/(7.-2.*cos(x/3))");
576 expr4.prepareFastEvaluator();
578 expr4.prepareExprEvaluationDouble(std::vector<std::string>(1,"x"),1,1,0,&z,&z+1);
579 expr4.prepareFastEvaluator();
581 expr4.evaluateDoubleInternal(stackOfVal);
582 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
583 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.23689586281558844,stackOfVal.back(),1e-12);
584 stackOfVal.pop_back();
586 expr4.evaluateDoubleInternal(stackOfVal);
587 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
588 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.2384018932069258,stackOfVal.back(),1e-12);
591 INTERP_KERNEL::ExprParser expr5("x-2*cos(y/3.)");
593 expr5.prepareFastEvaluator();
594 double *aa(new double[2]);
595 std::vector<std::string> vv(2); vv[0]="x"; vv[1]="y";
596 expr5.prepareExprEvaluationDouble(vv,2,1,0,aa,aa+2);
597 expr5.prepareFastEvaluator();
598 aa[0]=0.3; aa[1]=0.5;
599 expr5.evaluateDoubleInternal(stackOfVal);
600 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
601 CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.67228646312585,stackOfVal.back(),1e-14);
602 stackOfVal.pop_back();
603 aa[0]=0.5; aa[1]=0.3;
604 expr5.evaluateDoubleInternal(stackOfVal);
605 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
606 CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.4900083305560516,stackOfVal.back(),1e-14);
607 stackOfVal.pop_back();
611 INTERP_KERNEL::ExprParser expr6("x*IVec-2*cos(y/3.)*JVec");
614 vv.resize(2); vv[0]="x"; vv[1]="y";
615 expr6.prepareExprEvaluationDouble(vv,2,2,0,aa,aa+2);//emulate 1st interpreter
616 expr6.prepareFastEvaluator();
617 aa[0]=0.3; aa[1]=0.5;
618 expr6.evaluateDoubleInternal(stackOfVal);
619 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
620 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.3,stackOfVal.back(),1e-14);
621 stackOfVal.pop_back();
622 expr6.prepareExprEvaluationDouble(vv,2,2,1,aa,aa+2);//emulate 2nd interpreter
623 expr6.prepareFastEvaluator();
624 expr6.evaluateDoubleInternal(stackOfVal);
625 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
626 CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.97228646312585,stackOfVal.back(),1e-14);
627 stackOfVal.pop_back();
631 INTERP_KERNEL::ExprParser expr7("if(x>3.,-6,7.)");
633 expr7.prepareExprEvaluationDouble(std::vector<std::string>(1,"x"),1,1,0,&z,&z+1);
634 expr7.prepareFastEvaluator();
636 expr7.evaluateDoubleInternal(stackOfVal);
637 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
638 CPPUNIT_ASSERT_DOUBLES_EQUAL(-6.,stackOfVal.back(),1e-14);
639 stackOfVal.pop_back();
641 expr7.evaluateDoubleInternal(stackOfVal);
642 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
643 CPPUNIT_ASSERT_DOUBLES_EQUAL(7.,stackOfVal.back(),1e-14);
644 stackOfVal.pop_back();
647 INTERP_KERNEL::ExprParser expr8("if(x<3.,-6,7.)");
649 expr8.prepareExprEvaluationDouble(std::vector<std::string>(1,"x"),1,1,0,&z,&z+1);
650 expr8.prepareFastEvaluator();
652 expr8.evaluateDoubleInternal(stackOfVal);
653 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
654 CPPUNIT_ASSERT_DOUBLES_EQUAL(7.,stackOfVal.back(),1e-14);
655 stackOfVal.pop_back();
657 expr8.evaluateDoubleInternal(stackOfVal);
658 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
659 CPPUNIT_ASSERT_DOUBLES_EQUAL(-6.,stackOfVal.back(),1e-14);
660 stackOfVal.pop_back();
663 INTERP_KERNEL::ExprParser expr9("x*x/2");//this test is very important to test for iso priority with more than one
665 expr9.prepareExprEvaluationDouble(std::vector<std::string>(1,"x"),1,1,0,&z,&z+1);
666 expr9.prepareFastEvaluator();
668 expr9.evaluateDoubleInternal(stackOfVal);
669 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
670 CPPUNIT_ASSERT_DOUBLES_EQUAL(4.5,stackOfVal.back(),1e-14);
671 stackOfVal.pop_back();
674 INTERP_KERNEL::ExprParser expr10("2./3./4./5.");
676 expr10.prepareFastEvaluator();
677 expr10.evaluateDoubleInternal(stackOfVal);
678 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
679 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.03333333333333333,stackOfVal.back(),1e-13);
682 INTERP_KERNEL::ExprParser expr11("2./3./4.*5.");
684 expr11.prepareFastEvaluator();
685 expr11.evaluateDoubleInternal(stackOfVal);
686 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
687 CPPUNIT_ASSERT_DOUBLES_EQUAL(0.83333333333333333,stackOfVal.back(),1e-14);
690 INTERP_KERNEL::ExprParser expr12("2./3.*4.*5.");
692 expr12.prepareFastEvaluator();
693 expr12.evaluateDoubleInternal(stackOfVal);
694 CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
695 CPPUNIT_ASSERT_DOUBLES_EQUAL(13.333333333333333,stackOfVal.back(),1e-14);