muParser API -  1.35
muParserTest.h
Go to the documentation of this file.
1 /*
2 
3  _____ __ _____________ _______ ______ ___________
4  / \| | \____ \__ \\_ __ \/ ___// __ \_ __ \
5  | Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
6  |__|_| /____/| __(____ /__| /____ >\___ >__|
7  \/ |__| \/ \/ \/
8  Copyright (C) 2004 - 2020 Ingo Berg
9 
10  Redistribution and use in source and binary forms, with or without modification, are permitted
11  provided that the following conditions are met:
12 
13  * Redistributions of source code must retain the above copyright notice, this list of
14  conditions and the following disclaimer.
15  * Redistributions in binary form must reproduce the above copyright notice, this list of
16  conditions and the following disclaimer in the documentation and/or other materials provided
17  with the distribution.
18 
19  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
20  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
21  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25  IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28 
29 #ifndef MU_PARSER_TEST_H
30 #define MU_PARSER_TEST_H
31 
32 #include <string>
33 #include <cstdlib>
34 #include <numeric> // for accumulate
35 #include "muParser.h"
36 #include "muParserInt.h"
37 
38 /** \file
39  \brief This file contains the parser test class.
40 */
41 
42 namespace mu
43 {
44  /** \brief Namespace for test cases. */
45  namespace Test
46  {
47  /** \brief Test cases for unit testing. */
48  class API_EXPORT_CXX ParserTester final
49  {
50  private:
51  static int c_iCount;
52 
53  static value_type f0() { return 42; };
54 
55  // Multiarg callbacks
56  static value_type f1of1(value_type v) { return v; };
57 
58  static value_type f1of2(value_type v, value_type) { return v; };
59  static value_type f2of2(value_type, value_type v) { return v; };
60 
61  static value_type f1of3(value_type v, value_type, value_type) { return v; };
62  static value_type f2of3(value_type, value_type v, value_type) { return v; };
63  static value_type f3of3(value_type, value_type, value_type v) { return v; };
64 
65  static value_type f1of4(value_type v, value_type, value_type, value_type) { return v; }
66  static value_type f2of4(value_type, value_type v, value_type, value_type) { return v; }
67  static value_type f3of4(value_type, value_type, value_type v, value_type) { return v; }
68  static value_type f4of4(value_type, value_type, value_type, value_type v) { return v; }
69 
70  static value_type f1of5(value_type v, value_type, value_type, value_type, value_type) { return v; }
71  static value_type f2of5(value_type, value_type v, value_type, value_type, value_type) { return v; }
72  static value_type f3of5(value_type, value_type, value_type v, value_type, value_type) { return v; }
73  static value_type f4of5(value_type, value_type, value_type, value_type v, value_type) { return v; }
74  static value_type f5of5(value_type, value_type, value_type, value_type, value_type v) { return v; }
75 
76  static value_type Min(value_type a_fVal1, value_type a_fVal2) { return (a_fVal1 < a_fVal2) ? a_fVal1 : a_fVal2; }
77  static value_type Max(value_type a_fVal1, value_type a_fVal2) { return (a_fVal1 > a_fVal2) ? a_fVal1 : a_fVal2; }
78 
79  static value_type plus2(value_type v1) { return v1 + 2; }
80  static value_type times3(value_type v1) { return v1 * 3; }
81  static value_type sqr(value_type v1) { return v1 * v1; }
82  static value_type sign(value_type v) { return -v; }
83  static value_type add(value_type v1, value_type v2) { return v1 + v2; }
84  static value_type land(value_type v1, value_type v2) { return (int)v1 & (int)v2; }
85 
86 
87  static value_type FirstArg(const value_type* a_afArg, int a_iArgc)
88  {
89  if (!a_iArgc)
90  throw mu::Parser::exception_type(_T("too few arguments for function FirstArg."));
91 
92  return a_afArg[0];
93  }
94 
95  static value_type LastArg(const value_type* a_afArg, int a_iArgc)
96  {
97  if (!a_iArgc)
98  throw mu::Parser::exception_type(_T("too few arguments for function LastArg."));
99 
100  return a_afArg[a_iArgc - 1];
101  }
102 
103  static value_type Sum(const value_type* a_afArg, int a_iArgc)
104  {
105  if (!a_iArgc)
106  throw mu::Parser::exception_type(_T("too few arguments for function sum."));
107 
108  value_type fRes = 0;
109  for (int i = 0; i < a_iArgc; ++i) fRes += a_afArg[i];
110  return fRes;
111  }
112 
113  static value_type Rnd(value_type v)
114  {
115  return (value_type)(1 + (v * std::rand() / (RAND_MAX + 1.0)));
116  }
117 
118  static value_type RndWithString(const char_type*)
119  {
120  return (value_type)(1.0 + (1000.0 * std::rand() / (RAND_MAX + 1.0)));
121  }
122 
123  static value_type Ping()
124  {
125  return 10;
126  }
127 
128  static value_type ValueOf(const char_type*)
129  {
130  return 123;
131  }
132 
133  static value_type StrFun1(const char_type* v1)
134  {
135  int val(0);
136  stringstream_type(v1) >> val;
137  return (value_type)val;
138  }
139 
140  static value_type StrFun2(const char_type* v1, value_type v2)
141  {
142  int val(0);
143  stringstream_type(v1) >> val;
144  return (value_type)(val + v2);
145  }
146 
147  static value_type StrFun3(const char_type* v1, value_type v2, value_type v3)
148  {
149  int val(0);
150  stringstream_type(v1) >> val;
151  return val + v2 + v3;
152  }
153 
154  static value_type StrFun4(const char_type* v1, value_type v2, value_type v3, value_type v4)
155  {
156  int val(0);
157  stringstream_type(v1) >> val;
158  return val + v2 + v3 + v4;
159  }
160 
161  static value_type StrFun5(const char_type* v1, value_type v2, value_type v3, value_type v4, value_type v5)
162  {
163  int val(0);
164  stringstream_type(v1) >> val;
165  return val + v2 + v3 + v4 + v5;
166  }
167 
168  static value_type StrToFloat(const char_type* a_szMsg)
169  {
170  value_type val(0);
171  stringstream_type(a_szMsg) >> val;
172  return val;
173  }
174 
175  // postfix operator callback
176  static value_type Mega(value_type a_fVal) { return a_fVal * (value_type)1e6; }
177  static value_type Micro(value_type a_fVal) { return a_fVal * (value_type)1e-6; }
178  static value_type Milli(value_type a_fVal) { return a_fVal / (value_type)1e3; }
179 
180  // Custom value recognition
181  static int IsHexVal(const char_type* a_szExpr, int* a_iPos, value_type* a_fVal);
182 
183  int TestNames();
184  int TestSyntax();
185  int TestMultiArg();
186  int TestPostFix();
187  int TestExpression();
188  int TestInfixOprt();
189  int TestBinOprt();
190  int TestVarConst();
191  int TestInterface();
192  int TestException();
193  int TestStrArg();
194  int TestIfThenElse();
195  int TestBulkMode();
196  int TestOssFuzzTestCases();
197 
198  void Abort() const;
199 
200  public:
201  typedef int (ParserTester::* testfun_type)();
202 
203  ParserTester();
204  int Run();
205 
206  private:
207  std::vector<testfun_type> m_vTestFun;
208  void AddTest(testfun_type a_pFun);
209 
210  // Test Double Parser
211  int EqnTest(const string_type& a_str, double a_fRes, bool a_fPass);
212  int EqnTestWithVarChange(const string_type& a_str, double a_fRes1, double a_fVar1, double a_fRes2, double a_fVar2);
213  int ThrowTest(const string_type& a_str, int a_iErrc, bool a_bFail = true);
214 
215  // Test Int Parser
216  int EqnTestInt(const string_type& a_str, double a_fRes, bool a_fPass);
217 
218  // Test Bulkmode
219  int EqnTestBulk(const string_type& a_str, double a_fRes[4], bool a_fPass);
220 
221  };
222  } // namespace Test
223 } // namespace mu
224 
225 #endif
226 
#define _T(x)
Activate this option in order to compile with OpenMP support.
Definition: muParserDef.h:69
Definition of a parser using integer value.
Definition of the standard floating point parser.
std::basic_stringstream< char_type, std::char_traits< char_type >, std::allocator< char_type > > stringstream_type
Typedef for easily using stringstream that respect the parser stringtype.
Definition: muParserDef.h:309
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:294
Test cases for unit testing.
Definition: muParserTest.h:48
Namespace for mathematical applications.
Definition: muParser.cpp:46
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:306
MUP_STRING_TYPE string_type
The stringtype used by the parser.
Definition: muParserDef.h:300
ParserError exception_type
Type of the error class.
Definition: muParserBase.h:98