muParser: muParserTest.h Source File

- Main Page
- Namespaces
- Classes
- Files
- Examples

# muParserTest.h

Go to the documentation of this file.

    00001 /*
    00002                  __________
    00003     _____   __ __\______   \_____  _______  ______  ____ _______
    00004    /     \ |  |  \|     ___/\__  \ \_  __ \/  ___/_/ __ \\_  __ \
    00005   |  Y Y  \|  |  /|    |     / __ \_|  | \/\___ \ \  ___/ |  | \/
    00006   |__|_|  /|____/ |____|    (____  /|__|  /____  > \___  >|__|
    00007         \/                       \/            \/      \/
    00008   Copyright (C) 2004-2008 Ingo Berg
    00009
    00010   Permission is hereby granted, free of charge, to any person obtaining a copy of this
    00011   software and associated documentation files (the "Software"), to deal in the Software
    00012   without restriction, including without limitation the rights to use, copy, modify,
    00013   merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
    00014   permit persons to whom the Software is furnished to do so, subject to the following conditions:
    00015
    00016   The above copyright notice and this permission notice shall be included in all copies or
    00017   substantial portions of the Software.
    00018
    00019   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
    00020   NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
    00021   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
    00022   DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00023   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    00024 */
    00025
    00026 #ifndef MU_PARSER_TEST_H
    00027 #define MU_PARSER_TEST_H
    00028
    00029 #include <string>
    00030 #include <cstdlib>
    00031 #include <numeric> // for accumulate
    00032 #include "muParser.h"
    00033 #include "muParserInt.h"
    00034
    00039 namespace mu
    00040 {
    00042   namespace Test
    00043   {
    00044     //------------------------------------------------------------------------------
    00049     class ParserTester // final
    00050     {
    00051     private:
    00052         // Multiarg callbacks
    00053         static value_type f1of1(value_type v) { return v;};
    00054
    00055         static value_type f1of2(value_type v, value_type  ) {return v;};
    00056         static value_type f2of2(value_type  , value_type v) {return v;};
    00057
    00058         static value_type f1of3(value_type v, value_type  , value_type  ) {return v;};
    00059         static value_type f2of3(value_type  , value_type v, value_type  ) {return v;};
    00060         static value_type f3of3(value_type  , value_type  , value_type v) {return v;};
    00061
    00062         static value_type f1of4(value_type v, value_type,   value_type  , value_type  ) {return v;}
    00063         static value_type f2of4(value_type  , value_type v, value_type  , value_type  ) {return v;}
    00064         static value_type f3of4(value_type  , value_type,   value_type v, value_type  ) {return v;}
    00065         static value_type f4of4(value_type  , value_type,   value_type  , value_type v) {return v;}
    00066
    00067               static value_type f1of5(value_type v, value_type,   value_type  , value_type  , value_type  ) { return v; }
    00068               static value_type f2of5(value_type  , value_type v, value_type  , value_type  , value_type  ) { return v; }
    00069               static value_type f3of5(value_type  , value_type,   value_type v, value_type  , value_type  ) { return v; }
    00070               static value_type f4of5(value_type  , value_type,   value_type  , value_type v, value_type  ) { return v; }
    00071               static value_type f5of5(value_type  , value_type,   value_type  , value_type  , value_type v) { return v; }
    00072
    00073         static value_type Min(value_type a_fVal1, value_type a_fVal2) { return (a_fVal1<a_fVal2) ? a_fVal1 : a_fVal2; }
    00074             static value_type Max(value_type a_fVal1, value_type a_fVal2) { return (a_fVal1>a_fVal2) ? a_fVal1 : a_fVal2; }
    00075
    00076         static value_type plus2(value_type v1) { return v1+2; }
    00077         static value_type times3(value_type v1) { return v1*3; }
    00078         static value_type sqr(value_type v1) { return v1*v1; }
    00079         static value_type sign(value_type v) { return -v; }
    00080
    00081         static value_type FirstArg(const value_type* a_afArg, int a_iArgc)
    00082         {
    00083           if (!a_iArgc)
    00084             throw mu::Parser::exception_type( _T("too few arguments for function FirstArg.") );
    00085
    00086           return  a_afArg[0];
    00087         }
    00088
    00089         static value_type LastArg(const value_type* a_afArg, int a_iArgc)
    00090         {
    00091           if (!a_iArgc)
    00092             throw mu::Parser::exception_type( _T("too few arguments for function LastArg.") );
    00093
    00094           return  a_afArg[a_iArgc-1];
    00095         }
    00096
    00097         static value_type Sum(const value_type* a_afArg, int a_iArgc)
    00098         {
    00099           if (!a_iArgc)
    00100             throw mu::Parser::exception_type( _T("too few arguments for function sum.") );
    00101
    00102           value_type fRes=0;
    00103           for (int i=0; i<a_iArgc; ++i) fRes += a_afArg[i];
    00104           return fRes;
    00105         }
    00106
    00107         static value_type Rnd(value_type v)
    00108         {
    00109           return (value_type)(1+(v*std::rand()/(RAND_MAX+1.0)));
    00110         }
    00111
    00112         static value_type RndWithString(const char_type*)
    00113         {
    00114           return (value_type)( 1 + (1000.0f * std::rand() / (RAND_MAX + 1.0) ) );
    00115         }
    00116
    00117         static value_type Ping()
    00118         {
    00119           return 10;
    00120         }
    00121
    00122         static value_type ValueOf(const char_type*)
    00123         {
    00124           return 123;
    00125         }
    00126
    00127         static value_type StrFun1(const char_type* v1)
    00128         {
    00129           int val(0);
    00130           stringstream_type(v1) >> val;
    00131           return val;
    00132         }
    00133
    00134         static value_type StrFun2(const char_type* v1, value_type v2)
    00135         {
    00136           int val(0);
    00137           stringstream_type(v1) >> val;
    00138           return val + v2;
    00139         }
    00140
    00141         static value_type StrFun3(const char_type* v1, value_type v2, value_type v3)
    00142         {
    00143           int val(0);
    00144           stringstream_type(v1) >> val;
    00145           return val + v2 + v3;
    00146         }
    00147
    00148         static value_type StrToFloat(const char_type* a_szMsg)
    00149         {
    00150           double val(0);
    00151           stringstream_type(a_szMsg) >> val;
    00152           return val;
    00153
    00154 //          using namespace std; // atof is for some compilers in std for some not...
    00155 //          return atof(a_szMsg);
    00156         }
    00157
    00158         // postfix operator callback
    00159               static value_type Milli(value_type v) { return v/(value_type)1e3; }
    00160
    00161         static int c_iCount;
    00162
    00163               int TestNames();
    00164               int TestSyntax();
    00165               int TestMultiArg();
    00166               int TestVolatile();
    00167               int TestPostFix();
    00168               int TestExpression();
    00169               int TestInfixOprt();
    00170               int TestBinOprt();
    00171               int TestVarConst();
    00172               int TestInterface();
    00173               int TestException();
    00174         int TestStrArg();
    00175
    00176         void Abort() const;
    00177
    00178     public:
    00179         typedef int (ParserTester::*testfun_type)();
    00180
    00181               ParserTester();
    00182               void Run();
    00183
    00184     private:
    00185         std::vector<testfun_type> m_vTestFun;
    00186               void AddTest(testfun_type a_pFun);
    00187
    00188         // Test Double Parser
    00189         int EqnTest(const string_type& a_str, double a_fRes, bool a_fPass);
    00190         int ThrowTest(const string_type& a_str, int a_iErrc, bool a_bFail = true);
    00191
    00192         // Test Int Parser
    00193         int EqnTestInt(const string_type& a_str, double a_fRes, bool a_fPass);
    00194     };
    00195   } // namespace Test
    00196 } // namespace mu
    00197
    00198 #endif
    00199
    00200