Skip to content

Commit

Permalink
GRIDEDIT-759 Added start and end index bounds check and unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
BillSenior committed Jun 19, 2024
1 parent cc5a76b commit 0d6ca13
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
15 changes: 15 additions & 0 deletions libs/MeshKernel/src/Polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,21 @@ std::vector<meshkernel::Point> meshkernel::Polygon::Refine(const size_t startInd

std::vector<meshkernel::Point> meshkernel::Polygon::LinearRefine(const size_t startIndex, const size_t endIndex) const
{

if (startIndex >= m_nodes.size())
{
throw ConstraintError("The start index is greater than the number of points in the outer polygon: {} >= {}.",
startIndex,
m_nodes.size());
}

if (endIndex >= m_nodes.size())
{
throw ConstraintError("The end index is greater than the number of points in the outer polygon: {} >= {}.",
startIndex,
m_nodes.size());
}

if (startIndex == endIndex)
{
return m_nodes;
Expand Down
53 changes: 53 additions & 0 deletions libs/MeshKernel/tests/src/PolygonsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,36 @@ TEST(Polygons, BasicLinearRefinePolygon)
}
}

TEST(Polygons, LinearRefinePolygonSameStartEnd)
{
// Prepare
std::vector<meshkernel::Point> nodes;

nodes.push_back({0.0, 10.0});
nodes.push_back({1.0, 10.0});
nodes.push_back({3.0, 10.0});
nodes.push_back({11.0, 10.0});
nodes.push_back({11.0, 0.0});
nodes.push_back({0.0, 0.0});
nodes.push_back({0.0, 10.0});

std::vector<meshkernel::Point> expected(nodes);

meshkernel::Polygons polygons(nodes, meshkernel::Projection::cartesian);

// Execute
const auto refinedPolygon = polygons.LinearRefinePolygon(0, 1, 1);

ASSERT_EQ(expected.size(), refinedPolygon.size());
constexpr double tolerance = 1.0e-8;

for (size_t i = 0; i < refinedPolygon.size(); ++i)
{
EXPECT_NEAR(expected[i].x, refinedPolygon[i].x, tolerance);
EXPECT_NEAR(expected[i].y, refinedPolygon[i].y, tolerance);
}
}

TEST(Polygons, LinearRefinePolygonSameNodes)
{
// Prepare
Expand Down Expand Up @@ -1061,3 +1091,26 @@ TEST(Polygons, LinearRefinePolygonStartGtEnd)
EXPECT_NEAR(expected[i].y, refinedPolygon[i].y, tolerance);
}
}

TEST(Polygons, LinearRefinePolygonOutOfBoundsCheck)
{
// Prepare
std::vector<meshkernel::Point> nodes;

nodes.push_back({0.0, 10.0});
nodes.push_back({1.0, 10.0});
nodes.push_back({3.0, 10.0});
nodes.push_back({11.0, 10.0});
nodes.push_back({11.0, 0.0});
nodes.push_back({0.0, 0.0});
nodes.push_back({0.0, 10.0});

meshkernel::Polygons polygons(nodes, meshkernel::Projection::cartesian);

// polygon index incorrect
EXPECT_THROW(auto refinedPolygon = polygons.LinearRefinePolygon(1, 1, 4), meshkernel::ConstraintError);
// polygon start node index incorrect
EXPECT_THROW(auto refinedPolygon = polygons.LinearRefinePolygon(0, 8, 1), meshkernel::ConstraintError);
// polygon end node index incorrect
EXPECT_THROW(auto refinedPolygon = polygons.LinearRefinePolygon(0, 1, 9), meshkernel::ConstraintError);
}

0 comments on commit 0d6ca13

Please sign in to comment.