3 _____ __ _____________ _______ ______ ___________
4 / \| | \____ \__ \\_ __ \/ ___// __ \_ __ \
5 | Y Y \ | / |_> > __ \| | \/\___ \\ ___/| | \/
6 |__|_| /____/| __(____ /__| /____ >\___ >__|
8 Copyright (C) 2004 - 2020 Ingo Berg
10 Redistribution and use in source and binary forms, with or without modification, are permitted
11 provided that the following conditions are met:
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.
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.
29 #ifndef MU_PARSER_TEST_H
30 #define MU_PARSER_TEST_H
34 #include <numeric> // for accumulate
36 #include "muParserInt.h"
39 \brief This file contains the parser test class.
44 /** \brief Namespace for test cases. */
47 /** \brief Test cases for unit testing. */
48 class API_EXPORT_CXX ParserTester final
53 static value_type f0() { return 42; };
56 static value_type f1of1(value_type v) { return v; };
58 static value_type f1of2(value_type v, value_type) { return v; };
59 static value_type f2of2(value_type, value_type v) { return v; };
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; };
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; }
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; }
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; }
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; }
87 static value_type FirstArg(const value_type* a_afArg, int a_iArgc)
90 throw mu::Parser::exception_type(_T("too few arguments for function FirstArg."));
95 static value_type LastArg(const value_type* a_afArg, int a_iArgc)
98 throw mu::Parser::exception_type(_T("too few arguments for function LastArg."));
100 return a_afArg[a_iArgc - 1];
103 static value_type Sum(const value_type* a_afArg, int a_iArgc)
106 throw mu::Parser::exception_type(_T("too few arguments for function sum."));
109 for (int i = 0; i < a_iArgc; ++i) fRes += a_afArg[i];
113 static value_type Rnd(value_type v)
115 return (value_type)(1 + (v * std::rand() / (RAND_MAX + 1.0)));
118 static value_type RndWithString(const char_type*)
120 return (value_type)(1.0 + (1000.0 * std::rand() / (RAND_MAX + 1.0)));
123 static value_type Ping()
128 static value_type ValueOf(const char_type*)
133 static value_type StrFun1(const char_type* v1)
136 stringstream_type(v1) >> val;
137 return (value_type)val;
140 static value_type StrFun2(const char_type* v1, value_type v2)
143 stringstream_type(v1) >> val;
144 return (value_type)(val + v2);
147 static value_type StrFun3(const char_type* v1, value_type v2, value_type v3)
150 stringstream_type(v1) >> val;
151 return val + v2 + v3;
154 static value_type StrFun4(const char_type* v1, value_type v2, value_type v3, value_type v4)
157 stringstream_type(v1) >> val;
158 return val + v2 + v3 + v4;
161 static value_type StrFun5(const char_type* v1, value_type v2, value_type v3, value_type v4, value_type v5)
164 stringstream_type(v1) >> val;
165 return val + v2 + v3 + v4 + v5;
168 static value_type StrToFloat(const char_type* a_szMsg)
171 stringstream_type(a_szMsg) >> val;
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; }
180 // Custom value recognition
181 static int IsHexVal(const char_type* a_szExpr, int* a_iPos, value_type* a_fVal);
187 int TestExpression();
194 int TestIfThenElse();
196 int TestOssFuzzTestCases();
201 typedef int (ParserTester::* testfun_type)();
207 std::vector<testfun_type> m_vTestFun;
208 void AddTest(testfun_type a_pFun);
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);
216 int EqnTestInt(const string_type& a_str, double a_fRes, bool a_fPass);
219 int EqnTestBulk(const string_type& a_str, double a_fRes[4], bool a_fPass);