From 423cc42ac8dc1553268767469ffe7e8feb94af2e Mon Sep 17 00:00:00 2001 From: wmayer Date: Fri, 25 Aug 2023 15:54:48 +0200 Subject: [PATCH] Tests: add unit tests for: * TopoShape::getElementTypeAndIndex * ComplexGeoData::getTypeAndIndex and fix crashes there when passing a null pointer --- src/App/ComplexGeoData.cpp | 2 +- src/Mod/Part/App/TopoShape.cpp | 2 +- tests/CMakeLists.txt | 1 + tests/src/CMakeLists.txt | 1 + tests/src/Mod/CMakeLists.txt | 1 + tests/src/Mod/Part/App/CMakeLists.txt | 6 ++ tests/src/Mod/Part/App/TopoShape.cpp | 90 +++++++++++++++++++++++++++ tests/src/Mod/Part/CMakeLists.txt | 15 +++++ 8 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 tests/src/Mod/CMakeLists.txt create mode 100644 tests/src/Mod/Part/App/CMakeLists.txt create mode 100644 tests/src/Mod/Part/App/TopoShape.cpp create mode 100644 tests/src/Mod/Part/CMakeLists.txt diff --git a/src/App/ComplexGeoData.cpp b/src/App/ComplexGeoData.cpp index 50bb84d607..fcf7977e46 100644 --- a/src/App/ComplexGeoData.cpp +++ b/src/App/ComplexGeoData.cpp @@ -64,7 +64,7 @@ std::pair ComplexGeoData::getTypeAndIndex(const char boost::regex ex("^([^0-9]*)([0-9]*)$"); boost::cmatch what; - if (boost::regex_match(Name, what, ex)) { + if (Name && boost::regex_match(Name, what, ex)) { element = what[1].str(); index = std::atoi(what[2].str().c_str()); } diff --git a/src/Mod/Part/App/TopoShape.cpp b/src/Mod/Part/App/TopoShape.cpp index ba14734787..8705cc50d3 100644 --- a/src/Mod/Part/App/TopoShape.cpp +++ b/src/Mod/Part/App/TopoShape.cpp @@ -300,7 +300,7 @@ std::pair TopoShape::getElementTypeAndIndex(const ch boost::regex ex("^(Face|Edge|Vertex)([1-9][0-9]*)$"); boost::cmatch what; - if (boost::regex_match(Name, what, ex)) { + if (Name && boost::regex_match(Name, what, ex)) { element = what[1].str(); index = std::atoi(what[2].str().c_str()); } diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 299198e3b3..5af6ce9427 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -66,6 +66,7 @@ function(setup_qt_test) endfunction() add_executable(Tests_run) +add_executable(Part_tests_run) add_subdirectory(lib) add_subdirectory(src) target_include_directories(Tests_run PUBLIC diff --git a/tests/src/CMakeLists.txt b/tests/src/CMakeLists.txt index c95a1f3be2..9531af5dc6 100644 --- a/tests/src/CMakeLists.txt +++ b/tests/src/CMakeLists.txt @@ -2,4 +2,5 @@ add_subdirectory(Base) add_subdirectory(App) add_subdirectory(Gui) add_subdirectory(Misc) +add_subdirectory(Mod) add_subdirectory(zipios++) diff --git a/tests/src/Mod/CMakeLists.txt b/tests/src/Mod/CMakeLists.txt new file mode 100644 index 0000000000..22666473d0 --- /dev/null +++ b/tests/src/Mod/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(Part) diff --git a/tests/src/Mod/Part/App/CMakeLists.txt b/tests/src/Mod/Part/App/CMakeLists.txt new file mode 100644 index 0000000000..f8169423c6 --- /dev/null +++ b/tests/src/Mod/Part/App/CMakeLists.txt @@ -0,0 +1,6 @@ + +target_sources( + Part_tests_run + PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/TopoShape.cpp +) diff --git a/tests/src/Mod/Part/App/TopoShape.cpp b/tests/src/Mod/Part/App/TopoShape.cpp new file mode 100644 index 0000000000..0d838671d6 --- /dev/null +++ b/tests/src/Mod/Part/App/TopoShape.cpp @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: LGPL-2.1-or-later + +#include "gtest/gtest.h" +#include + +// clang-format off +TEST(TopoShape, TestElementTypeFace1) +{ + EXPECT_EQ(Part::TopoShape::getElementTypeAndIndex("Face1"), + std::make_pair(std::string("Face"), 1UL)); +} + +TEST(TopoShape, TestElementTypeEdge12) +{ + EXPECT_EQ(Part::TopoShape::getElementTypeAndIndex("Edge12"), + std::make_pair(std::string("Edge"), 12UL)); +} + +TEST(TopoShape, TestElementTypeVertex3) +{ + EXPECT_EQ(Part::TopoShape::getElementTypeAndIndex("Vertex3"), + std::make_pair(std::string("Vertex"), 3UL)); +} + +TEST(TopoShape, TestElementTypeFacer) +{ + EXPECT_EQ(Part::TopoShape::getElementTypeAndIndex("Facer"), + std::make_pair(std::string(), 0UL)); +} + +TEST(TopoShape, TestElementTypeVertex) +{ + EXPECT_EQ(Part::TopoShape::getElementTypeAndIndex("Vertex"), + std::make_pair(std::string(), 0UL)); +} + +TEST(TopoShape, TestElementTypeEmpty) +{ + EXPECT_EQ(Part::TopoShape::getElementTypeAndIndex(""), + std::make_pair(std::string(), 0UL)); +} + +TEST(TopoShape, TestElementTypeNull) +{ + EXPECT_EQ(Part::TopoShape::getElementTypeAndIndex(nullptr), + std::make_pair(std::string(), 0UL)); +} + +TEST(TopoShape, TestTypeFace1) +{ + EXPECT_EQ(Part::TopoShape::getTypeAndIndex("Face1"), + std::make_pair(std::string("Face"), 1UL)); +} + +TEST(TopoShape, TestTypeEdge12) +{ + EXPECT_EQ(Part::TopoShape::getTypeAndIndex("Edge12"), + std::make_pair(std::string("Edge"), 12UL)); +} + +TEST(TopoShape, TestTypeVertex3) +{ + EXPECT_EQ(Part::TopoShape::getTypeAndIndex("Vertex3"), + std::make_pair(std::string("Vertex"), 3UL)); +} + +TEST(TopoShape, TestTypeFacer) +{ + EXPECT_EQ(Part::TopoShape::getTypeAndIndex("Facer"), + std::make_pair(std::string("Facer"), 0UL)); +} + +TEST(TopoShape, TestTypeVertex) +{ + EXPECT_EQ(Part::TopoShape::getTypeAndIndex("Vertex"), + std::make_pair(std::string("Vertex"), 0UL)); +} + +TEST(TopoShape, TestTypeEmpty) +{ + EXPECT_EQ(Part::TopoShape::getTypeAndIndex(""), + std::make_pair(std::string(), 0UL)); +} + +TEST(TopoShape, TestTypeNull) +{ + EXPECT_EQ(Part::TopoShape::getTypeAndIndex(nullptr), + std::make_pair(std::string(), 0UL)); +} +// clang-format on diff --git a/tests/src/Mod/Part/CMakeLists.txt b/tests/src/Mod/Part/CMakeLists.txt new file mode 100644 index 0000000000..923821fcdc --- /dev/null +++ b/tests/src/Mod/Part/CMakeLists.txt @@ -0,0 +1,15 @@ + +target_include_directories(Part_tests_run PUBLIC + ${EIGEN3_INCLUDE_DIR} + ${OCC_INCLUDE_DIR} + ${Python3_INCLUDE_DIRS} + ${XercesC_INCLUDE_DIRS} +) + +target_link_libraries(Part_tests_run + gtest_main + ${Google_Tests_LIBS} + Part +) + +add_subdirectory(App)