diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2023-01-24 00:07:12 +0200 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2023-05-11 15:09:38 +0200 |
commit | 3f25c52914a25aed17c4b19f202e0304de74cf1f (patch) | |
tree | 97e891308b4b442587c5447c74aecb3641b3ab2d /tests | |
parent | ea64c6aec831aea06d955d0d54116eb15c13d928 (diff) |
QString: change test data to compile with QT_NO_CAST_FROM_ASCII [10/13]
Drive-by changes:
- Cleanup creating a QChar[], by creating a char16_t[] and
reinterpret_cast'ing it
- Use human-readable Unicode characters where possible
Change-Id: Ice2c36ff3ea4b6a5562cf907a7809166a51abd28
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
(cherry picked from commit 9d05fd9fa20229948da751da981f0a1b2f97fd54)
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/text/qstring/tst_qstring.cpp | 308 | ||||
-rw-r--r-- | tests/auto/corelib/text/qstring/tst_qstring_mac.mm | 11 |
2 files changed, 172 insertions, 147 deletions
diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index 444f093614..6eb2021f8a 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -1207,7 +1207,10 @@ void tst_QString::isEmpty() QVERIFY(a.isEmpty()); QVERIFY(!a.isDetached()); - QString c("Not empty"); + QString b = QString::fromLatin1("Not empty"); + QVERIFY(!b.isEmpty()); + + QString c = u"Not empty"_s; QVERIFY(!c.isEmpty()); } @@ -3233,13 +3236,11 @@ void tst_QString::append_bytearray_special_cases() void tst_QString::operator_pluseq_special_cases() { - { - QString a; - a += QChar::CarriageReturn; - a += '\r'; - a += u'\x1111'; - QCOMPARE(a, QStringView(u"\r\r\x1111")); - } + QString a; + a += QChar::CarriageReturn; + a += u'\r'; + a += u'\x1111'; + QCOMPARE(a, u"\r\r\x1111"); } void tst_QString::operator_pluseq_data(DataOptions options) @@ -3671,7 +3672,7 @@ void tst_QString::replace_string_extra() void tst_QString::replace_regexp() { static const QRegularExpression ignoreMessagePattern( - "^QString::replace\\(\\): called on an invalid QRegularExpression object" + u"^QString::replace\\(\\): called on an invalid QRegularExpression object"_s ); QFETCH( QString, string ); @@ -4934,25 +4935,25 @@ void tst_QString::endsWith() { QString a; - QVERIFY(!a.endsWith('A')); - QVERIFY(!a.endsWith("AB")); + QVERIFY(!a.endsWith(u'A')); + QVERIFY(!a.endsWith(u"AB"_s)); { - CREATE_VIEW("AB"); + CREATE_VIEW(u"AB"_s); QVERIFY(!a.endsWith(view)); } QVERIFY(!a.isDetached()); - a = "AB"; - QVERIFY( a.endsWith("B") ); - QVERIFY( a.endsWith("AB") ); - QVERIFY( !a.endsWith("C") ); - QVERIFY( !a.endsWith("ABCDEF") ); - QVERIFY( a.endsWith("") ); + a = u"AB"_s; + QVERIFY( a.endsWith(u"B"_s) ); + QVERIFY( a.endsWith(u"AB"_s) ); + QVERIFY( !a.endsWith(u"C"_s) ); + QVERIFY( !a.endsWith(u"ABCDEF"_s) ); + QVERIFY( a.endsWith(u""_s) ); QVERIFY( a.endsWith(QString()) ); - QVERIFY( a.endsWith('B') ); + QVERIFY( a.endsWith(u'B') ); QVERIFY( a.endsWith(QLatin1Char('B')) ); - QVERIFY( a.endsWith(QChar('B')) ); - QVERIFY( !a.endsWith('C') ); + QVERIFY( a.endsWith(QChar(u'B')) ); + QVERIFY( !a.endsWith(u'C') ); QVERIFY( !a.endsWith(QChar()) ); QVERIFY( !a.endsWith(QLatin1Char(0)) ); @@ -4963,24 +4964,24 @@ void tst_QString::endsWith() QVERIFY( a.endsWith(QLatin1String("")) ); QVERIFY( a.endsWith(QLatin1String(nullptr)) ); - QVERIFY( a.endsWith("B", Qt::CaseSensitive) ); - QVERIFY( a.endsWith("B", Qt::CaseInsensitive) ); - QVERIFY( !a.endsWith("b", Qt::CaseSensitive) ); - QVERIFY( a.endsWith("b", Qt::CaseInsensitive) ); - QVERIFY( !a.endsWith("aB", Qt::CaseSensitive) ); - QVERIFY( a.endsWith("aB", Qt::CaseInsensitive) ); - QVERIFY( !a.endsWith("C", Qt::CaseSensitive) ); - QVERIFY( !a.endsWith("C", Qt::CaseInsensitive) ); - QVERIFY( !a.endsWith("c", Qt::CaseSensitive) ); - QVERIFY( !a.endsWith("c", Qt::CaseInsensitive) ); - QVERIFY( !a.endsWith("abcdef", Qt::CaseInsensitive) ); - QVERIFY( a.endsWith("", Qt::CaseInsensitive) ); + QVERIFY( a.endsWith(u"B"_s, Qt::CaseSensitive) ); + QVERIFY( a.endsWith(u"B", Qt::CaseInsensitive) ); + QVERIFY( !a.endsWith(u"b", Qt::CaseSensitive) ); + QVERIFY( a.endsWith(u"b"_s, Qt::CaseInsensitive) ); + QVERIFY( !a.endsWith(u"aB"_s, Qt::CaseSensitive) ); + QVERIFY( a.endsWith(u"aB"_s, Qt::CaseInsensitive) ); + QVERIFY( !a.endsWith(u"C"_s, Qt::CaseSensitive) ); + QVERIFY( !a.endsWith(u"C"_s, Qt::CaseInsensitive) ); + QVERIFY( !a.endsWith(u"c"_s, Qt::CaseSensitive) ); + QVERIFY( !a.endsWith(u"c"_s, Qt::CaseInsensitive) ); + QVERIFY( !a.endsWith(u"abcdef"_s, Qt::CaseInsensitive) ); + QVERIFY( a.endsWith(u""_s, Qt::CaseInsensitive) ); QVERIFY( a.endsWith(QString(), Qt::CaseInsensitive) ); - QVERIFY( a.endsWith('b', Qt::CaseInsensitive) ); - QVERIFY( a.endsWith('B', Qt::CaseInsensitive) ); + QVERIFY( a.endsWith(u'b', Qt::CaseInsensitive) ); + QVERIFY( a.endsWith(u'B', Qt::CaseInsensitive) ); QVERIFY( a.endsWith(QLatin1Char('b'), Qt::CaseInsensitive) ); - QVERIFY( a.endsWith(QChar('b'), Qt::CaseInsensitive) ); - QVERIFY( !a.endsWith('c', Qt::CaseInsensitive) ); + QVERIFY( a.endsWith(QChar(u'b'), Qt::CaseInsensitive) ); + QVERIFY( !a.endsWith(u'c', Qt::CaseInsensitive) ); QVERIFY( !a.endsWith(QChar(), Qt::CaseInsensitive) ); QVERIFY( !a.endsWith(QLatin1Char(0), Qt::CaseInsensitive) ); @@ -4997,10 +4998,10 @@ void tst_QString::endsWith() QVERIFY( !a.endsWith(QLatin1String("abcdef"), Qt::CaseInsensitive) ); QVERIFY( a.endsWith(QLatin1String(""), Qt::CaseInsensitive) ); QVERIFY( a.endsWith(QLatin1String(nullptr), Qt::CaseInsensitive) ); - QVERIFY( a.endsWith('B', Qt::CaseSensitive) ); + QVERIFY( a.endsWith(u'B', Qt::CaseSensitive) ); QVERIFY( a.endsWith(QLatin1Char('B'), Qt::CaseSensitive) ); - QVERIFY( a.endsWith(QChar('B'), Qt::CaseSensitive) ); - QVERIFY( !a.endsWith('b', Qt::CaseSensitive) ); + QVERIFY( a.endsWith(QChar(u'B'), Qt::CaseSensitive) ); + QVERIFY( !a.endsWith(u'b', Qt::CaseSensitive) ); QVERIFY( !a.endsWith(QChar(), Qt::CaseSensitive) ); QVERIFY( !a.endsWith(QLatin1Char(0), Qt::CaseSensitive) ); @@ -5013,10 +5014,10 @@ void tst_QString::endsWith() TEST_VIEW_ENDS_WITH(QLatin1String(nullptr), true); #undef TEST_VIEW_ENDS_WITH - a = ""; - QVERIFY( a.endsWith("") ); + a = u""_s; + QVERIFY( a.endsWith(u""_s) ); QVERIFY( a.endsWith(QString()) ); - QVERIFY( !a.endsWith("ABC") ); + QVERIFY( !a.endsWith(u"ABC"_s) ); QVERIFY( !a.endsWith(QLatin1Char(0)) ); QVERIFY( !a.endsWith(QLatin1Char('x')) ); QVERIFY( !a.endsWith(QChar()) ); @@ -5026,9 +5027,9 @@ void tst_QString::endsWith() QVERIFY( !a.endsWith(QLatin1String("ABC")) ); a = QString(); - QVERIFY( !a.endsWith("") ); + QVERIFY( !a.endsWith(u""_s) ); QVERIFY( a.endsWith(QString()) ); - QVERIFY( !a.endsWith("ABC") ); + QVERIFY( !a.endsWith(u"ABC"_s) ); QVERIFY( !a.endsWith(QLatin1String("")) ); QVERIFY( a.endsWith(QLatin1String(nullptr)) ); @@ -5039,12 +5040,12 @@ void tst_QString::endsWith() QVERIFY( !a.endsWith(QChar()) ); // this test is independent of encoding - a = "\xc3\xa9"; - QVERIFY( a.endsWith("\xc3\xa9") ); - QVERIFY( !a.endsWith("\xc3\xa1") ); + a = u'é'; + QVERIFY(a.endsWith(u"é"_s)); + QVERIFY(!a.endsWith(u"á"_s)); // this one is dependent of encoding - QVERIFY( a.endsWith("\xc3\x89", Qt::CaseInsensitive) ); + QVERIFY(a.endsWith(u"É"_s, Qt::CaseInsensitive)); } void tst_QString::check_QDataStream() @@ -5053,7 +5054,7 @@ void tst_QString::check_QDataStream() QByteArray ar; { QDataStream out(&ar,QIODevice::WriteOnly); - out << QString("COMPARE Text"); + out << u"COMPARE Text"_s; } { QDataStream in(&ar,QIODevice::ReadOnly); @@ -5068,7 +5069,7 @@ void tst_QString::check_QTextStream() QByteArray ar; { QTextStream out(&ar,QIODevice::WriteOnly); - out << QString("This is COMPARE Text"); + out << u"This is COMPARE Text"_s; } { QTextStream in(&ar,QIODevice::ReadOnly); @@ -5081,21 +5082,21 @@ void tst_QString::check_QTextIOStream() { QString a; { - a=""; + a = u""_s; QTextStream ts(&a); // invalid Utf8 ts << "pi \261= " << 3.125; QCOMPARE(a, QString::fromUtf16(u"pi \xfffd= 3.125")); } { - a=""; + a = u""_s; QTextStream ts(&a); // valid Utf8 ts << "pi ø= " << 3.125; QCOMPARE(a, QString::fromUtf16(u"pi ø= 3.125")); } { - a="123 456"; + a = u"123 456"_s; int x,y; QTextStream(&a) >> x >> y; QCOMPARE(x,123); @@ -5144,7 +5145,7 @@ void tst_QString::setRawData() QVERIFY(cstr.data_ptr() == csd); // This tests the discarding of the shared data object - cstr = "foo"; + cstr = QString::fromUtf8("foo"); QVERIFY(cstr.isDetached()); QVERIFY(cstr.constData() != ptr2); @@ -5171,7 +5172,7 @@ void tst_QString::setUnicode() QCOMPARE(str, QString(ptr, 1)); // make sure that the string is resized, even if the data is nullptr - str = "test"; + str = u"test"_s; QCOMPARE(str.size(), 4); str.setUnicode(nullptr, 1); QCOMPARE(str.size(), 1); @@ -5183,7 +5184,7 @@ void tst_QString::fromStdString() QVERIFY(QString::fromStdString(std::string()).isEmpty()); std::string stroustrup = "foo"; QString eng = QString::fromStdString( stroustrup ); - QCOMPARE( eng, QString("foo") ); + QCOMPARE( eng, u"foo"_s ); const char cnull[] = "Embedded\0null\0character!"; std::string stdnull( cnull, sizeof(cnull)-1 ); QString qtnull = QString::fromStdString( stdnull ); @@ -5196,23 +5197,25 @@ void tst_QString::toStdString() QVERIFY(nullStr.toStdString().empty()); QVERIFY(!nullStr.isDetached()); - QString emptyStr(""); + QString emptyStr(u""_s); QVERIFY(emptyStr.toStdString().empty()); QVERIFY(!emptyStr.isDetached()); - QString nord = "foo"; + QString nord = u"foo"_s; std::string stroustrup1 = nord.toStdString(); QVERIFY( qstrcmp(stroustrup1.c_str(), "foo") == 0 ); + // For now, most QString constructors are also broken with respect // to embedded null characters, had to find one that works... - const QChar qcnull[] = { - 'E', 'm', 'b', 'e', 'd', 'd', 'e', 'd', '\0', - 'n', 'u', 'l', 'l', '\0', - 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', '!' - }; - QString qtnull( qcnull, sizeof(qcnull)/sizeof(QChar) ); + const char16_t utf16[] = u"Embedded\0null\0character!"; + const int size = std::size(utf16) - 1; // - 1, null terminator of the string literal + QString qtnull(reinterpret_cast<const QChar *>(utf16), size); + std::string stdnull = qtnull.toStdString(); - QCOMPARE( int(stdnull.size()), qtnull.size() ); + QCOMPARE(int(stdnull.size()), qtnull.size()); + + std::u16string stdu16null = qtnull.toStdU16String(); + QCOMPARE(int(stdu16null.size()), qtnull.size()); } void tst_QString::utf8() @@ -5362,7 +5365,7 @@ void tst_QString::fromLocal8Bit_data() for (int l=0;l<111;l++) { longQByteArray = longQByteArray + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; - longQString += "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + longQString += u"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"_s; } //QTest::newRow("longString") << longQByteArray << -1 << longQString; @@ -5631,7 +5634,7 @@ void tst_QString::toLatin1Roundtrip() QCOMPARE(std::move(s).toLatin1(), latin1); // and verify that the moved-from object can still be used - s = "foo"; + s = u"foo"_s; s.clear(); } @@ -5694,13 +5697,13 @@ void tst_QString::fromUcs4() static const char32_t str1[] = U"Hello Unicode World"; s = QString::fromUcs4(str1, sizeof(str1) / sizeof(str1[0]) - 1); - QCOMPARE(s, QString("Hello Unicode World")); + QCOMPARE(s, u"Hello Unicode World"); s = QString::fromUcs4(str1); - QCOMPARE(s, QString("Hello Unicode World")); + QCOMPARE(s, u"Hello Unicode World"); s = QString::fromUcs4(str1, 5); - QCOMPARE(s, QString("Hello")); + QCOMPARE(s, u"Hello"); s = QString::fromUcs4(U"\u221212\U000020AC\U00010000"); QCOMPARE(s, QString::fromUtf8("\342\210\222" "12" "\342\202\254" "\360\220\200\200")); @@ -6108,7 +6111,7 @@ void tst_QString::doubleOut() const QString expect(QStringLiteral("1e-06")); const double micro = 1e-6; QCOMPARE(QString::number(micro), expect); - QCOMPARE(QString("%1").arg(micro), expect); + QCOMPARE(u"%1"_s.arg(micro), expect); { QCOMPARE(QString::asprintf("%g", micro), expect); } @@ -6333,31 +6336,31 @@ void tst_QString::operator_eqeq_nullstring() /* Some of these might not be all that logical but it's the behaviour we've had since 3.0.0 so we should probably stick with it. */ - QVERIFY( QString() == "" ); - QVERIFY( "" == QString() ); + QVERIFY( QString() == u""_s ); + QVERIFY( u""_s == QString() ); - QVERIFY( QString("") == "" ); - QVERIFY( "" == QString("") ); + QVERIFY( QString(u""_s) == u""_s ); + QVERIFY( u""_s == QString(u""_s) ); QVERIFY(QString() == nullptr); QVERIFY(nullptr == QString()); - QVERIFY(QString("") == nullptr); - QVERIFY(nullptr == QString("")); + QVERIFY(QString(u""_s) == nullptr); + QVERIFY(nullptr == QString(u""_s)); QVERIFY( QString().size() == 0 ); - QVERIFY( QString("").size() == 0 ); + QVERIFY(u""_s.size() == 0); - QVERIFY( QString() == QString("") ); - QVERIFY( QString("") == QString() ); + QVERIFY(QString() == u""_s); + QVERIFY( QString(u""_s) == QString() ); } void tst_QString::operator_smaller() { QString null; - QString empty(""); - QString foo("foo"); + QString empty(u""_s); + QString foo(u"foo"_s); [[maybe_unused]] const char *nullC = nullptr; [[maybe_unused]] @@ -6366,8 +6369,8 @@ void tst_QString::operator_smaller() QVERIFY( !(null < QString()) ); QVERIFY( !(null > QString()) ); - QVERIFY( !(empty < QString("")) ); - QVERIFY( !(empty > QString("")) ); + QVERIFY(!(empty < u""_s)); + QVERIFY(!(empty > u""_s)); QVERIFY( !(null < empty) ); QVERIFY( !(null > empty) ); @@ -6711,7 +6714,7 @@ void tst_QString::iterators() QVERIFY(!emptyStr.isDetached()); QCOMPARE(emptyStr.begin(), emptyStr.end()); - QString s = "0123456789"; + QString s = u"0123456789"_s; auto it = s.begin(); auto constIt = s.cbegin(); @@ -6752,7 +6755,7 @@ void tst_QString::reverseIterators() QVERIFY(!emptyStr.isDetached()); QCOMPARE(emptyStr.rbegin(), emptyStr.rend()); - QString s = "1234"; + QString s(u"1234"_s); QString sr = s; std::reverse(sr.begin(), sr.end()); const QString &csr = sr; @@ -6853,7 +6856,7 @@ QT_WARNING_DISABLE_DEPRECATED QVERIFY(list == result); } - result.removeAll(""); + result.removeAll(u""_s); list = str.split(sep, Qt::SkipEmptyParts); QVERIFY(list == result); #if QT_CONFIG(regularexpression) @@ -6929,13 +6932,13 @@ void tst_QString::regularexpression_lifetime() return QString(QLatin1String("the quick brown fox jumps over the lazy dog")); }; - QRegularExpression re("\\w{5}"); + QRegularExpression re(u"\\w{5}"_s); { QString s = getString(); QRegularExpressionMatch match; const bool contains = std::move(s).contains(re, &match); - s.fill('X'); // NOLINT(bugprone-use-after-move) + s.fill(u'X'); // NOLINT(bugprone-use-after-move) QVERIFY(contains); QCOMPARE(match.capturedView(), u"quick"); } @@ -6944,7 +6947,7 @@ void tst_QString::regularexpression_lifetime() QString s = getString(); QRegularExpressionMatch match; const auto index = std::move(s).indexOf(re, 0, &match); - s.fill('X'); // NOLINT(bugprone-use-after-move) + s.fill(u'X'); // NOLINT(bugprone-use-after-move) QCOMPARE(index, 4); QCOMPARE(match.capturedView(), u"quick"); } @@ -6953,7 +6956,7 @@ void tst_QString::regularexpression_lifetime() QString s = getString(); QRegularExpressionMatch match; const auto lastIndex = std::move(s).lastIndexOf(re, &match); - s.fill('X'); // NOLINT(bugprone-use-after-move) + s.fill(u'X'); // NOLINT(bugprone-use-after-move) QCOMPARE(lastIndex, 20); QCOMPARE(match.capturedView(), u"jumps"); } @@ -6996,7 +6999,7 @@ void tst_QString::unicodeStrings() QVERIFY(nullStr.toStdU32String().empty()); QVERIFY(!nullStr.isDetached()); - QString emptyStr(""); + QString emptyStr(u""_s); QVERIFY(emptyStr.toStdU16String().empty()); QVERIFY(emptyStr.toStdU32String().empty()); QVERIFY(!emptyStr.isDetached()); @@ -7006,7 +7009,7 @@ void tst_QString::unicodeStrings() static const std::u32string u32str1(U"Hello Unicode World"); s1 = QString::fromStdU16String(u16str1); s2 = QString::fromStdU32String(u32str1); - QCOMPARE(s1, QString("Hello Unicode World")); + QCOMPARE(s1, u"Hello Unicode World"); QCOMPARE(s1, s2); QCOMPARE(s2.toStdU16String(), u16str1); @@ -7018,7 +7021,7 @@ void tst_QString::unicodeStrings() void tst_QString::latin1String() { - QString s("Hello"); + QString s(u"Hello"_s); QVERIFY(s == QLatin1String("Hello")); QVERIFY(s != QLatin1String("Hello World")); @@ -7108,20 +7111,20 @@ void tst_QString::nanAndInf() bool ok; double d; - d = QString("-INF").toDouble(&ok); + d = u"-INF"_s.toDouble(&ok); QVERIFY(ok); QVERIFY(d == -qInf()); - QString("INF").toLong(&ok); + u"INF"_s.toLong(&ok); QVERIFY(!ok); - QString("INF").toLong(&ok, 36); + u"INF"_s.toLong(&ok, 36); QVERIFY(ok); - QString("INF0").toLong(&ok, 36); + u"INF0"_s.toLong(&ok, 36); QVERIFY(ok); - QString("0INF0").toLong(&ok, 36); + u"0INF0"_s.toLong(&ok, 36); QVERIFY(ok); // Check that inf (float) => "inf" (QString) => inf (float). @@ -7141,36 +7144,36 @@ void tst_QString::nanAndInf() // Check that .arg(inf-or-nan, wide, fmt, 3, '0') padds with zeros QString form = QStringLiteral("%1"); - QCOMPARE(form.arg(qInf(), 5, 'f', 3, '0'), u"00inf"); - QCOMPARE(form.arg(qInf(), -5, 'f', 3, '0'), u"inf00"); - QCOMPARE(form.arg(-qInf(), 6, 'f', 3, '0'), u"00-inf"); - QCOMPARE(form.arg(-qInf(), -6, 'f', 3, '0'), u"-inf00"); - QCOMPARE(form.arg(qQNaN(), -5, 'f', 3, '0'), u"nan00"); - QCOMPARE(form.arg(qInf(), 5, 'F', 3, '0'), u"00INF"); - QCOMPARE(form.arg(qInf(), -5, 'F', 3, '0'), u"INF00"); - QCOMPARE(form.arg(-qInf(), 6, 'F', 3, '0'), u"00-INF"); - QCOMPARE(form.arg(-qInf(), -6, 'F', 3, '0'), u"-INF00"); - QCOMPARE(form.arg(qQNaN(), -5, 'F', 3, '0'), u"NAN00"); - QCOMPARE(form.arg(qInf(), 5, 'e', 3, '0'), u"00inf"); - QCOMPARE(form.arg(qInf(), -5, 'e', 3, '0'), u"inf00"); - QCOMPARE(form.arg(-qInf(), 6, 'e', 3, '0'), u"00-inf"); - QCOMPARE(form.arg(-qInf(), -6, 'e', 3, '0'), u"-inf00"); - QCOMPARE(form.arg(qQNaN(), -5, 'e', 3, '0'), u"nan00"); - QCOMPARE(form.arg(qInf(), 5, 'E', 3, '0'), u"00INF"); - QCOMPARE(form.arg(qInf(), -5, 'E', 3, '0'), u"INF00"); - QCOMPARE(form.arg(-qInf(), 6, 'E', 3, '0'), u"00-INF"); - QCOMPARE(form.arg(-qInf(), -6, 'E', 3, '0'), u"-INF00"); - QCOMPARE(form.arg(qQNaN(), -5, 'E', 3, '0'), u"NAN00"); - QCOMPARE(form.arg(qInf(), 5, 'g', 3, '0'), u"00inf"); - QCOMPARE(form.arg(qInf(), -5, 'g', 3, '0'), u"inf00"); - QCOMPARE(form.arg(-qInf(), 6, 'g', 3, '0'), u"00-inf"); - QCOMPARE(form.arg(-qInf(), -6, 'g', 3, '0'), u"-inf00"); - QCOMPARE(form.arg(qQNaN(), -5, 'g', 3, '0'), u"nan00"); - QCOMPARE(form.arg(qInf(), 5, 'G', 3, '0'), u"00INF"); - QCOMPARE(form.arg(qInf(), -5, 'G', 3, '0'), u"INF00"); - QCOMPARE(form.arg(-qInf(), 6, 'G', 3, '0'), u"00-INF"); - QCOMPARE(form.arg(-qInf(), -6, 'G', 3, '0'), u"-INF00"); - QCOMPARE(form.arg(qQNaN(), -5, 'G', 3, '0'), u"NAN00"); + QCOMPARE(form.arg(qInf(), 5, 'f', 3, u'0'), u"00inf"); + QCOMPARE(form.arg(qInf(), -5, 'f', 3, u'0'), u"inf00"); + QCOMPARE(form.arg(-qInf(), 6, 'f', 3, u'0'), u"00-inf"); + QCOMPARE(form.arg(-qInf(), -6, 'f', 3, u'0'), u"-inf00"); + QCOMPARE(form.arg(qQNaN(), -5, 'f', 3, u'0'), u"nan00"); + QCOMPARE(form.arg(qInf(), 5, 'F', 3, u'0'), u"00INF"); + QCOMPARE(form.arg(qInf(), -5, 'F', 3, u'0'), u"INF00"); + QCOMPARE(form.arg(-qInf(), 6, 'F', 3, u'0'), u"00-INF"); + QCOMPARE(form.arg(-qInf(), -6, 'F', 3, u'0'), u"-INF00"); + QCOMPARE(form.arg(qQNaN(), -5, 'F', 3, u'0'), u"NAN00"); + QCOMPARE(form.arg(qInf(), 5, 'e', 3, u'0'), u"00inf"); + QCOMPARE(form.arg(qInf(), -5, 'e', 3, u'0'), u"inf00"); + QCOMPARE(form.arg(-qInf(), 6, 'e', 3, u'0'), u"00-inf"); + QCOMPARE(form.arg(-qInf(), -6, 'e', 3, u'0'), u"-inf00"); + QCOMPARE(form.arg(qQNaN(), -5, 'e', 3, u'0'), u"nan00"); + QCOMPARE(form.arg(qInf(), 5, 'E', 3, u'0'), u"00INF"); + QCOMPARE(form.arg(qInf(), -5, 'E', 3, u'0'), u"INF00"); + QCOMPARE(form.arg(-qInf(), 6, 'E', 3, u'0'), u"00-INF"); + QCOMPARE(form.arg(-qInf(), -6, 'E', 3, u'0'), u"-INF00"); + QCOMPARE(form.arg(qQNaN(), -5, 'E', 3, u'0'), u"NAN00"); + QCOMPARE(form.arg(qInf(), 5, 'g', 3, u'0'), u"00inf"); + QCOMPARE(form.arg(qInf(), -5, 'g', 3, u'0'), u"inf00"); + QCOMPARE(form.arg(-qInf(), 6, 'g', 3, u'0'), u"00-inf"); + QCOMPARE(form.arg(-qInf(), -6, 'g', 3, u'0'), u"-inf00"); + QCOMPARE(form.arg(qQNaN(), -5, 'g', 3, u'0'), u"nan00"); + QCOMPARE(form.arg(qInf(), 5, 'G', 3, u'0'), u"00INF"); + QCOMPARE(form.arg(qInf(), -5, 'G', 3, u'0'), u"INF00"); + QCOMPARE(form.arg(-qInf(), 6, 'G', 3, u'0'), u"00-INF"); + QCOMPARE(form.arg(-qInf(), -6, 'G', 3, u'0'), u"-INF00"); + QCOMPARE(form.arg(qQNaN(), -5, 'G', 3, u'0'), u"NAN00"); } void tst_QString::arg_fillChar_data() @@ -7455,7 +7458,7 @@ void tst_QString::compare() void tst_QString::resize() { QString s; - s.resize(11, ' '); + s.resize(11, u' '); QCOMPARE(s.size(), 11); QCOMPARE(s, QLatin1String(" ")); @@ -7475,7 +7478,7 @@ void tst_QString::resize() void tst_QString::resizeAfterFromRawData() { - QString buffer("hello world"); + QString buffer(u"hello world"_s); QString array = QString::fromRawData(buffer.constData(), buffer.size()); QVERIFY(array.constData() == buffer.constData()); @@ -7491,7 +7494,7 @@ void tst_QString::resizeAfterReserve() QString s; s.reserve(100); - s += "hello world"; + s += u"hello world"_s; // resize should not affect capacity s.resize(s.size()); @@ -7507,12 +7510,12 @@ void tst_QString::resizeAfterReserve() // test resize(0) border case s.reserve(100); - s += "hello world"; + s += u"hello world"_s; s.resize(0); QVERIFY(s.capacity() == 100); // reserve() can't be used to truncate data - s.fill('x', 100); + s.fill(u'x', 100); s.reserve(50); QVERIFY(s.capacity() == 100); QVERIFY(s.size() == 100); @@ -8127,7 +8130,7 @@ void tst_QString::assignQChar() QCOMPARE(s.capacity(), 1); // assign to empty QString: - s = QString(""); + s = QString(u""_s); s.detach(); QCOMPARE(s.capacity(), 0); s = sp; @@ -8272,7 +8275,7 @@ void tst_QString::rawData() QCOMPARE(s.utf16(), reinterpret_cast<const ushort *>(constPtr)); QVERIFY(!s.isDetached()); - s = "abc"; // detached + s = QString::fromUtf8("abc"); // detached const QChar *dataConstPtr = s.constData(); QVERIFY(dataConstPtr != constPtr); @@ -8286,8 +8289,8 @@ void tst_QString::rawData() QVERIFY(s1Ptr != dataConstPtr); QVERIFY(s1.unicode() != s.unicode()); - *s1Ptr = 'd'; - QCOMPARE(s1, "dbc"); + *s1Ptr = u'd'; + QCOMPARE(s1, u"dbc"); // utf pointer is valid while the string is not changed QCOMPARE(QString::fromUtf16(char16Ptr), s); @@ -8300,7 +8303,7 @@ void tst_QString::clear() QVERIFY(s.isEmpty()); QVERIFY(!s.isDetached()); - s = "some tests string"; + s = u"some tests string"_s; QVERIFY(!s.isEmpty()); s.clear(); @@ -8315,7 +8318,7 @@ void tst_QString::sliced() QVERIFY(a.sliced(0, 0).isEmpty()); QVERIFY(!a.isDetached()); - a = "ABCDEFGHIEfGEFG"; // 15 chars + a = u"ABCDEFGHIEfGEFG"_s; // 15 chars QCOMPARE(a.sliced(5), u"FGHIEfGEFG"); QCOMPARE(a.sliced(5, 3), u"FGH"); @@ -8328,7 +8331,7 @@ void tst_QString::chopped() QVERIFY(a.chopped(0).isEmpty()); QVERIFY(!a.isDetached()); - a = "ABCDEFGHIEfGEFG"; // 15 chars + a = u"ABCDEFGHIEfGEFG"_s; // 15 chars QCOMPARE(a.chopped(10), u"ABCDE"); } @@ -8343,9 +8346,28 @@ void tst_QString::removeIf() QVERIFY(a.isEmpty()); QVERIFY(!a.isDetached()); - a = "aABbcCDd"; + // Test when the string is not shared + a = "aABbcCDd"_L1; + QVERIFY(!a.data_ptr()->needsDetach()); a.removeIf(pred); QCOMPARE(a, u"ABCD"); + + // Test when the string is shared + a = "aABbcCDd"_L1; + QString b = a; + QVERIFY(a.data_ptr()->needsDetach()); + a.removeIf(pred); + QCOMPARE(a, u"ABCD"); + QCOMPARE(b, "aABbcCDd"_L1); + + auto removeA = [](const QChar c) { return c == u'a' || c == u'A'; }; + + a = "aBcAbCa"_L1; // Not shared + QCOMPARE(a.removeIf(removeA), u"BcbC"); + + a = "aBcAbCa"_L1; + b = a; // Shared + QCOMPARE(a.removeIf(removeA), u"BcbC"); } // QString's collation order is only supported during the lifetime as QCoreApplication diff --git a/tests/auto/corelib/text/qstring/tst_qstring_mac.mm b/tests/auto/corelib/text/qstring/tst_qstring_mac.mm index 5cdf3a4341..180aed6369 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring_mac.mm +++ b/tests/auto/corelib/text/qstring/tst_qstring_mac.mm @@ -9,17 +9,20 @@ #include <CoreFoundation/CoreFoundation.h> #include <Foundation/Foundation.h> +using namespace Qt::StringLiterals; + void tst_QString_macTypes() { + const QLatin1StringView testString("test string"); // QString <-> CFString { - QString qtString("test string"); + QString qtString = testString; const CFStringRef cfString = qtString.toCFString(); QCOMPARE(QString::fromCFString(cfString), qtString); CFRelease(cfString); } { - QString qtString("test string"); + QString qtString = testString; const CFStringRef cfString = qtString.toCFString(); QString qtStringCopy(qtString); qtString = qtString.toUpper(); // modify @@ -29,14 +32,14 @@ void tst_QString_macTypes() { QMacAutoReleasePool pool; - QString qtString("test string"); + QString qtString = testString; const NSString *nsString = qtString.toNSString(); QCOMPARE(QString::fromNSString(nsString), qtString); } { QMacAutoReleasePool pool; - QString qtString("test string"); + QString qtString = testString; const NSString *nsString = qtString.toNSString(); QString qtStringCopy(qtString); qtString = qtString.toUpper(); // modify |