Tests: add unit tests for:

* TopoShape::getElementTypeAndIndex
* ComplexGeoData::getTypeAndIndex

and fix crashes there when passing a null pointer
This commit is contained in:
wmayer
2023-08-25 15:54:48 +02:00
committed by wwmayer
parent 66fb1c7777
commit 2c2347f746
8 changed files with 116 additions and 2 deletions

View File

@@ -64,7 +64,7 @@ std::pair<std::string, unsigned long> 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());
}

View File

@@ -300,7 +300,7 @@ std::pair<std::string, unsigned long> 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());
}

View File

@@ -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

View File

@@ -2,4 +2,5 @@ add_subdirectory(Base)
add_subdirectory(App)
add_subdirectory(Gui)
add_subdirectory(Misc)
add_subdirectory(Mod)
add_subdirectory(zipios++)

View File

@@ -0,0 +1 @@
add_subdirectory(Part)

View File

@@ -0,0 +1,6 @@
target_sources(
Part_tests_run
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/TopoShape.cpp
)

View File

@@ -0,0 +1,90 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
#include "gtest/gtest.h"
#include <Mod/Part/App/TopoShape.h>
// 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

View File

@@ -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)