muParserTest.cpp 50.7 KB
Newer Older
1
/*
2
3
                 __________
    _____   __ __\______   \_____  _______  ______  ____ _______
4
5
   /     \ |  |  \|     ___/\__  \ \_  __ \/  ___/_/ __ \\_  __ \
  |  Y Y  \|  |  /|    |     / __ \_|  | \/\___ \ \  ___/ |  | \/
6
7
  |__|_|  /|____/ |____|    (____  /|__|  /____  > \___  >|__|
        \/                       \/            \/      \/
8
9
  Copyright (C) 2010 Ingo Berg

10
  Permission is hereby granted, free of charge, to any person obtaining a copy of this
11
  software and associated documentation files (the "Software"), to deal in the Software
12
13
  without restriction, including without limitation the rights to use, copy, modify,
  merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
14
15
  permit persons to whom the Software is furnished to do so, subject to the following conditions:

16
  The above copyright notice and this permission notice shall be included in all copies or
17
18
19
  substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
20
21
22
23
  NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
*/

#include "muParserTest.h"

#include <cstdio>
#include <cmath>
#include <iostream>

#define PARSER_CONST_PI  3.141592653589793238462643
#define PARSER_CONST_E   2.718281828459045235360287

using namespace std;

/** \file
    \brief This file contains the implementation of parser test cases.
*/


namespace mu
{
  namespace Test
  {
    int ParserTester::c_iCount = 0;

    //---------------------------------------------------------------------------
    ParserTester::ParserTester()
      :m_vTestFun()
    {
      AddTest(&ParserTester::TestNames);
      AddTest(&ParserTester::TestSyntax);
      AddTest(&ParserTester::TestPostFix);
      AddTest(&ParserTester::TestInfixOprt);
      AddTest(&ParserTester::TestVarConst);
      AddTest(&ParserTester::TestVolatile);
      AddTest(&ParserTester::TestMultiArg);
      AddTest(&ParserTester::TestExpression);
      AddTest(&ParserTester::TestInterface);
      AddTest(&ParserTester::TestBinOprt);
      AddTest(&ParserTester::TestException);
      AddTest(&ParserTester::TestStrArg);

      ParserTester::c_iCount = 0;
    }

    //---------------------------------------------------------------------------
    int ParserTester::TestInterface()
    {
      int iStat = 0;
      mu::console() << _T("testing member functions...");
73

74
75
76
      // Test RemoveVar
      value_type afVal[3] = {1,2,3};
      Parser p;
77

78
79
80
81
82
83
84
85
86
87
      try
      {
        p.DefineVar( _T("a"), &afVal[0]);
        p.DefineVar( _T("b"), &afVal[1]);
        p.DefineVar( _T("c"), &afVal[2]);
        p.SetExpr( _T("a+b+c") );
        p.Eval();
      }
      catch(...)
      {
88
        iStat += 1;  // this is not supposed to happen
89
90
91
92
93
94
95
96
97
98
99
100
101
      }

      try
      {
        p.RemoveVar( _T("c") );
        p.Eval();
        iStat += 1;  // not supposed to reach this, nonexisting variable "c" deleted...
      }
      catch(...)
      {
        // failure is expected...
      }

102
      if (iStat==0)
103
        mu::console() << _T("passed") << endl;
104
      else
105
106
107
108
109
110
111
112
113
114
        mu::console() << _T("\n  failed with ") << iStat << _T(" errors") << endl;

      return iStat;
    }

    //---------------------------------------------------------------------------
    int ParserTester::TestStrArg()
    {
      int iStat = 0;
      mu::console() << _T("testing string arguments...");
115

116
117
118
119
120
121
122
123
124
125
126
127
128
      iStat += EqnTest(_T("valueof(\"\")"), 123, true);   // empty string arguments caused a crash
      iStat += EqnTest(_T("valueof(\"aaa\")+valueof(\"bbb\")  "), 246, true);
      iStat += EqnTest(_T("2*(valueof(\"aaa\")-23)+valueof(\"bbb\")"), 323, true);
      // use in expressions with variables
      iStat += EqnTest(_T("a*(atof(\"10\")-b)"), 8, true);
      iStat += EqnTest(_T("a-(atof(\"10\")*b)"), -19, true);
      // string + numeric arguments
      iStat += EqnTest(_T("strfun1(\"100\")"), 100, true);
      iStat += EqnTest(_T("strfun2(\"100\",1)"), 101, true);
      iStat += EqnTest(_T("strfun3(\"99\",1,2)"), 102, true);

      if (iStat==0)
        mu::console() << _T("passed") << endl;
129
      else
130
131
132
133
134
135
136
137
138
139
        mu::console() << _T("\n  failed with ") << iStat << _T(" errors") << endl;

      return iStat;
    }

    //---------------------------------------------------------------------------
    int ParserTester::TestBinOprt()
    {
      int iStat = 0;
      mu::console() << _T("testing binary operators...");
140

141
142
      // built in operators
      // xor operator
143
      iStat += EqnTest(_T("1 xor 2"), 3, true);
144
      iStat += EqnTest(_T("a xor b"), 3, true);            // with a=1 and b=2
145
      iStat += EqnTest(_T("1 xor 2 xor 3"), 0, true);
146
147
      iStat += EqnTest(_T("a xor b xor 3"), 0, true);      // with a=1 and b=2
      iStat += EqnTest(_T("a xor b xor c"), 0, true);      // with a=1 and b=2
148
      iStat += EqnTest(_T("(1 xor 2) xor 3"), 0, true);
149
150
      iStat += EqnTest(_T("(a xor b) xor c"), 0, true);    // with a=1 and b=2
      iStat += EqnTest(_T("(a) xor (b) xor c"), 0, true);  // with a=1 and b=2
151
      iStat += EqnTest(_T("1 or 2"), 3, true);
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
      iStat += EqnTest(_T("a or b"), 3, true);             // with a=1 and b=2
      iStat += EqnTest(_T("a++b"), 3, true);
      iStat += EqnTest(_T("a ++ b"), 3, true);
      iStat += EqnTest(_T("1++2"), 3, true);
      iStat += EqnTest(_T("1 ++ 2"), 3, true);
      iStat += EqnTest(_T("a add b"), 3, true);
      iStat += EqnTest(_T("1 add 2"), 3, true);
      iStat += EqnTest(_T("a<b"), 1, true);
      iStat += EqnTest(_T("b>a"), 1, true);
      iStat += EqnTest(_T("a>a"), 0, true);
      iStat += EqnTest(_T("a<a"), 0, true);
      iStat += EqnTest(_T("a>a"), 0, true);
      iStat += EqnTest(_T("a<=a"), 1, true);
      iStat += EqnTest(_T("a<=b"), 1, true);
      iStat += EqnTest(_T("b<=a"), 0, true);
      iStat += EqnTest(_T("a>=a"), 1, true);
      iStat += EqnTest(_T("b>=a"), 1, true);
      iStat += EqnTest(_T("a>=b"), 0, true);

      // Assignement operator
172
173
      iStat += EqnTest(_T("a = b"), 2, true);
      iStat += EqnTest(_T("a = sin(b)"), 0.909297, true);
174
175
176
177
178
179
      iStat += EqnTest(_T("a = 1+sin(b)"), 1.909297, true);
      iStat += EqnTest(_T("(a=b)*2"), 4, true);
      iStat += EqnTest(_T("2*(a=b)"), 4, true);
      iStat += EqnTest(_T("2*(a=b+1)"), 6, true);
      iStat += EqnTest(_T("(a=b+1)*2"), 6, true);

180
181
      iStat += EqnTest(_T("2^2^3"), 256, true);
      iStat += EqnTest(_T("1/2/3"), 1.0/6.0, true);
182
183

      // reference: http://www.wolframalpha.com/input/?i=3%2B4*2%2F%281-5%29^2^3
184
      iStat += EqnTest(_T("3+4*2/(1-5)^2^3"), 3.0001220703125, true);
185
186

      // Test user defined binary operators
187
188
189
      iStat += EqnTestInt(_T("1 | 2"), 3, true);
      iStat += EqnTestInt(_T("1 || 2"), 1, true);
      iStat += EqnTestInt(_T("123 & 456"), 72, true);
190
      iStat += EqnTestInt(_T("(123 & 456) % 10"), 2, true);
191
192
193
194
195
196
      iStat += EqnTestInt(_T("1 && 0"), 0, true);
      iStat += EqnTestInt(_T("123 && 456"), 1, true);
      iStat += EqnTestInt(_T("1 << 3"), 8, true);
      iStat += EqnTestInt(_T("8 >> 3"), 1, true);
      iStat += EqnTestInt(_T("9 / 4"), 2, true);
      iStat += EqnTestInt(_T("9 % 4"), 1, true);
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
      iStat += EqnTestInt(_T("if(5%2,1,0)"), 1, true);
      iStat += EqnTestInt(_T("if(4%2,1,0)"), 0, true);
      iStat += EqnTestInt(_T("-10+1"), -9, true);
      iStat += EqnTestInt(_T("1+2*3"), 7, true);
      iStat += EqnTestInt(_T("const1 != const2"), 1, true);
      iStat += EqnTestInt(_T("const1 != const2"), 0, false);
      iStat += EqnTestInt(_T("const1 == const2"), 0, true);
      iStat += EqnTestInt(_T("const1 == 1"), 1, true);
      iStat += EqnTestInt(_T("10*(const1 == 1)"), 10, true);
      iStat += EqnTestInt(_T("2*(const1 | const2)"), 6, true);
      iStat += EqnTestInt(_T("2*(const1 | const2)"), 7, false);
      iStat += EqnTestInt(_T("const1 < const2"), 1, true);
      iStat += EqnTestInt(_T("const2 > const1"), 1, true);
      iStat += EqnTestInt(_T("const1 <= 1"), 1, true);
      iStat += EqnTestInt(_T("const2 >= 2"), 1, true);
      iStat += EqnTestInt(_T("2*(const1 + const2)"), 6, true);
      iStat += EqnTestInt(_T("2*(const1 - const2)"), -2, true);
      iStat += EqnTestInt(_T("a != b"), 1, true);
      iStat += EqnTestInt(_T("a != b"), 0, false);
      iStat += EqnTestInt(_T("a == b"), 0, true);
      iStat += EqnTestInt(_T("a == 1"), 1, true);
      iStat += EqnTestInt(_T("10*(a == 1)"), 10, true);
      iStat += EqnTestInt(_T("2*(a | b)"), 6, true);
      iStat += EqnTestInt(_T("2*(a | b)"), 7, false);
      iStat += EqnTestInt(_T("a < b"), 1, true);
      iStat += EqnTestInt(_T("b > a"), 1, true);
      iStat += EqnTestInt(_T("a <= 1"), 1, true);
      iStat += EqnTestInt(_T("b >= 2"), 1, true);
      iStat += EqnTestInt(_T("2*(a + b)"), 6, true);
      iStat += EqnTestInt(_T("2*(a - b)"), -2, true);
      iStat += EqnTestInt(_T("a + (a << b)"), 5, true);
      iStat += EqnTestInt(_T("-2^2"), -4, true);
      iStat += EqnTestInt(_T("3--a"), 4, true);
      iStat += EqnTestInt(_T("3+-3^2"), -6, true);

      // Test reading of hex values:
      iStat += EqnTestInt(_T("0xff"), 255, true);
      iStat += EqnTestInt(_T("10+0xff"), 265, true);
      iStat += EqnTestInt(_T("0xff+10"), 265, true);
      iStat += EqnTestInt(_T("10*0xff"), 2550, true);
      iStat += EqnTestInt(_T("0xff*10"), 2550, true);
      iStat += EqnTestInt(_T("10+0xff+1"), 266, true);
      iStat += EqnTestInt(_T("1+0xff+10"), 266, true);

// incorrect: '^' is yor here, not power
//    iStat += EqnTestInt("-(1+2)^2", -9, true);
243
//    iStat += EqnTestInt("-1^3", -1, true);
244
245
246
247
248
249
250
251
252
253
254
255

      // Test precedence
      // a=1, b=2, c=3
      iStat += EqnTestInt(_T("a + b * c"), 7, true);
      iStat += EqnTestInt(_T("a * b + c"), 5, true);
      iStat += EqnTestInt(_T("a<b && b>10"), 0, true);
      iStat += EqnTestInt(_T("a<b && b<10"), 1, true);

      iStat += EqnTestInt(_T("a + b << c"), 17, true);
      iStat += EqnTestInt(_T("a << b + c"), 7, true);
      iStat += EqnTestInt(_T("c * b < a"), 0, true);
      iStat += EqnTestInt(_T("c * b == 6 * a"), 1, true);
256
      iStat += EqnTestInt(_T("2^2^3"), 256, true);
257
258
259
260


      if (iStat==0)
        mu::console() << _T("passed") << endl;
261
      else
262
263
264
265
266
267
268
269
270
271
272
273
274
        mu::console() << _T("\n  failed with ") << iStat << _T(" errors") << endl;

      return iStat;
    }

    //---------------------------------------------------------------------------
    /** \brief Check muParser name restriction enforcement. */
    int ParserTester::TestNames()
    {
      int  iStat= 0,
           iErr = 0;

      mu::console() << "testing name restriction enforcement...";
275

276
277
278
279
280
281
282
283
284
285
286
287
288
      Parser p;

  #define PARSER_THROWCHECK(DOMAIN, FAIL, EXPR, ARG) \
      iErr = 0;                                      \
      ParserTester::c_iCount++;                      \
      try                                            \
      {                                              \
        p.Define##DOMAIN(EXPR, ARG);                 \
      }                                              \
      catch(Parser::exception_type&)                 \
      {                                              \
        iErr = (FAIL==false) ? 0 : 1;                \
      }                                              \
289
290
      iStat += iErr;

291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
      // constant names
      PARSER_THROWCHECK(Const, false, _T("0a"), 1)
      PARSER_THROWCHECK(Const, false, _T("9a"), 1)
      PARSER_THROWCHECK(Const, false, _T("+a"), 1)
      PARSER_THROWCHECK(Const, false, _T("-a"), 1)
      PARSER_THROWCHECK(Const, false, _T("a-"), 1)
      PARSER_THROWCHECK(Const, false, _T("a*"), 1)
      PARSER_THROWCHECK(Const, false, _T("a?"), 1)
      PARSER_THROWCHECK(Const, true, _T("a"), 1)
      PARSER_THROWCHECK(Const, true, _T("a_min"), 1)
      PARSER_THROWCHECK(Const, true, _T("a_min0"), 1)
      PARSER_THROWCHECK(Const, true, _T("a_min9"), 1)
      // variable names
      value_type a;
      p.ClearConst();
      PARSER_THROWCHECK(Var, false, _T("123abc"), &a)
      PARSER_THROWCHECK(Var, false, _T("9a"), &a)
      PARSER_THROWCHECK(Var, false, _T("0a"), &a)
      PARSER_THROWCHECK(Var, false, _T("+a"), &a)
      PARSER_THROWCHECK(Var, false, _T("-a"), &a)
      PARSER_THROWCHECK(Var, false, _T("?a"), &a)
      PARSER_THROWCHECK(Var, false, _T("!a"), &a)
      PARSER_THROWCHECK(Var, false, _T("a+"), &a)
      PARSER_THROWCHECK(Var, false, _T("a-"), &a)
      PARSER_THROWCHECK(Var, false, _T("a*"), &a)
      PARSER_THROWCHECK(Var, false, _T("a?"), &a)
      PARSER_THROWCHECK(Var, true, _T("a"), &a)
      PARSER_THROWCHECK(Var, true, _T("a_min"), &a)
      PARSER_THROWCHECK(Var, true, _T("a_min0"), &a)
      PARSER_THROWCHECK(Var, true, _T("a_min9"), &a)
      PARSER_THROWCHECK(Var, false, _T("a_min9"), 0)
      // Postfix operators
      // fail
      PARSER_THROWCHECK(PostfixOprt, false, _T("(k"), f1of1)
      PARSER_THROWCHECK(PostfixOprt, false, _T("9+"), f1of1)
      PARSER_THROWCHECK(PostfixOprt, false, _T("+"), 0)
      // pass
      PARSER_THROWCHECK(PostfixOprt, true, _T("-a"),  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("?a"),  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("_"),   f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("#"),   f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("&&"),  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("||"),  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("&"),   f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("|"),   f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("++"),  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("--"),  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("?>"),  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("?<"),  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("**"),  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("xor"), f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("and"), f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("or"),  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("not"), f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, _T("!"),   f1of1)
      // Binary operator
      // The following must fail with builtin operators activated
      // p.EnableBuiltInOp(true); -> this is the default
      PARSER_THROWCHECK(Oprt, false, _T("+"),  f1of2)
      PARSER_THROWCHECK(Oprt, false, _T("-"),  f1of2)
      PARSER_THROWCHECK(Oprt, false, _T("*"),  f1of2)
      PARSER_THROWCHECK(Oprt, false, _T("/"),  f1of2)
      // without activated built in operators it should work
      p.EnableBuiltInOprt(false);
      PARSER_THROWCHECK(Oprt, true, _T("+"),  f1of2)
      PARSER_THROWCHECK(Oprt, true, _T("-"),  f1of2)
      PARSER_THROWCHECK(Oprt, true, _T("*"),  f1of2)
      PARSER_THROWCHECK(Oprt, true, _T("/"),  f1of2)
  #undef PARSER_THROWCHECK

361
      if (iStat==0)
362
        mu::console() << _T("passed") << endl;
363
      else
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
        mu::console() << _T("\n  failed with ") << iStat << _T(" errors") << endl;

      return iStat;
    }

    //---------------------------------------------------------------------------
    int ParserTester::TestSyntax()
    {
      int iStat = 0;
      mu::console() << _T("testing syntax engine...");

      iStat += EqnTest(_T("(1+ 2*a)"), 3, true);   // Spaces within formula
      iStat += EqnTest(_T("sqrt((4))"), 2, true);  // Multiple brackets
      iStat += EqnTest(_T("sqrt((2)+2)"), 2, true);// Multiple brackets
      iStat += EqnTest(_T("sqrt(2+(2))"), 2, true);// Multiple brackets
      iStat += EqnTest(_T("sqrt(a+(3))"), 2, true);// Multiple brackets
      iStat += EqnTest(_T("sqrt((3)+a)"), 2, true);// Multiple brackets
      iStat += EqnTest(_T("order(1,2)"), 1, true); // May not cause name collision with operator "or"
382
      iStat += EqnTest(_T("(2+"), 0, false);       // missing closing bracket
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
      iStat += EqnTest(_T("2++4"), 0, false);      // unexpected operator
      iStat += EqnTest(_T("2+-4"), 0, false);      // unexpected operator
      iStat += EqnTest(_T("(2+)"), 0, false);      // unexpected closing bracket
      iStat += EqnTest(_T("--2"), 0, false);       // double sign
      iStat += EqnTest(_T("ksdfj"), 0, false);     // unknown token
      iStat += EqnTest(_T("()"), 0, false);        // empty bracket without a function
      iStat += EqnTest(_T("5+()"), 0, false);      // empty bracket without a function
      iStat += EqnTest(_T("sin(cos)"), 0, false);  // unexpected function
      iStat += EqnTest(_T("5t6"), 0, false);       // unknown token
      iStat += EqnTest(_T("5 t 6"), 0, false);     // unknown token
      iStat += EqnTest(_T("8*"), 0, false);        // unexpected end of formula
      iStat += EqnTest(_T(",3"), 0, false);        // unexpected comma
      iStat += EqnTest(_T("3,5"), 0, false);       // unexpected comma
      iStat += EqnTest(_T("sin(8,8)"), 0, false);  // too many function args
      iStat += EqnTest(_T("(7,8)"), 0, false);     // too many function args
      iStat += EqnTest(_T("sin)"), 0, false);      // unexpected closing bracket
      iStat += EqnTest(_T("a)"), 0, false);        // unexpected closing bracket
      iStat += EqnTest(_T("pi)"), 0, false);       // unexpected closing bracket
      iStat += EqnTest(_T("sin(())"), 0, false);   // unexpected closing bracket
      iStat += EqnTest(_T("sin()"), 0, false);     // unexpected closing bracket

      if (iStat==0)
        mu::console() << _T("passed") << endl;
406
      else
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
        mu::console() << _T("\n  failed with ") << iStat << _T(" errors") << endl;

      return iStat;
    }

    //---------------------------------------------------------------------------
    int ParserTester::TestVarConst()
    {
      int iStat = 0;
      mu::console() << _T("testing variable/constant name recognition...");

      // distinguish constants with same basename
      iStat += EqnTest( _T("const"), 1, true);
      iStat += EqnTest( _T("const1"), 2, true);
      iStat += EqnTest( _T("const2"), 3, true);
      iStat += EqnTest( _T("2*const"), 2, true);
      iStat += EqnTest( _T("2*const1"), 4, true);
      iStat += EqnTest( _T("2*const2"), 6, true);
      iStat += EqnTest( _T("2*const+1"), 3, true);
      iStat += EqnTest( _T("2*const1+1"), 5, true);
      iStat += EqnTest( _T("2*const2+1"), 7, true);
      iStat += EqnTest( _T("const"), 0, false);
      iStat += EqnTest( _T("const1"), 0, false);
      iStat += EqnTest( _T("const2"), 0, false);

      // distinguish variables with same basename
      iStat += EqnTest( _T("a"), 1, true);
      iStat += EqnTest( _T("aa"), 2, true);
      iStat += EqnTest( _T("2*a"), 2, true);
      iStat += EqnTest( _T("2*aa"), 4, true);
      iStat += EqnTest( _T("2*a-1"), 1, true);
      iStat += EqnTest( _T("2*aa-1"), 3, true);

      // Finally test querying of used variables
      try
      {
        int idx;
        mu::Parser p;
        mu::value_type vVarVal[] = { 1, 2, 3, 4, 5};
        p.DefineVar( _T("a"), &vVarVal[0]);
        p.DefineVar( _T("b"), &vVarVal[1]);
        p.DefineVar( _T("c"), &vVarVal[2]);
        p.DefineVar( _T("d"), &vVarVal[3]);
        p.DefineVar( _T("e"), &vVarVal[4]);

        // Test lookup of defined variables
        // 4 used variables
        p.SetExpr( _T("a+b+c+d") );
        mu::varmap_type UsedVar = p.GetUsedVar();
        int iCount = (int)UsedVar.size();
457
        if (iCount!=4)
458
          throw false;
459
460

        // the next check will fail if the parser
461
462
463
464
465
466
467
        // erroneousely creates new variables internally
        if (p.GetVar().size()!=5)
          throw false;

        mu::varmap_type::const_iterator item = UsedVar.begin();
        for (idx=0; item!=UsedVar.end(); ++item)
        {
468
          if (&vVarVal[idx++]!=item->second)
469
470
471
472
473
474
475
            throw false;
        }

        // Test lookup of undefined variables
        p.SetExpr( _T("undef1+undef2+undef3") );
        UsedVar = p.GetUsedVar();
        iCount = (int)UsedVar.size();
476
        if (iCount!=3)
477
478
          throw false;

479
        // the next check will fail if the parser
480
481
482
483
484
485
        // erroneousely creates new variables internally
        if (p.GetVar().size()!=5)
          throw false;

        for (item = UsedVar.begin(); item!=UsedVar.end(); ++item)
        {
486
          if (item->second!=0)
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
            throw false; // all pointers to undefined variables must be null
        }

        // 1 used variables
        p.SetExpr( _T("a+b") );
        UsedVar = p.GetUsedVar();
        iCount = (int)UsedVar.size();
        if (iCount!=2) throw false;
        item = UsedVar.begin();
        for (idx=0; item!=UsedVar.end(); ++item)
          if (&vVarVal[idx++]!=item->second) throw false;

      }
      catch(...)
      {
        iStat += 1;
      }

505
      if (iStat==0)
506
507
508
509
510
511
512
513
514
515
516
517
        mu::console() << _T("passed") << endl;
      else
        mu::console() << _T("\n  failed with ") << iStat << _T(" errors") << endl;

      return iStat;
    }

    //---------------------------------------------------------------------------
    int ParserTester::TestMultiArg()
    {
      int iStat = 0;
      mu::console() << _T("testing multiarg functions...");
518

519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
      // Compound expressions
      iStat += EqnTest( _T("1,2,3"), 3, true);
      iStat += EqnTest( _T("a,b,c"), 3, true);
      iStat += EqnTest( _T("a=10,b=20,c=a*b"), 200, true);
      iStat += EqnTest( _T("1,\n2,\n3"), 3, true);
      iStat += EqnTest( _T("a,\nb,\nc"), 3, true);
      iStat += EqnTest( _T("a=10,\nb=20,\nc=a*b"), 200, true);
      iStat += EqnTest( _T("1,\r\n2,\r\n3"), 3, true);
      iStat += EqnTest( _T("a,\r\nb,\r\nc"), 3, true);
      iStat += EqnTest( _T("a=10,\r\nb=20,\r\nc=a*b"), 200, true);

      // picking the right argument
      iStat += EqnTest( _T("f1of1(1)"), 1, true);
      iStat += EqnTest( _T("f1of2(1, 2)"), 1, true);
      iStat += EqnTest( _T("f2of2(1, 2)"), 2, true);
      iStat += EqnTest( _T("f1of3(1, 2, 3)"), 1, true);
      iStat += EqnTest( _T("f2of3(1, 2, 3)"), 2, true);
      iStat += EqnTest( _T("f3of3(1, 2, 3)"), 3, true);
      iStat += EqnTest( _T("f1of4(1, 2, 3, 4)"), 1, true);
      iStat += EqnTest( _T("f2of4(1, 2, 3, 4)"), 2, true);
      iStat += EqnTest( _T("f3of4(1, 2, 3, 4)"), 3, true);
      iStat += EqnTest( _T("f4of4(1, 2, 3, 4)"), 4, true);
      iStat += EqnTest( _T("f1of5(1, 2, 3, 4, 5)"), 1, true);
      iStat += EqnTest( _T("f2of5(1, 2, 3, 4, 5)"), 2, true);
      iStat += EqnTest( _T("f3of5(1, 2, 3, 4, 5)"), 3, true);
      iStat += EqnTest( _T("f4of5(1, 2, 3, 4, 5)"), 4, true);
      iStat += EqnTest( _T("f5of5(1, 2, 3, 4, 5)"), 5, true);
      // Too few arguments / Too many arguments
      iStat += EqnTest( _T("1+ping()"), 11, true);
      iStat += EqnTest( _T("ping()+1"), 11, true);
      iStat += EqnTest( _T("2*ping()"), 20, true);
      iStat += EqnTest( _T("ping()*2"), 20, true);
      iStat += EqnTest( _T("ping(1,2)"), 0, false);
      iStat += EqnTest( _T("1+ping(1,2)"), 0, false);
      iStat += EqnTest( _T("f1of1(1,2)"), 0, false);
      iStat += EqnTest( _T("f1of1()"), 0, false);
      iStat += EqnTest( _T("f1of2(1, 2, 3)"), 0, false);
      iStat += EqnTest( _T("f1of2(1)"), 0, false);
      iStat += EqnTest( _T("f1of3(1, 2, 3, 4)"), 0, false);
      iStat += EqnTest( _T("f1of3(1)"), 0, false);
      iStat += EqnTest( _T("f1of4(1, 2, 3, 4, 5)"), 0, false);
      iStat += EqnTest( _T("f1of4(1)"), 0, false);
      iStat += EqnTest( _T("(1,2,3)"), 0, false);
      iStat += EqnTest( _T("1,2,3"), 0, false);
      iStat += EqnTest( _T("(1*a,2,3)"), 0, false);
      iStat += EqnTest( _T("1,2*a,3"), 0, false);
565

566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
      // correct calculation of arguments
      iStat += EqnTest( _T("min(a, 1)"),  1, true);
      iStat += EqnTest( _T("min(3*2, 1)"),  1, true);
      iStat += EqnTest( _T("min(3*2, 1)"),  6, false);
      iStat += EqnTest( _T("firstArg(2,3,4)"), 2, true);
      iStat += EqnTest( _T("lastArg(2,3,4)"), 4, true);
      iStat += EqnTest( _T("min(3*a+1, 1)"),  1, true);
      iStat += EqnTest( _T("max(3*a+1, 1)"),  4, true);
      iStat += EqnTest( _T("max(3*a+1, 1)*2"),  8, true);
      iStat += EqnTest( _T("2*max(3*a+1, 1)+2"),  10, true);

      // functions with Variable argument count
      iStat += EqnTest( _T("sum(a)"), 1, true);
      iStat += EqnTest( _T("sum(1,2,3)"),  6, true);
      iStat += EqnTest( _T("sum(a,b,c)"),  6, true);
      iStat += EqnTest( _T("sum(1,-max(1,2),3)*2"),  4, true);
      iStat += EqnTest( _T("2*sum(1,2,3)"),  12, true);
      iStat += EqnTest( _T("2*sum(1,2,3)+2"),  14, true);
      iStat += EqnTest( _T("2*sum(-1,2,3)+2"),  10, true);
      iStat += EqnTest( _T("2*sum(-1,2,-(-a))+2"),  6, true);
      iStat += EqnTest( _T("2*sum(-1,10,-a)+2"),  18, true);
      iStat += EqnTest( _T("2*sum(1,2,3)*2"),  24, true);
      iStat += EqnTest( _T("sum(1,-max(1,2),3)*2"),  4, true);
      iStat += EqnTest( _T("sum(1*3, 4, a+2)"),  10, true);
      iStat += EqnTest( _T("sum(1*3, 2*sum(1,2,2), a+2)"),  16, true);
      iStat += EqnTest( _T("sum(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2)"), 24, true);

      // some failures
      iStat += EqnTest( _T("sum()"),  0, false);
      iStat += EqnTest( _T("sum(,)"),  0, false);
      iStat += EqnTest( _T("sum(1,2,)"),  0, false);
      iStat += EqnTest( _T("sum(,1,2)"),  0, false);

599
      if (iStat==0)
600
601
602
        mu::console() << _T("passed") << endl;
      else
        mu::console() << _T("\n  failed with ") << iStat << _T(" errors") << endl;
603

604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
      return iStat;
    }


    //---------------------------------------------------------------------------
    int ParserTester::TestInfixOprt()
    {
      int iStat(0);
      mu::console() << "testing infix operators...";

      iStat += EqnTest( _T("-1"),    -1, true);
      iStat += EqnTest( _T("-(-1)"),  1, true);
      iStat += EqnTest( _T("-(-1)*2"),  2, true);
      iStat += EqnTest( _T("-(-2)*sqrt(4)"),  4, true);
      iStat += EqnTest( _T("-a"),  -1, true);
      iStat += EqnTest( _T("-(a)"),  -1, true);
      iStat += EqnTest( _T("-(-a)"),  1, true);
      iStat += EqnTest( _T("-(-a)*2"),  2, true);
      iStat += EqnTest( _T("-(8)"), -8, true);
      iStat += EqnTest( _T("-8"), -8, true);
      iStat += EqnTest( _T("-(2+1)"), -3, true);
      iStat += EqnTest( _T("-(f1of1(1+2*3)+1*2)"), -9, true);
      iStat += EqnTest( _T("-(-f1of1(1+2*3)+1*2)"), 5, true);
      iStat += EqnTest( _T("-sin(8)"), -0.989358, true);
      iStat += EqnTest( _T("3-(-a)"), 4, true);
      iStat += EqnTest( _T("3--a"), 4, true);
630

631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
      // Postfix / infix priorities
      iStat += EqnTest( _T("~2#"), 8, true);
      iStat += EqnTest( _T("~f1of1(2)#"), 8, true);
      iStat += EqnTest( _T("~(b)#"), 8, true);
      iStat += EqnTest( _T("(~b)#"), 12, true);
      iStat += EqnTest( _T("~(2#)"), 8, true);
      iStat += EqnTest( _T("~(f1of1(2)#)"), 8, true);
      //
      iStat += EqnTest( _T("-2^2"),-4, true);
      iStat += EqnTest( _T("-(a+b)^2"),-9, true);
      iStat += EqnTest( _T("(-3)^2"),9, true);
      iStat += EqnTest( _T("-(-2^2)"),4, true);
      iStat += EqnTest( _T("3+-3^2"),-6, true);
      // The following assumes use of sqr as postfix operator ("?") together
      // withn a sign operator of low priority:
      iStat += EqnTest( _T("-2?"), -4, true);
      iStat += EqnTest( _T("-(1+1)?"),-4, true);
      iStat += EqnTest( _T("2+-(1+1)?"),-2, true);
      iStat += EqnTest( _T("2+-2?"), -2, true);
      // This is the classic behaviour of the infix sign operator (here: "$") which is
      // now deprecated:
      iStat += EqnTest( _T("$2^2"),4, true);
      iStat += EqnTest( _T("$(a+b)^2"),9, true);
      iStat += EqnTest( _T("($3)^2"),9, true);
      iStat += EqnTest( _T("$($2^2)"),-4, true);
      iStat += EqnTest( _T("3+$3^2"),12, true);

      if (iStat==0)
        mu::console() << _T("passed") << endl;
      else
        mu::console() << _T("\n  failed with ") << iStat << _T(" errors") << endl;

      return iStat;
    }


    //---------------------------------------------------------------------------
    int ParserTester::TestPostFix()
    {
      int iStat = 0;
      mu::console() << _T("testing postfix operators...");

      // application
      iStat += EqnTest( _T("3{m}+5"), 5.003, true);
      iStat += EqnTest( _T("1000{m}"), 1, true);
      iStat += EqnTest( _T("1000 {m}"), 1, true);
      iStat += EqnTest( _T("(a){m}"), 1e-3, true);
      iStat += EqnTest( _T("a{m}"), 1e-3, true);
      iStat += EqnTest( _T("a {m}"), 1e-3, true);
      iStat += EqnTest( _T("-(a){m}"), -1e-3, true);
      iStat += EqnTest( _T("-2{m}"), -2e-3, true);
      iStat += EqnTest( _T("-2 {m}"), -2e-3, true);
      iStat += EqnTest( _T("f1of1(1000){m}"), 1, true);
      iStat += EqnTest( _T("-f1of1(1000){m}"), -1, true);
      iStat += EqnTest( _T("-f1of1(-1000){m}"), 1, true);
      iStat += EqnTest( _T("f4of4(0,0,0,1000){m}"), 1, true);
      iStat += EqnTest( _T("2+(a*1000){m}"), 3, true);

      // some incorrect results
      iStat += EqnTest( _T("1000{m}"), 0.1, false);
      iStat += EqnTest( _T("(a){m}"), 2, false);
      // failure due to syntax checking
      iStat += ThrowTest(_T("0x"), ecUNASSIGNABLE_TOKEN);  // incomplete hex definition
      iStat += ThrowTest(_T("3+"), ecUNEXPECTED_EOF);
      iStat += ThrowTest( _T("4 + {m}"), ecUNEXPECTED_OPERATOR);
      iStat += ThrowTest( _T("{m}4"), ecUNEXPECTED_OPERATOR);
      iStat += ThrowTest( _T("sin({m})"), ecUNEXPECTED_OPERATOR);
      iStat += ThrowTest( _T("{m} {m}"), ecUNEXPECTED_OPERATOR);
      iStat += ThrowTest( _T("{m}(8)"), ecUNEXPECTED_OPERATOR);
      iStat += ThrowTest( _T("4,{m}"), ecUNEXPECTED_OPERATOR);
      iStat += ThrowTest( _T("-{m}"), ecUNEXPECTED_OPERATOR);
      iStat += ThrowTest( _T("2(-{m})"), ecUNEXPECTED_PARENS);
      iStat += ThrowTest( _T("2({m})"), ecUNEXPECTED_PARENS);

      if (iStat==0)
        mu::console() << _T("passed") << endl;
      else
        mu::console() << _T("\n  failed with ") << iStat << _T(" errors") << endl;

      return iStat;
    }

    //---------------------------------------------------------------------------
    /** \brief Test volatile (nonoptimizeable functions). */
    int ParserTester::TestVolatile()
    {
      int iStat = 0;
      mu::console() << "testing volatile/nonvolatile functions...";

      // First test with volatile flag turned on
      try
      {
        mu::Parser p;
        p.DefineFun( _T("rnd"), Rnd, false);
        p.DefineFun( _T("valueof"), RndWithString, false);

        // 1st test, compare results from sucessive calculations
        p.SetExpr( _T("3+rnd(8)") );
        if (p.Eval()==p.Eval()) iStat += 1;

731
        // 2nd test, force bytecode creation, compare two results both
732
        // calculated from bytecode
733
        p.SetExpr( _T("3+rnd(8)") );
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
        p.Eval(); //<- Force bytecode creation
        if (p.Eval()==p.Eval()) iStat += 1;

        p.SetExpr( _T("3*rnd(8)+3") );
        p.Eval(); //<- Force bytecode creation
        if (p.Eval()==p.Eval()) iStat += 1;

        p.SetExpr( _T("10+3*sin(rnd(8))-1") );
        p.Eval(); //<- Force bytecode creation
        if (p.Eval()==p.Eval()) iStat += 1;

        p.SetExpr( _T("3+rnd(rnd(8))*2") );
        p.Eval(); //<- Force bytecode creation
        if (p.Eval()==p.Eval()) iStat += 1;

        p.SetExpr( _T("valueof(\"Das ist ein Test\")") );
        p.Eval(); //<- Force bytecode creation
        if (p.Eval()==p.Eval()) iStat += 1;
      }
      catch(Parser::exception_type &e)
      {
        mu::console() << _T("\n  ") << e.GetExpr() << _T(" : ") << e.GetMsg();
        iStat += 1;
      }

      // Second test with volatile flag turned off
      try
      {
        mu::Parser p;
        p.DefineFun( _T("rnd"), Rnd);
        p.DefineFun( _T("valueof"), RndWithString);

        // compare string parsing with bytecode
767
        p.SetExpr( _T("3+rnd(8)") );
768
769
        if (p.Eval()!=p.Eval()) iStat += 1;

770
        p.SetExpr( _T("3+rnd(8)") );
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
        p.Eval(); //<- Force bytecode creation
        if (p.Eval()!=p.Eval()) iStat += 1;

        p.SetExpr( _T("3*rnd(8)+3") );
        p.Eval(); //<- Force bytecode creation
        if (p.Eval()!=p.Eval()) iStat += 1;

        p.SetExpr( _T("10+3*sin(rnd(8))-1") );
        p.Eval(); //<- Force bytecode creation
        if (p.Eval()!=p.Eval()) iStat += 1;

        p.SetExpr( _T("3+rnd(rnd(8))*2") );
        p.Eval(); //<- Force bytecode creation
        if (p.Eval()!=p.Eval()) iStat += 1;
      }
      catch(Parser::exception_type &e)
      {
        mu::console() << _T("\n  ") << e.GetExpr() << _T(" : ") << e.GetMsg();
        iStat += 1;
      }
791

792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
      if (iStat==0)
        mu::console() << _T("passed") << endl;
      else
        mu::console() << _T("\n  failed with ") << iStat << _T(" errors") << endl;

      return iStat;
    }

    //---------------------------------------------------------------------------
    int ParserTester::TestExpression()
    {
      int iStat = 0;
      mu::console() << _T("testing expression samples...");

      // operator precedencs
      iStat += EqnTest( _T("1+2-3*4/5^6"), 2.99923, true);
      iStat += EqnTest( _T("1^2/3*4-5+6"), 2.3333, true);
      iStat += EqnTest( _T("1+2*3"), 7, true);
      iStat += EqnTest( _T("1+2*3"), 7, true);
      iStat += EqnTest( _T("(1+2)*3"), 9, true);
      iStat += EqnTest( _T("(1+2)*(-3)"), -9, true);
      iStat += EqnTest( _T("2/4"), 0.5, true);

      iStat += EqnTest( _T("exp(ln(7))"), 7, true);
      iStat += EqnTest( _T("e^ln(7)"), 7, true);
      iStat += EqnTest( _T("e^(ln(7))"), 7, true);
      iStat += EqnTest( _T("(e^(ln(7)))"), 7, true);
      iStat += EqnTest( _T("1-(e^(ln(7)))"), -6, true);
      iStat += EqnTest( _T("2*(e^(ln(7)))"), 14, true);
      iStat += EqnTest( _T("10^log(5)"), 5, true);
      iStat += EqnTest( _T("10^log10(5)"), 5, true);
      iStat += EqnTest( _T("2^log2(4)"), 4, true);
      iStat += EqnTest( _T("-(sin(0)+1)"), -1, true);
      iStat += EqnTest( _T("-(2^1.1)"), -2.14354692, true);

      iStat += EqnTest( _T("(cos(2.41)/b)"), -0.372056, true);
      iStat += EqnTest( _T("(1*(2*(3*(4*(5*(6*(a+b)))))))"), 2160, true);
      iStat += EqnTest( _T("(1*(2*(3*(4*(5*(6*(7*(a+b))))))))"), 15120, true);
      iStat += EqnTest( _T("(a/((((b+(((e*(((((pi*((((3.45*((pi+a)+pi))+b)+b)*a))+0.68)+e)+a)/a))+a)+b))+b)*a)-pi))"), 0.00377999, true);

      // long formula (Reference: Matlab)
      iStat += EqnTest(
        _T("(((-9))-e/(((((((pi-(((-7)+(-3)/4/e))))/(((-5))-2)-((pi+(-0))*(sqrt((e+e))*(-8))*(((-pi)+(-pi)-(-9)*(6*5))")
        _T("/(-e)-e))/2)/((((sqrt(2/(-e)+6)-(4-2))+((5/(-2))/(1*(-pi)+3))/8)*pi*((pi/((-2)/(-6)*1*(-1))*(-6)+(-e)))))/")
        _T("((e+(-2)+(-e)*((((-3)*9+(-e)))+(-9)))))))-((((e-7+(((5/pi-(3/1+pi)))))/e)/(-5))/(sqrt((((((1+(-7))))+((((-")
        _T("e)*(-e)))-8))*(-5)/((-e)))*(-6)-((((((-2)-(-9)-(-e)-1)/3))))/(sqrt((8+(e-((-6))+(9*(-9))))*(((3+2-8))*(7+6")
        _T("+(-5))+((0/(-e)*(-pi))+7)))+(((((-e)/e/e)+((-6)*5)*e+(3+(-5)/pi))))+pi))/sqrt((((9))+((((pi))-8+2))+pi))/e")
        _T("*4)*((-5)/(((-pi))*(sqrt(e)))))-(((((((-e)*(e)-pi))/4+(pi)*(-9)))))))+(-pi)"), -12.23016549, true);

      // long formula (Reference: Matlab)
      iStat += EqnTest(
          _T("(atan(sin((((((((((((((((pi/cos((a/((((0.53-b)-pi)*e)/b))))+2.51)+a)-0.54)/0.98)+b)*b)+e)/a)+b)+a)+b)+pi)/e")
          _T(")+a)))*2.77)"), -2.16995656, true);

      // long formula (Reference: Matlab)
      iStat += EqnTest( _T("1+2-3*4/5^6*(2*(1-5+(3*7^9)*(4+6*7-3)))+12"), -7995810.09926, true);
848
849
850
851

      if (iStat==0)
        mu::console() << _T("passed") << endl;
      else
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
        mu::console() << _T("\n  failed with ") << iStat << _T(" errors") << endl;

      return iStat;
    }


    //---------------------------------------------------------------------------
    int ParserTester::TestException()
    {
      int  iStat = 0;
      mu::console() << _T("testing error codes...");

      iStat += ThrowTest(_T("3+"),           ecUNEXPECTED_EOF);
      iStat += ThrowTest(_T("3+)"),          ecUNEXPECTED_PARENS);
      iStat += ThrowTest(_T("()"),           ecUNEXPECTED_PARENS);
      iStat += ThrowTest(_T("3+()"),         ecUNEXPECTED_PARENS);
      iStat += ThrowTest(_T("sin(3,4)"),     ecTOO_MANY_PARAMS);
      iStat += ThrowTest(_T("if(3)"),        ecTOO_FEW_PARAMS);
      iStat += ThrowTest(_T("(1+2"),         ecMISSING_PARENS);
      iStat += ThrowTest(_T("sin(3)3"),      ecUNEXPECTED_VAL);
      iStat += ThrowTest(_T("sin(3)xyz"),    ecUNASSIGNABLE_TOKEN);
      iStat += ThrowTest(_T("sin(3)cos(3)"), ecUNEXPECTED_FUN);
      iStat += ThrowTest(_T("a+b+c=10"),     ecUNEXPECTED_OPERATOR);
      iStat += ThrowTest(_T("a=b=3"),        ecUNEXPECTED_OPERATOR);

      // functions without parameter
      iStat += ThrowTest( _T("3+ping(2)"),    ecTOO_MANY_PARAMS);
      iStat += ThrowTest( _T("3+ping(a+2)"),  ecTOO_MANY_PARAMS);
      iStat += ThrowTest( _T("3+ping(sin(a)+2)"),  ecTOO_MANY_PARAMS);
      iStat += ThrowTest( _T("3+ping(1+sin(a))"),  ecTOO_MANY_PARAMS);

      // String function related
      iStat += ThrowTest( _T("valueof(\"xxx\")"),  999, false);
      iStat += ThrowTest( _T("valueof()"),          ecUNEXPECTED_PARENS);
      iStat += ThrowTest( _T("1+valueof(\"abc\""),  ecMISSING_PARENS);
      iStat += ThrowTest( _T("valueof(\"abc\""),    ecMISSING_PARENS);
      iStat += ThrowTest( _T("valueof(\"abc"),      ecUNTERMINATED_STRING);
      iStat += ThrowTest( _T("valueof(\"abc\",3)"), ecTOO_MANY_PARAMS);
      iStat += ThrowTest( _T("valueof(3)"),         ecSTRING_EXPECTED);
      iStat += ThrowTest( _T("sin(\"abc\")"),       ecVAL_EXPECTED);
      iStat += ThrowTest( _T("valueof(\"\\\"abc\\\"\")"),  999, false);
      iStat += ThrowTest( _T("\"hello world\""),    ecSTR_RESULT);
      iStat += ThrowTest( _T("(\"hello world\")"),  ecSTR_RESULT);
      iStat += ThrowTest( _T("\"abcd\"+100"),       ecOPRT_TYPE_CONFLICT);
      iStat += ThrowTest( _T("\"a\"+\"b\""),        ecOPRT_TYPE_CONFLICT);
      iStat += ThrowTest( _T("strfun1(\"100\",3)"),     ecTOO_MANY_PARAMS);
      iStat += ThrowTest( _T("strfun2(\"100\",3,5)"),   ecTOO_MANY_PARAMS);
      iStat += ThrowTest( _T("strfun3(\"100\",3,5,6)"), ecTOO_MANY_PARAMS);
      iStat += ThrowTest( _T("strfun2(\"100\")"),       ecTOO_FEW_PARAMS);
      iStat += ThrowTest( _T("strfun3(\"100\",6)"),   ecTOO_FEW_PARAMS);
      iStat += ThrowTest( _T("strfun2(1,1)"),         ecSTRING_EXPECTED);
      iStat += ThrowTest( _T("strfun2(a,1)"),         ecSTRING_EXPECTED);
      iStat += ThrowTest( _T("strfun2(1,1,1)"),       ecTOO_MANY_PARAMS);
      iStat += ThrowTest( _T("strfun2(a,1,1)"),       ecTOO_MANY_PARAMS);
      iStat += ThrowTest( _T("strfun3(1,2,3)"),         ecSTRING_EXPECTED);
      iStat += ThrowTest( _T("strfun3(1, \"100\",3)"),  ecSTRING_EXPECTED);
      iStat += ThrowTest( _T("strfun3(\"1\", \"100\",3)"),  ecVAL_EXPECTED);
      iStat += ThrowTest( _T("strfun3(\"1\", 3, \"100\")"),  ecVAL_EXPECTED);
      iStat += ThrowTest( _T("strfun3(\"1\", \"100\", \"100\", \"100\")"),  ecTOO_MANY_PARAMS);

      // assignement operator
      iStat += ThrowTest( _T("3=4"), ecUNEXPECTED_OPERATOR);
      iStat += ThrowTest( _T("sin(8)=4"), ecUNEXPECTED_OPERATOR);
      iStat += ThrowTest( _T("\"test\"=a"), ecUNEXPECTED_OPERATOR);

      // <ibg 20090529>
      // this is now legal, for reference see:
      // https://sourceforge.net/forum/message.php?msg_id=7411373
920
      //      iStat += ThrowTest( _T("sin=9"), ecUNEXPECTED_OPERATOR);
921
922
923
924
925
926
      // </ibg>

      iStat += ThrowTest( _T("(8)=5"), ecUNEXPECTED_OPERATOR);
      iStat += ThrowTest( _T("(a)=5"), ecUNEXPECTED_OPERATOR);
      iStat += ThrowTest( _T("a=\"tttt\""), ecOPRT_TYPE_CONFLICT);

927
      if (iStat==0)
928
        mu::console() << _T("passed") << endl;
929
      else
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
        mu::console() << _T("\n  failed with ") << iStat << _T(" errors") << endl;

      return iStat;
    }


    //---------------------------------------------------------------------------
    void ParserTester::AddTest(testfun_type a_pFun)
    {
      m_vTestFun.push_back(a_pFun);
    }

    //---------------------------------------------------------------------------
    void ParserTester::Run()
    {
      int iStat = 0;
      try
      {
        for (int i=0; i<(int)m_vTestFun.size(); ++i)
          iStat += (this->*m_vTestFun[i])();
      }
      catch(Parser::exception_type &e)
      {
        mu::console() << "\n" << e.GetMsg() << endl;
        mu::console() << e.GetToken() << endl;
        Abort();
      }
      catch(std::exception &e)
      {
        mu::console() << e.what() << endl;
        Abort();
      }
      catch(...)
      {
        mu::console() << "Internal error";
        Abort();
      }

968
      if (iStat==0)
969
970
971
      {
        mu::console() << "Test passed (" <<  ParserTester::c_iCount << " expressions)" << endl;
      }
972
      else
973
      {
974
975
        mu::console() << "Test failed with " << iStat
                  << " errors (" <<  ParserTester::c_iCount
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
                  << " expressions)" << endl;
      }
      ParserTester::c_iCount = 0;
    }


    //---------------------------------------------------------------------------
    int ParserTester::ThrowTest(const string_type &a_str, int a_iErrc, bool a_bFail)
    {
      ParserTester::c_iCount++;

      try
      {
        value_type fVal[] = {1,1,1};
        Parser p;

        p.DefineVar( _T("a"), &fVal[0]);
        p.DefineVar( _T("b"), &fVal[1]);
        p.DefineVar( _T("c"), &fVal[2]);
        p.DefinePostfixOprt( _T("{m}"), Milli);
        p.DefineFun( _T("ping"), Ping);
        p.DefineFun( _T("valueof"), ValueOf);
        p.DefineFun( _T("strfun1"), StrFun1);
        p.DefineFun( _T("strfun2"), StrFun2);
        p.DefineFun( _T("strfun3"), StrFun3);
For faster browsing, not all history is shown. View entire blame