Files
ResInsight/ApplicationLibCode/UnitTests/RifElementPropertyTableReader-Test.cpp
Magne Sjaastad 3a257effd2 #14906: Handle missing element property file when loading element results
Crash signature c0fae31c2301.

RifElementPropertyReader reads column metadata when the file is added and the data later, on cell pick. If the file has since been moved or deleted, readData returns no data and CAF_ASSERT( dataColumns.size() == table.data.size() ) aborts.

Return an empty result when the table has no data or the column count does not match. New test ReadAllElementPropertiesFromRemovedFile.

Call stack:
[5] cvf::AssertHandlerConsole::handleAssert(char const*, int, char const*, char const*) at ResInsight/Fwk/VizFwk/LibCore/cvfAssert.cpp:125
[6] RifElementPropertyReader::readAllElementPropertiesInFileContainingField(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) at ResInsight/ApplicationLibCode/FileInterface/RifElementPropertyReader.cpp:120
[7] RigFemPartResultsCollection::findOrLoadScalarResult(int, RigFemResultAddress const&) at ResInsight/ApplicationLibCode/GeoMech/GeoMechDataModel/RigFemPartResultsCollection.cpp:451
[8] RigFemPartResultsCollection::assertResultsLoaded(RigFemResultAddress const&) at ResInsight/ApplicationLibCode/GeoMech/GeoMechDataModel/RigFemPartResultsCollection.cpp:1014
[9] RimGeoMechResultDefinition::hasResult() at ResInsight/ApplicationLibCode/ProjectDataModel/GeoMech/RimGeoMechResultDefinition.cpp:722
[10] RiuSelectionChangedHandler::addResultCurveFromSelectionItem(RiuGeoMechSelectionItem const*) const at ResInsight/ApplicationLibCode/UserInterface/RiuSelectionChangedHandler.cpp:208
[11] RiuSelectionChangedHandler::handleItemAppended(RiuSelectionItem const*) const at ResInsight/ApplicationLibCode/UserInterface/RiuSelectionChangedHandler.cpp:119
[12] RiuCellAndNncPickEventHandler::handle3dPickEvent(Ric3dPickEvent const&) at ResInsight/ApplicationLibCode/UserInterface/RiuCellAndNncPickEventHandler.cpp:459
[13] RiuViewerCommands::handlePickAction(int, int, QFlags<Qt::KeyboardModifier>) at ResInsight/ApplicationLibCode/UserInterface/RiuViewerCommands.cpp:718
[17] RiaGuiApplication::notify(QObject*, QEvent*) at ResInsight/ApplicationLibCode/Application/RiaGuiApplication.cpp:1828
[23] RiaGuiApplication::notify(QObject*, QEvent*) at ResInsight/ApplicationLibCode/Application/RiaGuiApplication.cpp:1828
[34] main at ResInsight/ApplicationExeCode/RiaMain.cpp:267
[35] __libc_start_main at :0
2026-10-12 19:00:45 +02:00

177 lines
6.9 KiB
C++

#include "gtest/gtest.h"
#include "RiaTestDataDirectory.h"
#include "RifElementPropertyReader.h"
#include "RifElementPropertyTableReader.h"
#include "RifFileParseTools.h"
#include <QFile>
#include <QString>
#include <QTemporaryDir>
#include <numeric>
static const QString ELEM_PROP_TEST_DATA_DIRECTORY = QString( "%1/RifElementPropertyTableReader/" ).arg( TEST_DATA_DIR );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RicElementPropertyTableReaderTest, BasicUsage )
{
RifElementPropertyMetadata metadata =
RifElementPropertyTableReader::readMetadata( ELEM_PROP_TEST_DATA_DIRECTORY + "ELASTIC_TABLE.inp" );
RifElementPropertyTable table;
RifElementPropertyTableReader::readData( &metadata, &table );
EXPECT_TRUE( table.hasData );
EXPECT_EQ( 2u, metadata.dataColumns.size() );
EXPECT_STREQ( "MODULUS", metadata.dataColumns[0].toStdString().c_str() );
EXPECT_STREQ( "RATIO", metadata.dataColumns[1].toStdString().c_str() );
EXPECT_EQ( 2u, table.data.size() );
EXPECT_EQ( 4320u, table.elementIds.size() );
EXPECT_EQ( 4320u, table.data[0].size() );
EXPECT_EQ( 4320u, table.data[1].size() );
EXPECT_FLOAT_EQ( 11198814808.2538f, table.data[0].front() );
EXPECT_FLOAT_EQ( 13250853676.3636f, table.data[0].back() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RicElementPropertyTableReaderTest, ParseFailedForTooManyColumns )
{
try
{
RifElementPropertyMetadata metadata =
RifElementPropertyTableReader::readMetadata( ELEM_PROP_TEST_DATA_DIRECTORY + "ELASTIC_TABLE_error_too_many_columns.inp" );
RifElementPropertyTable table;
RifElementPropertyTableReader::readData( &metadata, &table );
EXPECT_TRUE( false );
}
catch ( FileParseException e )
{
EXPECT_TRUE( e.message.startsWith( "Number of columns mismatch" ) );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RicElementPropertyTableReaderTest, ParseFailedForTooFewColumns )
{
try
{
RifElementPropertyMetadata metadata =
RifElementPropertyTableReader::readMetadata( ELEM_PROP_TEST_DATA_DIRECTORY + "ELASTIC_TABLE_error_too_few_columns.inp" );
RifElementPropertyTable table;
RifElementPropertyTableReader::readData( &metadata, &table );
EXPECT_TRUE( false );
}
catch ( FileParseException e )
{
EXPECT_TRUE( e.message.startsWith( "Number of columns mismatch" ) );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RicElementPropertyTableReaderTest, MoreThanEightColumns )
{
RifElementPropertyMetadata metadata =
RifElementPropertyTableReader::readMetadata( ELEM_PROP_TEST_DATA_DIRECTORY + "ELASTIC_TABLE_morethan8columns.inp" );
RifElementPropertyTable table;
RifElementPropertyTableReader::readData( &metadata, &table );
EXPECT_TRUE( table.hasData );
EXPECT_EQ( 9u, metadata.dataColumns.size() );
EXPECT_STREQ( "MODULUS", metadata.dataColumns[0].toStdString().c_str() );
EXPECT_STREQ( "MODULUS", metadata.dataColumns[1].toStdString().c_str() );
EXPECT_STREQ( "MODULUS", metadata.dataColumns[2].toStdString().c_str() );
EXPECT_STREQ( "RATIO", metadata.dataColumns[3].toStdString().c_str() );
EXPECT_STREQ( "RATIO", metadata.dataColumns[4].toStdString().c_str() );
EXPECT_STREQ( "RATIO", metadata.dataColumns[5].toStdString().c_str() );
EXPECT_STREQ( "MODULUS", metadata.dataColumns[6].toStdString().c_str() );
EXPECT_STREQ( "MODULUS", metadata.dataColumns[7].toStdString().c_str() );
EXPECT_STREQ( "MODULUS", metadata.dataColumns[8].toStdString().c_str() );
EXPECT_EQ( 9u, table.data.size() );
EXPECT_EQ( 8u, table.elementIds.size() );
EXPECT_EQ( 8u, table.data[0].size() );
EXPECT_EQ( 8u, table.data[1].size() );
EXPECT_EQ( 8u, table.data[2].size() );
EXPECT_EQ( 8u, table.data[3].size() );
EXPECT_EQ( 8u, table.data[4].size() );
EXPECT_EQ( 8u, table.data[5].size() );
EXPECT_EQ( 8u, table.data[6].size() );
EXPECT_EQ( 8u, table.data[7].size() );
EXPECT_EQ( 8u, table.data[8].size() );
EXPECT_FLOAT_EQ( 4998669605.36f, table.data[0].front() );
EXPECT_FLOAT_EQ( 4998444377.89f, table.data[0].back() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RicElementPropertyTableReaderTest, ReadAllElementPropertiesInFileContainingField )
{
RifElementPropertyMetadata metadata =
RifElementPropertyTableReader::readMetadata( ELEM_PROP_TEST_DATA_DIRECTORY + "ELASTIC_TABLE.inp" );
RifElementPropertyTable table;
RifElementPropertyTableReader::readData( &metadata, &table );
RifElementPropertyReader reader( table.elementIds );
reader.addFile( ( ELEM_PROP_TEST_DATA_DIRECTORY + "ELASTIC_TABLE.inp" ).toStdString() );
auto result = reader.readAllElementPropertiesInFileContainingField( "MODULUS" );
EXPECT_EQ( 2u, result.size() );
EXPECT_EQ( 1u, result.count( "MODULUS" ) );
EXPECT_EQ( 1u, result.count( "RATIO" ) );
const auto& modulusData = result.at( "MODULUS" );
EXPECT_EQ( 4320u, modulusData.size() );
const float paToGPa = 0.000000001f;
EXPECT_FLOAT_EQ( 11198814808.2538f * paToGPa, modulusData.front() );
EXPECT_FLOAT_EQ( 13250853676.3636f * paToGPa, modulusData.back() );
const auto& ratioData = result.at( "RATIO" );
EXPECT_EQ( 4320u, ratioData.size() );
EXPECT_FLOAT_EQ( 0.19041f, ratioData.front() );
EXPECT_FLOAT_EQ( 0.25f, ratioData.back() );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RicElementPropertyTableReaderTest, ReadAllElementPropertiesFromRemovedFile )
{
QTemporaryDir tempDir;
ASSERT_TRUE( tempDir.isValid() );
const QString fileName = tempDir.filePath( "ELASTIC_TABLE.inp" );
ASSERT_TRUE( QFile::copy( ELEM_PROP_TEST_DATA_DIRECTORY + "ELASTIC_TABLE.inp", fileName ) );
RifElementPropertyReader reader( { 1, 2, 3 } );
reader.addFile( fileName.toStdString() );
ASSERT_TRUE( QFile::remove( fileName ) );
auto result = reader.readAllElementPropertiesInFileContainingField( "MODULUS" );
EXPECT_TRUE( result.empty() );
}