NCBI C++ ToolKit
muParserTest.h
Go to the documentation of this file.

Go to the SVN repository for 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 
35 /** \file
36  \brief This file contains the parser test class.
37 */
38 
39 namespace mu
40 {
41  /** \brief Namespace for test cases. */
42  namespace Test
43  {
44  //------------------------------------------------------------------------------
45  /** \brief Test cases for unit testing.
46 
47  (C) 2004-2006 Ingo Berg
48  */
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 
66 
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( MUP_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( MUP_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( MUP_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 
108  {
109  return (value_type)(1+(v*std::rand()/(RAND_MAX+1.0)));
110  }
111 
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 
135  {
136  int val(0);
137  stringstream_type(v1) >> val;
138  return val + v2;
139  }
140 
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:
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 
ParserError exception_type
Type of the error class.
Definition: muParserBase.h:92
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:63
static value_type ValueOf(const char_type *)
Definition: muParserTest.h:122
static value_type Sum(const value_type *a_afArg, int a_iArgc)
Definition: muParserTest.h:97
static value_type f2of3(value_type, value_type v, value_type)
Definition: muParserTest.h:59
static value_type StrFun3(const char_type *v1, value_type v2, value_type v3)
Definition: muParserTest.h:141
std::vector< testfun_type > m_vTestFun
Definition: muParserTest.h:185
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:73
int(ParserTester::* testfun_type)()
Definition: muParserTest.h:179
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 f4of5(value_type, value_type, value_type, value_type v, value_type)
Definition: muParserTest.h:70
static value_type Milli(value_type v)
Definition: muParserTest.h:159
static value_type f3of3(value_type, value_type, value_type v)
Definition: muParserTest.h:60
static value_type FirstArg(const value_type *a_afArg, int a_iArgc)
Definition: muParserTest.h:81
static value_type plus2(value_type v1)
Definition: muParserTest.h:76
static value_type times3(value_type v1)
Definition: muParserTest.h:77
static value_type RndWithString(const char_type *)
Definition: muParserTest.h:112
int TestVolatile()
Test volatile (nonoptimizeable functions).
static value_type LastArg(const value_type *a_afArg, int a_iArgc)
Definition: muParserTest.h:89
static value_type Ping()
Definition: muParserTest.h:117
static value_type Rnd(value_type v)
Definition: muParserTest.h:107
static value_type f2of2(value_type, value_type v)
Definition: muParserTest.h:56
static value_type f4of4(value_type, value_type, value_type, value_type v)
Definition: muParserTest.h:65
static value_type f1of4(value_type v, value_type, value_type, value_type)
Definition: muParserTest.h:62
static value_type Max(value_type a_fVal1, value_type a_fVal2)
Definition: muParserTest.h:74
static value_type sign(value_type v)
Definition: muParserTest.h:79
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:64
static value_type f2of5(value_type, value_type v, value_type, value_type, value_type)
Definition: muParserTest.h:68
static value_type StrFun1(const char_type *v1)
Definition: muParserTest.h:127
int TestNames()
Check muParser name restriction enforcement.
static value_type f1of3(value_type v, value_type, value_type)
Definition: muParserTest.h:58
static value_type f1of1(value_type v)
Definition: muParserTest.h:53
static value_type f3of5(value_type, value_type, value_type v, value_type, value_type)
Definition: muParserTest.h:69
static value_type StrToFloat(const char_type *a_szMsg)
Definition: muParserTest.h:148
static value_type f5of5(value_type, value_type, value_type, value_type, value_type v)
Definition: muParserTest.h:71
static value_type f1of2(value_type v, value_type)
Definition: muParserTest.h:55
static value_type sqr(value_type v1)
Definition: muParserTest.h:78
static value_type f1of5(value_type v, value_type, value_type, value_type, value_type)
Definition: muParserTest.h:67
static value_type StrFun2(const char_type *v1, value_type v2)
Definition: muParserTest.h:134
static void Test(const char *bind1, SQLSMALLINT type1, const char *bind2, SQLSMALLINT type2)
Definition: convert_error.c:15
const CVect2< U > & v2
Definition: globals.hpp:440
unsigned int
A callback function used to compare two keys in a database.
Definition: types.hpp:1210
int i
#define MUP_T
Definition: muParserDef.h:62
Definition of a parser using integer value.
Definition of the standard floating point parser.
Namespace for mathematical applications.
Definition: muParser.h:41
string_type::value_type char_type
The character type used by the parser.
Definition: muParserDef.h:246
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:251
double value_type
The numeric datatype used by the parser.
Definition: muParserDef.h:228
std::string string_type
The stringtype used by the parser.
Definition: muParserDef.h:234
Modified on Sun Jun 16 04:32:09 2024 by modify_doxy.py rev. 669887