From 358e13a5e1829afdb65c27fd5b2157df5ad222c6 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Sun, 16 Jul 2023 23:40:40 +0300 Subject: [PATCH] QByteArray: add trimmed() unittest Change-Id: Ib0e808ea45bb68fb0ec79da2f32959677eae988f Reviewed-by: Thiago Macieira --- .../text/qbytearray/tst_qbytearray.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp index 617737d9ed..890cb8a565 100644 --- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp @@ -122,6 +122,8 @@ private slots: void fill(); void dataPointers(); void truncate(); + void trimmed_data(); + void trimmed(); void simplified(); void simplified_data(); void left(); @@ -2648,6 +2650,39 @@ void tst_QByteArray::truncate() QVERIFY(a.isEmpty()); } +void tst_QByteArray::trimmed_data() +{ + QTest::addColumn("full" ); + QTest::addColumn("trimmed" ); + + QTest::addRow("null") << QByteArray() << QByteArray(); + QTest::addRow("simple") << "Text"_ba << "Text"_ba; + QTest::addRow("single-space") << " "_ba << ""_ba; + QTest::addRow("single-char") << " a "_ba << "a"_ba; + QTest::addRow("mixed") << " a \n\t\v b "_ba << "a \n\t\v b"_ba; +} + +void tst_QByteArray::trimmed() +{ + QFETCH(QByteArray, full); + QFETCH(QByteArray, trimmed); + + // Shared + if (!full.isNull()) + QVERIFY(!full.isDetached()); + QCOMPARE(full.trimmed(), trimmed); // lvalue + QCOMPARE(QByteArray(full).trimmed(), trimmed); // rvalue + QCOMPARE(full.isNull(), trimmed.isNull()); + + // Not shared + full = QByteArrayView(full).toByteArray(); + if (!full.isNull()) + QVERIFY(full.isDetached()); + QCOMPARE(full.trimmed(), trimmed); // lvalue + QCOMPARE(QByteArray(full).trimmed(), trimmed); // rvalue + QCOMPARE(full.isNull(), trimmed.isNull()); +} + void tst_QByteArray::simplified() { QFETCH(QByteArray, source);