ResInsight/ApplicationLibCode/UnitTests/RifGridCalculationIO-Test.cpp

68 lines
2.8 KiB
C++
Raw Normal View History

#include "gtest/gtest.h"
#include "RiaTestDataDirectory.h"
#include "RifGridCalculation.h"
#include "RifGridCalculationExporter.h"
#include "RifGridCalculationImporter.h"
static const std::string GRID_CALCULATION_DIR = std::string( TEST_DATA_DIR ) + std::string( "/RifGridCalculationIO/" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RifGridCalculationIO, importAndExport )
{
std::string fileName = GRID_CALCULATION_DIR + "calculations.toml";
RifGridCalculation calc1;
calc1.description = "My first calculation";
calc1.expression = "answer = a + b";
calc1.unit = "meter";
RifGridCalculationVariable variable1;
variable1.name = "a";
variable1.resultVariable = "PRESSURE";
variable1.resultType = "DYNAMIC_NATIVE";
RifGridCalculationVariable variable2;
variable2.name = "b";
variable2.resultVariable = "PORO";
variable2.resultType = "STATIC_NATIVE";
calc1.variables = { variable1, variable2 };
RifGridCalculation calc2;
calc2.description = "My second calculation";
calc2.expression = "answer = x + y";
calc2.unit = "meter";
RifGridCalculationVariable variable3;
variable3.name = "x";
variable3.resultVariable = "PRESSURE";
variable3.resultType = "DYNAMIC_NATIVE";
RifGridCalculationVariable variable4;
variable4.name = "y";
variable4.resultVariable = "PORO";
variable4.resultType = "STATIC_NATIVE";
calc2.variables = { variable3, variable4 };
std::vector<RifGridCalculation> calculations = { calc1, calc2 };
std::stringstream stream;
auto [isOk, errorMessage] = RifGridCalculationExporter::writeToStream( calculations, stream );
EXPECT_TRUE( isOk );
EXPECT_TRUE( errorMessage.empty() );
auto [importedCalculations, importErrorMessage] = RifGridCalculationImporter::readFromStream( stream );
ASSERT_EQ( calculations.size(), importedCalculations.size() );
ASSERT_EQ( calculations[0].description, importedCalculations[0].description );
ASSERT_EQ( calculations[0].expression, importedCalculations[0].expression );
ASSERT_EQ( calculations[0].unit, importedCalculations[0].unit );
ASSERT_EQ( calculations[0].variables.size(), importedCalculations[0].variables.size() );
for ( size_t v = 0; v < calculations[0].variables.size(); v++ )
{
ASSERT_EQ( calculations[0].variables[v].name, importedCalculations[0].variables[v].name );
ASSERT_EQ( calculations[0].variables[v].resultType, importedCalculations[0].variables[v].resultType );
ASSERT_EQ( calculations[0].variables[v].resultVariable, importedCalculations[0].variables[v].resultVariable );
}
}