From 4dda7fc05f771cb759399805277d70c96b07d722 Mon Sep 17 00:00:00 2001 From: Aaron Kimbrell Date: Tue, 7 Oct 2025 09:40:44 -0500 Subject: [PATCH] cleanup tests --- tests/dCommonTests/CMakeLists.txt | 37 ++- .../LxfmlTestFiles/deeply_nested.lxfml | 50 ++++ .../LxfmlTestFiles/empty_transform.lxfml | 11 + .../LxfmlTestFiles/invalid_transform.lxfml | 20 ++ .../mixed_invalid_transform.lxfml | 11 + .../LxfmlTestFiles/mixed_valid_invalid.lxfml | 44 ++++ .../LxfmlTestFiles/no_bricks.lxfml | 4 + .../non_numeric_transform.lxfml | 11 + .../LxfmlTestFiles/too_few_values.lxfml | 11 + tests/dCommonTests/LxfmlTests.cpp | 246 ++++++------------ 10 files changed, 278 insertions(+), 167 deletions(-) create mode 100644 tests/dCommonTests/LxfmlTestFiles/deeply_nested.lxfml create mode 100644 tests/dCommonTests/LxfmlTestFiles/empty_transform.lxfml create mode 100644 tests/dCommonTests/LxfmlTestFiles/invalid_transform.lxfml create mode 100644 tests/dCommonTests/LxfmlTestFiles/mixed_invalid_transform.lxfml create mode 100644 tests/dCommonTests/LxfmlTestFiles/mixed_valid_invalid.lxfml create mode 100644 tests/dCommonTests/LxfmlTestFiles/no_bricks.lxfml create mode 100644 tests/dCommonTests/LxfmlTestFiles/non_numeric_transform.lxfml create mode 100644 tests/dCommonTests/LxfmlTestFiles/too_few_values.lxfml diff --git a/tests/dCommonTests/CMakeLists.txt b/tests/dCommonTests/CMakeLists.txt index e1311f1d..efac921d 100644 --- a/tests/dCommonTests/CMakeLists.txt +++ b/tests/dCommonTests/CMakeLists.txt @@ -34,5 +34,38 @@ target_link_libraries(dCommonTests ${COMMON_LIBRARIES} GTest::gtest_main) add_subdirectory(TestBitStreams) file(COPY ${TESTBITSTREAMS} DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) -# Discover the tests -gtest_discover_tests(dCommonTests) +# Copy LXFML test files to the build directory where the executable runs from +# Use a custom command to ensure files are copied when the target is built +add_custom_command(TARGET dCommonTests POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${CMAKE_CURRENT_SOURCE_DIR}/LxfmlTestFiles/test.lxfml" + "$/test.lxfml" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${CMAKE_CURRENT_SOURCE_DIR}/LxfmlTestFiles/invalid_transform.lxfml" + "$/invalid_transform.lxfml" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${CMAKE_CURRENT_SOURCE_DIR}/LxfmlTestFiles/empty_transform.lxfml" + "$/empty_transform.lxfml" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${CMAKE_CURRENT_SOURCE_DIR}/LxfmlTestFiles/too_few_values.lxfml" + "$/too_few_values.lxfml" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${CMAKE_CURRENT_SOURCE_DIR}/LxfmlTestFiles/non_numeric_transform.lxfml" + "$/non_numeric_transform.lxfml" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${CMAKE_CURRENT_SOURCE_DIR}/LxfmlTestFiles/mixed_invalid_transform.lxfml" + "$/mixed_invalid_transform.lxfml" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${CMAKE_CURRENT_SOURCE_DIR}/LxfmlTestFiles/no_bricks.lxfml" + "$/no_bricks.lxfml" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${CMAKE_CURRENT_SOURCE_DIR}/LxfmlTestFiles/mixed_valid_invalid.lxfml" + "$/mixed_valid_invalid.lxfml" + COMMAND ${CMAKE_COMMAND} -E copy_if_different + "${CMAKE_CURRENT_SOURCE_DIR}/LxfmlTestFiles/deeply_nested.lxfml" + "$/deeply_nested.lxfml" + COMMENT "Copying LXFML test files to executable directory" +) + +# Discover the tests and set working directory to where the executable and test files are +gtest_discover_tests(dCommonTests WORKING_DIRECTORY "$") diff --git a/tests/dCommonTests/LxfmlTestFiles/deeply_nested.lxfml b/tests/dCommonTests/LxfmlTestFiles/deeply_nested.lxfml new file mode 100644 index 00000000..40f49f38 --- /dev/null +++ b/tests/dCommonTests/LxfmlTestFiles/deeply_nested.lxfml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/dCommonTests/LxfmlTestFiles/empty_transform.lxfml b/tests/dCommonTests/LxfmlTestFiles/empty_transform.lxfml new file mode 100644 index 00000000..3d360853 --- /dev/null +++ b/tests/dCommonTests/LxfmlTestFiles/empty_transform.lxfml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/tests/dCommonTests/LxfmlTestFiles/invalid_transform.lxfml b/tests/dCommonTests/LxfmlTestFiles/invalid_transform.lxfml new file mode 100644 index 00000000..d6355dea --- /dev/null +++ b/tests/dCommonTests/LxfmlTestFiles/invalid_transform.lxfml @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/dCommonTests/LxfmlTestFiles/mixed_invalid_transform.lxfml b/tests/dCommonTests/LxfmlTestFiles/mixed_invalid_transform.lxfml new file mode 100644 index 00000000..32c4ee97 --- /dev/null +++ b/tests/dCommonTests/LxfmlTestFiles/mixed_invalid_transform.lxfml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/tests/dCommonTests/LxfmlTestFiles/mixed_valid_invalid.lxfml b/tests/dCommonTests/LxfmlTestFiles/mixed_valid_invalid.lxfml new file mode 100644 index 00000000..d0916865 --- /dev/null +++ b/tests/dCommonTests/LxfmlTestFiles/mixed_valid_invalid.lxfml @@ -0,0 +1,44 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/dCommonTests/LxfmlTestFiles/no_bricks.lxfml b/tests/dCommonTests/LxfmlTestFiles/no_bricks.lxfml new file mode 100644 index 00000000..67ff618a --- /dev/null +++ b/tests/dCommonTests/LxfmlTestFiles/no_bricks.lxfml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/tests/dCommonTests/LxfmlTestFiles/non_numeric_transform.lxfml b/tests/dCommonTests/LxfmlTestFiles/non_numeric_transform.lxfml new file mode 100644 index 00000000..0bdf7cc8 --- /dev/null +++ b/tests/dCommonTests/LxfmlTestFiles/non_numeric_transform.lxfml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/tests/dCommonTests/LxfmlTestFiles/too_few_values.lxfml b/tests/dCommonTests/LxfmlTestFiles/too_few_values.lxfml new file mode 100644 index 00000000..f7a957c3 --- /dev/null +++ b/tests/dCommonTests/LxfmlTestFiles/too_few_values.lxfml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/tests/dCommonTests/LxfmlTests.cpp b/tests/dCommonTests/LxfmlTests.cpp index 68044031..acbe5fe7 100644 --- a/tests/dCommonTests/LxfmlTests.cpp +++ b/tests/dCommonTests/LxfmlTests.cpp @@ -11,39 +11,24 @@ using namespace TinyXmlUtils; -static std::string ReadFile(const std::string& path) { - std::ifstream in(path, std::ios::in | std::ios::binary); +static std::string ReadFile(const std::string& filename) { + std::ifstream in(filename, std::ios::in | std::ios::binary); + if (!in.is_open()) { + return ""; + } std::ostringstream ss; ss << in.rdbuf(); return ss.str(); } TEST(LxfmlTests, SplitUsesAllBricksAndNoDuplicates) { - // Read the sample test.lxfml included in tests. Resolve path relative to this source file. - std::filesystem::path srcDir = std::filesystem::path(__FILE__).parent_path(); - std::filesystem::path filePath = srcDir / "LxfmlTestFiles" / "test.lxfml"; - std::ifstream in(filePath, std::ios::in | std::ios::binary); - std::ostringstream ss; - ss << in.rdbuf(); - std::string data = ss.str(); - ASSERT_FALSE(data.empty()) << "Failed to read " << filePath.string(); - + // Read the test.lxfml file copied to build directory by CMake + std::string data = ReadFile("test.lxfml"); + ASSERT_FALSE(data.empty()) << "Failed to read test.lxfml from build directory"; auto results = Lxfml::Split(data); ASSERT_GT(results.size(), 0); - // Write split outputs to disk for manual inspection - std::filesystem::path outDir = srcDir / "LxfmlTestFiles" / "lxfml_splits"; - std::error_code ec; - std::filesystem::create_directories(outDir, ec); - for (size_t i = 0; i < results.size(); ++i) { - auto outPath = outDir / ("split_" + std::to_string(i) + ".lxfml"); - std::ofstream ofs(outPath, std::ios::out | std::ios::binary); - ASSERT_TRUE(ofs) << "Failed to open output file: " << outPath.string(); - ofs << results[i].lxfml; - ofs.close(); - } - // parse original to count bricks tinyxml2::XMLDocument doc; ASSERT_EQ(doc.Parse(data.c_str()), tinyxml2::XML_SUCCESS); @@ -152,26 +137,8 @@ TEST(LxfmlTests, SplitUsesAllBricksAndNoDuplicates) { TEST(LxfmlTests, InvalidLxfmlHandling) { // Test LXFML with invalid transformation matrices - std::string invalidTransformData = R"( - - - - - - - - - - - - - - - - - - -)"; + std::string invalidTransformData = ReadFile("invalid_transform.lxfml"); + ASSERT_FALSE(invalidTransformData.empty()) << "Failed to read invalid_transform.lxfml from build directory"; // The Split function should handle invalid transformation matrices gracefully std::vector results; @@ -195,82 +162,80 @@ TEST(LxfmlTests, EmptyLxfmlHandling) { EXPECT_EQ(results.size(), 0) << "Empty input should return empty results"; } -TEST(LxfmlTests, InvalidTransformHandling) { - // Test with various types of invalid transformation matrices - std::vector invalidTransformTests = { - // LXFML with empty transformation - R"()", - - // LXFML with too few transformation values (needs 12, has 6) - R"()", - - // LXFML with non-numeric transformation values - R"()", - - // LXFML with mixed valid/invalid transformation values - R"()", - - // LXFML with no Bricks section (should return empty gracefully) - R"()" - }; +TEST(LxfmlTests, EmptyTransformHandling) { + // Test LXFML with empty transformation matrix + std::string testData = ReadFile("empty_transform.lxfml"); + ASSERT_FALSE(testData.empty()) << "Failed to read empty_transform.lxfml from build directory"; - for (size_t i = 0; i < invalidTransformTests.size(); ++i) { - std::vector results; - EXPECT_NO_FATAL_FAILURE({ - results = Lxfml::Split(invalidTransformTests[i]); - }) << "Split should not crash on invalid transform test case " << i; - - // The function should handle invalid transforms gracefully - // May return empty results or skip invalid bricks - } + std::vector results; + EXPECT_NO_FATAL_FAILURE({ + results = Lxfml::Split(testData); + }) << "Split should not crash on empty transformation matrix"; + + // The function should handle empty transforms gracefully + // May return empty results or skip invalid bricks +} + +TEST(LxfmlTests, TooFewValuesTransformHandling) { + // Test LXFML with too few transformation values (needs 12, has fewer) + std::string testData = ReadFile("too_few_values.lxfml"); + ASSERT_FALSE(testData.empty()) << "Failed to read too_few_values.lxfml from build directory"; + + std::vector results; + EXPECT_NO_FATAL_FAILURE({ + results = Lxfml::Split(testData); + }) << "Split should not crash on transformation matrix with too few values"; + + // The function should handle incomplete transforms gracefully + // May return empty results or skip invalid bricks +} + +TEST(LxfmlTests, NonNumericTransformHandling) { + // Test LXFML with non-numeric transformation values + std::string testData = ReadFile("non_numeric_transform.lxfml"); + ASSERT_FALSE(testData.empty()) << "Failed to read non_numeric_transform.lxfml from build directory"; + + std::vector results; + EXPECT_NO_FATAL_FAILURE({ + results = Lxfml::Split(testData); + }) << "Split should not crash on non-numeric transformation values"; + + // The function should handle non-numeric transforms gracefully + // May return empty results or skip invalid bricks +} + +TEST(LxfmlTests, MixedInvalidTransformHandling) { + // Test LXFML with mixed valid/invalid transformation values within a matrix + std::string testData = ReadFile("mixed_invalid_transform.lxfml"); + ASSERT_FALSE(testData.empty()) << "Failed to read mixed_invalid_transform.lxfml from build directory"; + + std::vector results; + EXPECT_NO_FATAL_FAILURE({ + results = Lxfml::Split(testData); + }) << "Split should not crash on mixed valid/invalid transformation values"; + + // The function should handle mixed valid/invalid transforms gracefully + // May return empty results or skip invalid bricks +} + +TEST(LxfmlTests, NoBricksHandling) { + // Test LXFML with no Bricks section (should return empty gracefully) + std::string testData = ReadFile("no_bricks.lxfml"); + ASSERT_FALSE(testData.empty()) << "Failed to read no_bricks.lxfml from build directory"; + + std::vector results; + EXPECT_NO_FATAL_FAILURE({ + results = Lxfml::Split(testData); + }) << "Split should not crash on LXFML with no Bricks section"; + + // Should return empty results gracefully when no bricks are present + EXPECT_EQ(results.size(), 0) << "LXFML with no bricks should return empty results"; } TEST(LxfmlTests, MixedValidInvalidTransformsHandling) { // Test LXFML with mix of valid and invalid transformation data - std::string mixedValidData = R"( - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -)"; + std::string mixedValidData = ReadFile("mixed_valid_invalid.lxfml"); + ASSERT_FALSE(mixedValidData.empty()) << "Failed to read mixed_valid_invalid.lxfml from build directory"; // The Split function should handle mixed valid/invalid transforms gracefully std::vector results; @@ -299,57 +264,8 @@ TEST(LxfmlTests, MixedValidInvalidTransformsHandling) { TEST(LxfmlTests, DeepCloneDepthProtection) { // Test that deep cloning has protection against excessive nesting - // Create a deeply nested XML structure that would exceed reasonable limits - std::string deeplyNestedLxfml = R"( - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -)"; + std::string deeplyNestedLxfml = ReadFile("deeply_nested.lxfml"); + ASSERT_FALSE(deeplyNestedLxfml.empty()) << "Failed to read deeply_nested.lxfml from build directory"; // The Split function should handle deeply nested structures without hanging std::vector results;