⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 muparsertest.cpp

📁 Mathematical expressions parser library
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*
  Copyright (C) 2004, 2005 Ingo Berg

  Permission is hereby granted, free of charge, to any person obtaining a copy of this 
  software and associated documentation files (the "Software"), to deal in the Software
  without restriction, including without limitation the rights to use, copy, modify, 
  merge, publish, distribute, sublicense, and/or sell copies of the Software, and to 
  permit persons to whom the Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all copies or 
  substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
*/
#include "muParserTest.h"

#include <cstdio>
#include <cmath>
#include <iostream>

#define PARSER_CONST_PI  3.141592653589793238462643
#define PARSER_CONST_E   2.718281828459045235360287

using namespace std;


namespace mu
{

namespace Test
{
  int ParserTester::c_iCount = 0;

  //---------------------------------------------------------------------------
  ParserTester::ParserTester()
    :m_vTestFun()
    ,m_stream(&std::cout)
  {
    AddTest(&ParserTester::TestNames);
  	AddTest(&ParserTester::TestSyntax);
  	AddTest(&ParserTester::TestPostFix);
    AddTest(&ParserTester::TestInfixOprt);
  	AddTest(&ParserTester::TestVarConst);
  	AddTest(&ParserTester::TestVolatile);
  	AddTest(&ParserTester::TestMultiArg);
  	AddTest(&ParserTester::TestFormula);
  	AddTest(&ParserTester::TestInterface);
  	AddTest(&ParserTester::TestBinOprt);
    AddTest(&ParserTester::TestException);
    AddTest(&ParserTester::TestStrArg);

    ParserTester::c_iCount = 0;
  }

  //---------------------------------------------------------------------------
  int ParserTester::TestInterface()
  {
    int iStat = 0;
    *m_stream << "testing member functions...";
   
    // Test RemoveVar
    value_type afVal[3] = {1,2,3};
	  Parser p;
    try
    {
      p.DefineVar("a", &afVal[0]);
      p.DefineVar("b", &afVal[1]);
      p.DefineVar("c", &afVal[2]);
      p.SetExpr("a+b+c");
      p.Eval();
    }
    catch(...)
    {
      iStat += 1;  // this is not supposed to happen 
    }

    try
    {
      p.RemoveVar("c");
      p.Eval();
      iStat += 1;  // not supposed to reach this, nonexisting variable "c" deleted...
    }
    catch(...)
    {
      // failure is expected...
    }

    if (iStat==0) 
      *m_stream << "passed" << endl;
	  else 
      *m_stream << "\n  failed with " << iStat << " errors" << endl;

    return iStat;
  }

  //---------------------------------------------------------------------------
  int ParserTester::TestStrArg()
  {
    int iStat = 0;
    *m_stream << "testing string arguments...";
 
    iStat += EqnTest("valueof(\"aaa\")+valueof(\"bbb\")  ", 246, true);
    iStat += EqnTest("2*(valueof(\"aaa\")-23)+valueof(\"bbb\")", 323, true);
    // use in expressions with variables
    iStat += EqnTest("a*(atof(\"10\")-b)", 8, true);
    iStat += EqnTest("a-(atof(\"10\")*b)", -19, true);

	  if (iStat==0)
      *m_stream << "passed" << endl;
	  else 
      *m_stream << "\n  failed with " << iStat << " errors" << endl;

    return iStat;
  }

  //---------------------------------------------------------------------------
  int ParserTester::TestBinOprt()
  {
    int iStat = 0;
    *m_stream << "testing binary operators...";
   
    // built in operators
    // xor operator
    iStat += EqnTest("1 xor 2", 3, true); 
    iStat += EqnTest("a xor b", 3, true);            // with a=1 and b=2
    iStat += EqnTest("1 xor 2 xor 3", 0, true); 
    iStat += EqnTest("a xor b xor 3", 0, true);      // with a=1 and b=2
    iStat += EqnTest("a xor b xor c", 0, true);      // with a=1 and b=2
    iStat += EqnTest("(1 xor 2) xor 3", 0, true); 
    iStat += EqnTest("(a xor b) xor c", 0, true);    // with a=1 and b=2
    iStat += EqnTest("(a) xor (b) xor c", 0, true);  // with a=1 and b=2
    iStat += EqnTest("1 or 2", 3, true); 
    iStat += EqnTest("a or b", 3, true);             // with a=1 and b=2

    // Assignement operator
    iStat += EqnTest("a = b", 2, true); 
    iStat += EqnTest("a = sin(b)", 0.909297, true); 
    iStat += EqnTest("a = 1+sin(b)", 1.909297, true); 

    // Test user defined binary operators
    iStat += EqnTestInt("1 | 2", 3, true);          
    iStat += EqnTestInt("1 || 2", 1, true);          
    iStat += EqnTestInt("123 & 456", 72, true);          
    iStat += EqnTestInt("(123 & 456) % 10", 2, true);          
    iStat += EqnTestInt("1 && 0", 0, true);          
    iStat += EqnTestInt("123 && 456", 1, true);          
    iStat += EqnTestInt("1 << 3", 8, true);          
    iStat += EqnTestInt("8 >> 3", 1, true);          
    iStat += EqnTestInt("10 ^ 10", 0, true);          
    iStat += EqnTestInt("10 * 10 ^ 99", 7, true);          
    iStat += EqnTestInt("9 / 4", 2, true);  
    iStat += EqnTestInt("9 % 4", 1, true);  
    iStat += EqnTestInt("if(5%2,1,0)", 1, true);
    iStat += EqnTestInt("if(4%2,1,0)", 0, true);
    iStat += EqnTestInt("-10+1", -9, true);
    iStat += EqnTestInt("1+2*3", 7, true);
    iStat += EqnTestInt("const1 != const2", 1, true);
    iStat += EqnTestInt("const1 != const2", 0, false);
    iStat += EqnTestInt("const1 == const2", 0, true);
    iStat += EqnTestInt("const1 == 1", 1, true);
    iStat += EqnTestInt("10*(const1 == 1)", 10, true);
    iStat += EqnTestInt("2*(const1 | const2)", 6, true);
    iStat += EqnTestInt("2*(const1 | const2)", 7, false);
    iStat += EqnTestInt("const1 < const2", 1, true);
    iStat += EqnTestInt("const2 > const1", 1, true);
    iStat += EqnTestInt("const1 <= 1", 1, true);
    iStat += EqnTestInt("const2 >= 2", 1, true);
    iStat += EqnTestInt("2*(const1 + const2)", 6, true);
    iStat += EqnTestInt("2*(const1 - const2)", -2, true);

    iStat += EqnTestInt("a != b", 1, true);
    iStat += EqnTestInt("a != b", 0, false);
    iStat += EqnTestInt("a == b", 0, true);
    iStat += EqnTestInt("a == 1", 1, true);
    iStat += EqnTestInt("10*(a == 1)", 10, true);
    iStat += EqnTestInt("2*(a | b)", 6, true);
    iStat += EqnTestInt("2*(a | b)", 7, false);
    iStat += EqnTestInt("a < b", 1, true);
    iStat += EqnTestInt("b > a", 1, true);
    iStat += EqnTestInt("a <= 1", 1, true);
    iStat += EqnTestInt("b >= 2", 1, true);
    iStat += EqnTestInt("2*(a + b)", 6, true);
    iStat += EqnTestInt("2*(a - b)", -2, true);
    iStat += EqnTestInt("a + (a << b)", 5, true);

    // Test priorities
    // a=1, b=2, c=3
    iStat += EqnTestInt("a + b * c", 7, true);
    iStat += EqnTestInt("a * b + c", 5, true);
    iStat += EqnTestInt("a<b && b>10", 0, true);
    iStat += EqnTestInt("a<b && b<10", 1, true);

    iStat += EqnTestInt("a + b << c", 17, true);
    iStat += EqnTestInt("a << b + c", 7, true);
    iStat += EqnTestInt("c * b < a", 0, true);
    iStat += EqnTestInt("c * b == 6 * a", 1, true);

	if (iStat==0)
      *m_stream << "passed" << endl;
	else 
      *m_stream << "\n  failed with " << iStat << " errors" << endl;

    return iStat;
  }

  //---------------------------------------------------------------------------
  int ParserTester::TestNames()
  {
    int  iStat= 0,
         iErr = 0;

	*m_stream << "testing name restriction enforcement...";
    
  	Parser p;

#define PARSER_THROWCHECK(DOMAIN, FAIL, EXPR, ARG) \
    iErr = 0;                                    \
    ParserTester::c_iCount++;                    \
    try                                          \
    {                                            \
      p.Define##DOMAIN(EXPR, ARG);               \
    }                                            \
    catch(Parser::exception_type&)               \
    {                                            \
      iErr = (FAIL==false) ? 0 : 1;              \
    }                                            \
    iStat += iErr;      
      
      // constant names
      PARSER_THROWCHECK(Const, false, "0a", 1)
      PARSER_THROWCHECK(Const, false, "9a", 1)
      PARSER_THROWCHECK(Const, false, "+a", 1)
      PARSER_THROWCHECK(Const, false, "-a", 1)
      PARSER_THROWCHECK(Const, false, "a-", 1)
      PARSER_THROWCHECK(Const, false, "a*", 1)
      PARSER_THROWCHECK(Const, false, "a?", 1)
      PARSER_THROWCHECK(Const, false, "a?", 1)
      PARSER_THROWCHECK(Const, false, "a?", 1)
      PARSER_THROWCHECK(Const, true, "a", 1)
      PARSER_THROWCHECK(Const, true, "a_min", 1)
      PARSER_THROWCHECK(Const, true, "a_min0", 1)
      PARSER_THROWCHECK(Const, true, "a_min9", 1)
      // variable names
      value_type a;
      p.ClearConst();
      PARSER_THROWCHECK(Var, false, "9a", &a)
      PARSER_THROWCHECK(Var, false, "0a", &a)
      PARSER_THROWCHECK(Var, false, "+a", &a)
      PARSER_THROWCHECK(Var, false, "-a", &a)
      PARSER_THROWCHECK(Var, false, "?a", &a)
      PARSER_THROWCHECK(Var, false, "!a", &a)
      PARSER_THROWCHECK(Var, false, "a+", &a)
      PARSER_THROWCHECK(Var, false, "a-", &a)
      PARSER_THROWCHECK(Var, false, "a*", &a)
      PARSER_THROWCHECK(Var, false, "a?", &a)
      PARSER_THROWCHECK(Var, true, "a", &a)
      PARSER_THROWCHECK(Var, true, "a_min", &a)
      PARSER_THROWCHECK(Var, true, "a_min0", &a)
      PARSER_THROWCHECK(Var, true, "a_min9", &a)
      PARSER_THROWCHECK(Var, false, "a_min9", 0)
      // Postfix operators
      // fail
      PARSER_THROWCHECK(PostfixOprt, false, "(k", f1of1)
      PARSER_THROWCHECK(PostfixOprt, false, "9+", f1of1)
      PARSER_THROWCHECK(PostfixOprt, false, "+", 0)
      // pass
      PARSER_THROWCHECK(PostfixOprt, true, "-a",  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "?a",  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "_",   f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "#",   f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "&&",  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "||",  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "&",   f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "|",   f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "++",  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "--",  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "?>",  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "?<",  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "**",  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "xor", f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "and", f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "or",  f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "not", f1of1)
      PARSER_THROWCHECK(PostfixOprt, true, "!",   f1of1)
      // Binary operator
      // The following must fail with builtin operators activated
      // p.EnableBuiltInOp(true); -> this is the default
      PARSER_THROWCHECK(Oprt, false, "+",  f1of2)
      PARSER_THROWCHECK(Oprt, false, "-",  f1of2)
      PARSER_THROWCHECK(Oprt, false, "*",  f1of2)
      PARSER_THROWCHECK(Oprt, false, "/",  f1of2)
      // without activated built in operators it should work
      p.EnableBuiltInOprt(false);
      PARSER_THROWCHECK(Oprt, true, "+",  f1of2)
      PARSER_THROWCHECK(Oprt, true, "-",  f1of2)
      PARSER_THROWCHECK(Oprt, true, "*",  f1of2)
      PARSER_THROWCHECK(Oprt, true, "/",  f1of2)

#undef PARSER_THROWCHECK

      if (iStat==0) 
        *m_stream << "passed" << endl;
	    else 
        *m_stream << "\n  failed with " << iStat << " errors" << endl;

      return iStat;
    }

	//---------------------------------------------------------------------------
	int ParserTester::TestSyntax()
	{
    int iStat = 0;
    *m_stream << "testing syntax engine...";
   
    iStat += EqnTest("(1+ 2*a)", 3, true); // Spaces within formula
	  iStat += EqnTest("(2+", 0, false);   // missing closing bracket 
	  iStat += EqnTest("2++4", 0, false);  // unexpected operator
	  iStat += EqnTest("2+-4", 0, false);  // unexpected operator
	  iStat += EqnTest("(2+)", 0, false);  // unexpected closing bracket
	  iStat += EqnTest("--2", 0, false);   // double sign
	  iStat += EqnTest("ksdfj", 0, false); // unknown token
	  iStat += EqnTest("()", 0, false);    // empty bracket
	  iStat += EqnTest("sin(cos)", 0, false); // unexpected function
	  iStat += EqnTest("5t6", 0, false);   // unknown token
	  iStat += EqnTest("5 t 6", 0, false); // unknown token
	  iStat += EqnTest("8*", 0, false);    // unexpected end of formula
	  iStat += EqnTest(",3", 0, false);    // unexpected comma
	  iStat += EqnTest("3,5", 0, false);   // unexpected comma
	  iStat += EqnTest("sin(8,8)", 0, false); // too many function args
	  iStat += EqnTest("(7,8)", 0, false); // too many function args
	  iStat += EqnTest("sin)", 0, false);  // unexpected closing bracket
	  iStat += EqnTest("a)", 0, false);    // unexpected closing bracket
	  iStat += EqnTest("pi)", 0, false);   // unexpected closing bracket
	  iStat += EqnTest("sin(())", 0, false);   // unexpected closing bracket
	  iStat += EqnTest("sin()", 0, false);   // unexpected closing bracket

	  if (iStat==0)
      *m_stream << "passed" << endl;
	  else 
      *m_stream << "\n  failed with " << iStat << " errors" << endl;

    return iStat;
	}

  //---------------------------------------------------------------------------
  int ParserTester::TestVarConst()
  {
    int iStat = 0;
    *m_stream << "testing variable/constant name recognition...";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -