mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
#7316 Curve Calculator: Add support for single line if-statement
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
#include "RimSummaryCalculation.h"
|
||||
|
||||
#include "ExpressionParserImpl.h"
|
||||
#include "expressionparser/ExpressionParser.h"
|
||||
|
||||
#include <numeric>
|
||||
@@ -75,3 +76,87 @@ TEST( RicExpressionParserTest, FindLeftHandSide )
|
||||
EXPECT_STREQ( s.toStdString().data(), "c" );
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RicExpressionParserTest, ForLoopWithIfStatement )
|
||||
{
|
||||
std::vector<double> a( 10 );
|
||||
std::iota( a.begin(), a.end(), 10 );
|
||||
|
||||
std::vector<double> b( 10 );
|
||||
std::iota( b.begin(), b.end(), 100 );
|
||||
|
||||
std::vector<double> c( 10 );
|
||||
|
||||
ExpressionParser parser;
|
||||
parser.assignVector( "a", a );
|
||||
parser.assignVector( "b", b );
|
||||
parser.assignVector( "c", c );
|
||||
|
||||
QString expr = "for (var i := 0; i < min(a[],b[],c[]); i += 1)\n"
|
||||
"{ \n"
|
||||
" c[i] := if((a[i] > 13), a[i], b[i]); \n"
|
||||
"} \n";
|
||||
|
||||
EXPECT_TRUE( parser.evaluate( expr ) );
|
||||
|
||||
EXPECT_DOUBLE_EQ( c[0], 100.0 );
|
||||
EXPECT_DOUBLE_EQ( c[9], 19.0 );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RicExpressionParserTest, ExpandedIfStatement )
|
||||
{
|
||||
std::vector<double> a( 10 );
|
||||
std::iota( a.begin(), a.end(), 10 );
|
||||
|
||||
std::vector<double> b( 10 );
|
||||
std::iota( b.begin(), b.end(), 100 );
|
||||
|
||||
std::vector<double> c( 10 );
|
||||
|
||||
ExpressionParser parser;
|
||||
parser.assignVector( "a", a );
|
||||
parser.assignVector( "b", b );
|
||||
parser.assignVector( "c", c );
|
||||
|
||||
QString expr = "c := if((a > 13), a, b)";
|
||||
auto expandedText = ExpressionParserImpl::expandIfStatements( expr );
|
||||
|
||||
// std::cout << expandedText.toStdString();
|
||||
|
||||
EXPECT_TRUE( parser.evaluate( expandedText ) );
|
||||
|
||||
EXPECT_DOUBLE_EQ( c[0], 100.0 );
|
||||
EXPECT_DOUBLE_EQ( c[9], 19.0 );
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( RicExpressionParserTest, ExpandIfStatementsAndEvaluate )
|
||||
{
|
||||
std::vector<double> a( 10 );
|
||||
std::iota( a.begin(), a.end(), 10 );
|
||||
|
||||
std::vector<double> b( 10 );
|
||||
std::iota( b.begin(), b.end(), 100 );
|
||||
|
||||
std::vector<double> c( 10 );
|
||||
|
||||
ExpressionParser parser;
|
||||
parser.assignVector( "a", a );
|
||||
parser.assignVector( "b", b );
|
||||
parser.assignVector( "c", c );
|
||||
|
||||
QString expr = "c := if((a > 13), a, b)";
|
||||
|
||||
EXPECT_TRUE( parser.expandIfStatementsAndEvaluate( expr ) );
|
||||
|
||||
EXPECT_DOUBLE_EQ( c[0], 100.0 );
|
||||
EXPECT_DOUBLE_EQ( c[9], 19.0 );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user