Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions include/shape/trapezoid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ class GeneralizedTrapezoid
left_side_decreasing_not_vertical_ = false;
left_side_increasing_not_vertical_ = false;
a_left_ = 0;
// The widths and the area change with the left side, as computed in
// the constructor.
width_top_ = x_top_right() - x_top_left();
width_bottom_ = x_bottom_right() - x_bottom_left();
area_ = (width_top() + width_bottom()) * height() / 2.0;
}

void set_top_covered(bool top_covered) { top_covered_ = top_covered; }
Expand Down
72 changes: 72 additions & 0 deletions test/trapezoid_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,75 @@ INSTANTIATE_TEST_SUITE_P(
[](const testing::TestParamInfo<GeneralizedTrapezoidComputeTopRightShiftTest::ParamType>& info) {
return std::to_string(info.index);
});


struct GeneralizedTrapezoidExtendLeftTestParams
{
std::string name;
GeneralizedTrapezoid trapezoid;
LengthDbl l;
// Expected trapezoid, built directly with its left side at x = l.
GeneralizedTrapezoid expected_trapezoid;
};

void PrintTo(const GeneralizedTrapezoidExtendLeftTestParams& params, std::ostream* os)
{
*os << "trapezoid " << params.trapezoid << "\n";
*os << "l " << params.l << "\n";
*os << "expected_trapezoid " << params.expected_trapezoid << "\n";
}

class GeneralizedTrapezoidExtendLeftTest: public testing::TestWithParam<GeneralizedTrapezoidExtendLeftTestParams> { };

TEST_P(GeneralizedTrapezoidExtendLeftTest, GeneralizedTrapezoidExtendLeft)
{
GeneralizedTrapezoidExtendLeftTestParams test_params = GetParam();
PrintTo(test_params, &std::cout);
GeneralizedTrapezoid trapezoid = test_params.trapezoid;
trapezoid.extend_left(test_params.l);
std::cout << "trapezoid " << trapezoid << std::endl;

// The extended trapezoid is the same as the one built directly, including
// its computed attributes.
const GeneralizedTrapezoid& expected = test_params.expected_trapezoid;
EXPECT_EQ(trapezoid, expected);
EXPECT_TRUE(equal(trapezoid.width_bottom(), expected.width_bottom()));
EXPECT_TRUE(equal(trapezoid.width_top(), expected.width_top()));
EXPECT_TRUE(equal(trapezoid.height(), expected.height()));
EXPECT_TRUE(equal(trapezoid.area(), expected.area()));
EXPECT_TRUE(equal(trapezoid.x_min(), expected.x_min()));
EXPECT_TRUE(equal(trapezoid.x_max(), expected.x_max()));
EXPECT_TRUE(equal(trapezoid.a_left(), expected.a_left()));
EXPECT_EQ(trapezoid.left_side_increasing_not_vertical(), expected.left_side_increasing_not_vertical());
EXPECT_EQ(trapezoid.left_side_decreasing_not_vertical(), expected.left_side_decreasing_not_vertical());
}

INSTANTIATE_TEST_SUITE_P(
Shape,
GeneralizedTrapezoidExtendLeftTest,
testing::ValuesIn(std::vector<GeneralizedTrapezoidExtendLeftTestParams>{
{
"LeftSideGoingRight",
GeneralizedTrapezoid(0, 2, 2, 6, 3, 5),
0,
GeneralizedTrapezoid(0, 2, 0, 6, 0, 5),
}, {
"LeftSideGoingLeft",
GeneralizedTrapezoid(0, 2, 3, 6, 1, 5),
-1,
GeneralizedTrapezoid(0, 2, -1, 6, -1, 5),
}, {
"VerticalLeftSide",
GeneralizedTrapezoid(1, 3, 2, 4, 2, 4),
0,
GeneralizedTrapezoid(1, 3, 0, 4, 0, 4),
}, {
"Triangle",
GeneralizedTrapezoid(0, 2, 2, 6, 4, 4),
1,
GeneralizedTrapezoid(0, 2, 1, 6, 1, 4),
},
}),
[](const testing::TestParamInfo<GeneralizedTrapezoidExtendLeftTest::ParamType>& info) {
return info.param.name;
});
Loading