From 21983a83778f36edcda9eaadd021a2105a27f34c Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Thu, 20 Jul 2023 11:07:30 +0200 Subject: [PATCH] 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 Reviewed-by: Qt CI Patch Build Bot --- src/libs/utils/smallstring.h | 2 +- .../unittests/utils/smallstring-test.cpp | 40 ++++++++++++++++--- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/src/libs/utils/smallstring.h b/src/libs/utils/smallstring.h index f4cd6b749f4..b3d4434a9d0 100644 --- a/src/libs/utils/smallstring.h +++ b/src/libs/utils/smallstring.h @@ -455,7 +455,7 @@ public: constexpr size_type temporaryArraySize = Size * 6; size_type oldSize = size(); - size_type maximumRequiredSize = static_cast(encoder.requiredSpace(oldSize)); + size_type maximumRequiredSize = static_cast(encoder.requiredSpace(string.size())); char *newEnd = nullptr; if (maximumRequiredSize > temporaryArraySize) { diff --git a/tests/unit/tests/unittests/utils/smallstring-test.cpp b/tests/unit/tests/unittests/utils/smallstring-test.cpp index 98cdc266f9a..134891e5839 100644 --- a/tests/unit/tests/unittests/utils/smallstring-test.cpp +++ b/tests/unit/tests/unittests/utils/smallstring-test.cpp @@ -9,6 +9,8 @@ #include #include +#include + using Utils::PathString; using Utils::SmallString; using Utils::SmallStringLiteral; @@ -613,16 +615,44 @@ TEST(SmallString, to_q_string) ASSERT_THAT(qStringText, QStringLiteral("short string")); } -TEST(SmallString, from_q_string) +class FromQString : public testing::TestWithParam { - 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 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(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) { QByteArray qByteArray = QByteArrayLiteral("short string");