summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorAhmad Samir <a.samirh78@gmail.com>2023-07-01 15:01:58 +0300
committerAhmad Samir <a.samirh78@gmail.com>2024-02-29 00:27:25 +0200
commit9bf68a47e1ba8790fca9d24bcb7b45cd56e79320 (patch)
tree7ce4b7644ce8e5e8d315e774d9bc37c8f19602e2 /tests
parentb2ec2e1137ceb0b83978a7fa35485b1b97c73648 (diff)
QString/QByteArray: add slice() methods
[ChangeLog][QtCore][QString/QByteArray] Added slice() methods that work like sliced(), but modify the string/byte-array they are called on. Task-number: QTBUG-99218 Change-Id: I3075562983ef123d9aa022a2304c7e774cf2ea42 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp27
-rw-r--r--tests/auto/corelib/text/qstring/tst_qstring.cpp24
-rw-r--r--tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp27
3 files changed, 78 insertions, 0 deletions
diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
index 1a947dfb4f..81d79da38b 100644
--- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
+++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
@@ -132,6 +132,7 @@ private slots:
void mid();
void length();
void length_data();
+ void slice() const;
};
static const QByteArray::DataPointer staticStandard = {
@@ -2890,5 +2891,31 @@ void tst_QByteArray::length_data()
QTest::newRow("with '\\0' no size") << QByteArray("abc\0def") << qsizetype(3);
}
+void tst_QByteArray::slice() const
+{
+ QByteArray a;
+
+ a.slice(0);
+ QVERIFY(a.isEmpty());
+ QVERIFY(a.isNull());
+ a.slice(0, 0);
+ QVERIFY(a.isEmpty());
+ QVERIFY(a.isNull());
+
+ a = "Five pineapples";
+
+ a.slice(5);
+ QCOMPARE_EQ(a, "pineapples");
+
+ a.slice(4, 3);
+ QCOMPARE_EQ(a, "app");
+
+ a.slice(a.size());
+ QVERIFY(a.isEmpty());
+
+ a.slice(0, 0);
+ QVERIFY(a.isEmpty());
+}
+
QTEST_MAIN(tst_QByteArray)
#include "tst_qbytearray.moc"
diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp
index 14447d6798..c888daaa57 100644
--- a/tests/auto/corelib/text/qstring/tst_qstring.cpp
+++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp
@@ -710,6 +710,7 @@ private slots:
void first();
void last();
void sliced();
+ void slice();
void chopped();
void removeIf();
@@ -9143,6 +9144,29 @@ void tst_QString::sliced()
QCOMPARE(a, u"ABCDEFGHIEfGEFG");
}
+void tst_QString::slice()
+{
+ QString a;
+
+ a.slice(0);
+ QVERIFY(a.isEmpty());
+ QVERIFY(a.isNull());
+ a.slice(0, 0);
+ QVERIFY(a.isEmpty());
+ QVERIFY(a.isNull());
+
+ a = u"Five pineapples"_s;
+
+ a.slice(5);
+ QCOMPARE_EQ(a, u"pineapples");
+
+ a.slice(4, 3);
+ QCOMPARE_EQ(a, u"app");
+
+ a.slice(a.size());
+ QVERIFY(a.isEmpty());
+}
+
void tst_QString::chopped()
{
QString a;
diff --git a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp
index 927af3a47e..443ea4ce64 100644
--- a/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp
+++ b/tests/auto/corelib/text/qstringapisymmetry/tst_qstringapisymmetry.cpp
@@ -694,6 +694,7 @@ private:
void sliced_data();
template <typename String> void sliced_impl();
+ template <typename String> void slice_impl();
void first_data();
template <typename String> void first_impl();
@@ -782,6 +783,11 @@ private Q_SLOTS:
void sliced_QByteArrayView_data() { sliced_data(); }
void sliced_QByteArrayView() { sliced_impl<QByteArrayView>(); }
+ void slice_QString_data() { sliced_data(); }
+ void slice_QString() { slice_impl<QString>(); }
+ void slice_QByteArray_data() { sliced_data(); }
+ void slice_QByteArray() { slice_impl<QByteArray>(); }
+
void first_truncate_QString_data() { first_data(); }
void first_truncate_QString() { first_impl<QString>(); }
void first_truncate_QStringView_data() { first_data(); }
@@ -2426,6 +2432,27 @@ void tst_QStringApiSymmetry::sliced_impl()
}
}
+template <typename String>
+void tst_QStringApiSymmetry::slice_impl()
+{
+ QFETCH(const QStringView, unicode);
+ QFETCH(const QLatin1String, latin1);
+ QFETCH(const int, pos);
+ QFETCH(const int, n);
+ QFETCH(const QAnyStringView, result);
+ QFETCH(const QAnyStringView, result2);
+
+ const auto str = make<String>(unicode, latin1, unicode.toUtf8());
+
+ auto s = str;
+ s.slice(pos);
+ QCOMPARE_EQ(s, result);
+
+ s = str;
+ s.slice(pos, n);
+ QCOMPARE_EQ(s, result2);
+}
+
void tst_QStringApiSymmetry::first_data()
{
QTest::addColumn<QStringView>("unicode");