]> SALOME platform Git repositories - modules/med.git/blob - medtool/src/INTERP_KERNELTest/ExprEvalInterpTest.cxx
Salome HOME
Move medtool folder to MED base repository
[modules/med.git] / medtool / src / INTERP_KERNELTest / ExprEvalInterpTest.cxx
1 // Copyright (C) 2007-2015  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, or (at your option) any later version.
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 // Author : Anthony Geay (CEA/DEN)
20
21 #include "ExprEvalInterpTest.hxx"
22 #include "InterpKernelExprParser.hxx"
23
24 #include <limits>
25 #include <iterator>
26
27 using namespace INTERP_TEST;
28
29 void ExprEvalInterpTest::testBuildStringFromFortran()
30 {
31   char toto1[]="123456  ";
32   char result[]="123456";
33   std::string titi;
34   titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto1,8);
35   CPPUNIT_ASSERT_EQUAL(6,(int)titi.length());
36   CPPUNIT_ASSERT(titi==result);
37   //
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());
43   //
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());
49   //
50   char toto4[]="        ";
51   titi=INTERP_KERNEL::ExprParser::buildStringFromFortran(toto4,8);
52   CPPUNIT_ASSERT_EQUAL(0,(int)titi.length());
53   //
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());
58 }
59
60 void ExprEvalInterpTest::testDeleteWhiteSpaces()
61 {
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());
68   //
69   char toto2[]=" jkhjkh     ooooppp    l ";
70   totoS=toto2;
71   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
72   CPPUNIT_ASSERT(totoR==result);
73   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
74   //
75   char toto3[]=" jkhjkh     oooo pppl ";
76   totoS=toto3;
77   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
78   CPPUNIT_ASSERT(totoR==result);
79   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
80   //
81   char toto4[]=" jkhjkh     oooo pppl";
82   totoS=toto4;
83   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
84   CPPUNIT_ASSERT(totoR==result);
85   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
86   //
87   char toto5[]="jkhjkh     oooo pppl";
88   totoS=toto5;
89   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
90   CPPUNIT_ASSERT(totoR==result);
91   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
92   //
93   totoS=result;
94   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
95   CPPUNIT_ASSERT(totoR==result);
96   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
97   //
98   char toto6[]="j k h j k h o o o o p p p l";
99   totoS=toto6;
100   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
101   CPPUNIT_ASSERT(totoR==result);
102   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
103   //
104   char toto7[]="j  k  h j    k h   o  o  o  o  p  pp    l";
105   totoS=toto7;
106   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
107   CPPUNIT_ASSERT(totoR==result);
108   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
109   //
110   char toto8[]="           ";
111   totoS=toto8;
112   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
113   CPPUNIT_ASSERT(totoR.empty());
114   //
115   char toto9[]="";
116   totoS=toto9;
117   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
118   CPPUNIT_ASSERT(totoR.empty());
119   //
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";
121   totoS=toto10;
122   totoR=INTERP_KERNEL::ExprParser::deleteWhiteSpaces(totoS);
123   CPPUNIT_ASSERT(totoR==result);
124   CPPUNIT_ASSERT_EQUAL(14,(int)totoR.length());
125 }
126
127 void ExprEvalInterpTest::testInterpreter0()
128 {
129   INTERP_KERNEL::ExprParser expr1("3*-2");
130   expr1.parse();
131   CPPUNIT_ASSERT_DOUBLES_EQUAL(-6.,expr1.evaluate(),1e-15);
132   INTERP_KERNEL::ExprParser expr2("-2.3");
133   expr2.parse();
134   CPPUNIT_ASSERT_DOUBLES_EQUAL(-2.3,expr2.evaluate(),1e-15);
135   INTERP_KERNEL::ExprParser expr3("--2.3");
136   expr3.parse();
137   CPPUNIT_ASSERT_DOUBLES_EQUAL(2.3,expr3.evaluate(),1e-15);
138   INTERP_KERNEL::ExprParser expr4("-++2.3");
139   expr4.parse();
140   CPPUNIT_ASSERT_DOUBLES_EQUAL(-2.3,expr4.evaluate(),1e-15);
141   INTERP_KERNEL::ExprParser expr5("+2.3");
142   expr5.parse();
143   CPPUNIT_ASSERT_DOUBLES_EQUAL(2.3,expr5.evaluate(),1e-15);
144   INTERP_KERNEL::ExprParser expr6("3^-1");
145   expr6.parse();
146   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.33333333333333333,expr6.evaluate(),1e-15);
147 }
148
149 void ExprEvalInterpTest::testInterpreter1()
150 {
151   INTERP_KERNEL::ExprParser expr1("3+2*5");
152   expr1.parse();
153   CPPUNIT_ASSERT_DOUBLES_EQUAL(13.,expr1.evaluate(),1e-14);
154   INTERP_KERNEL::ExprParser expr2("3+2^3*5");
155   expr2.parse();
156   CPPUNIT_ASSERT_DOUBLES_EQUAL(43.,expr2.evaluate(),1e-14);
157   INTERP_KERNEL::ExprParser expr3("3+2^(2*5)");
158   expr3.parse();
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)");
161   expr4.parse();
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)");
164   expr5.parse();
165   CPPUNIT_ASSERT_DOUBLES_EQUAL(6.9355510138337619,expr5.evaluate(),1e-14);
166   INTERP_KERNEL::ExprParser expr6("3+2-4-7+4.3");
167   expr6.parse();
168   CPPUNIT_ASSERT_DOUBLES_EQUAL(-1.7,expr6.evaluate(),1e-14);
169   INTERP_KERNEL::ExprParser expr7("3.2*4.5/3.3/2.2");
170   expr7.parse();
171   CPPUNIT_ASSERT_DOUBLES_EQUAL(1.9834710743801653,expr7.evaluate(),1e-14);
172   INTERP_KERNEL::ExprParser expr8("3.2*4.5/3.3/2.2");
173   expr8.parse();
174   CPPUNIT_ASSERT_DOUBLES_EQUAL(1.9834710743801653,expr8.evaluate(),1e-14);
175   INTERP_KERNEL::ExprParser expr9("(((1.23456789)))");
176   expr9.parse();
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.))");
179   expr10.parse();
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.)))))");
182   expr11.parse();
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.)))))");
185   expr12.parse();
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.)))))");
188   expr13.parse();
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.)))))");
191   expr14.parse();
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.)))))");
194   expr15.parse();
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.)))))");
197   expr16.parse();
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.)))))");
200   expr17.parse();
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.)))))");
203   expr18.parse();
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.)))))");
206   expr19.parse();
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.)))))");
209   expr20.parse();
210   CPPUNIT_ASSERT_DOUBLES_EQUAL(9.7179974940325309,expr20.evaluate(),1e-14);
211   INTERP_KERNEL::ExprParser expr21("max(3.2,4.5)");
212   expr21.parse();
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.))))))");
215   expr22.parse();
216   CPPUNIT_ASSERT_DOUBLES_EQUAL(10788678114.535484,expr22.evaluate(),1e-5);
217   INTERP_KERNEL::ExprParser expr23("min(3.2,4.5)");
218   expr23.parse();
219   CPPUNIT_ASSERT_DOUBLES_EQUAL(3.2,expr23.evaluate(),1e-14);
220 }
221
222 void ExprEvalInterpTest::testInterpreter2()
223 {
224   INTERP_KERNEL::ExprParser expr1("3.5*x+x*x*x/(2+x)+2*5*y");
225   expr1.parse();
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.};
232   double res1;
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);
237   xyValue[0]=-2.;
238   CPPUNIT_ASSERT_THROW(expr1.evaluateExpr(1,xyValue,&res1),INTERP_KERNEL::Exception);
239   double res2[2];
240   xyValue[0]=1.;
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)");
245   expr2.parse();
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.");
256   expr3.parse();
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.");
262   expr4.parse();
263   CPPUNIT_ASSERT_THROW(expr4.prepareExprEvaluationVec(),INTERP_KERNEL::Exception);
264 }
265
266 void ExprEvalInterpTest::testInterpreterUnit0()
267 {
268   INTERP_KERNEL::ExprParser expr1("kg");
269   expr1.parse();
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");
273   expr2.parse();
274   CPPUNIT_ASSERT_THROW(expr2.evaluateUnit(),INTERP_KERNEL::Exception);
275   INTERP_KERNEL::ExprParser expr3("g");
276   expr3.parse();
277   unit=expr3.evaluateUnit();
278   CPPUNIT_ASSERT(unit.isEqual(1,0,0,0,0,0.,1.));
279   INTERP_KERNEL::ExprParser expr4("g*m");
280   expr4.parse();
281   unit=expr4.evaluateUnit();
282   CPPUNIT_ASSERT(unit.isEqual(1,1,0,0,0,0.,1.));
283   INTERP_KERNEL::ExprParser expr5("g*m/K");
284   expr5.parse();
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");
288   expr6.parse();
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");
292   expr7.parse();
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)");
296   expr8.parse();
297   unit=expr8.evaluateUnit();
298   CPPUNIT_ASSERT(unit.isEqual(1,-1,0,0,-2,0.,1.));
299   INTERP_KERNEL::ExprParser expr9("km/h");
300   expr9.parse();
301   unit=expr9.evaluateUnit();
302   CPPUNIT_ASSERT(unit.isEqual(0,1,-1,0,0,0.,0.27777777777777779));
303   INTERP_KERNEL::ExprParser expr10("m/s");
304   expr10.parse();
305   unit=expr10.evaluateUnit();
306   CPPUNIT_ASSERT(unit.isEqual(0,1,-1,0,0,0.,1.));
307   INTERP_KERNEL::ExprParser expr11("m+s");
308   expr11.parse();
309   CPPUNIT_ASSERT_THROW(expr11.evaluateUnit(),INTERP_KERNEL::Exception);
310   INTERP_KERNEL::ExprParser expr12("m-m");
311   expr12.parse();
312   CPPUNIT_ASSERT_THROW(expr12.evaluateUnit(),INTERP_KERNEL::Exception);
313   const char expr13C[3]={-0x50,0x43,0x0};
314   INTERP_KERNEL::ExprParser expr13(expr13C);
315   expr13.parse();
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);
320   expr14.parse();
321   unit=expr14.evaluateUnit();
322   CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,273.15,1.));
323   INTERP_KERNEL::ExprParser expr15("kN/kg");
324   expr15.parse();
325   unit=expr15.evaluateUnit();
326   CPPUNIT_ASSERT(unit.isEqual(0,1,-2,0,0,0.,1000.));
327   INTERP_KERNEL::ExprParser expr16("cm");
328   expr16.parse();
329   unit=expr16.evaluateUnit();
330   CPPUNIT_ASSERT(unit.isEqual(0,1,0,0,0,0.,0.01));
331   INTERP_KERNEL::ExprParser expr17("m");
332   expr17.parse();
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);
337   expr18.parse();
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);
342   expr19.parse();
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);
347   expr20.parse();
348   unit=expr20.evaluateUnit();
349   CPPUNIT_ASSERT(unit.isEqual(0,0,0,0,1,0.,1.));
350 }
351
352 void ExprEvalInterpTest::testInterpreterUnit1()
353 {
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());
384 }
385
386 void ExprEvalInterpTest::testInterpreter3()
387 {
388   std::set<std::string> res;
389   double input[3];
390   double res2[3];
391   INTERP_KERNEL::ExprParser expr1("2.3+x>5.");
392   expr1.parse();
393   expr1.getSetOfVars(res);
394   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
395   CPPUNIT_ASSERT(*(res.begin())=="x");
396   expr1.prepareExprEvaluationVec();
397   input[0]=0.;
398   expr1.evaluateExpr(1,input,res2);
399   CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
400   input[0]=2.8;
401   expr1.evaluateExpr(1,input,res2);
402   CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
403   input[0]=2.6;
404   expr1.evaluateExpr(1,input,res2);
405   CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
406   //
407   INTERP_KERNEL::ExprParser expr2("2.3+x<5.");
408   expr2.parse();
409   res.clear();
410   expr2.getSetOfVars(res);
411   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
412   CPPUNIT_ASSERT(*(res.begin())=="x");
413   expr2.prepareExprEvaluationVec();
414   input[0]=0.;
415   expr2.evaluateExpr(1,input,res2);
416   CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
417   input[0]=2.8;
418   expr2.evaluateExpr(1,input,res2);
419   CPPUNIT_ASSERT(-std::numeric_limits<double>::max()==res2[0]);
420   input[0]=2.6;
421   expr2.evaluateExpr(1,input,res2);
422   CPPUNIT_ASSERT(std::numeric_limits<double>::max()==res2[0]);
423   //
424   INTERP_KERNEL::ExprParser expr3("if(2.3+x<5.,2+3*x,3+x/2)");
425   expr3.parse();
426   res.clear();
427   expr3.getSetOfVars(res);
428   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
429   CPPUNIT_ASSERT(*(res.begin())=="x");
430   expr3.prepareExprEvaluationVec();
431   input[0]=0.;
432   expr3.evaluateExpr(1,input,res2);
433   CPPUNIT_ASSERT_DOUBLES_EQUAL(2.,res2[0],1e-12);
434   input[0]=2.8;
435   expr3.evaluateExpr(1,input,res2);
436   CPPUNIT_ASSERT_DOUBLES_EQUAL(4.4,res2[0],1e-12);
437   input[0]=2.6;
438   expr3.evaluateExpr(1,input,res2);
439   CPPUNIT_ASSERT_DOUBLES_EQUAL(9.8,res2[0],1e-12);
440   //
441   INTERP_KERNEL::ExprParser expr4("if(x>1000,2*x,x/3)");
442   expr4.parse();
443   res.clear();
444   expr4.getSetOfVars(res);
445   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
446   CPPUNIT_ASSERT(*(res.begin())=="x");
447   expr4.prepareExprEvaluationVec();
448   input[0]=2.7;
449   expr4.evaluateExpr(1,input,res2);
450   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.9,res2[0],1e-12);
451   input[0]=999.;
452   expr4.evaluateExpr(1,input,res2);
453   CPPUNIT_ASSERT_DOUBLES_EQUAL(333.,res2[0],1e-12);
454   input[0]=1002.;
455   expr4.evaluateExpr(1,input,res2);
456   CPPUNIT_ASSERT_DOUBLES_EQUAL(2004.,res2[0],1e-12);
457   //
458   INTERP_KERNEL::ExprParser expr5("4.4*x*log10(x)*10");
459   expr5.parse();
460   res.clear();
461   expr5.getSetOfVars(res);
462   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
463   CPPUNIT_ASSERT(*(res.begin())=="x");
464   expr5.prepareExprEvaluationVec();
465   input[0]=273.15;
466   expr5.evaluateExpr(1,input,res2);
467   CPPUNIT_ASSERT_DOUBLES_EQUAL(29282.131520617437,res2[0],1e-9);
468   input[0]=0.;
469   CPPUNIT_ASSERT_THROW(expr5.evaluateExpr(1,input,res2),INTERP_KERNEL::Exception);
470 }
471
472 /*!
473  * Bug detected by Marc concerning expressions with blanks.
474  */
475 void ExprEvalInterpTest::testInterpreter4()
476 {
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);
480   varsV[0] = "x";
481   varsV[1] = "y";
482   varsV[2] = "z";
483   expr.parse();
484   expr.prepareExprEvaluation(varsV,3,1);
485   double result;
486   expr.evaluateExpr(1,vals, &result);
487   CPPUNIT_ASSERT_DOUBLES_EQUAL(1.2,result,1e-12);
488 }
489
490 /*!
491  * Allowing scientific format for floats.
492  */
493 void ExprEvalInterpTest::testInterpreter5()
494 {
495   std::set<std::string> res;
496   double input[3];
497   double res2[3];
498   INTERP_KERNEL::ExprParser expr1("1.85e-3*x");
499   expr1.parse();
500   expr1.getSetOfVars(res);
501   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
502   CPPUNIT_ASSERT(*(res.begin())=="x");
503   input[0]=56.7;
504   expr1.prepareExprEvaluationVec();
505   expr1.evaluateExpr(1,input,res2);
506   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.104895,res2[0],1e-12);
507   input[0]=-65.7;
508   expr1.evaluateExpr(1,input,res2);
509   CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.121545,res2[0],1e-12);
510   //
511   INTERP_KERNEL::ExprParser expr2("x*1.85e-3");
512   expr2.parse();
513   expr2.getSetOfVars(res);
514   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
515   CPPUNIT_ASSERT(*(res.begin())=="x");
516   input[0]=56.7;
517   expr2.prepareExprEvaluationVec();
518   expr2.evaluateExpr(1,input,res2);
519   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.104895,res2[0],1e-12);
520   input[0]=-65.7;
521   expr2.evaluateExpr(1,input,res2);
522   CPPUNIT_ASSERT_DOUBLES_EQUAL(-0.121545,res2[0],1e-12);
523   //
524   INTERP_KERNEL::ExprParser expr3("2.6E+1+x*1.85e-3");
525   expr3.parse();
526   expr3.getSetOfVars(res);
527   CPPUNIT_ASSERT_EQUAL(1,(int)res.size());
528   CPPUNIT_ASSERT(*(res.begin())=="x");
529   input[0]=56.7;
530   expr3.prepareExprEvaluationVec();
531   expr3.evaluateExpr(1,input,res2);
532   CPPUNIT_ASSERT_DOUBLES_EQUAL(26.104895,res2[0],1e-12);
533   input[0]=-65.7;
534   expr3.evaluateExpr(1,input,res2);
535   CPPUNIT_ASSERT_DOUBLES_EQUAL(25.878455,res2[0],1e-12);
536   //
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.))))))");
538   expr4.parse();
539   CPPUNIT_ASSERT_DOUBLES_EQUAL(6994207.8359543988,expr4.evaluate(),1e-5);
540 }
541
542 /*!
543  * Test focusing on fast evaluator.
544  */
545 void ExprEvalInterpTest::testInterpreter6()
546 {
547   std::vector<double> stackOfVal;
548   //
549   stackOfVal.clear();
550   INTERP_KERNEL::ExprParser expr1("1.-2./3.");
551   expr1.parse();
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);
556   //
557   stackOfVal.clear();
558   INTERP_KERNEL::ExprParser expr2("1.-2.^3.");
559   expr2.parse();
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);
564   //
565   stackOfVal.clear();
566   INTERP_KERNEL::ExprParser expr3("(7.-2.)^3.");
567   expr3.parse();
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
573   stackOfVal.clear();
574   INTERP_KERNEL::ExprParser expr4("1.2/(7.-2.*cos(x/3))");
575   expr4.parse();
576   expr4.prepareFastEvaluator();
577   double z;
578   expr4.prepareExprEvaluationDouble(std::vector<std::string>(1,"x"),1,1,0,&z,&z+1);
579   expr4.prepareFastEvaluator();
580   z=0.77;
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();
585   z=0.55;
586   expr4.evaluateDoubleInternal(stackOfVal);
587   CPPUNIT_ASSERT_EQUAL(1,(int)stackOfVal.size());
588   CPPUNIT_ASSERT_DOUBLES_EQUAL(0.2384018932069258,stackOfVal.back(),1e-12);
589   //
590   stackOfVal.clear();
591   INTERP_KERNEL::ExprParser expr5("x-2*cos(y/3.)");
592   expr5.parse();
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();
608   delete [] aa;
609   //
610   stackOfVal.clear();
611   INTERP_KERNEL::ExprParser expr6("x*IVec-2*cos(y/3.)*JVec");
612   expr6.parse();
613   aa=new double[2];
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();
628   delete [] aa;
629   //
630   stackOfVal.clear();
631   INTERP_KERNEL::ExprParser expr7("if(x>3.,-6,7.)");
632   expr7.parse();
633   expr7.prepareExprEvaluationDouble(std::vector<std::string>(1,"x"),1,1,0,&z,&z+1);
634   expr7.prepareFastEvaluator();
635   z=3.1;
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();
640   z=2.8;
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();
645   //
646   stackOfVal.clear();
647   INTERP_KERNEL::ExprParser expr8("if(x<3.,-6,7.)");
648   expr8.parse();
649   expr8.prepareExprEvaluationDouble(std::vector<std::string>(1,"x"),1,1,0,&z,&z+1);
650   expr8.prepareFastEvaluator();
651   z=3.1;
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();
656   z=2.8;
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();
661   //
662   stackOfVal.clear();
663   INTERP_KERNEL::ExprParser expr9("x*x/2");//this test is very important to test for iso priority with more than one
664   expr9.parse();
665   expr9.prepareExprEvaluationDouble(std::vector<std::string>(1,"x"),1,1,0,&z,&z+1);
666   expr9.prepareFastEvaluator();
667   z=3.;
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();
672   //
673   stackOfVal.clear();
674   INTERP_KERNEL::ExprParser expr10("2./3./4./5.");
675   expr10.parse();
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);
680   //
681   stackOfVal.clear();
682   INTERP_KERNEL::ExprParser expr11("2./3./4.*5.");
683   expr11.parse();
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);
688   //
689   stackOfVal.clear();
690   INTERP_KERNEL::ExprParser expr12("2./3.*4.*5.");
691   expr12.parse();
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);
696 }