Commit b8e391e5 authored by Marco Bubke's avatar Marco Bubke

Utils: Fix SmallString reserve for read only references

The capacity of a real only reference is zero but the size is larger than
zero. So if we reserve memory the new capacity has to be bigger than the
size and the capacity.

Change-Id: I8b423da7e7c1cf7cee081d1b3f3464b6fb3f67cf
Reviewed-by: Tim Jenssen's avatarTim Jenssen <tim.jenssen@qt.io>
Reviewed-by: Tobias Hunger's avatarTobias Hunger <tobias.hunger@qt.io>
parent a290850a
......@@ -240,6 +240,7 @@ public:
m_data.allocated.data.capacity = newCapacity;
} else {
const size_type oldSize = size();
newCapacity = std::max(newCapacity, oldSize);
const char *oldData = data();
char *newData = Memory::allocate(newCapacity + 1);
......
......@@ -815,25 +815,43 @@ TEST(SmallString, EndsWithSmallString)
ASSERT_TRUE(text.endsWith('h'));
}
TEST(SmallString, ReserveSmallerThanReference)
TEST(SmallString, ReserveSmallerThanShortStringCapacity)
{
SmallString text("text");
text.reserve(2);
ASSERT_THAT(text.capacity(), 30);
ASSERT_THAT(text.capacity(), AnyOf(30, 4));
}
TEST(SmallString, ReserveBiggerThanReference)
TEST(SmallString, ReserveSmallerThanReference)
{
SmallString text("some very very very very very very very very very very very long string");
text.reserve(35);
ASSERT_THAT(text.capacity(), 71);
}
TEST(SmallString, ReserveBiggerThanShortStringCapacity)
{
SmallString text("text");
text.reserve(10);
ASSERT_THAT(text.capacity(), 30);
ASSERT_THAT(text.capacity(), AnyOf(30, 10));
}
TEST(SmallString, ReserveBiggerThanReference)
{
SmallString text("some very very very very very very very very very very very long string");
text.reserve(35);
ASSERT_THAT(text.capacity(), 71);
}
TEST(SmallString, ReserveMuchBiggerThanReference)
TEST(SmallString, ReserveMuchBiggerThanShortStringCapacity)
{
SmallString text("text");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment