Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1662 Change erase return type from iterator to bool
Browse files Browse the repository at this point in the history
Signed-off-by: Marika Lehmann <[email protected]>
  • Loading branch information
FerdinandSpitzschnueffler committed Sep 23, 2022
1 parent eef896a commit e8bbe93
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 45 deletions.
4 changes: 2 additions & 2 deletions iceoryx_hoofs/include/iceoryx_hoofs/cxx/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ class vector
/// the middle of the vector every element is moved one place to the
/// left to ensure that the elements are stored contiguously
/// @param[in] position at which the element shall be removed
/// @return iterator following the removed element if begin() <= position < end(), otherwise nullptr
iterator erase(iterator position) noexcept;
/// @return true if the element was removed, i.e. begin() <= position < end(), otherwise false
bool erase(iterator position) noexcept;

private:
T& at_unchecked(const uint64_t index) noexcept;
Expand Down
6 changes: 3 additions & 3 deletions iceoryx_hoofs/include/iceoryx_hoofs/internal/cxx/vector.inl
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ inline typename vector<T, Capacity>::const_iterator vector<T, Capacity>::end() c
}

template <typename T, uint64_t Capacity>
inline typename vector<T, Capacity>::iterator vector<T, Capacity>::erase(iterator position) noexcept
inline bool vector<T, Capacity>::erase(iterator position) noexcept
{
if (begin() <= position && position < end())
{
Expand All @@ -359,9 +359,9 @@ inline typename vector<T, Capacity>::iterator vector<T, Capacity>::erase(iterato
}
at(n).~T();
m_size--;
return &at_unchecked(index);
return true;
}
return nullptr;
return false;
}

template <typename T, uint64_t Capacity>
Expand Down
51 changes: 11 additions & 40 deletions iceoryx_hoofs/test/moduletests/test_cxx_vector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -907,40 +907,11 @@ TEST_F(vector_test, IterateUsingConstSquareBracket)
}
}

TEST_F(vector_test, EraseReturnsNullWhenElementIsInvalid)
TEST_F(vector_test, EraseFailsWhenElementIsInvalid)
{
::testing::Test::RecordProperty("TEST_ID", "ff7c1c4a-4ef5-4905-a107-6f1d27462d47");
auto* i = sut.begin() + 5U;
EXPECT_THAT(sut.erase(i), Eq(nullptr));
}

TEST_F(vector_test, EraseReturnsCorrectIteratorWhenElementIsValid)
{
::testing::Test::RecordProperty("TEST_ID", "4ebc10a8-8cb3-4151-aa70-824d4c0b5597");
sut.emplace_back(1U);
sut.emplace_back(2U);
sut.emplace_back(3U);
sut.emplace_back(4U);

auto* iter = sut.erase(sut.begin());
ASSERT_THAT(sut.size(), Eq(3));
ASSERT_NE(iter, nullptr);
EXPECT_THAT(iter, Eq(sut.begin()));

iter = sut.erase(sut.end() - 1);
ASSERT_THAT(sut.size(), Eq(2));
ASSERT_NE(iter, nullptr);
EXPECT_THAT(iter, Eq(sut.end()));

iter = sut.erase(sut.begin() + 1);
ASSERT_THAT(sut.size(), Eq(1));
ASSERT_NE(iter, nullptr);
EXPECT_THAT(iter, Eq(sut.end()));

iter = sut.erase(sut.begin());
ASSERT_THAT(sut.size(), Eq(0));
ASSERT_NE(iter, nullptr);
EXPECT_THAT(iter, Eq(sut.end()));
EXPECT_FALSE(sut.erase(i));
}

TEST_F(vector_test, ErasingElementDecreasesSize)
Expand All @@ -949,8 +920,8 @@ TEST_F(vector_test, ErasingElementDecreasesSize)
sut.emplace_back(3U);
sut.emplace_back(4U);
sut.emplace_back(5U);
sut.erase(sut.begin() + 2U);
sut.erase(sut.begin());
EXPECT_TRUE(sut.erase(sut.begin() + 2U));
EXPECT_TRUE(sut.erase(sut.begin()));
EXPECT_THAT(sut.size(), Eq(1U));
}

Expand All @@ -962,7 +933,7 @@ TEST_F(vector_test, EraseOfLastElementCallsDTorOnly)
sut1.emplace_back(8U);
sut1.emplace_back(9U);

sut1.erase(sut1.begin() + 2U);
EXPECT_TRUE(sut1.erase(sut1.begin() + 2U));

EXPECT_THAT(dTor, Eq(1U));
EXPECT_THAT(classValue, Eq(9U));
Expand All @@ -978,7 +949,7 @@ TEST_F(vector_test, EraseOfMiddleElementCallsDTorAndMove)
sut1.emplace_back(10U);
sut1.emplace_back(11U);

sut1.erase(sut1.begin() + 2U);
EXPECT_TRUE(sut1.erase(sut1.begin() + 2U));

EXPECT_THAT(dTor, Eq(1U));
EXPECT_THAT(moveAssignment, Eq(2U));
Expand All @@ -994,7 +965,7 @@ TEST_F(vector_test, EraseOfFrontElementCallsDTorAndMove)
sut1.emplace_back(10U);
sut1.emplace_back(11U);

sut1.erase(sut1.begin());
EXPECT_TRUE(sut1.erase(sut1.begin()));

EXPECT_THAT(dTor, Eq(1U));
EXPECT_THAT(moveAssignment, Eq(4U));
Expand All @@ -1008,7 +979,7 @@ TEST_F(vector_test, EraseMiddleElementDataCorrectAfterwards)
sut.emplace_back(98U);
sut.emplace_back(99U);

sut.erase(sut.begin() + 1U);
EXPECT_TRUE(sut.erase(sut.begin() + 1U));

for (uint64_t k = 0U; k < sut.size(); ++k)
{
Expand All @@ -1024,7 +995,7 @@ TEST_F(vector_test, EraseFrontElementDataCorrectAfterwards)
sut.emplace_back(598U);
sut.emplace_back(599U);

sut.erase(sut.begin());
EXPECT_TRUE(sut.erase(sut.begin()));

for (uint64_t k = 0U; k < sut.size(); ++k)
{
Expand All @@ -1042,7 +1013,7 @@ TEST_F(vector_test, EraseLastElementDataCorrectAfterwards)
sut.emplace_back(7601U);
sut.emplace_back(76101U);

sut.erase(sut.begin() + 5U);
EXPECT_TRUE(sut.erase(sut.begin() + 5U));

for (uint64_t k = 0U; k < sut.size(); ++k)
{
Expand All @@ -1058,7 +1029,7 @@ TEST_F(vector_test, EraseLastElementOfFullVectorDataCorrectAfterwards)
sut.emplace_back(i * 123U);
}

sut.erase(sut.begin() + sut.size() - 1U);
EXPECT_TRUE(sut.erase(sut.begin() + sut.size() - 1U));

for (uint64_t k = 0; k < sut.size(); ++k)
{
Expand Down

0 comments on commit e8bbe93

Please sign in to comment.