NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
muParserTest.h
Go to the documentation of this file.
1/*
2 __________
3 _____ __ __\______ \_____ _______ ______ ____ _______
4 / \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
5 | Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
6 |__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
7 \/ \/ \/ \/
8 Copyright (C) 2011 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
39namespace mu
40{
42 namespace Test
43 {
44 //------------------------------------------------------------------------------
49 class ParserTester // final
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( _nrT("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( _nrT("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( _nrT("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
123 {
124 return 10;
125 }
126
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
140 {
141 int val(0);
142 stringstream_type(v1) >> val;
143 return (value_type)(val + v2);
144 }
145
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
181 void Abort() const;
182
183 public:
184 typedef int (ParserTester::*testfun_type)();
185
186 ParserTester();
187 void Run();
188
189 private:
190 std::vector<testfun_type> m_vTestFun;
191 void AddTest(testfun_type a_pFun);
192
193 // Test Double Parser
194 int EqnTest(const string_type& a_str, double a_fRes, bool a_fPass);
195 int EqnTestWithVarChange(const string_type& a_str,
196 double a_fRes1,
197 double a_fVar1,
198 double a_fRes2,
199 double a_fVar2);
200 int ThrowTest(const string_type& a_str, int a_iErrc, bool a_bFail = true);
201
202 // Test Int Parser
203 int EqnTestInt(const string_type& a_str, double a_fRes, bool a_fPass);
204 };
205 } // namespace Test
206} // namespace mu
207
208#endif
209
210
ParserError exception_type
Type of the error class.
Definition: muParserBase.h:96
Test cases for unit testing.
Definition: muParserTest.h:50
static value_type f2of4(value_type, value_type v, value_type, value_type)
Definition: muParserTest.h:65
static value_type ValueOf(const char_type *)
Definition: muParserTest.h:127
static value_type Sum(const value_type *a_afArg, int a_iArgc)
Definition: muParserTest.h:102
static value_type f2of3(value_type, value_type v, value_type)
Definition: muParserTest.h:61
static value_type StrFun3(const char_type *v1, value_type v2, value_type v3)
Definition: muParserTest.h:146
std::vector< testfun_type > m_vTestFun
Definition: muParserTest.h:190
static value_type Mega(value_type a_fVal)
Definition: muParserTest.h:161
void Abort() const
Internal error in test class Test is going to be aborted.
static value_type Min(value_type a_fVal1, value_type a_fVal2)
Definition: muParserTest.h:75
int(ParserTester::* testfun_type)()
Definition: muParserTest.h:184
int ThrowTest(const string_type &a_str, int a_iErrc, bool a_bFail=true)
int EqnTestInt(const string_type &a_str, double a_fRes, bool a_fPass)
static value_type add(value_type v1, value_type v2)
Definition: muParserTest.h:82
static value_type f4of5(value_type, value_type, value_type, value_type v, value_type)
Definition: muParserTest.h:72
static value_type Micro(value_type a_fVal)
Definition: muParserTest.h:162
static value_type f3of3(value_type, value_type, value_type v)
Definition: muParserTest.h:62
static value_type FirstArg(const value_type *a_afArg, int a_iArgc)
Definition: muParserTest.h:86
static value_type plus2(value_type v1)
Definition: muParserTest.h:78
static value_type land(value_type v1, value_type v2)
Definition: muParserTest.h:83
static value_type times3(value_type v1)
Definition: muParserTest.h:79
static value_type RndWithString(const char_type *)
Definition: muParserTest.h:117
static value_type LastArg(const value_type *a_afArg, int a_iArgc)
Definition: muParserTest.h:94
static value_type Ping()
Definition: muParserTest.h:122
static value_type Rnd(value_type v)
Definition: muParserTest.h:112
static value_type f2of2(value_type, value_type v)
Definition: muParserTest.h:58
static value_type f4of4(value_type, value_type, value_type, value_type v)
Definition: muParserTest.h:67
static value_type Milli(value_type a_fVal)
Definition: muParserTest.h:163
static value_type f1of4(value_type v, value_type, value_type, value_type)
Definition: muParserTest.h:64
static value_type Max(value_type a_fVal1, value_type a_fVal2)
Definition: muParserTest.h:76
static value_type sign(value_type v)
Definition: muParserTest.h:81
void AddTest(testfun_type a_pFun)
int EqnTest(const string_type &a_str, double a_fRes, bool a_fPass)
Evaluate a tet expression.
static value_type f3of4(value_type, value_type, value_type v, value_type)
Definition: muParserTest.h:66
static value_type f2of5(value_type, value_type v, value_type, value_type, value_type)
Definition: muParserTest.h:70
int EqnTestWithVarChange(const string_type &a_str, double a_fRes1, double a_fVar1, double a_fRes2, double a_fVar2)
Evaluate a tet expression.
static value_type StrFun1(const char_type *v1)
Definition: muParserTest.h:132
int TestNames()
Check muParser name restriction enforcement.
static value_type f1of3(value_type v, value_type, value_type)
Definition: muParserTest.h:60
static value_type f1of1(value_type v)
Definition: muParserTest.h:55
static value_type f3of5(value_type, value_type, value_type v, value_type, value_type)
Definition: muParserTest.h:71
static value_type StrToFloat(const char_type *a_szMsg)
Definition: muParserTest.h:153
static value_type f5of5(value_type, value_type, value_type, value_type, value_type v)
Definition: muParserTest.h:73
static value_type f1of2(value_type v, value_type)
Definition: muParserTest.h:57
static value_type sqr(value_type v1)
Definition: muParserTest.h:80
static value_type f1of5(value_type v, value_type, value_type, value_type, value_type)
Definition: muParserTest.h:69
static value_type StrFun2(const char_type *v1, value_type v2)
Definition: muParserTest.h:139
static int IsHexVal(const char_type *a_szExpr, int *a_iPos, value_type *a_fVal)
Definition of the standard floating point parser.
#define _nrT(x)
Activate this option in order to compile with OpenMP support.
Definition: muParserDef.h:62
Definition of a parser using integer value.
Namespace for mathematical applications.
Definition: muParser.cpp:53
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:251
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:263
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:268
std::string string_type
The stringtype used by the parser.
Definition: muParserDef.h:257