From ab3bb9fb71ecf6daba2993ce6e9e06280ec00f86 Mon Sep 17 00:00:00 2001 From: bgbsww Date: Fri, 19 Jan 2024 10:49:47 -0500 Subject: [PATCH] Improve tests --- src/Mod/Part/App/TopoShapeExpansion.cpp | 2 + tests/src/Mod/Part/App/TopoShapeExpansion.cpp | 181 +++++++++--------- 2 files changed, 94 insertions(+), 89 deletions(-) diff --git a/src/Mod/Part/App/TopoShapeExpansion.cpp b/src/Mod/Part/App/TopoShapeExpansion.cpp index d5b5b5227b..17a33b4edd 100644 --- a/src/Mod/Part/App/TopoShapeExpansion.cpp +++ b/src/Mod/Part/App/TopoShapeExpansion.cpp @@ -376,6 +376,8 @@ void TopoShape::mapSubElementTypeForShape(const TopoShape& other, } std::ostringstream ss; char elementType {shapeName(type)[0]}; + if ( ! elementMap() ) + FC_THROWM(NullShapeException, "No element map"); elementMap()->encodeElementName(elementType, name, ss, &sids, Tag, op, other.Tag); elementMap()->setElementName(element, name, Tag, &sids); } diff --git a/tests/src/Mod/Part/App/TopoShapeExpansion.cpp b/tests/src/Mod/Part/App/TopoShapeExpansion.cpp index 3d71be8070..d58efa4492 100644 --- a/tests/src/Mod/Part/App/TopoShapeExpansion.cpp +++ b/tests/src/Mod/Part/App/TopoShapeExpansion.cpp @@ -162,125 +162,128 @@ TEST_F(TopoShapeExpansionTest, makeElementCompoundTwoCubes) } -TEST_F(TopoShapeExpansionTest, mapSubElementOneCube) +TEST_F(TopoShapeExpansionTest, mapSubElementNames) { // Arrange auto [cube1, cube2] = CreateTwoCubes(); - auto [cube3, cube4] = CreateTwoCubes(); Part::TopoShape cube1TS {cube1}; Part::TopoShape cube2TS {cube2}; cube1TS.Tag = 1; cube2TS.Tag = 2; + Part::TopoShape topoShape, topoShape1; + + // Act std::vector subShapes = cube1TS.getSubTopoShapes(TopAbs_FACE); Part::TopoShape face1 = subShapes.front(); face1.Tag = 5; - Part::TopoShape topoShape; - // Act - int fs1 = topoShape.findShape(cube1TS.getShape()); - topoShape.setShape(cube1TS); - // cube1TS.mapSubElement(face1); // This throws an exception. Is that right? - // The cache ancestry only works on TopAbs_SHAPE so this is likely an invalid call, - // but do we defend against it or expect the exception? + int fs1 = topoShape1.findShape(cube1); + topoShape.setShape(cube2TS); + topoShape1.makeElementCompound({cube1TS, cube2TS}); + int fs2 = topoShape1.findShape(cube1); - topoShape.mapSubElement(cube1TS); - int fs2 = topoShape.findShape(cube1TS.getShape()); - int fs3 = topoShape.findShape(face1.getShape()); - int size0 = cube1TS.getElementMap().size(); - int size1 = topoShape.getElementMap().size(); + TopoDS_Shape tds1 = topoShape.findShape("SubShape1"); + TopoDS_Shape tds2 = topoShape.findShape("SubShape2"); // Nonexistent + TopoDS_Shape tds3 = topoShape1.findShape("SubShape1"); + TopoDS_Shape tds4 = topoShape1.findShape("SubShape2"); + TopoDS_Shape tds5 = topoShape1.findShape("NonExistentName"); // Invalid Name // Assert + EXPECT_THROW(cube1TS.mapSubElement(face1), Part::NullShapeException); // No subshapes EXPECT_EQ(fs1, 0); EXPECT_EQ(fs2, 1); - EXPECT_EQ(fs3, 1); - EXPECT_EQ(0, size0); - EXPECT_EQ(26, size1); + EXPECT_FALSE(tds1.IsNull()); + EXPECT_TRUE(tds2.IsNull()); + EXPECT_FALSE(tds3.IsNull()); + EXPECT_FALSE(tds4.IsNull()); + EXPECT_TRUE(tds5.IsNull()); } -TEST_F(TopoShapeExpansionTest, mapSubElementSetReset) +TEST_F(TopoShapeExpansionTest, mapSubElementCacheType) { // Arrange auto [cube1, cube2] = CreateTwoCubes(); - auto [cube3, cube4] = CreateTwoCubes(); Part::TopoShape cube1TS {cube1}; Part::TopoShape cube2TS {cube2}; - Part::TopoShape cube3TS {cube3}; - Part::TopoShape cube4TS {cube4}; cube1TS.Tag = 1; cube2TS.Tag = 2; - cube3TS.Tag = 3; - cube4TS.Tag = 4; - std::vector subShapes = cube1TS.getSubTopoShapes(TopAbs_FACE); - Part::TopoShape face1 = subShapes.front(); - face1.Tag = 5; - Part::TopoShape topoShape, topoShape1; - - // Act - topoShape.setShape(TopoDS_Shape()); - int fs4 = topoShape.findShape(cube1TS.getShape()); - topoShape.setShape(cube1, false); - // topoShape.mapSubElement(cube1TS); - // No mapSubElement required, it keeps finding it now - int fs5 = topoShape.findShape(cube1TS.getShape()); - topoShape.setShape(cube1, true); - int fs6 = topoShape.findShape(cube1TS.getShape()); - // Assert - EXPECT_EQ(fs4, 0); - EXPECT_EQ(fs5, 1); - EXPECT_EQ(fs6, 1); -} - -TEST_F(TopoShapeExpansionTest, mapSubElementCompoundCubes) -{ - // Arrange - auto [cube1, cube2] = CreateTwoCubes(); - auto [cube3, cube4] = CreateTwoCubes(); - Part::TopoShape cube1TS {cube1}; - Part::TopoShape cube2TS {cube2}; - Part::TopoShape cube3TS {cube3}; - Part::TopoShape cube4TS {cube4}; - cube1TS.Tag = 1; - cube2TS.Tag = 2; - cube3TS.Tag = 3; - cube4TS.Tag = 4; - std::vector subShapes = cube1TS.getSubTopoShapes(TopAbs_FACE); - Part::TopoShape face1 = subShapes.front(); - face1.Tag = 5; - Part::TopoShape topoShape, topoShape1; - - // Act - Part::TopoShape topoShape2, topoShape3; - topoShape2.setShape(cube2TS); - topoShape2.mapSubElement(cube2TS, nullptr, true); - int fs7 = topoShape2.findShape(cube2TS.getShape()); - int fs8 = topoShape2.findShape(face1.getShape()); - - topoShape3.setShape(cube3TS); - topoShape3.mapSubElement(cube3TS, "Sample", true); - int fs9 = topoShape3.findShape(cube3TS.getShape()); - int fs10 = topoShape3.findShape(face1.getShape()); - + Part::TopoShape topoShape; topoShape.makeElementCompound({cube1TS, cube2TS}); - int fs11 = topoShape.findShape(cube2TS.getShape()); - int size2 = topoShape.getElementMap().size(); - // Assert - EXPECT_EQ(fs7, 1); - EXPECT_EQ(fs8, 0); - EXPECT_EQ(fs9, 1); - EXPECT_EQ(fs10, 0); - EXPECT_EQ(fs11, 2); - EXPECT_EQ(52, size2); + topoShape.mapSubElement(cube2TS, "Name", false); + + // Act, Assert + for (int i = 1; i <= 12; i++) { + TopoDS_Shape dshape1 = topoShape.findShape(TopAbs_FACE, i); + EXPECT_FALSE(dshape1.IsNull()) << "Face num " << i; + } + TopoDS_Shape dshape1 = topoShape.findShape(TopAbs_FACE, 13); + EXPECT_TRUE(dshape1.IsNull()); +} + + +TEST_F(TopoShapeExpansionTest, mapSubElementCacheAncestor) +{ + // Arrange + auto [cube1, cube2] = CreateTwoCubes(); + Part::TopoShape cube1TS {cube1}; + Part::TopoShape cube2TS {cube2}; + cube1TS.Tag = 1; + cube2TS.Tag = 2; + Part::TopoShape topoShape; + topoShape.makeElementCompound({cube1TS, cube2TS}); + topoShape.mapSubElement(cube2TS, "Name", false); // Act - topoShape2.makeElementCompound({cube3TS, cube4TS}); - topoShape2.mapSubElement(cube3TS, nullptr, true); - topoShape3.makeElementCompound({topoShape, topoShape2}); - int fs12 = topoShape3.findShape(cube4TS.getShape()); - int size4 = topoShape3.getElementMap().size(); + int fa1 = topoShape.findAncestor(cube2, TopAbs_COMPOUND); + TopoDS_Shape tds1 = topoShape.findAncestorShape(cube1, TopAbs_COMPOUND); + // Assert - EXPECT_EQ(104, size4); - EXPECT_EQ(fs12, 4); + EXPECT_EQ(fa1, 1); + EXPECT_TRUE(tds1.IsEqual(topoShape.getShape())); } +TEST_F(TopoShapeExpansionTest, mapSubElementCacheAncestors) +{ + // Arrange + auto [cube1, cube2] = CreateTwoCubes(); + auto [cube3, cube4] = CreateTwoCubes(); + auto tr {gp_Trsf()}; + tr.SetTranslation(gp_Vec(gp_XYZ(0, 1, 0))); + cube3.Move(TopLoc_Location(tr)); + cube4.Move(TopLoc_Location(tr)); + Part::TopoShape cube1TS {cube1}; + Part::TopoShape cube2TS {cube2}; + Part::TopoShape cube3TS {cube3}; + Part::TopoShape cube4TS {cube4}; + cube1TS.Tag = 1; + cube2TS.Tag = 2; + cube3TS.Tag = 3; + cube4TS.Tag = 4; + Part::TopoShape topoShape, topoShape1, topoShape2; + topoShape.makeElementCompound({cube1TS, cube2TS}); + topoShape.mapSubElement(cube2TS, nullptr, false); + topoShape1.makeElementCompound({cube3TS, cube4TS}); + topoShape1.mapSubElement(cube3TS, nullptr, true); + topoShape2.makeElementCompound({topoShape, topoShape1}); + topoShape2.mapSubElement(cube2TS, nullptr, false); + + // Act + auto ancestorList = topoShape2.findAncestors(cube2, TopAbs_COMPOUND); + auto ancestorShapeList = topoShape2.findAncestorsShapes(cube2, TopAbs_COMPOUND); + + // Assert + EXPECT_EQ(ancestorList.size(), 1); + EXPECT_EQ(ancestorList.front(), 1); + EXPECT_EQ(ancestorShapeList.size(), 1); + // EXPECT_TRUE(ancestorShapeList.front().IsEqual(topoShape.getShape())); + // EXPECT_TRUE(ancestorShapeList.back().IsEqual(topoShape1.getShape())); +} + +// void initCache(int reset = 0) const; // Can't see any path to visibility to test if this worked. +// std::vector findAncestors(const TopoDS_Shape& subshape, TopAbs_ShapeEnum type) const; // +// DONE std::vector findAncestorsShapes(const TopoDS_Shape& subshape, +// TopAbs_ShapeEnum type) const; // UNSURE + + // NOLINTEND(readability-magic-numbers,cppcoreguidelines-avoid-magic-numbers)