Add wxVector::shrink_to_fit() for C++11 compatibility

Also use this for wxArray::Shrink() implementation as it's more
efficient than the old swap-based implementation which requires an extra
memory allocation instead of really shrinking the existing one.
This commit is contained in:
Vadim Zeitlin
2017-11-19 22:09:37 +01:00
parent 9e714fdb5e
commit 53443b5370
3 changed files with 42 additions and 0 deletions

View File

@@ -361,3 +361,27 @@ TEST_CASE("wxVector::reverse_iterator", "[vector][reverse_iterator]")
CHECK( ri - rb == 2 );
CHECK( re - ri == 8 );
}
TEST_CASE("wxVector::capacity", "[vector][capacity][shrink_to_fit]")
{
wxVector<int> v;
CHECK( v.capacity() == 0 );
v.push_back(0);
// When using the standard library vector, we don't know what growth
// strategy it uses, so we can't rely on this check passing, but with our
// own one we can, allowing us to check that shrink_to_fit() really shrinks
// the capacity below.
#if !wxUSE_STD_CONTAINERS
CHECK( v.capacity() > 1 );
#endif
v.shrink_to_fit();
CHECK( v.capacity() == 1 );
v.erase(v.begin());
CHECK( v.capacity() == 1 );
v.shrink_to_fit();
CHECK( v.capacity() == 0 );
}