From 1fceabe8c7b5e99509a67aec8303241b6ba4099a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Nowacki?= Date: Wed, 14 May 2014 16:56:10 +0200 Subject: Implement QStringRef::split [ChangeLog][QtCore] Added the QStringRef::split() function Change-Id: I28709c9761785dea7be4e7d621ecf4e1ae007a72 Reviewed-by: Thiago Macieira --- .../corelib/tools/qstringref/tst_qstringref.cpp | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) (limited to 'tests') diff --git a/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp b/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp index 342abb7ea8..fe5fe92872 100644 --- a/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp +++ b/tests/auto/corelib/tools/qstringref/tst_qstringref.cpp @@ -93,6 +93,8 @@ private slots: void left(); void right(); void mid(); + void split_data(); + void split(); }; static QStringRef emptyRef() @@ -1984,6 +1986,79 @@ void tst_QStringRef::mid() QVERIFY(emptyRef.mid(-10, 3).isEmpty()); } +static bool operator ==(const QStringList &left, const QVector &right) +{ + if (left.size() != right.size()) + return false; + + QStringList::const_iterator iLeft = left.constBegin(); + QVector::const_iterator iRight = right.constBegin(); + for (; iLeft != left.end(); ++iLeft, ++iRight) { + if (*iLeft != *iRight) + return false; + } + return true; +} +static inline bool operator ==(const QVector &left, const QStringList &right) { return right == left; } + +void tst_QStringRef::split_data() +{ + QTest::addColumn("str"); + QTest::addColumn("sep"); + QTest::addColumn("result"); + + QTest::newRow("a,b,c") << "a,b,c" << "," << (QStringList() << "a" << "b" << "c"); + QTest::newRow("a,b,c,a,b,c") << "a,b,c,a,b,c" << "," << (QStringList() << "a" << "b" << "c" << "a" << "b" << "c"); + QTest::newRow("a,b,c,,a,b,c") << "a,b,c,,a,b,c" << "," << (QStringList() << "a" << "b" << "c" << "" << "a" << "b" << "c"); + QTest::newRow("2") << QString("-rw-r--r-- 1 0 0 519240 Jul 9 2002 bigfile") + << " " + << (QStringList() << "-rw-r--r--" << "" << "1" << "0" << "" << "0" << "" + << "519240" << "Jul" << "" << "9" << "" << "2002" << "bigfile"); + QTest::newRow("one-empty") << "" << " " << (QStringList() << ""); + QTest::newRow("two-empty") << " " << " " << (QStringList() << "" << ""); + QTest::newRow("three-empty") << " " << " " << (QStringList() << "" << "" << ""); + + QTest::newRow("all-empty") << "" << "" << (QStringList() << "" << ""); + QTest::newRow("all-null") << QString() << QString() << (QStringList() << QString() << QString()); + QTest::newRow("sep-empty") << "abc" << "" << (QStringList() << "" << "a" << "b" << "c" << ""); +} + +void tst_QStringRef::split() +{ + QFETCH(QString, str); + QFETCH(QString, sep); + QFETCH(QStringList, result); + + QVector list; + // we construct a bigger valid string to check + // if ref.split is using the right size + QString source = str + str + str; + QStringRef ref = source.midRef(str.size(), str.size()); + QCOMPARE(ref.size(), str.size()); + + list = ref.split(sep); + QVERIFY(list == result); + if (sep.size() == 1) { + list = ref.split(sep.at(0)); + QVERIFY(list == result); + } + + list = ref.split(sep, QString::KeepEmptyParts); + QVERIFY(list == result); + if (sep.size() == 1) { + list = ref.split(sep.at(0), QString::KeepEmptyParts); + QVERIFY(list == result); + } + + result.removeAll(""); + list = ref.split(sep, QString::SkipEmptyParts); + QVERIFY(list == result); + if (sep.size() == 1) { + list = ref.split(sep.at(0), QString::SkipEmptyParts); + QVERIFY(list == result); + } +} + QTEST_APPLESS_MAIN(tst_QStringRef) #include "tst_qstringref.moc" -- cgit v1.2.3