Skip to content

Commit

Permalink
Fixed: shrink_to_fit lost elements when relocation was non-trivial
Browse files Browse the repository at this point in the history
Caused by wrong return value from _detail::Relocate
  • Loading branch information
OleErikPeistorpet committed Sep 14, 2023
1 parent 9797b18 commit 1a886ad
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
2 changes: 1 addition & 1 deletion auxi/impl_algo.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ namespace _detail
::new(static_cast<void *>(dest + i)) T( std::move(src[i]) );
src[i].~T();
}
return dest;
return dest + n;
}
#undef OEL_CHECK_NULL_MEMCPY

Expand Down
38 changes: 32 additions & 6 deletions unit_test/dynarray_mutate_gtest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -682,15 +682,19 @@ void testEraseOne()
EXPECT_EQ(1, static_cast<double>(d.front()));
}

TEST_F(dynarrayTest, eraseSingle)
TEST_F(dynarrayTest, eraseSingleInt)
{
testEraseOne<int>();
}

TrivialRelocat::clearCount();
TEST_F(dynarrayTest, eraseSingleTrivialReloc)
{
testEraseOne<TrivialRelocat>();
EXPECT_EQ(TrivialRelocat::nConstructions, TrivialRelocat::nDestruct);
}

MoveOnly::clearCount();
TEST_F(dynarrayTest, eraseSingle)
{
testEraseOne<MoveOnly>();
EXPECT_EQ(MoveOnly::nConstructions, MoveOnly::nDestruct);
}
Expand All @@ -712,15 +716,19 @@ void testErase()
EXPECT_EQ(s, static_cast<double>(d.back()));
}

TEST_F(dynarrayTest, eraseRange)
TEST_F(dynarrayTest, eraseRangeInt)
{
testErase<int>();
}

TrivialRelocat::clearCount();
TEST_F(dynarrayTest, eraseRangeTrivialReloc)
{
testErase<TrivialRelocat>();
EXPECT_EQ(TrivialRelocat::nConstructions, TrivialRelocat::nDestruct);
}

MoveOnly::clearCount();
TEST_F(dynarrayTest, eraseRange)
{
testErase<MoveOnly>();
EXPECT_EQ(MoveOnly::nConstructions, MoveOnly::nDestruct);
}
Expand Down Expand Up @@ -777,9 +785,27 @@ void testEraseUnstable()
TEST_F(dynarrayTest, eraseUnstable)
{
testEraseUnstable<MoveOnly>();
}

TEST_F(dynarrayTest, eraseUnstableTrivialReloc)
{
testEraseUnstable<TrivialRelocat>();
}

TEST_F(dynarrayTest, shrinkToFit)
{
{
dynarray<MoveOnly> d(oel::reserve, 9);
d.emplace_back(-5);

d.shrink_to_fit();

EXPECT_GT(9u, d.capacity());
EXPECT_EQ(1u, d.size());
}
EXPECT_EQ(MoveOnly::nConstructions, MoveOnly::nDestruct);
}

TEST_F(dynarrayTest, overAligned)
{
static unsigned int const testAlignment = 64;
Expand Down

0 comments on commit 1a886ad

Please sign in to comment.