ResInsight/ApplicationCode/UnitTests/RicExpressionParser-Test.cpp

78 lines
2.2 KiB
C++
Raw Normal View History

2017-10-11 12:44:13 -05:00
#include "gtest/gtest.h"
2017-10-23 07:27:04 -05:00
#include "RimSummaryCalculation.h"
2017-10-11 12:44:13 -05:00
#include "expressionparser/ExpressionParser.h"
#include <numeric>
//--------------------------------------------------------------------------------------------------
///
2017-10-11 12:44:13 -05:00
//--------------------------------------------------------------------------------------------------
TEST( RicExpressionParserTest, BasicUsage )
2017-10-11 12:44:13 -05:00
{
std::vector<double> a( 10 );
std::iota( a.begin(), a.end(), 10 );
2017-10-11 12:44:13 -05:00
std::vector<double> b( 10 );
std::iota( b.begin(), b.end(), 100 );
2017-10-11 12:44:13 -05:00
std::vector<double> c( 10 );
2017-10-11 12:44:13 -05:00
ExpressionParser parser;
parser.assignVector( "a", a );
parser.assignVector( "b", b );
parser.assignVector( "c", c );
2017-10-11 12:44:13 -05:00
QString expr = "c := a + b";
EXPECT_TRUE( parser.evaluate( expr ) );
2017-10-11 12:44:13 -05:00
EXPECT_DOUBLE_EQ( c[0], 110.0 );
EXPECT_DOUBLE_EQ( c[9], 128.0 );
2017-10-11 12:44:13 -05:00
}
//--------------------------------------------------------------------------------------------------
///
2017-10-11 12:44:13 -05:00
//--------------------------------------------------------------------------------------------------
TEST( RicExpressionParserTest, DetectVariables )
2017-10-11 12:44:13 -05:00
{
QString expr = "c := a + (x / y)";
std::vector<QString> variables = ExpressionParser::detectReferencedVariables( expr );
2017-10-11 12:44:13 -05:00
EXPECT_STREQ( variables[0].toStdString().data(), "c" );
EXPECT_STREQ( variables[1].toStdString().data(), "a" );
EXPECT_STREQ( variables[2].toStdString().data(), "x" );
EXPECT_STREQ( variables[3].toStdString().data(), "y" );
2017-10-11 12:44:13 -05:00
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RicExpressionParserTest, FindLeftHandSide )
{
{
QString expr = "c := a";
QString s = RimSummaryCalculation::findLeftHandSide( expr );
EXPECT_STREQ( s.toStdString().data(), "c" );
}
{
QString expr = "c:=a";
QString s = RimSummaryCalculation::findLeftHandSide( expr );
EXPECT_STREQ( s.toStdString().data(), "c" );
}
{
QString expr = "\na:=b\n\nc:=a";
QString s = RimSummaryCalculation::findLeftHandSide( expr );
EXPECT_STREQ( s.toStdString().data(), "c" );
}
}