forked from qt-creator/qt-creator
Utils: Fix SmallString::append(QStringView string)
It used the size of the destination string but it had to use the size of the appended string to inquire the maximum required size. Change-Id: I1c910abab0ac60ed5fec1b3dc3a358e438532281 Reviewed-by: Vikas Pachdha <vikas.pachdha@qt.io> Reviewed-by: Qt CI Patch Build Bot <ci_patchbuild_bot@qt.io>
This commit is contained in:
@@ -455,7 +455,7 @@ public:
|
||||
constexpr size_type temporaryArraySize = Size * 6;
|
||||
|
||||
size_type oldSize = size();
|
||||
size_type maximumRequiredSize = static_cast<size_type>(encoder.requiredSpace(oldSize));
|
||||
size_type maximumRequiredSize = static_cast<size_type>(encoder.requiredSpace(string.size()));
|
||||
char *newEnd = nullptr;
|
||||
|
||||
if (maximumRequiredSize > temporaryArraySize) {
|
||||
|
@@ -9,6 +9,8 @@
|
||||
#include <utils/smallstringio.h>
|
||||
#include <utils/smallstringvector.h>
|
||||
|
||||
#include <random>
|
||||
|
||||
using Utils::PathString;
|
||||
using Utils::SmallString;
|
||||
using Utils::SmallStringLiteral;
|
||||
@@ -613,15 +615,43 @@ TEST(SmallString, to_q_string)
|
||||
ASSERT_THAT(qStringText, QStringLiteral("short string"));
|
||||
}
|
||||
|
||||
TEST(SmallString, from_q_string)
|
||||
class FromQString : public testing::TestWithParam<qsizetype>
|
||||
{
|
||||
QString qStringText = QStringLiteral("short string");
|
||||
protected:
|
||||
QString randomString(qsizetype size)
|
||||
{
|
||||
static constexpr char16_t characters[] = u"0123456789"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
|
||||
auto text = SmallString::fromQString(qStringText);
|
||||
static std::mt19937 generator{std::random_device{}()};
|
||||
static std::uniform_int_distribution<std::size_t> pick(0, std::size(characters) - 1);
|
||||
|
||||
ASSERT_THAT(text, SmallString("short string"));
|
||||
QString string;
|
||||
|
||||
string.reserve(size);
|
||||
|
||||
std::generate_n(std::back_inserter(string), size, [&]() {
|
||||
return characters[pick(generator)];
|
||||
});
|
||||
|
||||
return string;
|
||||
}
|
||||
|
||||
protected:
|
||||
qsizetype size = GetParam();
|
||||
};
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(SmallString, FromQString, testing::Range<qsizetype>(0, 10000, 300));
|
||||
|
||||
TEST_P(FromQString, from_qstring)
|
||||
{
|
||||
const QString qStringText = randomString(size);
|
||||
|
||||
auto text = SmallString(qStringText);
|
||||
|
||||
ASSERT_THAT(text, qStringText.toStdString());
|
||||
}
|
||||
|
||||
TEST(SmallString, from_q_byte_array)
|
||||
{
|
||||
|
Reference in New Issue
Block a user