Skip to content

Commit

Permalink
TEST: Add more unit tests to the STL file Reader to test error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Jackson <[email protected]>
  • Loading branch information
imikejackson committed Apr 28, 2024
1 parent cf5dca1 commit ad115ee
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,9 @@ Result<> ReadStlFile::operator()()
fgetpos(f, &pos);
if(pos >= stlFileSize)

Check failure on line 221 in src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/ReadStlFile.cpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, g++-9)

no match for ‘operator>=’ (operand types are ‘fpos_t’ {aka ‘_G_fpos64_t’} and ‘long unsigned int’)

Check failure on line 221 in src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/ReadStlFile.cpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, g++-10)

no match for ‘operator>=’ (operand types are ‘fpos_t’ and ‘long unsigned int’)

Check failure on line 221 in src/Plugins/SimplnxCore/src/SimplnxCore/Filters/Algorithms/ReadStlFile.cpp

View workflow job for this annotation

GitHub Actions / build (ubuntu-20.04, clang++-10)

invalid operands to binary expression ('fpos_t' (aka '_G_fpos64_t') and 'unsigned long')
{
std::string msg =
fmt::format("Trying to read at file position {} >= file size {}. The STL File is probably corrupt or not written properly.\n The file header was '{}'", pos, stlFileSize, stlHeaderStr);
std::string msg = fmt::format(
"Trying to read at file position {} >= file size {}.\n File Header: '{}'\n Header Triangle Count: {} Current Triangle: {}\n The STL File does not conform to the STL file specification.",
pos, stlFileSize, stlHeaderStr, triCount, t);
return MakeErrorResult(nx::core::StlConstants::k_StlFileLengthError, msg);
}

Expand Down
1 change: 1 addition & 0 deletions src/Plugins/SimplnxCore/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ if(EXISTS "${DREAM3D_DATA_DIR}" AND SIMPLNX_DOWNLOAD_TEST_FILES)
download_test_data(DREAM3D_DATA_DIR ${DREAM3D_DATA_DIR} ARCHIVE_NAME remove_flagged_triangles_test.tar.gz SHA512 cd5c6f3ea16a6d09e00e0c0bd0f941b27dca8a0beaeabb7262a2a6adaad83c829187c5d1aa433718123b628eaa839f016604c1134ced9f870723594b2df4be99)
download_test_data(DREAM3D_DATA_DIR ${DREAM3D_DATA_DIR} ARCHIVE_NAME generate_color_table_test.tar.gz SHA512 b5683c758964eb723267400b14047f8adb0d5365ee9ca93d1a6940e9b6ad198cd4739c1ca799eb787b7706e668dbc16ab8243642034cdba5b71d64c27e682d3f)
download_test_data(DREAM3D_DATA_DIR ${DREAM3D_DATA_DIR} ARCHIVE_NAME read_vtk_structured_points_test.tar.gz SHA512 e7a07a4e3901204c2562754cd71e0fdba1a46de2a5135bad2b6d66b40eefd0e63bed4dbe0ccd6ccadafb708ef63e20635d080aa3a35c172c4ced6986e0f75d5c)
download_test_data(DREAM3D_DATA_DIR ${DREAM3D_DATA_DIR} ARCHIVE_NAME ReadSTLFileTest.tar.gz SHA512 975587206625ffa183160308934e767347de55a34a16272cf5c121114efa286b3c6939e3c6a397e8728fdefe1771bc024bd4c9b409afdff0b76f2f56fcb9eb69)

endif()

Expand Down
90 changes: 88 additions & 2 deletions src/Plugins/SimplnxCore/test/ReadStlFileTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ namespace fs = std::filesystem;
using namespace nx::core;
using namespace nx::core::Constants;

TEST_CASE("SimplnxCore::ReadStlFileFilter", "[SimplnxCore][ReadStlFileFilter]")
TEST_CASE("SimplnxCore::ReadStlFileFilter:Valid_File", "[SimplnxCore][ReadStlFileFilter]")
{
const nx::core::UnitTest::TestFileSentinel testDataSentinel(nx::core::unit_test::k_CMakeExecutable, nx::core::unit_test::k_TestFilesDir, "ReadSTLFileTest.tar.gz", "ReadSTLFileTest");

// Instantiate the filter, a DataStructure object and an Arguments Object
DataStructure dataStructure;
Arguments args;
ReadStlFileFilter filter;

DataPath triangleGeomDataPath({"[Triangle Geometry]"});

std::string inputFile = fmt::format("{}/ASTMD638_specimen.stl", unit_test::k_ComplexTestDataSourceDir.view());
std::string inputFile = fmt::format("{}/ReadSTLFileTest/ASTMD638_specimen.stl", unit_test::k_TestFilesDir);

// Create default Parameters for the filter.
args.insertOrAssign(ReadStlFileFilter::k_StlFilePath_Key, std::make_any<FileSystemPathParameter::ValueType>(fs::path(inputFile)));
Expand All @@ -48,3 +50,87 @@ TEST_CASE("SimplnxCore::ReadStlFileFilter", "[SimplnxCore][ReadStlFileFilter]")
WriteTestDataStructure(dataStructure, fs::path(fmt::format("{}/StlFileReaderTest.dream3d", unit_test::k_BinaryTestOutputDir)));
#endif
}

TEST_CASE("SimplnxCore::ReadStlFileFilter:STLParseError", "[SimplnxCore][ReadStlFileFilter]")
{
const nx::core::UnitTest::TestFileSentinel testDataSentinel(nx::core::unit_test::k_CMakeExecutable, nx::core::unit_test::k_TestFilesDir, "ReadSTLFileTest.tar.gz", "ReadSTLFileTest");

// Instantiate the filter, a DataStructure object and an Arguments Object
DataStructure dataStructure;
Arguments args;
ReadStlFileFilter filter;

DataPath triangleGeomDataPath({"[Triangle Geometry]"});

std::string inputFile = fmt::format("{}/ReadSTLFileTest/stl_test_wrong_num_triangles.stl", unit_test::k_TestFilesDir);

// Create default Parameters for the filter.
args.insertOrAssign(ReadStlFileFilter::k_StlFilePath_Key, std::make_any<FileSystemPathParameter::ValueType>(fs::path(inputFile)));
args.insertOrAssign(ReadStlFileFilter::k_CreatedTriangleGeometryPath_Key, std::make_any<DataPath>(triangleGeomDataPath));

// Preflight the filter and check result
auto preflightResult = filter.preflight(dataStructure, args);
SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions);

// Execute the filter and check the result
auto executeResult = filter.execute(dataStructure, args);
SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result);

REQUIRE(executeResult.result.errors().front().code == -1108);
}

TEST_CASE("SimplnxCore::ReadStlFileFilter:TriangleParseError", "[SimplnxCore][ReadStlFileFilter]")
{
const nx::core::UnitTest::TestFileSentinel testDataSentinel(nx::core::unit_test::k_CMakeExecutable, nx::core::unit_test::k_TestFilesDir, "ReadSTLFileTest.tar.gz", "ReadSTLFileTest");

// Instantiate the filter, a DataStructure object and an Arguments Object
DataStructure dataStructure;
Arguments args;
ReadStlFileFilter filter;

DataPath triangleGeomDataPath({"[Triangle Geometry]"});

std::string inputFile = fmt::format("{}/ReadSTLFileTest/stl_test_2_TriangleParseError.stl", unit_test::k_TestFilesDir);

// Create default Parameters for the filter.
args.insertOrAssign(ReadStlFileFilter::k_StlFilePath_Key, std::make_any<FileSystemPathParameter::ValueType>(fs::path(inputFile)));
args.insertOrAssign(ReadStlFileFilter::k_CreatedTriangleGeometryPath_Key, std::make_any<DataPath>(triangleGeomDataPath));

// Preflight the filter and check result
auto preflightResult = filter.preflight(dataStructure, args);
SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions);

// Execute the filter and check the result
auto executeResult = filter.execute(dataStructure, args);
SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result);

REQUIRE(executeResult.result.errors().front().code == -1106);
}

TEST_CASE("SimplnxCore::ReadStlFileFilter:AttributeParseError", "[SimplnxCore][ReadStlFileFilter]")
{
const nx::core::UnitTest::TestFileSentinel testDataSentinel(nx::core::unit_test::k_CMakeExecutable, nx::core::unit_test::k_TestFilesDir, "ReadSTLFileTest.tar.gz", "ReadSTLFileTest");

// Instantiate the filter, a DataStructure object and an Arguments Object
DataStructure dataStructure;
Arguments args;
ReadStlFileFilter filter;

DataPath triangleGeomDataPath({"[Triangle Geometry]"});

std::string inputFile = fmt::format("{}/ReadSTLFileTest/stl_test_2_AttributeParseError.stl", unit_test::k_TestFilesDir);

// Create default Parameters for the filter.
args.insertOrAssign(ReadStlFileFilter::k_StlFilePath_Key, std::make_any<FileSystemPathParameter::ValueType>(fs::path(inputFile)));
args.insertOrAssign(ReadStlFileFilter::k_CreatedTriangleGeometryPath_Key, std::make_any<DataPath>(triangleGeomDataPath));

// Preflight the filter and check result
auto preflightResult = filter.preflight(dataStructure, args);
SIMPLNX_RESULT_REQUIRE_VALID(preflightResult.outputActions);

// Execute the filter and check the result
auto executeResult = filter.execute(dataStructure, args);
SIMPLNX_RESULT_REQUIRE_INVALID(executeResult.result);

REQUIRE(executeResult.result.errors().front().code == -1107);
}

0 comments on commit ad115ee

Please sign in to comment.