Seamly2D
Code documentation
qmuparsertest.h
Go to the documentation of this file.
1 /***************************************************************************************************
2  **
3  ** Copyright (C) 2013 Ingo Berg
4  **
5  ** Permission is hereby granted, free of charge, to any person obtaining a copy of this
6  ** software and associated documentation files (the "Software"), to deal in the Software
7  ** without restriction, including without limitation the rights to use, copy, modify,
8  ** merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
9  ** permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  **
11  ** The above copyright notice and this permission notice shall be included in all copies or
12  ** substantial portions of the Software.
13  **
14  ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
15  ** NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
16  ** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17  ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  **
20  ******************************************************************************************************/
21 
22 #ifndef QMUPARSERTEST_H
23 #define QMUPARSERTEST_H
24 
25 #include <stdlib.h>
26 #include <QMetaObject>
27 #include <QObject>
28 #include <QString>
29 #include <QVector>
30 #include <QtGlobal>
31 #include <QRandomGenerator>
32 #include <locale>
33 #include <QRandomGenerator>
34 
35 #include "qmuparser.h"
36 #include "qmuparserdef.h"
37 #include "qmuparsererror.h"
38 
39 /**
40  * @file
41  * @brief This file contains the parser test class.
42  */
43 
44 namespace qmu
45 {
46 /**
47  * @brief Namespace for test cases.
48  */
49 namespace Test
50 {
51 //---------------------------------------------------------------------------------------------------------------------
52 /**
53  * @brief Test cases for unit testing.
54  *
55  * (C) 2004-2011 Ingo Berg
56  */
57 class QmuParserTester : public QObject // final
58 {
59  Q_OBJECT
60 public:
61  typedef int ( QmuParserTester::*testfun_type ) ();
62 
63  explicit QmuParserTester(QObject *parent = nullptr);
64 
65 private slots:
66  void Run();
67 
68 private:
69  Q_DISABLE_COPY(QmuParserTester)
71  static int c_iCount;
72 
73  void AddTest ( testfun_type a_pFun );
74 
75  // Test Double Parser
76  static int EqnTest ( const QString &a_str, double a_fRes, bool a_fPass );
77  static int EqnTestWithVarChange (const QString &a_str, double a_fRes1, double a_fVar1, double a_fRes2,
78  double a_fVar2);
79  static int ThrowTest ( const QString &a_str, int a_iErrc, bool a_bFail = true );
80 
81  // Test Bulkmode
82  static int EqnTestBulk(const QString &a_str, double a_fRes[4], bool a_fPass);
83 
84  // Multiarg callbacks
85  static qreal f1of1 ( qreal v )
86  {
87  return v;
88  }
89 
90  static qreal f1of2 ( qreal v, qreal )
91  {
92  return v;
93  }
94 
95  static qreal f2of2 ( qreal, qreal v )
96  {
97  return v;
98  }
99 
100  static qreal f1of3 ( qreal v, qreal, qreal )
101  {
102  return v;
103  }
104 
105  static qreal f2of3 ( qreal, qreal v, qreal )
106  {
107  return v;
108  }
109 
110  static qreal f3of3 ( qreal, qreal, qreal v )
111  {
112  return v;
113  }
114 
115  static qreal f1of4 ( qreal v, qreal, qreal, qreal )
116  {
117  return v;
118  }
119 
120  static qreal f2of4 ( qreal, qreal v, qreal, qreal )
121  {
122  return v;
123  }
124 
125  static qreal f3of4 ( qreal, qreal, qreal v, qreal )
126  {
127  return v;
128  }
129 
130  static qreal f4of4 ( qreal, qreal, qreal, qreal v )
131  {
132  return v;
133  }
134 
135  static qreal f1of5 ( qreal v, qreal, qreal, qreal, qreal )
136  {
137  return v;
138  }
139 
140  static qreal f2of5 ( qreal, qreal v, qreal, qreal, qreal )
141  {
142  return v;
143  }
144 
145  static qreal f3of5 ( qreal, qreal, qreal v, qreal, qreal )
146  {
147  return v;
148  }
149 
150  static qreal f4of5 ( qreal, qreal, qreal, qreal v, qreal )
151  {
152  return v;
153  }
154 
155  static qreal f5of5 ( qreal, qreal, qreal, qreal, qreal v )
156  {
157  return v;
158  }
159 
160  static qreal Min ( qreal a_fVal1, qreal a_fVal2 )
161  {
162  return ( a_fVal1 < a_fVal2 ) ? a_fVal1 : a_fVal2;
163  }
164 
165  static qreal Max ( qreal a_fVal1, qreal a_fVal2 )
166  {
167  return ( a_fVal1 > a_fVal2 ) ? a_fVal1 : a_fVal2;
168  }
169 
170  static qreal plus2 ( qreal v1 )
171  {
172  return v1 + 2;
173  }
174 
175  static qreal times3 ( qreal v1 )
176  {
177  return v1 * 3;
178  }
179 
180  static qreal sqr ( qreal v1 )
181  {
182  return v1 * v1;
183  }
184 
185  static qreal sign ( qreal v )
186  {
187  return -v;
188  }
189 
190  static qreal add ( qreal v1, qreal v2 )
191  {
192  return v1 + v2;
193  }
194 
195  static qreal land ( qreal v1, qreal v2 )
196  {
197  return static_cast<int>( v1 ) & static_cast<int>( v2 );
198  }
199 
200  static qreal FirstArg ( const qreal* a_afArg, int a_iArgc )
201  {
202  if ( a_iArgc == 0)
203  {
204  throw QmuParserError ( "too few arguments for function FirstArg." );
205  }
206 
207  return a_afArg[0];
208  }
209 
210  static qreal LastArg ( const qreal* a_afArg, int a_iArgc )
211  {
212  if ( a_iArgc == 0)
213  {
214  throw QmuParserError ( "too few arguments for function LastArg." );
215  }
216 
217  return a_afArg[a_iArgc - 1];
218  }
219 
220  static qreal Sum ( const qreal* a_afArg, int a_iArgc )
221  {
222  if ( a_iArgc == 0)
223  {
224  throw QmuParserError ( "too few arguments for function sum." );
225  }
226 
227  qreal fRes = 0;
228  for ( int i = 0; i < a_iArgc; ++i )
229  {
230  fRes += a_afArg[i];
231  }
232  return fRes;
233  }
234 
235  static qreal Rnd ( qreal v )
236  {
237  return static_cast<qreal>( ( 1 + ( v * QRandomGenerator::global()->generate() / ( RAND_MAX + 1.0 ) ) ) );
238  }
239 
240  static qreal RndWithString ( const char_type* )
241  {
242  return static_cast<qreal>( ( 1 + ( 1000.0f * static_cast<qreal>(QRandomGenerator::global()->generate()) / ( RAND_MAX + 1.0 ) ) ) );
243  }
244 
245  static qreal Ping()
246  {
247  return 10;
248  }
249 
250  static qreal ValueOf ( const QString & )
251  {
252  return 123;
253  }
254 
255  static qreal StrFun1 ( const QString & v1 )
256  {
257  int val = v1.toInt();
258  return static_cast<qreal>(val);
259  }
260 
261  static qreal StrFun2 ( const QString & v1, qreal v2 )
262  {
263  int val = v1.toInt();
264  return static_cast<qreal>( val + v2 );
265  }
266 
267  static qreal StrFun3 ( const QString & v1, qreal v2, qreal v3 )
268  {
269  int val = v1.toInt();
270  return val + v2 + v3;
271  }
272 
273  static qreal StrToFloat ( const QString & a_szMsg )
274  {
275  qreal val = a_szMsg.toDouble();
276  return val;
277  }
278 
279  // postfix operator callback
280  static qreal Mega ( qreal a_fVal )
281  {
282  return a_fVal * static_cast<qreal>( 1e6 );
283  }
284 
285  static qreal Micro ( qreal a_fVal )
286  {
287  return a_fVal * static_cast<qreal>( 1e-6 );
288  }
289 
290  static qreal Milli ( qreal a_fVal )
291  {
292  return a_fVal / static_cast<qreal>( 1e3 );
293  }
294 
295  // Custom value recognition
296  static int IsHexVal (const QString &a_szExpr, int *a_iPos, qreal *a_fVal, const QLocale &locale,
297  const QChar &decimal, const QChar &thousand);
298 
299  // cppcheck-suppress functionStatic
300  int TestNames();
301  // cppcheck-suppress functionStatic
302  int TestSyntax();
303  // cppcheck-suppress functionStatic
304  int TestMultiArg();
305  // cppcheck-suppress functionStatic
306  int TestPostFix();
307  // cppcheck-suppress functionStatic
308  int TestExpression();
309  // cppcheck-suppress functionStatic
310  int TestInfixOprt();
311  // cppcheck-suppress functionStatic
312  int TestBinOprt();
313  // cppcheck-suppress functionStatic
314  int TestVarConst();
315  // cppcheck-suppress functionStatic
316  int TestInterface();
317  // cppcheck-suppress functionStatic
318  int TestException();
319  // cppcheck-suppress functionStatic
320  int TestStrArg();
321  // cppcheck-suppress functionStatic
322  int TestIfThenElse();
323  // cppcheck-suppress functionStatic
324  int TestBulkMode();
325 
326  static void Abort();
327 };
328 } // namespace Test
329 } // namespace qmu
330 
331 #endif
Error class of the parser.
Test cases for unit testing.
Definition: qmuparsertest.h:58
static qreal ValueOf(const QString &)
static qreal land(qreal v1, qreal v2)
QmuParserTester(QObject *parent=nullptr)
static qreal f3of5(qreal, qreal, qreal v, qreal, qreal)
static qreal f2of2(qreal, qreal v)
Definition: qmuparsertest.h:95
int(QmuParserTester::* testfun_type)()
Definition: qmuparsertest.h:61
static qreal f2of3(qreal, qreal v, qreal)
static qreal StrToFloat(const QString &a_szMsg)
static qreal f1of2(qreal v, qreal)
Definition: qmuparsertest.h:90
static void Abort()
Internal error in test class Test is going to be aborted.
static qreal f4of4(qreal, qreal, qreal, qreal v)
static int EqnTest(const QString &a_str, double a_fRes, bool a_fPass)
Evaluate a tet expression.
static qreal f4of5(qreal, qreal, qreal, qreal v, qreal)
static qreal Mega(qreal a_fVal)
static int ThrowTest(const QString &a_str, int a_iErrc, bool a_bFail=true)
int TestNames()
Check muParser name restriction enforcement.
static qreal f1of4(qreal v, qreal, qreal, qreal)
static qreal f2of5(qreal, qreal v, qreal, qreal, qreal)
static qreal f3of4(qreal, qreal, qreal v, qreal)
static qreal add(qreal v1, qreal v2)
static qreal StrFun3(const QString &v1, qreal v2, qreal v3)
static qreal sign(qreal v)
static qreal Sum(const qreal *a_afArg, int a_iArgc)
static qreal f2of4(qreal, qreal v, qreal, qreal)
static qreal f1of1(qreal v)
Definition: qmuparsertest.h:85
static qreal StrFun1(const QString &v1)
static qreal RndWithString(const char_type *)
static int EqnTestWithVarChange(const QString &a_str, double a_fRes1, double a_fVar1, double a_fRes2, double a_fVar2)
Evaluate a tet expression.
static qreal f3of3(qreal, qreal, qreal v)
static qreal sqr(qreal v1)
static qreal f1of5(qreal v, qreal, qreal, qreal, qreal)
static qreal Min(qreal a_fVal1, qreal a_fVal2)
static qreal Rnd(qreal v)
static qreal Max(qreal a_fVal1, qreal a_fVal2)
void AddTest(testfun_type a_pFun)
static qreal f5of5(qreal, qreal, qreal, qreal, qreal v)
static qreal LastArg(const qreal *a_afArg, int a_iArgc)
static qreal f1of3(qreal v, qreal, qreal)
static qreal plus2(qreal v1)
QVector< testfun_type > m_vTestFun
Definition: qmuparsertest.h:70
static qreal Milli(qreal a_fVal)
static qreal FirstArg(const qreal *a_afArg, int a_iArgc)
static int IsHexVal(const QString &a_szExpr, int *a_iPos, qreal *a_fVal, const QLocale &locale, const QChar &decimal, const QChar &thousand)
static qreal StrFun2(const QString &v1, qreal v2)
static qreal times3(qreal v1)
static qreal Micro(qreal a_fVal)
static int EqnTestBulk(const QString &a_str, double a_fRes[4], bool a_fPass)
Test an expression in Bulk Mode.
Namespace for mathematical applications.
string_type::value_type char_type
The character type used by the parser.
Definition: qmuparserdef.h:202
Definition of the standard floating point parser.
This file contains standard definitions used by the parser.
This file defines the error class used by the parser.