MADNESS  version 0.9
muParserTest.h
Go to the documentation of this file.
1 /*
2  __________
3  _____ __ __\______ \_____ _______ ______ ____ _______
4  / \ | | \| ___/\__ \ \_ __ \/ ___/_/ __ \\_ __ \
5  | Y Y \| | /| | / __ \_| | \/\___ \ \ ___/ | | \/
6  |__|_| /|____/ |____| (____ /|__| /____ > \___ >|__|
7  \/ \/ \/ \/
8  Copyright (C) 2004-2008 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  //------------------------------------------------------------------------------
49  class ParserTester // final
50  {
51  private:
52  // Multiarg callbacks
53  static value_type f1of1(value_type v) { return v;};
54 
55  static value_type f1of2(value_type v, value_type ) {return v;};
56  static value_type f2of2(value_type , value_type v) {return v;};
57 
58  static value_type f1of3(value_type v, value_type , value_type ) {return v;};
59  static value_type f2of3(value_type , value_type v, value_type ) {return v;};
60  static value_type f3of3(value_type , value_type , value_type v) {return v;};
61 
62  static value_type f1of4(value_type v, value_type, value_type , value_type ) {return v;}
63  static value_type f2of4(value_type , value_type v, value_type , value_type ) {return v;}
64  static value_type f3of4(value_type , value_type, value_type v, value_type ) {return v;}
65  static value_type f4of4(value_type , value_type, value_type , value_type v) {return v;}
66 
67  static value_type f1of5(value_type v, value_type, value_type , value_type , value_type ) { return v; }
68  static value_type f2of5(value_type , value_type v, value_type , value_type , value_type ) { return v; }
69  static value_type f3of5(value_type , value_type, value_type v, value_type , value_type ) { return v; }
70  static value_type f4of5(value_type , value_type, value_type , value_type v, value_type ) { return v; }
71  static value_type f5of5(value_type , value_type, value_type , value_type , value_type v) { return v; }
72 
73  static value_type Min(value_type a_fVal1, value_type a_fVal2) { return (a_fVal1<a_fVal2) ? a_fVal1 : a_fVal2; }
74  static value_type Max(value_type a_fVal1, value_type a_fVal2) { return (a_fVal1>a_fVal2) ? a_fVal1 : a_fVal2; }
75 
76  static value_type plus2(value_type v1) { return v1+2; }
77  static value_type times3(value_type v1) { return v1*3; }
78  static value_type sqr(value_type v1) { return v1*v1; }
79  static value_type sign(value_type v) { return -v; }
80 
81  static value_type FirstArg(const value_type* a_afArg, int a_iArgc)
82  {
83  if (!a_iArgc)
84  throw mu::Parser::exception_type( _T("too few arguments for function FirstArg.") );
85 
86  return a_afArg[0];
87  }
88 
89  static value_type LastArg(const value_type* a_afArg, int a_iArgc)
90  {
91  if (!a_iArgc)
92  throw mu::Parser::exception_type( _T("too few arguments for function LastArg.") );
93 
94  return a_afArg[a_iArgc-1];
95  }
96 
97  static value_type Sum(const value_type* a_afArg, int a_iArgc)
98  {
99  if (!a_iArgc)
100  throw mu::Parser::exception_type( _T("too few arguments for function sum.") );
101 
102  value_type fRes=0;
103  for (int i=0; i<a_iArgc; ++i) fRes += a_afArg[i];
104  return fRes;
105  }
106 
107  static value_type Rnd(value_type v)
108  {
109  return (value_type)(1+(v*std::rand()/(RAND_MAX+1.0)));
110  }
111 
112  static value_type RndWithString(const char_type*)
113  {
114  return (value_type)( 1 + (1000.0f * std::rand() / (RAND_MAX + 1.0) ) );
115  }
116 
117  static value_type Ping()
118  {
119  return 10;
120  }
121 
122  static value_type ValueOf(const char_type*)
123  {
124  return 123;
125  }
126 
127  static value_type StrFun1(const char_type* v1)
128  {
129  int val(0);
130  stringstream_type(v1) >> val;
131  return val;
132  }
133 
134  static value_type StrFun2(const char_type* v1, value_type v2)
135  {
136  int val(0);
137  stringstream_type(v1) >> val;
138  return val + v2;
139  }
140 
141  static value_type StrFun3(const char_type* v1, value_type v2, value_type v3)
142  {
143  int val(0);
144  stringstream_type(v1) >> val;
145  return val + v2 + v3;
146  }
147 
148  static value_type StrToFloat(const char_type* a_szMsg)
149  {
150  double val(0);
151  stringstream_type(a_szMsg) >> val;
152  return val;
153 
154 // using namespace std; // atof is for some compilers in std for some not...
155 // return atof(a_szMsg);
156  }
157 
158  // postfix operator callback
159  static value_type Milli(value_type v) { return v/(value_type)1e3; }
160 
161  static int c_iCount;
162 
163  int TestNames();
164  int TestSyntax();
165  int TestMultiArg();
166  int TestVolatile();
167  int TestPostFix();
168  int TestExpression();
169  int TestInfixOprt();
170  int TestBinOprt();
171  int TestVarConst();
172  int TestInterface();
173  int TestException();
174  int TestStrArg();
175 
176  void Abort() const;
177 
178  public:
179  typedef int (ParserTester::*testfun_type)();
180 
181  ParserTester();
182  void Run();
183 
184  private:
185  std::vector<testfun_type> m_vTestFun;
186  void AddTest(testfun_type a_pFun);
187 
188  // Test Double Parser
189  int EqnTest(const string_type& a_str, double a_fRes, bool a_fPass);
190  int ThrowTest(const string_type& a_str, int a_iErrc, bool a_bFail = true);
191 
192  // Test Int Parser
193  int EqnTestInt(const string_type& a_str, double a_fRes, bool a_fPass);
194  };
195  } // namespace Test
196 } // namespace mu
197 
198 #endif
199 
200 
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:245
const muChar_t muFun0_t a_pFun
Definition: muParserDLL.h:84
MUP_BASETYPE value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:222
Test cases for unit testing.
Definition: muParserTest.h:49
Namespace for mathematical applications.
Definition: muParser.cpp:47
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:240
MUP_STRING_TYPE string_type
The stringtype used by the parser.
Definition: muParserDef.h:228
#define _T
Definition: muParserDef.h:62
ParserError exception_type
Type of the error class.
Definition: muParserBase.h:92
int(ParserTester::* testfun_type)()
Definition: muParserTest.h:179