OpcUaCanOpen
CANopen OPC-UA server
muParserTest.h
Go to the documentation of this file.
1 /*
2  __________
3  _____ __ __\______ \_____ _______ ______ ____ _______
4  / \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
5  | Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
6  |__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
7  \/ \/ \/ \/
8  Copyright (C) 2013 Ingo Berg
9 
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  without restriction, including without limitation the rights to use, copy, modify,
13  merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
14  permit persons to whom the Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in all copies or
17  substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
20  NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
22  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 */
25 
26 #ifndef MU_PARSER_TEST_H
27 #define MU_PARSER_TEST_H
28 
29 #include <string>
30 #include <cstdlib>
31 #include <numeric> // for accumulate
32 #include "muParser.h"
33 #include "muParserInt.h"
34 
39 namespace mu
40 {
42  namespace Test
43  {
44  //------------------------------------------------------------------------------
50  {
51  private:
52  static int c_iCount;
53 
54  // Multiarg callbacks
55  static value_type f1of1(value_type v) { return v;};
56 
57  static value_type f1of2(value_type v, value_type ) {return v;};
58  static value_type f2of2(value_type , value_type v) {return v;};
59 
60  static value_type f1of3(value_type v, value_type , value_type ) {return v;};
61  static value_type f2of3(value_type , value_type v, value_type ) {return v;};
62  static value_type f3of3(value_type , value_type , value_type v) {return v;};
63 
68 
74 
75  static value_type Min(value_type a_fVal1, value_type a_fVal2) { return (a_fVal1<a_fVal2) ? a_fVal1 : a_fVal2; }
76  static value_type Max(value_type a_fVal1, value_type a_fVal2) { return (a_fVal1>a_fVal2) ? a_fVal1 : a_fVal2; }
77 
78  static value_type plus2(value_type v1) { return v1+2; }
79  static value_type times3(value_type v1) { return v1*3; }
80  static value_type sqr(value_type v1) { return v1*v1; }
81  static value_type sign(value_type v) { return -v; }
82  static value_type add(value_type v1, value_type v2) { return v1+v2; }
83  static value_type land(value_type v1, value_type v2) { return (int)v1 & (int)v2; }
84 
85 
86  static value_type FirstArg(const value_type* a_afArg, int a_iArgc)
87  {
88  if (!a_iArgc)
89  throw mu::Parser::exception_type( _T("too few arguments for function FirstArg.") );
90 
91  return a_afArg[0];
92  }
93 
94  static value_type LastArg(const value_type* a_afArg, int a_iArgc)
95  {
96  if (!a_iArgc)
97  throw mu::Parser::exception_type( _T("too few arguments for function LastArg.") );
98 
99  return a_afArg[a_iArgc-1];
100  }
101 
102  static value_type Sum(const value_type* a_afArg, int a_iArgc)
103  {
104  if (!a_iArgc)
105  throw mu::Parser::exception_type( _T("too few arguments for function sum.") );
106 
107  value_type fRes=0;
108  for (int i=0; i<a_iArgc; ++i) fRes += a_afArg[i];
109  return fRes;
110  }
111 
113  {
114  return (value_type)(1+(v*std::rand()/(RAND_MAX+1.0)));
115  }
116 
118  {
119  return (value_type)( 1 + (1000.0f * std::rand() / (RAND_MAX + 1.0) ) );
120  }
121 
122  static value_type Ping()
123  {
124  return 10;
125  }
126 
127  static value_type ValueOf(const char_type*)
128  {
129  return 123;
130  }
131 
132  static value_type StrFun1(const char_type* v1)
133  {
134  int val(0);
135  stringstream_type(v1) >> val;
136  return (value_type)val;
137  }
138 
139  static value_type StrFun2(const char_type* v1, value_type v2)
140  {
141  int val(0);
142  stringstream_type(v1) >> val;
143  return (value_type)(val + v2);
144  }
145 
146  static value_type StrFun3(const char_type* v1, value_type v2, value_type v3)
147  {
148  int val(0);
149  stringstream_type(v1) >> val;
150  return val + v2 + v3;
151  }
152 
153  static value_type StrToFloat(const char_type* a_szMsg)
154  {
155  value_type val(0);
156  stringstream_type(a_szMsg) >> val;
157  return val;
158  }
159 
160  // postfix operator callback
161  static value_type Mega(value_type a_fVal) { return a_fVal * (value_type)1e6; }
162  static value_type Micro(value_type a_fVal) { return a_fVal * (value_type)1e-6; }
163  static value_type Milli(value_type a_fVal) { return a_fVal / (value_type)1e3; }
164 
165  // Custom value recognition
166  static int IsHexVal(const char_type *a_szExpr, int *a_iPos, value_type *a_fVal);
167 
168  int TestNames();
169  int TestSyntax();
170  int TestMultiArg();
171  int TestPostFix();
172  int TestExpression();
173  int TestInfixOprt();
174  int TestBinOprt();
175  int TestVarConst();
176  int TestInterface();
177  int TestException();
178  int TestStrArg();
179  int TestIfThenElse();
180  int TestBulkMode();
181 
182  void Abort() const;
183 
184  public:
185  typedef int (ParserTester::*testfun_type)();
186 
187  ParserTester();
188  void Run();
189 
190  private:
191  std::vector<testfun_type> m_vTestFun;
192  void AddTest(testfun_type a_pFun);
193 
194  // Test Double Parser
195  int EqnTest(const string_type& a_str, double a_fRes, bool a_fPass);
196  int EqnTestWithVarChange(const string_type& a_str,
197  double a_fRes1,
198  double a_fVar1,
199  double a_fRes2,
200  double a_fVar2);
201  int ThrowTest(const string_type& a_str, int a_iErrc, bool a_bFail = true);
202 
203  // Test Int Parser
204  int EqnTestInt(const string_type& a_str, double a_fRes, bool a_fPass);
205 
206  // Test Bulkmode
207  int EqnTestBulk(const string_type& a_str, double a_fRes[4], bool a_fPass);
208  };
209  } // namespace Test
210 } // namespace mu
211 
212 #endif
213 
214