Skip to content

Commit

Permalink
FIX: throw on wrong indices in removeColumns() / removeRows()
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Oct 6, 2023
1 parent 6aa9fd5 commit ce2df7d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/source/doxygen-docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Version 2.10.3: UNRELEASED
- BUG FIXES:
- Fix python wrapper FTBFS in armhf and other architectures.
- Fix matrices removeColumns() and removeRows() won't throw if user specified a non-existing index.

# Version 2.10.2: Released Oct 5th, 2023
- Build system:
Expand Down
8 changes: 8 additions & 0 deletions libs/math/src/MatrixBase_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ void MatrixBase<Scalar, Derived>::removeColumns(
std::sort(idxs.begin(), idxs.end());
auto itEnd = std::unique(idxs.begin(), idxs.end());
idxs.resize(itEnd - idxs.begin());
for (const auto idx : idxs)
{
ASSERT_LT_(idx, mbDerived().cols());
}
unsafeRemoveColumns(idxs);
}

Expand Down Expand Up @@ -70,6 +74,10 @@ void MatrixBase<Scalar, Derived>::removeRows(
std::sort(idxs.begin(), idxs.end());
auto itEnd = std::unique(idxs.begin(), idxs.end());
idxs.resize(itEnd - idxs.begin());
for (const auto idx : idxs)
{
ASSERT_LT_(idx, mbDerived().rows());
}
unsafeRemoveRows(idxs);
}

Expand Down
36 changes: 36 additions & 0 deletions libs/math/src/matrix_ops_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,39 @@ TEST(Matrices, extractSubmatrixSymmetrical)
EXPECT_TRUE(E_expected == E);
}
}

TEST(Matrices, removeColumns)
{
for (size_t i = 0; i < 6; i++)
{
auto M = mrpt::math::CMatrixDouble::Identity(6);
EXPECT_EQ(M.cols(), 6);
M.removeColumns({i});
EXPECT_EQ(M.cols(), 5) << "For {i}=" << i;
}

{
auto M = mrpt::math::CMatrixDouble(); // empty
EXPECT_EQ(M.cols(), 0);
EXPECT_ANY_THROW(M.removeColumns({0}));
EXPECT_ANY_THROW(M.removeColumns({1}));
}
}

TEST(Matrices, removeRows)
{
for (size_t i = 0; i < 6; i++)
{
auto M = mrpt::math::CMatrixDouble::Identity(6);
EXPECT_EQ(M.rows(), 6);
M.removeRows({i});
EXPECT_EQ(M.rows(), 5) << "For {i}=" << i;
}

{
auto M = mrpt::math::CMatrixDouble(); // empty
EXPECT_EQ(M.rows(), 0);
EXPECT_ANY_THROW(M.removeRows({0}));
EXPECT_ANY_THROW(M.removeRows({1}));
}
}

0 comments on commit ce2df7d

Please sign in to comment.