Skip to content

Commit

Permalink
added unit test for #136
Browse files Browse the repository at this point in the history
  • Loading branch information
beltoforion committed Dec 23, 2023
1 parent 009b8f5 commit 519ee34
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"*.rmd": "markdown",
"stdexcept": "cpp"
}
}
4 changes: 3 additions & 1 deletion include/muParserTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ namespace mu
int TestBulkMode();
int TestOssFuzzTestCases();
int TestOptimizer();

int TestLocalization();

void Abort() const;

public:
Expand All @@ -275,6 +276,7 @@ namespace mu
// Test Double Parser
int EqnTest(const string_type& a_str, double a_fRes, bool a_fPass);
int EqnTestWithVarChange(const string_type& a_str, double a_fRes1, double a_fVar1, double a_fRes2, double a_fVar2);
int EqnTestLocalized(const string_type& a_str, double a_fRes, bool a_fPass);
int ThrowTest(const string_type& a_str, int a_iErrc, bool a_bFail = true);

// Test Int Parser
Expand Down
23 changes: 13 additions & 10 deletions samples/example1/example1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,9 @@ void CalcBulk()
static void Calc()
{
mu::Parser parser;
parser.SetDecSep(',');
parser.SetArgSep(';');
parser.SetThousandsSep('.');

// Add some variables
value_type vVarVal[] = { 1, 2 }; // Values of the parser variables
Expand Down Expand Up @@ -476,13 +479,13 @@ static void Calc()
parser.SetVarFactory(AddVariable, &parser);

// You can extract the bytecode of a parsed functions and save it for later use.
parser.SetExpr(_T("sin(a)+strfun2(sVar1, 1 , 2)"));
parser.Eval();
ParserByteCode bytecode1(parser.GetByteCode());
// parser.SetExpr(_T("sin(a)+strfun2(sVar1, 1 , 2)"));
// parser.Eval();
// ParserByteCode bytecode1(parser.GetByteCode());

parser.SetExpr(_T("10*cos(a)"));
parser.Eval();
ParserByteCode bytecode2(parser.GetByteCode());
// parser.SetExpr(_T("10*cos(a)"));
// parser.Eval();
// ParserByteCode bytecode2(parser.GetByteCode());

for (;;)
{
Expand All @@ -503,13 +506,13 @@ static void Calc()

if (sLine == _T("restore1"))
{
parser.SetByteCode(bytecode1);
bytecode1.AsciiDump();
// parser.SetByteCode(bytecode1);
// bytecode1.AsciiDump();
}
else if (sLine == _T("restore2"))
{
parser.SetByteCode(bytecode2);
bytecode2.AsciiDump();
// parser.SetByteCode(bytecode2);
// bytecode2.AsciiDump();
}
else
{
Expand Down
7 changes: 4 additions & 3 deletions src/muParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ namespace mu
*/
int Parser::IsVal(const char_type* a_szExpr, int* a_iPos, value_type* a_fVal)
{
/*
// fix for #123; std::Stringstream is broken on Mac; use std::stod instead
try
{
Expand All @@ -70,11 +71,12 @@ namespace mu
{
return 0;
}
/*
*/

value_type fVal(0);

stringstream_type stream(a_szExpr);
stream.seekg(0); // todo: check if this really is necessary
// stream.seekg(0); // todo: check if this really is necessary
stream.imbue(Parser::s_locale);
stream >> fVal;
stringstream_type::pos_type iEnd = stream.tellg(); // Position after reading
Expand All @@ -85,7 +87,6 @@ namespace mu
*a_iPos += (int)iEnd;
*a_fVal = fVal;
return 1;
*/
}


Expand Down
67 changes: 64 additions & 3 deletions src/muParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ namespace mu
AddTest(&ParserTester::TestStrArg);
AddTest(&ParserTester::TestBulkMode);
AddTest(&ParserTester::TestOptimizer);
AddTest(&ParserTester::TestLocalization);

ParserTester::c_iCount = 0;
}
Expand Down Expand Up @@ -1234,14 +1235,27 @@ namespace mu
return iStat;
}

int ParserTester::TestLocalization()
{
int iStat = 0;
mu::console() << _T("testing localization...");

iStat += EqnTestLocalized(_T("1,2"), 1.2, true);

if (iStat == 0)
mu::console() << _T("passed") << endl;
else
mu::console() << _T("\n failed with ") << iStat << _T(" errors") << endl;

return iStat;
}


//---------------------------------------------------------------------------
void ParserTester::AddTest(testfun_type a_pFun)
{
m_vTestFun.push_back(a_pFun);
}

//---------------------------------------------------------------------------
int ParserTester::Run()
{
int iStat = 0;
Expand Down Expand Up @@ -1336,7 +1350,7 @@ namespace mu
}

//---------------------------------------------------------------------------
/** \brief Evaluate a tet expression.
/** \brief Evaluate a test expression.
\return 1 in case of a failure, 0 otherwise.
*/
Expand Down Expand Up @@ -1390,6 +1404,53 @@ namespace mu
return 0;
}

//---------------------------------------------------------------------------
/** \brief Evaluate a test expression.
\return 1 in case of a failure, 0 otherwise.
*/
int ParserTester::EqnTestLocalized(const string_type& a_str, double a_fRes, bool a_fPass)
{
ParserTester::c_iCount++;

try
{
Parser p;
value_type var[2] = { 1, 2 };

// variable
p.SetDecSep(',');
p.SetArgSep(';');
p.SetThousandsSep('.');

p.DefineVar(_T("a"), &var[0]);
p.DefineVar(_T("b"), &var[1]);
p.SetExpr(a_str);

auto result = p.Eval();

if (fabs(result - a_fRes) > 0.0000000001)
throw std::runtime_error("incorrect result (first pass)");
}
catch (Parser::exception_type& e)
{
mu::console() << _T("\n fail: ") << a_str.c_str() << _T(" (") << e.GetMsg() << _T(")");
return 1;
}
catch (std::exception& e)
{
mu::console() << _T("\n fail: ") << a_str.c_str() << _T(" (") << e.what() << _T(")");
return 1; // always return a failure since this exception is not expected
}
catch (...)
{
mu::console() << _T("\n fail: ") << a_str.c_str() << _T(" (unexpected exception)");
return 1; // exceptions other than ParserException are not allowed
}

return 0;
}

//---------------------------------------------------------------------------
/** \brief Evaluate a tet expression.
Expand Down

0 comments on commit 519ee34

Please sign in to comment.