File size: 5,814 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | // SPDX-License-Identifier: LGPL-2.1-or-later
#include <gtest/gtest.h>
#include "Mod/Part/App/FeaturePartCommon.h"
#include <src/App/InitApplication.h>
#include "PartTestHelpers.h"
class FeaturePartCommonTest: public ::testing::Test, public PartTestHelpers::PartTestHelperClass
{
protected:
static void SetUpTestSuite()
{
tests::initApplication();
}
void SetUp() override
{
createTestDoc();
_common = _doc->addObject<Part::Common>();
}
void TearDown() override
{}
Part::Common* _common = nullptr; // NOLINT Can't be private in a test framework
};
TEST_F(FeaturePartCommonTest, testIntersecting)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[1]);
// Act
_common->execute();
Part::TopoShape ts = _common->Shape.getValue();
double volume = PartTestHelpers::getVolume(ts.getShape());
Base::BoundBox3d bb = ts.getBoundBox();
// Assert
EXPECT_DOUBLE_EQ(volume, 3.0);
// double check using bounds:
EXPECT_DOUBLE_EQ(bb.MinX, 0.0);
EXPECT_DOUBLE_EQ(bb.MinY, 1.0);
EXPECT_DOUBLE_EQ(bb.MinZ, 0.0);
EXPECT_DOUBLE_EQ(bb.MaxX, 1.0);
EXPECT_DOUBLE_EQ(bb.MaxY, 2.0);
EXPECT_DOUBLE_EQ(bb.MaxZ, 3.0);
}
TEST_F(FeaturePartCommonTest, testNonIntersecting)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[2]);
// Act
_common->execute();
Part::TopoShape ts = _common->Shape.getValue();
double volume = PartTestHelpers::getVolume(ts.getShape());
Base::BoundBox3d bb = ts.getBoundBox();
// Assert
EXPECT_FALSE(bb.IsValid());
EXPECT_DOUBLE_EQ(volume, 0.0);
}
TEST_F(FeaturePartCommonTest, testTouching)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[3]);
// Act
_common->execute();
Part::TopoShape ts = _common->Shape.getValue();
double volume = PartTestHelpers::getVolume(ts.getShape());
Base::BoundBox3d bb = ts.getBoundBox();
// Assert
EXPECT_FALSE(bb.IsValid());
EXPECT_DOUBLE_EQ(volume, 0.0);
}
TEST_F(FeaturePartCommonTest, testAlmostTouching)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[4]);
// Act
_common->execute();
Part::TopoShape ts = _common->Shape.getValue();
double volume = PartTestHelpers::getVolume(ts.getShape());
Base::BoundBox3d bb = ts.getBoundBox();
// Assert
EXPECT_FALSE(bb.IsValid());
EXPECT_DOUBLE_EQ(volume, 0.0);
}
TEST_F(FeaturePartCommonTest, testBarelyIntersecting)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[5]); // NOLINT magic number
// Act
_common->execute();
Part::TopoShape ts = _common->Shape.getValue();
double volume = PartTestHelpers::getVolume(ts.getShape());
double target = PartTestHelpers::minimalDistance * 3; // 3 dimensions in a Volume
Base::BoundBox3d bb = ts.getBoundBox();
// Assert
// Using FLOAT, not DOUBLE here so test library comparison is of reasonable precision 1e07
// rather than 1e15 See
// https://google.github.io/googletest/reference/assertions.html#floating-point
EXPECT_FLOAT_EQ(volume, target); // Should be approximately 0.00029999999999996696
// double check using bounds:
EXPECT_DOUBLE_EQ(bb.MinX, 0.0);
EXPECT_DOUBLE_EQ(bb.MinY, 2.0 - PartTestHelpers::minimalDistance);
EXPECT_DOUBLE_EQ(bb.MinZ, 0.0);
EXPECT_DOUBLE_EQ(bb.MaxX, 1.0);
EXPECT_DOUBLE_EQ(bb.MaxY, 2.0);
EXPECT_DOUBLE_EQ(bb.MaxZ, 3.0);
}
TEST_F(FeaturePartCommonTest, testMustExecute)
{
// Assert initially we don't need to execute
EXPECT_FALSE(_common->mustExecute());
// Act to change one property
_common->Base.setValue(_boxes[0]);
// Assert we still can't execute
EXPECT_FALSE(_common->mustExecute());
// Act to complete the properties we need
_common->Tool.setValue(_boxes[1]);
// Assert that we now must execute
EXPECT_TRUE(_common->mustExecute());
// Act to execute
_doc->recompute();
// Assert we don't need to execute anymore
EXPECT_FALSE(_common->mustExecute());
}
TEST_F(FeaturePartCommonTest, testGetProviderName)
{
// Act
_common->execute();
const char* name = _common->getViewProviderName();
// Assert
EXPECT_STREQ(name, "PartGui::ViewProviderBoolean");
}
TEST_F(FeaturePartCommonTest, testHistory)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[1]);
// Manually create the histories classically generated by FreeCAD for comparison
using MapList = std::map<int, std::vector<int>>;
using List = std::vector<int>;
MapList compare1 = {
{0, List {0}},
{1, List {5}}, // NOLINT magic number
{2, List()},
{3, List {2}},
{4, List {3}},
{5, List {1}}
}; // NOLINT magic number
MapList compare2 = {
{0, List {0}},
{1, List {5}}, // NOLINT magic number
{2, List {4}},
{3, List()},
{4, List {3}},
{5, List {1}}
}; // NOLINT magic number
// Act and Assert no histories yet
std::vector<Part::ShapeHistory> hist = _common->History.getValues();
EXPECT_EQ(hist.size(), 0);
// Act to generate histories
_common->execute();
hist = _common->History.getValues();
// Assert
ASSERT_EQ(
hist.size(),
0
); // TODO: with TNP enabled, this becomes 0, matches the code. Correct?
}
TEST_F(FeaturePartCommonTest, testMapping)
{
// Arrange
_common->Base.setValue(_boxes[0]);
_common->Tool.setValue(_boxes[1]);
// Act
_common->execute();
const Part::TopoShape& ts1 = _common->Shape.getShape();
// Assert
EXPECT_EQ(ts1.getElementMap().size(), 26);
}
|