diff options
Diffstat (limited to 'tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp')
-rw-r--r-- | tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp | 187 |
1 files changed, 118 insertions, 69 deletions
diff --git a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp index 58d2fab09f..6ac8236d57 100644 --- a/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp +++ b/tests/auto/corelib/text/qstringlist/tst_qstringlist.cpp @@ -1,5 +1,5 @@ // Copyright (C) 2021 The Qt Company Ltd. -// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <QTest> #include <qlist.h> @@ -11,6 +11,8 @@ #include <algorithm> +using namespace Qt::StringLiterals; + class tst_QStringList : public QObject { Q_OBJECT @@ -21,6 +23,7 @@ private slots: void replaceInStrings(); void removeDuplicates(); void removeDuplicates_data(); + void contains_data(); void contains(); void indexOf_data(); void indexOf(); @@ -75,26 +78,33 @@ void tst_QStringList::constructors() void tst_QStringList::indexOf_data() { + QTest::addColumn<QStringList>("list"); QTest::addColumn<QString>("search"); QTest::addColumn<int>("from"); QTest::addColumn<int>("expectedResult"); - QTest::newRow("harald") << "harald" << 0 << 0; - QTest::newRow("trond") << "trond" << 0 << 1; - QTest::newRow("vohi") << "vohi" << 0 << 2; - QTest::newRow("harald-1") << "harald" << 1 << 3; + QStringList searchIn{"harald", "trond", "vohi", "harald"}; + QTest::newRow("harald") << searchIn << "harald" << 0 << 0; + QTest::newRow("trond") << searchIn << "trond" << 0 << 1; + QTest::newRow("vohi") << searchIn << "vohi" << 0 << 2; + QTest::newRow("harald-1") << searchIn << "harald" << 1 << 3; + + QTest::newRow("hans") << searchIn << "hans" << 0 << -1; + QTest::newRow("trond-1") << searchIn << "trond" << 2 << -1; + QTest::newRow("harald-2") << searchIn << "harald" << -1 << 3; + QTest::newRow("vohi-1") << searchIn << "vohi" << -3 << 2; + + QTest::newRow("from-bigger-than-size") << searchIn << "harald" << 100 << -1; - QTest::newRow("hans") << "hans" << 0 << -1; - QTest::newRow("trond-1") << "trond" << 2 << -1; - QTest::newRow("harald-2") << "harald" << -1 << 3; - QTest::newRow("vohi-1") << "vohi" << -3 << 2; + searchIn = {"lost+found", "foo.bar"}; + QTest::newRow("string-with-regex-meta-char1") << searchIn << "lost+found" << 0 << 0; + QTest::newRow("string-with-regex-meta-char2") << searchIn << "foo.bar" << 0 << 1; + QTest::newRow("string-with-regex-meta-char3") << searchIn << "foo.bar" << 2 << -1; } void tst_QStringList::indexOf() { - QStringList list; - list << "harald" << "trond" << "vohi" << "harald"; - + QFETCH(QStringList, list); QFETCH(QString, search); QFETCH(int, from); QFETCH(int, expectedResult); @@ -102,30 +112,45 @@ void tst_QStringList::indexOf() QCOMPARE(list.indexOf(search, from), expectedResult); QCOMPARE(list.indexOf(QStringView(search), from), expectedResult); QCOMPARE(list.indexOf(QLatin1String(search.toLatin1()), from), expectedResult); + QCOMPARE(list.indexOf(QRegularExpression(QRegularExpression::escape(search)), from), expectedResult); + + QString searchUpper = search.toUpper(); + QCOMPARE(list.indexOf(searchUpper, from, Qt::CaseInsensitive), expectedResult); + QCOMPARE(list.indexOf(QStringView(searchUpper), from, Qt::CaseInsensitive), expectedResult); + QCOMPARE(list.indexOf(QLatin1StringView(searchUpper.toLatin1()), from, Qt::CaseInsensitive), + expectedResult); + const QRegularExpression re(QRegularExpression::escape(searchUpper), + QRegularExpression::CaseInsensitiveOption); + QCOMPARE(list.indexOf(re, from), expectedResult); } void tst_QStringList::lastIndexOf_data() { + QTest::addColumn<QStringList>("list"); QTest::addColumn<QString>("search"); QTest::addColumn<int>("from"); QTest::addColumn<int>("expectedResult"); - QTest::newRow("harald") << "harald" << -1 << 3; - QTest::newRow("trond") << "trond" << -1 << 1; - QTest::newRow("vohi") << "vohi" << -1 << 2; - QTest::newRow("harald-1") << "harald" << 2 << 0; - - QTest::newRow("hans") << "hans" << -1 << -1; - QTest::newRow("vohi-1") << "vohi" << 1 << -1; - QTest::newRow("vohi-2") << "vohi" << -1 << 2; - QTest::newRow("vohi-3") << "vohi" << -3 << -1; + QStringList list{"harald", "trond", "vohi", "harald"}; + QTest::newRow("harald") << list << "harald" << -1 << 3; + QTest::newRow("trond") << list << "trond" << -1 << 1; + QTest::newRow("vohi") << list << "vohi" << -1 << 2; + QTest::newRow("harald-1") << list << "harald" << 2 << 0; + + QTest::newRow("hans") << list << "hans" << -1 << -1; + QTest::newRow("vohi-1") << list << "vohi" << 1 << -1; + QTest::newRow("vohi-2") << list << "vohi" << -1 << 2; + QTest::newRow("vohi-3") << list << "vohi" << -3 << -1; + + list = {"lost+found", "foo.bar"}; + QTest::newRow("string-with-regex-meta-char1") << list << "lost+found" << -1 << 0; + QTest::newRow("string-with-regex-meta-char2") << list << "foo.bar" << -1 << 1; + QTest::newRow("string-with-regex-meta-char3") << list << "foo.bar" << -2 << -1; } void tst_QStringList::lastIndexOf() { - QStringList list; - list << "harald" << "trond" << "vohi" << "harald"; - + QFETCH(QStringList, list); QFETCH(QString, search); QFETCH(int, from); QFETCH(int, expectedResult); @@ -133,27 +158,40 @@ void tst_QStringList::lastIndexOf() QCOMPARE(list.lastIndexOf(search, from), expectedResult); QCOMPARE(list.lastIndexOf(QStringView(search), from), expectedResult); QCOMPARE(list.lastIndexOf(QLatin1String(search.toLatin1()), from), expectedResult); + QCOMPARE(list.lastIndexOf(QRegularExpression(QRegularExpression::escape(search)), from), expectedResult); + + const QString searchUpper = search.toUpper(); + QCOMPARE(list.lastIndexOf(searchUpper, from, Qt::CaseInsensitive), expectedResult); + QCOMPARE(list.lastIndexOf(QStringView(searchUpper), from, Qt::CaseInsensitive), expectedResult); + QCOMPARE(list.lastIndexOf(QLatin1String(searchUpper.toLatin1()), from, Qt::CaseInsensitive), + expectedResult); + const QRegularExpression re(QRegularExpression::escape(searchUpper), + QRegularExpression::CaseInsensitiveOption); + QCOMPARE(list.lastIndexOf(re, from), expectedResult); } void tst_QStringList::filter() { - QStringList list1, list2; - list1 << "Bill Gates" << "Joe Blow" << "Bill Clinton"; - list1 = list1.filter( "Bill" ); - list2 << "Bill Gates" << "Bill Clinton"; - QCOMPARE( list1, list2 ); - - QStringList list5, list6; - list5 << "Bill Gates" << "Joe Blow" << "Bill Clinton"; - list5 = list5.filter( QRegularExpression("[i]ll") ); - list6 << "Bill Gates" << "Bill Clinton"; - QCOMPARE( list5, list6 ); + const QStringList list = {u"Bill Gates"_s, u"Joe Blow"_s, u"Bill Clinton"_s, u"bIll"_s}; + + { // CaseSensitive + const QStringList expected{u"Bill Gates"_s, u"Bill Clinton"_s}; + QCOMPARE(list.filter(u"Bill"_s), expected); + QCOMPARE(list.filter(u"Bill"), expected); + QCOMPARE(list.filter("Bill"_L1), expected); + QCOMPARE(list.filter(QRegularExpression(u"[i]ll"_s)), expected); + QCOMPARE(list.filter(QStringMatcher(u"Bill")), expected); + } - QStringList list7, list8; - list7 << "Bill Gates" << "Joe Blow" << "Bill Clinton"; - list7 = list7.filter( QStringView(QString("Bill")) ); - list8 << "Bill Gates" << "Bill Clinton"; - QCOMPARE( list7, list8 ); + { // CaseInsensitive + const QStringList expected = {u"Bill Gates"_s, u"Bill Clinton"_s, u"bIll"_s}; + QCOMPARE(list.filter(u"bill"_s, Qt::CaseInsensitive), expected); + QCOMPARE(list.filter(u"bill", Qt::CaseInsensitive), expected); + QCOMPARE(list.filter("bill"_L1, Qt::CaseInsensitive), expected); + QCOMPARE(list.filter(QRegularExpression(u"[i]ll"_s, QRegularExpression::CaseInsensitiveOption)), + expected); + QCOMPARE(list.filter(QStringMatcher(u"Bill", Qt::CaseInsensitive)), expected); + } } void tst_QStringList::sort() @@ -211,39 +249,50 @@ void tst_QStringList::replaceInStrings() list13.replaceInStrings( QString("a"), QStringView(QString("o")) ); list14 << "olpho" << "beto" << "gommo" << "epsilon"; QCOMPARE( list11, list12 ); + + QStringList list{"alpha", "beta", "gamma"}; + QStringList copy = list; + QVERIFY(!copy.isDetached()); + + // No matches, no detach + copy.replaceInStrings("z", "y"); + QVERIFY(!copy.isDetached()); + QCOMPARE(copy, list); + + copy.replaceInStrings("a", "y"); + QVERIFY(copy.isDetached()); + QCOMPARE(copy, (QStringList{"ylphy", "bety", "gymmy"})); +} + +void tst_QStringList::contains_data() +{ + QTest::addColumn<QString>("needle"); + QTest::addColumn<Qt::CaseSensitivity>("cs"); + QTest::addColumn<bool>("expected"); + + QTest::newRow("arthur") << u"arthur"_s << Qt::CaseSensitive << true; + QTest::newRow("ArthuR") << u"ArthuR"_s << Qt::CaseSensitive << false; + QTest::newRow("arthur") << u"arthur"_s << Qt::CaseInsensitive << true; + QTest::newRow("ArthuR") << u"ArthuR"_s << Qt::CaseInsensitive << true; + QTest::newRow("ARTHUR") << u"ARTHUR"_s << Qt::CaseInsensitive << true; + QTest::newRow("Hans") << u"Hans"_s << Qt::CaseSensitive << false; + QTest::newRow("hans") << u"hans"_s << Qt::CaseInsensitive << false; + QTest::newRow("dent") << u"dent"_s << Qt::CaseInsensitive << true; } void tst_QStringList::contains() { - QStringList list; - list << "arthur" << "Arthur" << "arthuR" << "ARTHUR" << "Dent" << "Hans Dent"; - - QVERIFY(list.contains("arthur")); - QVERIFY(!list.contains("ArthuR")); - QVERIFY(!list.contains("Hans")); - QVERIFY(list.contains("arthur", Qt::CaseInsensitive)); - QVERIFY(list.contains("ArthuR", Qt::CaseInsensitive)); - QVERIFY(list.contains("ARTHUR", Qt::CaseInsensitive)); - QVERIFY(list.contains("dent", Qt::CaseInsensitive)); - QVERIFY(!list.contains("hans", Qt::CaseInsensitive)); - - QVERIFY(list.contains(QLatin1String("arthur"))); - QVERIFY(!list.contains(QLatin1String("ArthuR"))); - QVERIFY(!list.contains(QLatin1String("Hans"))); - QVERIFY(list.contains(QLatin1String("arthur"), Qt::CaseInsensitive)); - QVERIFY(list.contains(QLatin1String("ArthuR"), Qt::CaseInsensitive)); - QVERIFY(list.contains(QLatin1String("ARTHUR"), Qt::CaseInsensitive)); - QVERIFY(list.contains(QLatin1String("dent"), Qt::CaseInsensitive)); - QVERIFY(!list.contains(QLatin1String("hans"), Qt::CaseInsensitive)); - - QVERIFY(list.contains(QStringView(QString("arthur")))); - QVERIFY(!list.contains(QStringView(QString("ArthuR")))); - QVERIFY(!list.contains(QStringView(QString("Hans")))); - QVERIFY(list.contains(QStringView(QString("arthur")), Qt::CaseInsensitive)); - QVERIFY(list.contains(QStringView(QString("ArthuR")), Qt::CaseInsensitive)); - QVERIFY(list.contains(QStringView(QString("ARTHUR")), Qt::CaseInsensitive)); - QVERIFY(list.contains(QStringView(QString("dent")), Qt::CaseInsensitive)); - QVERIFY(!list.contains(QStringView(QString("hans")), Qt::CaseInsensitive)); + QFETCH(QString, needle); + QFETCH(Qt::CaseSensitivity, cs); + QFETCH(bool, expected); + + const QStringList list = { + u"arthur"_s, u"Arthur"_s, u"arthuR"_s, u"ARTHUR"_s, u"Dent"_s, u"Hans Dent"_s + }; + + QCOMPARE(list.contains(needle, cs), expected); + QCOMPARE(list.contains(QStringView(needle), cs), expected); + QCOMPARE(list.contains(QLatin1StringView(needle.toLatin1()), cs), expected); } void tst_QStringList::removeDuplicates_data() |