forked from qt-creator/qt-creator
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 <tim.jenssen@qt.io> Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
@@ -240,6 +240,7 @@ public:
|
|||||||
m_data.allocated.data.capacity = newCapacity;
|
m_data.allocated.data.capacity = newCapacity;
|
||||||
} else {
|
} else {
|
||||||
const size_type oldSize = size();
|
const size_type oldSize = size();
|
||||||
|
newCapacity = std::max(newCapacity, oldSize);
|
||||||
const char *oldData = data();
|
const char *oldData = data();
|
||||||
|
|
||||||
char *newData = Memory::allocate(newCapacity + 1);
|
char *newData = Memory::allocate(newCapacity + 1);
|
||||||
|
|||||||
@@ -815,25 +815,43 @@ TEST(SmallString, EndsWithSmallString)
|
|||||||
ASSERT_TRUE(text.endsWith('h'));
|
ASSERT_TRUE(text.endsWith('h'));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(SmallString, ReserveSmallerThanReference)
|
TEST(SmallString, ReserveSmallerThanShortStringCapacity)
|
||||||
{
|
{
|
||||||
SmallString text("text");
|
SmallString text("text");
|
||||||
|
|
||||||
text.reserve(2);
|
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");
|
SmallString text("text");
|
||||||
|
|
||||||
text.reserve(10);
|
text.reserve(10);
|
||||||
|
|
||||||
ASSERT_THAT(text.capacity(), 30);
|
ASSERT_THAT(text.capacity(), AnyOf(30, 10));
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(SmallString, ReserveMuchBiggerThanReference)
|
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, ReserveMuchBiggerThanShortStringCapacity)
|
||||||
{
|
{
|
||||||
SmallString text("text");
|
SmallString text("text");
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user