summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-05-22 09:57:33 +0200
committerLars Knoll <lars.knoll@qt.io>2020-05-30 23:11:09 +0200
commita1056096fc5ec316a650fd8ae1c2a22b6944d091 (patch)
tree8586103ce9378295addf3f7d3fc0beae2d9e6664 /tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp
parent2b7b75f721b6786a6dc35e2f9b693bb2e2dfac01 (diff)
Add support for count() to QStringView
Make the API more symmetric with regards to both QString and QStringRef. Change-Id: Ia67c53ba708f6c33874d1a127de8e2857ad9b5b8 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp')
-rw-r--r--tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp51
1 files changed, 51 insertions, 0 deletions
diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp
index 0fdad63b24..a55f2f13eb 100644
--- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp
+++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp
@@ -603,6 +603,20 @@ private Q_SLOTS:
void toNumber_QByteArray_data() { toNumber_data(); }
void toNumber_QByteArray() { toNumber_impl<QByteArray>(); }
+private:
+ void count_data();
+ template <typename String> void count_impl();
+
+private Q_SLOTS:
+ void count_QString_data() { count_data(); }
+ void count_QString() { count_impl<QString>(); }
+ void count_QStringRef_data() { count_data(); }
+ void count_QStringRef() { count_impl<QStringRef>(); }
+ void count_QStringView_data() { count_data(); }
+ void count_QStringView() { count_impl<QStringView>(); }
+ void count_QByteArray_data() { count_data(); }
+ void count_QByteArray() { count_impl<QByteArray>(); }
+
//
// UTF-16-only checks:
//
@@ -1713,6 +1727,43 @@ void tst_QStringApiSymmetry::toNumber_impl()
}
}
+
+void tst_QStringApiSymmetry::count_data()
+{
+ QTest::addColumn<QString>("data");
+ QTest::addColumn<QString>("needle");
+ QTest::addColumn<qsizetype>("result");
+
+ QTest::addRow("xxx") << QString::fromUtf8("xxx") << QString::fromUtf8("x") << qsizetype(3);
+ QTest::addRow("xyzaaaxyz") << QString::fromUtf8("xyzaaaxyz") << QString::fromUtf8("xyz") << qsizetype(2);
+}
+
+template <typename String>
+void tst_QStringApiSymmetry::count_impl()
+{
+ QFETCH(const QString, data);
+ QFETCH(const QString, needle);
+ QFETCH(qsizetype, result);
+
+ const auto utf8 = data.toUtf8();
+ const auto l1s = data.toLatin1();
+ const auto l1 = l1s.isNull() ? QLatin1String() : QLatin1String(l1s);
+
+ const auto ref = data.isNull() ? QStringRef() : QStringRef(&data);
+ const auto s = make<String>(ref, l1, utf8);
+
+ const auto nutf8 = needle.toUtf8();
+ const auto nl1s = needle.toLatin1();
+ const auto nl1 = nl1s.isNull() ? QLatin1String() : QLatin1String(l1s);
+
+ const auto nref = needle.isNull() ? QStringRef() : QStringRef(&needle);
+ const auto ns = make<String>(nref, nl1, nutf8);
+
+ QCOMPARE(s.count(ns), result);
+ if (ns.length() == 1)
+ QCOMPARE(s.count(ns.data()[0]), result);
+}
+
//
//
// UTF-16-only checks: