From e24ed3486732f2fa0829aa8f7f23d7e98ccb1e1f Mon Sep 17 00:00:00 2001 From: Chris Hennes Date: Sat, 27 Jan 2024 10:49:59 -0600 Subject: [PATCH] Tests/Toponaming: Post-merge cleanup --- src/Mod/Part/App/TopoShape.h | 21 --- src/Mod/Part/App/TopoShapeExpansion.cpp | 12 +- tests/src/Mod/Part/App/CMakeLists.txt | 1 - tests/src/Mod/Part/App/PartTestHelpers.cpp | 18 ++ tests/src/Mod/Part/App/PartTestHelpers.h | 2 + tests/src/Mod/Part/App/TopoShapeExpansion.cpp | 140 +++++++-------- .../Part/App/TopoShapeExpansionHelpers.cpp | 26 --- .../Mod/Part/App/TopoShapeExpansionHelpers.h | 13 -- .../App/TopoShapeMakeShapeWithElementMap.cpp | 166 ++++++++++-------- 9 files changed, 190 insertions(+), 209 deletions(-) delete mode 100644 tests/src/Mod/Part/App/TopoShapeExpansionHelpers.cpp delete mode 100644 tests/src/Mod/Part/App/TopoShapeExpansionHelpers.h diff --git a/src/Mod/Part/App/TopoShape.h b/src/Mod/Part/App/TopoShape.h index bf30436a82..b04caf52ed 100644 --- a/src/Mod/Part/App/TopoShape.h +++ b/src/Mod/Part/App/TopoShape.h @@ -677,27 +677,6 @@ public: const std::vector &sources, const char *op=nullptr); - /** Helper class to return the generated and modified shape given an input shape - * - * Shape history information is extracted using OCCT APIs - * BRepBuilderAPI_MakeShape::Generated/Modified(). However, there is often - * some glitches in various derived class. So we use this class as an - * abstraction, and create various derived classes to deal with the glitches. - */ - struct PartExport Mapper { - /// Helper vector for temporary storage of both generated and modified shapes - mutable std::vector _res; - virtual ~Mapper() {} - /// Return a list of shape generated from the given input shape - virtual const std::vector &generated(const TopoDS_Shape &) const { - return _res; - } - /// Return a list of shape modified from the given input shape - virtual const std::vector &modified(const TopoDS_Shape &) const { - return _res; - } - }; - /** Make a compound shape * * @param shapes: input shapes diff --git a/src/Mod/Part/App/TopoShapeExpansion.cpp b/src/Mod/Part/App/TopoShapeExpansion.cpp index a57780f7da..eb6b03be29 100644 --- a/src/Mod/Part/App/TopoShapeExpansion.cpp +++ b/src/Mod/Part/App/TopoShapeExpansion.cpp @@ -45,6 +45,8 @@ #include "TopoShapeOpCode.h" #include "FaceMaker.h" +#include + FC_LOG_LEVEL_INIT("TopoShape", true, true) // NOLINT @@ -606,31 +608,31 @@ struct NameInfo const std::string& modPostfix() { - static std::string postfix(TopoShape::elementMapPrefix() + ":M"); + static std::string postfix(Data::POSTFIX_MOD); return postfix; } const std::string& modgenPostfix() { - static std::string postfix(modPostfix() + "G"); + static std::string postfix(Data::POSTFIX_MODGEN); return postfix; } const std::string& genPostfix() { - static std::string postfix(TopoShape::elementMapPrefix() + ":G"); + static std::string postfix(Data::POSTFIX_GEN); return postfix; } const std::string& upperPostfix() { - static std::string postfix(TopoShape::elementMapPrefix() + ":U"); + static std::string postfix(Data::POSTFIX_UPPER); return postfix; } const std::string& lowerPostfix() { - static std::string postfix(TopoShape::elementMapPrefix() + ":L"); + static std::string postfix(Data::POSTFIX_LOWER); return postfix; } diff --git a/tests/src/Mod/Part/App/CMakeLists.txt b/tests/src/Mod/Part/App/CMakeLists.txt index 4c5bedf30d..f239b3a27b 100644 --- a/tests/src/Mod/Part/App/CMakeLists.txt +++ b/tests/src/Mod/Part/App/CMakeLists.txt @@ -15,7 +15,6 @@ target_sources( ${CMAKE_CURRENT_SOURCE_DIR}/TopoShape.cpp ${CMAKE_CURRENT_SOURCE_DIR}/TopoShapeCache.cpp ${CMAKE_CURRENT_SOURCE_DIR}/TopoShapeExpansion.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/TopoShapeExpansionHelpers.cpp ${CMAKE_CURRENT_SOURCE_DIR}/TopoShapeMakeShapeWithElementMap.cpp ${CMAKE_CURRENT_SOURCE_DIR}/TopoShapeMapper.cpp ) diff --git a/tests/src/Mod/Part/App/PartTestHelpers.cpp b/tests/src/Mod/Part/App/PartTestHelpers.cpp index 8b0390b7e5..36dc63f067 100644 --- a/tests/src/Mod/Part/App/PartTestHelpers.cpp +++ b/tests/src/Mod/Part/App/PartTestHelpers.cpp @@ -1,5 +1,7 @@ // SPDX-License-Identifier: LGPL-2.1-or-later +#include + #include "PartTestHelpers.h" // NOLINTBEGIN(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers) @@ -137,6 +139,22 @@ std::map elementMap(const TopoShape& shape) return result; } +std::pair CreateTwoCubes() +{ + auto boxMaker1 = BRepPrimAPI_MakeBox(1.0, 1.0, 1.0); + boxMaker1.Build(); + auto box1 = boxMaker1.Shape(); + + auto boxMaker2 = BRepPrimAPI_MakeBox(1.0, 1.0, 1.0); + boxMaker2.Build(); + auto box2 = boxMaker2.Shape(); + auto transform = gp_Trsf(); + transform.SetTranslation(gp_Pnt(0.0, 0.0, 0.0), gp_Pnt(1.0, 0.0, 0.0)); + box2.Location(TopLoc_Location(transform)); + + return {box1, box2}; +} + } // namespace PartTestHelpers // NOLINTEND(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers) diff --git a/tests/src/Mod/Part/App/PartTestHelpers.h b/tests/src/Mod/Part/App/PartTestHelpers.h index 79147f6531..8b829e4ba6 100644 --- a/tests/src/Mod/Part/App/PartTestHelpers.h +++ b/tests/src/Mod/Part/App/PartTestHelpers.h @@ -57,4 +57,6 @@ boxesMatch(const Base::BoundBox3d& b1, const Base::BoundBox3d& b2, double prec = std::map elementMap(const TopoShape& shape); +std::pair CreateTwoCubes(); + } // namespace PartTestHelpers diff --git a/tests/src/Mod/Part/App/TopoShapeExpansion.cpp b/tests/src/Mod/Part/App/TopoShapeExpansion.cpp index dd72b48d20..f1b82a9822 100644 --- a/tests/src/Mod/Part/App/TopoShapeExpansion.cpp +++ b/tests/src/Mod/Part/App/TopoShapeExpansion.cpp @@ -2,23 +2,21 @@ #include "gtest/gtest.h" #include "src/App/InitApplication.h" -#include "TopoShapeExpansionHelpers.h" #include #include #include "PartTestHelpers.h" #include -#include #include #include #include #include -#include #include // NOLINTBEGIN(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers) +using namespace PartTestHelpers; class TopoShapeExpansionTest: public ::testing::Test { @@ -32,7 +30,6 @@ protected: { _docName = App::GetApplication().getUniqueDocumentName("test"); App::GetApplication().newDocument(_docName.c_str(), "testUser"); - _sids = &_sid; _hasher = Base::Reference(new App::StringHasher); ASSERT_EQ(_hasher.getRefCount(), 1); } @@ -46,7 +43,6 @@ protected: private: std::string _docName; Data::ElementIDRefs _sid; - QVector* _sids = nullptr; App::StringHasherRef _hasher; }; @@ -129,7 +125,7 @@ TEST_F(TopoShapeExpansionTest, makeElementCompoundTwoShapesGeneratesMap) TEST_F(TopoShapeExpansionTest, makeElementCompoundTwoCubes) { // Arrange - auto [cube1, cube2] = TopoShapeExpansionHelpers::CreateTwoCubes(); + auto [cube1, cube2] = CreateTwoCubes(); Part::TopoShape cube1TS {cube1}; cube1TS.Tag = 1; Part::TopoShape cube2TS {cube2}; @@ -150,42 +146,20 @@ TEST_F(TopoShapeExpansionTest, makeElementCompoundTwoCubes) // 26 subshapes each } -std::tuple -CreateRectFace(float len = 2.0, float wid = 3.0) -{ - auto edge1 = BRepBuilderAPI_MakeEdge(gp_Pnt(0.0, 0.0, 0.0), gp_Pnt(len, 0.0, 0.0)).Edge(); - auto edge2 = BRepBuilderAPI_MakeEdge(gp_Pnt(len, 0.0, 0.0), gp_Pnt(len, wid, 0.0)).Edge(); - auto edge3 = BRepBuilderAPI_MakeEdge(gp_Pnt(len, wid, 0.0), gp_Pnt(0.0, wid, 0.0)).Edge(); - auto edge4 = BRepBuilderAPI_MakeEdge(gp_Pnt(0.0, wid, 0.0), gp_Pnt(0.0, 0.0, 0.0)).Edge(); - auto wire1 = BRepBuilderAPI_MakeWire({edge1, edge2, edge3, edge4}).Wire(); - auto face1 = BRepBuilderAPI_MakeFace(wire1).Face(); - return {face1, wire1, edge1, edge2, edge3, edge4}; -} - -std::tuple -CreateFaceWithRoundHole(float len = 2.0, float wid = 3.0, float radius = 1.0) -{ - auto [face1, wire1, edge1, edge2, edge3, edge4] = CreateRectFace(len, wid); - auto circ1 = - GC_MakeCircle(gp_Pnt(len / 2.0, wid / 2.0, 0), gp_Dir(0.0, 0.0, 1.0), radius).Value(); - auto edge5 = BRepBuilderAPI_MakeEdge(circ1).Edge(); - auto wire2 = BRepBuilderAPI_MakeWire(edge5).Wire(); - auto face2 = BRepBuilderAPI_MakeFace(face1, wire2).Face(); - return {face2, wire1, wire2}; -} - TEST_F(TopoShapeExpansionTest, makeElementFaceNull) { // Arrange - const double Len = 3, Wid = 2, Rad = 1; + const float Len = 3; + const float Wid = 2; + const float Rad = 1; auto [face1, wire1, wire2] = CreateFaceWithRoundHole(Len, Wid, Rad); Part::TopoShape topoShape {face1}; - double area = PartTestHelpers::getArea(face1); - double area1 = PartTestHelpers::getArea(topoShape.getShape()); + double area = getArea(face1); + double area1 = getArea(topoShape.getShape()); // Act Part::TopoShape newFace = topoShape.makeElementFace(nullptr); - double area2 = PartTestHelpers::getArea(newFace.getShape()); - double area3 = PartTestHelpers::getArea(topoShape.getShape()); + double area2 = getArea(newFace.getShape()); + double area3 = getArea(topoShape.getShape()); // Assert EXPECT_FALSE(face1.IsEqual(newFace.getShape())); EXPECT_FLOAT_EQ(area, Len * Wid + M_PI * Rad * Rad); @@ -198,15 +172,17 @@ TEST_F(TopoShapeExpansionTest, makeElementFaceNull) TEST_F(TopoShapeExpansionTest, makeElementFaceSimple) { // Arrange - const double Len = 3, Wid = 2, Rad = 1; + const float Len = 3; + const float Wid = 2; + const float Rad = 1; auto [face1, wire1, wire2] = CreateFaceWithRoundHole(Len, Wid, Rad); Part::TopoShape topoShape {face1}; - double area = PartTestHelpers::getArea(face1); - double area1 = PartTestHelpers::getArea(topoShape.getShape()); + double area = getArea(face1); + double area1 = getArea(topoShape.getShape()); // Act Part::TopoShape newFace = topoShape.makeElementFace(wire1); - double area2 = PartTestHelpers::getArea(newFace.getShape()); - double area3 = PartTestHelpers::getArea(topoShape.getShape()); + double area2 = getArea(newFace.getShape()); + double area3 = getArea(topoShape.getShape()); // Assert EXPECT_TRUE(newFace.getShape().IsEqual(topoShape.getShape())); // topoShape was altered EXPECT_FALSE(face1.IsEqual(newFace.getShape())); @@ -220,16 +196,18 @@ TEST_F(TopoShapeExpansionTest, makeElementFaceSimple) TEST_F(TopoShapeExpansionTest, makeElementFaceParams) { // Arrange - const double Len = 3, Wid = 2, Rad = 1; + const float Len = 3; + const float Wid = 2; + const float Rad = 1; auto [face1, wire1, wire2] = CreateFaceWithRoundHole(Len, Wid, Rad); Part::TopoShape topoShape {face1, 1L}; - double area = PartTestHelpers::getArea(face1); - double area1 = PartTestHelpers::getArea(topoShape.getShape()); + double area = getArea(face1); + double area1 = getArea(topoShape.getShape()); // Act Part::TopoShape newFace = topoShape.makeElementFace(wire1, "Cut", "Part::FaceMakerBullseye", nullptr); - double area2 = PartTestHelpers::getArea(newFace.getShape()); - double area3 = PartTestHelpers::getArea(topoShape.getShape()); + double area2 = getArea(newFace.getShape()); + double area3 = getArea(topoShape.getShape()); // Assert EXPECT_TRUE(newFace.getShape().IsEqual(topoShape.getShape())); // topoShape was altered EXPECT_FALSE(face1.IsEqual(newFace.getShape())); @@ -243,16 +221,18 @@ TEST_F(TopoShapeExpansionTest, makeElementFaceParams) TEST_F(TopoShapeExpansionTest, makeElementFaceFromFace) { // Arrange - const double Len = 3, Wid = 2, Rad = 1; + const float Len = 3; + const float Wid = 2; + const float Rad = 1; auto [face1, wire1, wire2] = CreateFaceWithRoundHole(Len, Wid, Rad); Part::TopoShape topoShape {face1, 1L}; - double area = PartTestHelpers::getArea(face1); - double area1 = PartTestHelpers::getArea(topoShape.getShape()); + double area = getArea(face1); + double area1 = getArea(topoShape.getShape()); // Act Part::TopoShape newFace = topoShape.makeElementFace(face1, "Cut", "Part::FaceMakerBullseye", nullptr); - double area2 = PartTestHelpers::getArea(newFace.getShape()); - double area3 = PartTestHelpers::getArea(topoShape.getShape()); + double area2 = getArea(newFace.getShape()); + double area3 = getArea(topoShape.getShape()); // Assert EXPECT_TRUE(newFace.getShape().IsEqual(topoShape.getShape())); // topoShape was altered EXPECT_FALSE(face1.IsEqual(newFace.getShape())); @@ -267,15 +247,17 @@ TEST_F(TopoShapeExpansionTest, makeElementFaceFromFace) TEST_F(TopoShapeExpansionTest, makeElementFaceOpenWire) { // Arrange - const double Len = 3, Wid = 2, Rad = 1; + const float Len = 3; + const float Wid = 2; + const float Rad = 1; auto [face1, wire1, wire2] = CreateFaceWithRoundHole(Len, Wid, Rad); Part::TopoShape topoShape {wire1, 1L}; - double area = PartTestHelpers::getArea(face1); - double area1 = PartTestHelpers::getArea(topoShape.getShape()); + double area = getArea(face1); + double area1 = getArea(topoShape.getShape()); // Act Part::TopoShape newFace = topoShape.makeElementFace(wire1, "Cut", nullptr, nullptr); - double area2 = PartTestHelpers::getArea(newFace.getShape()); - double area3 = PartTestHelpers::getArea(topoShape.getShape()); + double area2 = getArea(newFace.getShape()); + double area3 = getArea(topoShape.getShape()); // Assert EXPECT_TRUE(newFace.getShape().IsEqual(topoShape.getShape())); // topoShape was altered EXPECT_FALSE(face1.IsEqual(newFace.getShape())); @@ -290,16 +272,18 @@ TEST_F(TopoShapeExpansionTest, makeElementFaceOpenWire) TEST_F(TopoShapeExpansionTest, makeElementFaceClosedWire) { // Arrange - const double Len = 3, Wid = 2, Rad = 1; + const float Len = 3; + const float Wid = 2; + const float Rad = 1; auto [face1, wire1, wire2] = CreateFaceWithRoundHole(Len, Wid, Rad); Part::TopoShape topoShape {wire2, 1L}; - double area = PartTestHelpers::getArea(face1); - double area1 = PartTestHelpers::getArea(topoShape.getShape()); + double area = getArea(face1); + double area1 = getArea(topoShape.getShape()); // Act Part::TopoShape newFace = topoShape.makeElementFace(wire2, "Cut", "Part::FaceMakerBullseye", nullptr); - double area2 = PartTestHelpers::getArea(newFace.getShape()); - double area3 = PartTestHelpers::getArea(topoShape.getShape()); + double area2 = getArea(newFace.getShape()); + double area3 = getArea(topoShape.getShape()); // Assert EXPECT_TRUE(newFace.getShape().IsEqual(topoShape.getShape())); // topoShape was altered EXPECT_FALSE(face1.IsEqual(newFace.getShape())); @@ -395,7 +379,9 @@ TEST_F(TopoShapeExpansionTest, setElementComboNameCompound) TEST_F(TopoShapeExpansionTest, splitWires) { // Arrange - const double Len = 3, Wid = 2, Rad = 1; + const float Len = 3; + const float Wid = 2; + const float Rad = 1; auto [face1, wire1, wire2] = CreateFaceWithRoundHole(Len, Wid, Rad); Part::TopoShape topoShape {face1, 1L}; std::vector inner; @@ -405,8 +391,8 @@ TEST_F(TopoShapeExpansionTest, splitWires) topoShape.splitWires(&inner, Part::TopoShape::SplitWireReorient::ReorientReversed); // Assert EXPECT_EQ(inner.size(), 1); - EXPECT_FLOAT_EQ(PartTestHelpers::getLength(wire.getShape()), 2 + 2 + 3 + 3); - EXPECT_FLOAT_EQ(PartTestHelpers::getLength(inner.front().getShape()), M_PI * Rad * 2); + EXPECT_FLOAT_EQ(getLength(wire.getShape()), 2 + 2 + 3 + 3); + EXPECT_FLOAT_EQ(getLength(inner.front().getShape()), M_PI * Rad * 2); EXPECT_EQ(wire.getShape().Orientation(), TopAbs_REVERSED); for (Part::TopoShape& shape : inner) { EXPECT_EQ(shape.getShape().Orientation(), TopAbs_FORWARD); @@ -415,8 +401,8 @@ TEST_F(TopoShapeExpansionTest, splitWires) // Possible future tests: // splitWires without inner Wires -// splitWires with allfour reorientation values NoReorient, ReOrient, ReorientForward, -// ReorientRevesed +// splitWires with all four reorientation values NoReorient, ReOrient, ReorientForward, +// ReorientReversed TEST_F(TopoShapeExpansionTest, mapSubElementInvalidParm) { @@ -442,7 +428,8 @@ TEST_F(TopoShapeExpansionTest, mapSubElementFindShapeByNames) Part::TopoShape cube2TS {cube2}; cube1TS.Tag = 1; cube2TS.Tag = 2; - Part::TopoShape topoShape, topoShape1; + Part::TopoShape topoShape; + Part::TopoShape topoShape1; // Act int fs1 = topoShape1.findShape(cube1); @@ -527,8 +514,13 @@ TEST_F(TopoShapeExpansionTest, mapSubElementFindAncestors) cube2TS.Tag = 2; cube3TS.Tag = 3; cube4TS.Tag = 4; - Part::TopoShape topoShape, topoShape1, topoShape2; - Part::TopoShape topoShape3, topoShape4, topoShape5, topoShape6; + Part::TopoShape topoShape; + Part::TopoShape topoShape1; + Part::TopoShape topoShape2; + Part::TopoShape topoShape3; + Part::TopoShape topoShape4; + Part::TopoShape topoShape5; + Part::TopoShape topoShape6; topoShape.makeElementCompound({cube1TS, cube2TS}); topoShape1.makeElementCompound({cube3TS, cube4TS}); topoShape2.makeElementCompound({cube1TS, cube3TS}); @@ -576,7 +568,8 @@ TEST_F(TopoShapeExpansionTest, makeElementShellInvalid) TEST_F(TopoShapeExpansionTest, makeElementShellSingle) { // Arrange - const double Len = 3, Wid = 2; + const float Len = 3; + const float Wid = 2; auto [face1, wire1, edge1, edge2, edge3, _] = CreateRectFace(Len, Wid); Part::TopoShape topoShape {face1, 1L}; // Act @@ -594,7 +587,8 @@ TEST_F(TopoShapeExpansionTest, makeElementShellSingle) TEST_F(TopoShapeExpansionTest, makeElementShellOpen) { // Arrange - const double Len = 3, Wid = 2; + const float Len = 3; + const float Wid = 2; auto [face1, wire1, edge1, edge2, edge3, edge4] = CreateRectFace(Len, Wid); auto transform {gp_Trsf()}; transform.SetRotation(gp_Ax1(gp_Pnt(0, 0, 0), gp_Dir(1, 0, 0)), M_PI / 2); @@ -625,7 +619,7 @@ TEST_F(TopoShapeExpansionTest, makeElementShellClosed) Part::TopoShape topoShape {cube1}; std::vector shapes; for (const auto& face : topoShape.getSubShapes(TopAbs_FACE)) { - shapes.push_back(Part::TopoShape {face}); + shapes.emplace_back(face); } // Act Part::TopoShape topoShape1 {1L}; @@ -652,11 +646,11 @@ TEST_F(TopoShapeExpansionTest, makeElementShellIntersecting) Part::TopoShape topoShape {cube1}; std::vector shapes; for (const auto& face : topoShape.getSubShapes(TopAbs_FACE)) { - shapes.push_back(Part::TopoShape {face}); + shapes.emplace_back(face); } topoShape.setShape(cube2); for (const auto& face : topoShape.getSubShapes(TopAbs_FACE)) { - shapes.push_back(Part::TopoShape {face}); + shapes.emplace_back(face); } // Act Part::TopoShape topoShape1 {1L}; diff --git a/tests/src/Mod/Part/App/TopoShapeExpansionHelpers.cpp b/tests/src/Mod/Part/App/TopoShapeExpansionHelpers.cpp deleted file mode 100644 index ad401ee7b6..0000000000 --- a/tests/src/Mod/Part/App/TopoShapeExpansionHelpers.cpp +++ /dev/null @@ -1,26 +0,0 @@ -// SPDX-License-Identifier: LGPL-2.1-or-later - -#include "TopoShapeExpansionHelpers.h" - -#include - -namespace TopoShapeExpansionHelpers -{ - -std::pair CreateTwoCubes() -{ - auto boxMaker1 = BRepPrimAPI_MakeBox(1.0, 1.0, 1.0); - boxMaker1.Build(); - auto box1 = boxMaker1.Shape(); - - auto boxMaker2 = BRepPrimAPI_MakeBox(1.0, 1.0, 1.0); - boxMaker2.Build(); - auto box2 = boxMaker2.Shape(); - auto transform = gp_Trsf(); - transform.SetTranslation(gp_Pnt(0.0, 0.0, 0.0), gp_Pnt(1.0, 0.0, 0.0)); - box2.Location(TopLoc_Location(transform)); - - return {box1, box2}; -} - -} // namespace TopoShapeExpansionHelpers diff --git a/tests/src/Mod/Part/App/TopoShapeExpansionHelpers.h b/tests/src/Mod/Part/App/TopoShapeExpansionHelpers.h deleted file mode 100644 index d92c18f4d4..0000000000 --- a/tests/src/Mod/Part/App/TopoShapeExpansionHelpers.h +++ /dev/null @@ -1,13 +0,0 @@ -// SPDX-License-Identifier: LGPL-2.1-or-later - -#ifndef FREECAD_TOPOSHAPEEXPANSIONHELPERS_H -#define FREECAD_TOPOSHAPEEXPANSIONHELPERS_H - -#include - -namespace TopoShapeExpansionHelpers -{ -std::pair CreateTwoCubes(); -} - -#endif // FREECAD_TOPOSHAPEEXPANSIONHELPERS_H diff --git a/tests/src/Mod/Part/App/TopoShapeMakeShapeWithElementMap.cpp b/tests/src/Mod/Part/App/TopoShapeMakeShapeWithElementMap.cpp index 7ee0f759ca..d7827ff781 100644 --- a/tests/src/Mod/Part/App/TopoShapeMakeShapeWithElementMap.cpp +++ b/tests/src/Mod/Part/App/TopoShapeMakeShapeWithElementMap.cpp @@ -5,7 +5,7 @@ #include "gtest/gtest.h" #include "src/App/InitApplication.h" -#include "TopoShapeExpansionHelpers.h" +#include "PartTestHelpers.h" #include #include // #include @@ -34,7 +34,6 @@ protected: { _docName = App::GetApplication().getUniqueDocumentName("test"); App::GetApplication().newDocument(_docName.c_str(), "testUser"); - _sids = &_sid; } void TearDown() override @@ -52,10 +51,12 @@ protected: return &_mapper; } + void testFindSourceSubShapesInElementMapForSource(const std::vector& sources, + const TopoShape& source); + private: std::string _docName; Data::ElementIDRefs _sid; - QVector* _sids = nullptr; Part::TopoShape _shape; Part::TopoShape::Mapper _mapper; }; @@ -63,7 +64,7 @@ private: TEST_F(TopoShapeMakeShapeWithElementMapTests, nullShapeThrows) { // Arrange - auto [cube1, cube2] = TopoShapeExpansionHelpers::CreateTwoCubes(); + auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes(); std::vector sources {cube1, cube2}; TopoDS_Vertex nullVertex; TopoDS_Edge nullEdge; @@ -96,11 +97,11 @@ TEST_F(TopoShapeMakeShapeWithElementMapTests, nullShapeThrows) using Data::IndexedName, Data::MappedName; using Part::TopoShape; -std::map elementMap(const TopoShape &shape) +std::map elementMap(const TopoShape& shape) { std::map result {}; auto elements = shape.getElementMap(); - for (auto const & entry : elements) { + for (auto const& entry : elements) { result[entry.index] = entry.name; } return result; @@ -116,7 +117,7 @@ std::map elementMap(const TopoShape &shape) TEST_F(TopoShapeMakeShapeWithElementMapTests, mapCompoundCount) { // Arrange - auto [cube1, cube2] = TopoShapeExpansionHelpers::CreateTwoCubes(); + auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes(); std::vector sources {cube1, cube2}; sources[0].Tag = 1; sources[1].Tag = 2; @@ -127,7 +128,7 @@ TEST_F(TopoShapeMakeShapeWithElementMapTests, mapCompoundCount) compound.makeShapeWithElementMap(compound.getShape(), *Mapper(), sources); auto postElements = elementMap(compound); // Map after mapping // Assert - EXPECT_EQ(preElements.size(), 52); // Check the before map. + EXPECT_EQ(preElements.size(), 52); // Check the before map. EXPECT_EQ(postElements.size(), 52); // 12 Edges, 8 Vertexes, 6 Faces per each Cube EXPECT_EQ(postElements.count(IndexedName("Edge", 24)), 1); EXPECT_EQ(postElements.count(IndexedName("Edge", 25)), 0); @@ -143,7 +144,7 @@ TEST_F(TopoShapeMakeShapeWithElementMapTests, mapCompoundCount) TEST_F(TopoShapeMakeShapeWithElementMapTests, mapCompoundMap) { // Arrange - auto [cube1, cube2] = TopoShapeExpansionHelpers::CreateTwoCubes(); + auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes(); std::vector sources {cube1, cube2}; sources[0].Tag = 1; sources[1].Tag = 2; @@ -165,7 +166,7 @@ TEST_F(TopoShapeMakeShapeWithElementMapTests, mapCompoundMap) TEST_F(TopoShapeMakeShapeWithElementMapTests, emptySourceShapes) { // Arrange - auto [cube1, cube2] = TopoShapeExpansionHelpers::CreateTwoCubes(); + auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes(); std::vector emptySources; std::vector nonEmptySources {cube1, cube2}; @@ -183,7 +184,7 @@ TEST_F(TopoShapeMakeShapeWithElementMapTests, emptySourceShapes) TEST_F(TopoShapeMakeShapeWithElementMapTests, nonMappableSources) { // Arrange - auto [cube1, cube2] = TopoShapeExpansionHelpers::CreateTwoCubes(); + auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes(); std::vector sources {cube1, cube2}; // Act and assert @@ -202,10 +203,34 @@ TEST_F(TopoShapeMakeShapeWithElementMapTests, nonMappableSources) } } +void testFindSourceShapesInSingleShape(const Part::TopoShape& cmpdShape, + const Part::TopoShape& source, + const std::vector& sources, + const TopoShape::Mapper& mapper) +{ + std::vector tmpSources {source}; + for (const auto& subSource : sources) { + Part::TopoShape tmpShape {source.getShape()}; + tmpShape.makeShapeWithElementMap(source.getShape(), mapper, tmpSources); + if (&source == &subSource) { + EXPECT_NE(tmpShape.findShape(subSource.getShape()), + 0); // if tmpShape uses, for example, cube1 and we search for cube1 than + // we should find it + } + else { + EXPECT_EQ(tmpShape.findShape(subSource.getShape()), + 0); // if tmpShape uses, for example, cube1 and we search for cube2 than + // we shouldn't find it + } + } + EXPECT_NE(cmpdShape.findShape(source.getShape()), + 0); // as cmpdShape is made with cube1 and cube2 we should find both of them +} + TEST_F(TopoShapeMakeShapeWithElementMapTests, findSourceShapesInShape) { // Arrange - auto [cube1, cube2] = TopoShapeExpansionHelpers::CreateTwoCubes(); + auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes(); std::vector sources {cube1, cube2}; sources[0].Tag = 1; // setting Tag explicitly otherwise it is likely that this test will be // more or less the same of nonMappableSources @@ -216,30 +241,69 @@ TEST_F(TopoShapeMakeShapeWithElementMapTests, findSourceShapesInShape) // Act and assert for (const auto& source : sources) { - std::vector tmpSources {source}; - for (const auto& subSource : sources) { - Part::TopoShape tmpShape {source.getShape()}; - tmpShape.makeShapeWithElementMap(source.getShape(), *Mapper(), tmpSources); - if (&source == &subSource) { - EXPECT_NE(tmpShape.findShape(subSource.getShape()), - 0); // if tmpShape uses, for example, cube1 and we search for cube1 than - // we should find it - } - else { - EXPECT_EQ(tmpShape.findShape(subSource.getShape()), - 0); // if tmpShape uses, for example, cube1 and we search for cube2 than - // we shouldn't find it - } - } - EXPECT_NE(cmpdShape.findShape(source.getShape()), - 0); // as cmpdShape is made with cube1 and cube2 we should find both of them + testFindSourceShapesInSingleShape(cmpdShape, source, sources, *Mapper()); + } +} + +void testFindSubShapesForSourceWithTypeAndIndex(const std::string& shapeTypeStr, + std::map& elementStdMap, + unsigned long shapeIndex) +{ + std::string shapeIndexStr = std::to_string(shapeIndex); + std::string shapeName {shapeTypeStr + shapeIndexStr}; + + IndexedName indexedName {shapeTypeStr.c_str(), (int)shapeIndex}; + MappedName mappedName {elementStdMap[indexedName]}; + const char shapeTypePrefix {indexedName.toString()[0]}; + + EXPECT_NO_THROW(elementStdMap.at(indexedName)); // We check that the IndexedName + // is one of the keys... + EXPECT_NE(mappedName.find(shapeName.c_str()), + -1); // ... that the element name is in the MappedName... + EXPECT_EQ(mappedName.toString().back(), shapeTypePrefix); +} + +void testFindSubShapesForSourceWithType(const TopoShape& source, + const char* shapeType, + std::map& elementStdMap) +{ + std::string shapeTypeStr {shapeType}; + + // ... and all the elements of the various types in the source TopoShape ... + for (unsigned long shapeIndex = 1U; shapeIndex <= source.countSubElements(shapeType); + shapeIndex++) { + testFindSubShapesForSourceWithTypeAndIndex(shapeTypeStr, elementStdMap, shapeIndex); + } + + // ... we also check that we don't find shapes that don't exist and therefore don't + // have either an IndexedName or a MappedName + IndexedName fakeIndexedName {shapeTypeStr.c_str(), (int)source.countSubElements(shapeType) + 1}; + EXPECT_THROW(elementStdMap.at(fakeIndexedName), std::out_of_range); +} + +void TopoShapeMakeShapeWithElementMapTests::testFindSourceSubShapesInElementMapForSource( + const std::vector& sources, + const TopoShape& source) +{ + TopoShape tmpShape {source.getShape()}; + tmpShape.makeShapeWithElementMap(source.getShape(), *Mapper(), sources); + + // First we create a map with the IndexedNames and MappedNames + std::map elementStdMap; + for (const auto& mappedElement : tmpShape.getElementMap()) { + elementStdMap.emplace(mappedElement.index, mappedElement.name); + } + + // Then for all the elements types (Vertex, Edge, Face) ... + for (const auto& shapeType : source.getElementTypes()) { + testFindSubShapesForSourceWithType(source, shapeType, elementStdMap); } } TEST_F(TopoShapeMakeShapeWithElementMapTests, findSourceSubShapesInElementMap) { // Arrange - auto [cube1, cube2] = TopoShapeExpansionHelpers::CreateTwoCubes(); + auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes(); std::vector sources {cube1, cube2}; sources[0].Tag = 1; // setting Tag explicitly otherwise it is likely that this test will be // more or less the same of nonMappableSources @@ -249,52 +313,14 @@ TEST_F(TopoShapeMakeShapeWithElementMapTests, findSourceSubShapesInElementMap) // Act and assert // Testing with all the source TopoShapes for (const auto& source : sources) { - TopoShape tmpShape {source.getShape()}; - tmpShape.makeShapeWithElementMap(source.getShape(), *Mapper(), sources); - - // First we create a map with the IndexedNames and MappedNames - std::map elementStdMap; - for (const auto& mappedElement : tmpShape.getElementMap()) { - elementStdMap.emplace(mappedElement.index, mappedElement.name); - } - - // Then for all the elements types (Vertex, Edge, Face) ... - for (const auto& shapeType : source.getElementTypes()) { - std::string shapeTypeStr {shapeType}; - - // ... and all the elements of the various types in the source TopoShape ... - for (unsigned long shapeIndex = 1U; shapeIndex <= source.countSubElements(shapeType); - shapeIndex++) { - std::string shapeIndexStr = std::to_string(shapeIndex); - std::string shapeName {shapeTypeStr + shapeIndexStr}; - - IndexedName indexedName {shapeTypeStr.c_str(), (int)shapeIndex}; - MappedName mappedName {elementStdMap[indexedName]}; - const char shapeTypePrefix[1] {indexedName.toString()[0]}; - - EXPECT_NO_THROW(elementStdMap.at(indexedName)); // .. we check that the IndexedName - // is one of the keys... - EXPECT_NE(mappedName.find(shapeName.c_str()), - -1); // ... that the element name is in the MappedName... - EXPECT_EQ( - mappedName.rfind(shapeTypePrefix), - mappedName.toString().length() - - 1); // ... that the element prefix is at the end of the MappedName ... - } - - // ... we also check that we don't find shapes that don't exist and therefor that don't - // have neither an IndexedName nor a MappedName - IndexedName fakeIndexedName {shapeTypeStr.c_str(), - (int)source.countSubElements(shapeType) + 1}; - EXPECT_THROW(elementStdMap.at(fakeIndexedName), std::out_of_range); - } + testFindSourceSubShapesInElementMapForSource(sources, source); } } TEST_F(TopoShapeMakeShapeWithElementMapTests, findMakerOpInElementMap) { // Arrange - auto [cube1, cube2] = TopoShapeExpansionHelpers::CreateTwoCubes(); + auto [cube1, cube2] = PartTestHelpers::CreateTwoCubes(); std::vector sources {cube1, cube2}; sources[0].Tag = 1; // setting Tag explicitly otherwise it is likely that this test will be // more or less the same of nonMappableSources