| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#ifndef MU_PARSER_TEST_H |
| 27 |
#define MU_PARSER_TEST_H |
| 28 |
|
| 29 |
#include <string> |
| 30 |
#include <cstdlib> |
| 31 |
#include <numeric> |
| 32 |
#include "muParser.h" |
| 33 |
#include "muParserInt.h" |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
namespace mu |
| 40 |
{ |
| 41 |
|
| 42 |
namespace Test |
| 43 |
{ |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
class ParserTester |
| 50 |
{ |
| 51 |
private: |
| 52 |
static int c_iCount; |
| 53 |
|
| 54 |
|
| 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 |
|
| 64 |
static value_type f1of4(value_type v, value_type, value_type , value_type ) {return v;} |
| 65 |
static value_type f2of4(value_type , value_type v, value_type , value_type ) {return v;} |
| 66 |
static value_type f3of4(value_type , value_type, value_type v, value_type ) {return v;} |
| 67 |
static value_type f4of4(value_type , value_type, value_type , value_type v) {return v;} |
| 68 |
|
| 69 |
static value_type f1of5(value_type v, value_type, value_type , value_type , value_type ) { return v; } |
| 70 |
static value_type f2of5(value_type , value_type v, value_type , value_type , value_type ) { return v; } |
| 71 |
static value_type f3of5(value_type , value_type, value_type v, value_type , value_type ) { return v; } |
| 72 |
static value_type f4of5(value_type , value_type, value_type , value_type v, value_type ) { return v; } |
| 73 |
static value_type f5of5(value_type , value_type, value_type , value_type , value_type v) { return v; } |
| 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 |
|
| 112 |
static value_type Rnd(value_type v) |
| 113 |
{ |
| 114 |
return (value_type)(1+(v*std::rand()/(RAND_MAX+1.0))); |
| 115 |
} |
| 116 |
|
| 117 |
static value_type RndWithString(const char_type*) |
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 204 |
int EqnTestInt(const string_type& a_str, double a_fRes, bool a_fPass); |
| 205 |
|
| 206 |
|
| 207 |
int EqnTestBulk(const string_type& a_str, double a_fRes[4], bool a_fPass); |
| 208 |
}; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
#endif |
| 213 |
|
| 214 |
|