diff options
Diffstat (limited to 'tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp')
-rw-r--r-- | tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp | 632 |
1 files changed, 471 insertions, 161 deletions
diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp index ff3feb33db..81d79da38b 100644 --- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp @@ -1,6 +1,6 @@ // Copyright (C) 2022 The Qt Company Ltd. // Copyright (C) 2016 Intel Corporation. -// 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> @@ -12,8 +12,10 @@ #include "../shared/test_number_shared.h" -#include <stdexcept> -#include <string_view> +#include <QtCore/q20iterator.h> +#include <sstream> + +using namespace Qt::StringLiterals; class tst_QByteArray : public QObject { @@ -26,13 +28,6 @@ private slots: void swap(); void qChecksum_data(); void qChecksum(); -#ifndef QT_NO_COMPRESS - void qCompress_data(); - void qCompress(); - void qUncompressCorruptedData_data(); - void qUncompressCorruptedData(); - void qCompressionZeroTermination(); -#endif void constByteArray(); void leftJustified(); void rightJustified(); @@ -43,7 +38,6 @@ private slots: void split(); void base64_data(); void base64(); - void base64_2GiB(); void fromBase64_data(); void fromBase64(); void qvsnprintf(); @@ -57,14 +51,22 @@ private slots: void prependExtended_data(); void prependExtended(); void append(); + void appendFromRawData(); void appendExtended_data(); void appendExtended(); + void appendEmptyNull(); + void assign(); + void assignShared(); + void assignUsesPrependBuffer(); void insert(); void insertExtended_data(); void insertExtended(); void remove_data(); void remove(); + void remove_extra(); void removeIf(); + void erase(); + void erase_single_arg(); void replace_data(); void replace(); void replaceWithSpecifiedLength(); @@ -78,7 +80,6 @@ private slots: void blockSizeCalculations(); void resizeAfterFromRawData(); - void appendAfterFromRawData(); void toFromHex_data(); void toFromHex(); void toFromPercentEncoding(); @@ -105,8 +106,8 @@ private slots: void reserveExtended_data(); void reserveExtended(); void resize(); - void movablity_data(); - void movablity(); + void movability_data(); + void movability(); void literals(); void userDefinedLiterals(); void toUpperLower_data(); @@ -122,6 +123,8 @@ private slots: void fill(); void dataPointers(); void truncate(); + void trimmed_data(); + void trimmed(); void simplified(); void simplified_data(); void left(); @@ -129,6 +132,7 @@ private slots: void mid(); void length(); void length_data(); + void slice() const; }; static const QByteArray::DataPointer staticStandard = { @@ -142,6 +146,15 @@ static const QByteArray::DataPointer staticNotNullTerminated = { 4 }; +template <typename String> String detached(String s) +{ + if (!s.isNull()) { // detaching loses nullness, but we need to preserve it + auto d = s.data(); + Q_UNUSED(d); + } + return s; +} + template <class T> const T &verifyZeroTermination(const T &t) { return t; } QByteArray verifyZeroTermination(const QByteArray &ba) @@ -228,86 +241,13 @@ void tst_QByteArray::qChecksum() QFETCH(Qt::ChecksumType, standard); QFETCH(uint, checksum); - QCOMPARE(data.length(), int(len)); + QCOMPARE(data.size(), int(len)); if (standard == Qt::ChecksumIso3309) { QCOMPARE(::qChecksum(QByteArrayView(data.constData(), len)), static_cast<quint16>(checksum)); } QCOMPARE(::qChecksum(QByteArrayView(data.constData(), len), standard), static_cast<quint16>(checksum)); } -#ifndef QT_NO_COMPRESS -void tst_QByteArray::qCompress_data() -{ - QTest::addColumn<QByteArray>("ba"); - - const int size1 = 1024*1024; - QByteArray ba1( size1, 0 ); - - QTest::newRow( "00" ) << QByteArray(); - - int i; - for ( i=0; i<size1; i++ ) - ba1[i] = (char)( i / 1024 ); - QTest::newRow( "01" ) << ba1; - - for ( i=0; i<size1; i++ ) - ba1[i] = (char)( i % 256 ); - QTest::newRow( "02" ) << ba1; - - ba1.fill( 'A' ); - QTest::newRow( "03" ) << ba1; - - QFile file( QFINDTESTDATA("rfc3252.txt") ); - QVERIFY( file.open(QIODevice::ReadOnly) ); - QTest::newRow( "04" ) << file.readAll(); -} - -void tst_QByteArray::qCompress() -{ - QFETCH( QByteArray, ba ); - QByteArray compressed = ::qCompress( ba ); - QTEST( ::qUncompress( compressed ), "ba" ); -} - -void tst_QByteArray::qUncompressCorruptedData_data() -{ - QTest::addColumn<QByteArray>("in"); - - QTest::newRow("0x00000000") << QByteArray("\x00\x00\x00\x00", 4); - QTest::newRow("0x000000ff") << QByteArray("\x00\x00\x00\xff", 4); - QTest::newRow("0x3f000000") << QByteArray("\x3f\x00\x00\x00", 4); - QTest::newRow("0x3fffffff") << QByteArray("\x3f\xff\xff\xff", 4); - QTest::newRow("0x7fffff00") << QByteArray("\x7f\xff\xff\x00", 4); - QTest::newRow("0x7fffffff") << QByteArray("\x7f\xff\xff\xff", 4); - QTest::newRow("0x80000000") << QByteArray("\x80\x00\x00\x00", 4); - QTest::newRow("0x800000ff") << QByteArray("\x80\x00\x00\xff", 4); - QTest::newRow("0xcf000000") << QByteArray("\xcf\x00\x00\x00", 4); - QTest::newRow("0xcfffffff") << QByteArray("\xcf\xff\xff\xff", 4); - QTest::newRow("0xffffff00") << QByteArray("\xff\xff\xff\x00", 4); - QTest::newRow("0xffffffff") << QByteArray("\xff\xff\xff\xff", 4); -} - -// This test is expected to produce some warning messages in the test output. -void tst_QByteArray::qUncompressCorruptedData() -{ - QFETCH(QByteArray, in); - - QByteArray res; - res = ::qUncompress(in); - QCOMPARE(res, QByteArray()); - - res = ::qUncompress(in + "blah"); - QCOMPARE(res, QByteArray()); -} - -void tst_QByteArray::qCompressionZeroTermination() -{ - QByteArray s = "Hello, I'm a string."; - QByteArray ba = ::qUncompress(::qCompress(s)); - QCOMPARE(ba.data()[ba.size()], '\0'); - QCOMPARE(ba, s); -} -#endif void tst_QByteArray::constByteArray() { @@ -498,7 +438,7 @@ void tst_QByteArray::split() QFETCH(int, size); QList<QByteArray> list = sample.split(' '); - QCOMPARE(list.count(), size); + QCOMPARE(list.size(), size); } void tst_QByteArray::swap() @@ -579,45 +519,6 @@ void tst_QByteArray::base64() QCOMPARE(arr64, base64urlnoequals); } -void tst_QByteArray::base64_2GiB() -{ -#ifdef Q_OS_ANDROID - QSKIP("Android kills the test when using too much memory"); -#endif - if constexpr (sizeof(qsizetype) > sizeof(int)) { - try { - constexpr qint64 GiB = 1024 * 1024 * 1024; - static_assert((2 * GiB + 1) % 3 == 0); - const char inputChar = '\0'; // all-NULs encode as - const char outputChar = 'A'; // all-'A's - const qint64 inputSize = 2 * GiB + 1; - const qint64 outputSize = inputSize / 3 * 4; - const auto sv = [](const QByteArray &ba) { - return std::string_view{ba.data(), size_t(ba.size())}; - }; - QByteArray output; - { - const QByteArray input(inputSize, inputChar); - output = input.toBase64(); - QCOMPARE(output.size(), outputSize); - QCOMPARE(sv(output).find_first_not_of(outputChar), - std::string_view::npos); - } - { - auto r = QByteArray::fromBase64Encoding(output); - QCOMPARE_EQ(r.decodingStatus, QByteArray::Base64DecodingStatus::Ok); - QCOMPARE(r.decoded.size(), inputSize); - QCOMPARE(sv(r.decoded).find_first_not_of(inputChar), - std::string_view::npos); - } - } catch (const std::bad_alloc &) { - QSKIP("Could not allocate enough RAM."); - } - } else { - QSKIP("This is a 64-bit only test"); - } -} - //different from the previous test as the input are invalid void tst_QByteArray::fromBase64_data() { @@ -860,7 +761,10 @@ void tst_QByteArray::qstrncpy() // src == nullptr QCOMPARE(::qstrncpy(dst.data(), 0, 0), (char*)0); + QCOMPARE(*dst.data(), 'b'); // must not have written to dst QCOMPARE(::qstrncpy(dst.data(), 0, 10), (char*)0); + QCOMPARE(*dst.data(), '\0'); // must have written to dst + *dst.data() = 'b'; // restore // valid pointers, but len == 0 QCOMPARE(::qstrncpy(dst.data(), src.data(), 0), dst.data()); @@ -1021,6 +925,20 @@ void tst_QByteArray::append() } } +void tst_QByteArray::appendFromRawData() +{ + char rawData[] = "Hello World!"; + QByteArray ba = QByteArray::fromRawData(rawData, std::size(rawData) - 1); + + QByteArray copy; + copy.append(ba); + QCOMPARE(copy, ba); + // We make an _actual_ copy, because appending a byte array + // created with fromRawData() might be optimized to copy the DataPointer, + // which means we may point to temporary stack data. + QCOMPARE_NE((void *)copy.constData(), (void *)ba.constData()); +} + void tst_QByteArray::appendExtended_data() { prependExtended_data(); @@ -1045,6 +963,207 @@ void tst_QByteArray::appendExtended() QCOMPARE(array.size(), 11); } +void tst_QByteArray::appendEmptyNull() +{ + QByteArray a; + QVERIFY(a.isEmpty()); + QVERIFY(a.isNull()); + + QByteArray b(""); + QVERIFY(b.isEmpty()); + QVERIFY(!b.isNull()); + + // Concatenating a null and an empty-but-not-null byte arrays results in + // an empty but not null byte array + QByteArray r = a + b; + QVERIFY(r.isEmpty()); + QVERIFY(!r.isNull()); +} + +void tst_QByteArray::assign() +{ + // QByteArray &assign(QByteArrayView) + { + QByteArray ba; + QByteArray test("data"); + QCOMPARE(ba.assign(test), test); + QCOMPARE(ba.size(), test.size()); + test = "data\0data"; + QCOMPARE(ba.assign(test), test); + QCOMPARE(ba.size(), test.size()); + test = "data\0data"_ba; + QCOMPARE(ba.assign(test), test); + QCOMPARE(ba.size(), test.size()); + } + // QByteArray &assign(qsizetype, char); + { + QByteArray ba; + QByteArray test("ddd"); + QCOMPARE(ba.assign(3, 'd'), test); + QCOMPARE(ba.size(), test.size()); + test = "xx"; + QCOMPARE(ba.assign(20, 'd').assign(2, 'x'), test); + QCOMPARE(ba.size(), test.size()); + test = "ddddd"; + QCOMPARE(ba.assign(0, 'x').assign(5, 'd'), test); + QCOMPARE(ba.size(), test.size()); + test = "\0\0\0"_ba; + QCOMPARE(ba.assign(0, 'x').assign(3, '\0'), test); + QCOMPARE(ba.size(), test.size()); + } + // QByteArray &assign(InputIterator, InputIterator) + { + QByteArray ba; + QByteArrayView test; + + QList<char> l = {'\0', 'T', 'E', 'S', 'T'}; + ba.assign(l.begin(), l.end()); + test = "\0TEST"_ba; + QCOMPARE(ba, test); + QCOMPARE(ba.size(), test.size()); + + const std::byte bytes[] = {std::byte('T'), std::byte(0), std::byte('S'), std::byte('T')}; + test = QByteArrayView::fromArray(bytes); + QCOMPARE(ba.assign(test.begin(), test.end()), test); + QCOMPARE(ba.size(), test.size()); + + std::stringstream ss; + ss << "T " << '\0' << ' ' << "S " << "T "; + ba.assign(std::istream_iterator<char>{ss}, std::istream_iterator<char>{}); + test = "T\0ST"_ba; + QCOMPARE(ba, test); + QCOMPARE(ba.size(), test.size()); + } + // Test chaining + { + QByteArray ba; + QByteArray test("TTTTT"); + char arr[] = {'T', 'E', 'S', 'T'}; + ba.assign(std::begin(arr), std::end(arr)).assign({"Hello World!"}).assign(5, 'T'); + QCOMPARE(ba, test); + QCOMPARE(ba.size(), test.size()); + test = "DATA"; + QCOMPARE(ba.assign(300, 'T').assign({"DATA"}), test); + QCOMPARE(ba.size(), test.size()); + test = QByteArray(arr, q20::ssize(arr)); + QCOMPARE(ba.assign(10, 'c').assign(std::begin(arr), std::end(arr)), test); + QCOMPARE(ba.size(), test.size()); + test = "TTT"; + QCOMPARE(ba.assign("data").assign(QByteArrayView::fromArray( + {std::byte('T'), std::byte('T'), std::byte('T')})), test); + QCOMPARE(ba.size(), test.size()); + test = "\0data"; + QCOMPARE(ba.assign("data").assign("\0data"), test); + QCOMPARE(ba.size(), test.size()); + } +} + +void tst_QByteArray::assignShared() +{ + { + QByteArray ba; + ba.assign({"DATA"}); + QVERIFY(ba.isDetached()); + QCOMPARE(ba, QByteArray("DATA")); + + auto baCopy = ba; + QVERIFY(!ba.isDetached()); + QVERIFY(!baCopy.isDetached()); + QVERIFY(ba.isSharedWith(baCopy)); + QVERIFY(baCopy.isSharedWith(ba)); + + ba.assign(10, 'D'); + QVERIFY(ba.isDetached()); + QVERIFY(baCopy.isDetached()); + QVERIFY(!ba.isSharedWith(baCopy)); + QVERIFY(!baCopy.isSharedWith(ba)); + QCOMPARE(ba, QByteArray("DDDDDDDDDD")); + QCOMPARE(baCopy, QByteArray("DATA")); + } + { + QByteArray ba("START"); + QByteArrayView bav("DATA"); + QVERIFY(ba.isDetached()); + QCOMPARE(ba, QByteArray("START")); + + auto copyForwardIt = ba; + QVERIFY(!ba.isDetached()); + QVERIFY(!copyForwardIt.isDetached()); + QVERIFY(ba.isSharedWith(copyForwardIt)); + QVERIFY(copyForwardIt.isSharedWith(ba)); + + ba.assign(bav.begin(), bav.end()); + QVERIFY(ba.isDetached()); + QVERIFY(copyForwardIt.isDetached()); + QVERIFY(!ba.isSharedWith(copyForwardIt)); + QVERIFY(!copyForwardIt.isSharedWith(ba)); + QCOMPARE(ba, QByteArray("DATA")); + QCOMPARE(copyForwardIt, QByteArray("START")); + + auto copyInputIt = ba; + QVERIFY(!ba.isDetached()); + QVERIFY(!copyInputIt.isDetached()); + QVERIFY(ba.isSharedWith(copyInputIt)); + QVERIFY(copyInputIt.isSharedWith(ba)); + + std::stringstream ss("1 2 3 4 5 6 "); + ba.assign(std::istream_iterator<char>{ss}, std::istream_iterator<char>{}); + QVERIFY(ba.isDetached()); + QVERIFY(copyInputIt.isDetached()); + QVERIFY(!ba.isSharedWith(copyInputIt)); + QVERIFY(!copyInputIt.isSharedWith(ba)); + QCOMPARE(ba, QByteArray("123456")); + QCOMPARE(copyInputIt, QByteArray("DATA")); + } +} + +void tst_QByteArray::assignUsesPrependBuffer() +{ + const auto capBegin = [](const QByteArray &ba) { + return ba.begin() - ba.d.freeSpaceAtBegin(); + }; + const auto capEnd = [](const QByteArray &ba) { + return ba.end() + ba.d.freeSpaceAtEnd(); + }; + // QByteArray &assign(QByteArrayView) + { + QByteArray withFreeSpaceAtBegin; + for (int i = 0; i < 100 && withFreeSpaceAtBegin.d.freeSpaceAtBegin() < 2; ++i) + withFreeSpaceAtBegin.prepend("data"); + QCOMPARE_GT(withFreeSpaceAtBegin.d.freeSpaceAtBegin(), 1); + + const auto oldCapBegin = capBegin(withFreeSpaceAtBegin); + const auto oldCapEnd = capEnd(withFreeSpaceAtBegin); + + std::string test(withFreeSpaceAtBegin.d.freeSpaceAtBegin(), 'd'); + withFreeSpaceAtBegin.assign(test); + + QCOMPARE_EQ(withFreeSpaceAtBegin.d.freeSpaceAtBegin(), 0); // we used the prepend buffer + QCOMPARE_EQ(capBegin(withFreeSpaceAtBegin), oldCapBegin); + QCOMPARE_EQ(capEnd(withFreeSpaceAtBegin), oldCapEnd); + QCOMPARE(withFreeSpaceAtBegin, test.data()); + } + // QByteArray &assign(InputIterator, InputIterator) + { + QByteArray withFreeSpaceAtBegin; + for (int i = 0; i < 100 && withFreeSpaceAtBegin.d.freeSpaceAtBegin() < 2; ++i) + withFreeSpaceAtBegin.prepend("data"); + QCOMPARE_GT(withFreeSpaceAtBegin.d.freeSpaceAtBegin(), 1); + + const auto oldCapBegin = capBegin(withFreeSpaceAtBegin); + const auto oldCapEnd = capEnd(withFreeSpaceAtBegin); + + std::stringstream ss; + for (qsizetype i = 0; i < withFreeSpaceAtBegin.d.freeSpaceAtBegin(); ++i) + ss << "d "; + + withFreeSpaceAtBegin.assign(std::istream_iterator<char>{ss}, std::istream_iterator<char>{}); + QCOMPARE_EQ(withFreeSpaceAtBegin.d.freeSpaceAtBegin(), 0); // we used the prepend buffer + QCOMPARE_EQ(capBegin(withFreeSpaceAtBegin), oldCapBegin); + QCOMPARE_EQ(capEnd(withFreeSpaceAtBegin), oldCapEnd); + } +} + void tst_QByteArray::insert() { const char data[] = "data"; @@ -1181,7 +1300,30 @@ void tst_QByteArray::remove() QFETCH(int, position); QFETCH(int, length); QFETCH(QByteArray, expected); - QCOMPARE(src.remove(position, length), expected); + // Test when it's shared + QByteArray ba1 = src; + QCOMPARE(ba1.remove(position, length), expected); + + // Test when it's not shared + QByteArray ba2 = src; + ba2.detach(); + QCOMPARE(ba2.remove(position, length), expected); +} + +void tst_QByteArray::remove_extra() +{ + QByteArray ba = "Clock"; + ba.removeFirst(); + QCOMPARE(ba, "lock"); + ba.removeLast(); + QCOMPARE(ba, "loc"); + ba.removeAt(ba.indexOf('o')); + QCOMPARE(ba, "lc"); + ba.clear(); + // No crash on empty byte arrays + ba.removeFirst(); + ba.removeLast(); + ba.removeAt(2); } void tst_QByteArray::removeIf() @@ -1193,7 +1335,58 @@ void tst_QByteArray::removeIf() QVERIFY(!a.isDetached()); a = QByteArray("aBcAbC"); + // Test when it's not shared + QVERIFY(a.isDetached()); QCOMPARE(a.removeIf(removeA), QByteArray("BcbC")); + + a = QByteArray("aBcAbC"); + QByteArray b = a; + // Test when it's shared + QVERIFY(!b.isDetached()); + QCOMPARE(b.removeIf(removeA), QByteArray("BcbC")); +} + +void tst_QByteArray::erase() +{ + { + QByteArray ba = "kittens"; + auto it = ba.erase(ba.cbegin(), ba.cbegin() + 2); + QCOMPARE(ba, "ttens"); + QCOMPARE(it, ba.cbegin()); + } + + { + QByteArray ba = "kittens"; + auto it = ba.erase(ba.cbegin(), ba.cend()); + QCOMPARE(ba, ""); + QCOMPARE(it, ba.cbegin()); + QCOMPARE(ba.cbegin(), ba.cend()); + } + + { + QByteArray ba = "kite"; + auto it = ba.erase(ba.cbegin(), ba.cbegin()); + // erase() should return an iterator (not const_iterator) + *it = 'Z'; + QCOMPARE(ba, "Zite"); + QCOMPARE(it, ba.cbegin()); + } +} + +void tst_QByteArray::erase_single_arg() +{ + QByteArray ba = "abcdefg"; + ba.erase(ba.cend()); + auto it = ba.erase(ba.cbegin()); + QCOMPARE_EQ(ba, "bcdefg"); + QCOMPARE(it, ba.cbegin()); + + it = ba.erase(std::prev(ba.end())); + QCOMPARE_EQ(ba, "bcdef"); + QCOMPARE(it, ba.cend()); + + it = ba.erase(std::find(ba.begin(), ba.end(), QChar('d'))); + QCOMPARE(it, ba.begin() + 2); } void tst_QByteArray::replace_data() @@ -1310,7 +1503,7 @@ void tst_QByteArray::number_double_data() QTest::addRow("%s, format '%c', precision %d", title, datum.f, datum.p) << datum.d << datum.f << datum.p << ba; if (datum.f != 'f') { // Also test uppercase format - datum.f = toupper(datum.f); + datum.f = QtMiscUtils::toAsciiUpper(datum.f); QByteArray upper = ba.toUpper(); QByteArray upperTitle = QByteArray(title); if (!datum.optTitle.isEmpty()) @@ -1519,17 +1712,6 @@ void tst_QByteArray::resizeAfterFromRawData() QVERIFY(array.constData()[5] == 0); } -void tst_QByteArray::appendAfterFromRawData() -{ - QByteArray arr; - { - char data[] = "X"; - arr += QByteArray::fromRawData(data, sizeof(data)); - data[0] = 'Y'; - } - QCOMPARE(arr.at(0), 'X'); -} - void tst_QByteArray::toFromHex_data() { QTest::addColumn<QByteArray>("str"); @@ -1609,7 +1791,7 @@ void tst_QByteArray::toFromHex_data() << QByteArray("af") << QByteArray("xaf"); - QTest::newRow("no-leading-zero") + QTest::newRow("no-leading-zero-long") << QByteArray("\xd\xde\xad\xc0\xde") << '\0' << QByteArray("0ddeadc0de") @@ -2127,9 +2309,9 @@ void tst_QByteArray::resize() QCOMPARE(ba, "aaaaabbbbb"); } -void tst_QByteArray::movablity_data() +void tst_QByteArray::movability_data() { - QTest::addColumn<QByteArray>("array"); + prependExtended_data(); QTest::newRow("0x00000000") << QByteArray("\x00\x00\x00\x00", 4); QTest::newRow("0x000000ff") << QByteArray("\x00\x00\x00\xff", 4); @@ -2137,11 +2319,9 @@ void tst_QByteArray::movablity_data() QTest::newRow("empty") << QByteArray(""); QTest::newRow("null") << QByteArray(); QTest::newRow("sss") << QByteArray(3, 's'); - - prependExtended_data(); } -void tst_QByteArray::movablity() +void tst_QByteArray::movability() { QFETCH(QByteArray, array); @@ -2212,7 +2392,7 @@ void tst_QByteArray::literals() { QByteArray str(QByteArrayLiteral("abcd")); - QVERIFY(str.length() == 4); + QVERIFY(str.size() == 4); QCOMPARE(str.capacity(), 0); QVERIFY(str == "abcd"); QVERIFY(!str.data_ptr()->isMutable()); @@ -2224,20 +2404,19 @@ void tst_QByteArray::literals() // detach on non const access QVERIFY(str.data() != s); - QVERIFY(str.capacity() >= str.length()); + QVERIFY(str.capacity() >= str.size()); QVERIFY(str2.constData() == s); QVERIFY(str2.data() != s); - QVERIFY(str2.capacity() >= str2.length()); + QVERIFY(str2.capacity() >= str2.size()); } void tst_QByteArray::userDefinedLiterals() { { - using namespace Qt::StringLiterals; QByteArray str = "abcd"_ba; - QVERIFY(str.length() == 4); + QVERIFY(str.size() == 4); QCOMPARE(str.capacity(), 0); QVERIFY(str == "abcd"); QVERIFY(!str.data_ptr()->isMutable()); @@ -2249,18 +2428,18 @@ void tst_QByteArray::userDefinedLiterals() // detach on non const access QVERIFY(str.data() != s); - QVERIFY(str.capacity() >= str.length()); + QVERIFY(str.capacity() >= str.size()); QVERIFY(str2.constData() == s); QVERIFY(str2.data() != s); - QVERIFY(str2.capacity() >= str2.length()); + QVERIFY(str2.capacity() >= str2.size()); } #if QT_DEPRECATED_SINCE(6, 8) { QT_IGNORE_DEPRECATIONS(QByteArray str = "abcd"_qba;) - QVERIFY(str.length() == 4); + QVERIFY(str.size() == 4); QCOMPARE(str.capacity(), 0); QVERIFY(str == "abcd"); QVERIFY(!str.data_ptr()->isMutable()); @@ -2272,11 +2451,11 @@ void tst_QByteArray::userDefinedLiterals() // detach on non const access QVERIFY(str.data() != s); - QVERIFY(str.capacity() >= str.length()); + QVERIFY(str.capacity() >= str.size()); QVERIFY(str2.constData() == s); QVERIFY(str2.data() != s); - QVERIFY(str2.capacity() >= str2.length()); + QVERIFY(str2.capacity() >= str2.size()); } #endif // QT_DEPRECATED_SINCE(6, 8) } @@ -2394,7 +2573,7 @@ void tst_QByteArray::isLower() void tst_QByteArray::macTypes() { -#ifndef Q_OS_MAC +#ifndef Q_OS_DARWIN QSKIP("This is a Apple-only test"); #else extern void tst_QByteArray_macTypes(); // in qbytearray_mac.mm @@ -2407,7 +2586,7 @@ void tst_QByteArray::stdString() std::string stdstr( "QByteArray" ); const QByteArray stlqt = QByteArray::fromStdString(stdstr); - QCOMPARE(stlqt.length(), int(stdstr.length())); + QCOMPARE(stlqt.size(), int(stdstr.length())); QCOMPARE(stlqt.data(), stdstr.c_str()); QCOMPARE(stlqt.toStdString(), stdstr); @@ -2502,6 +2681,39 @@ void tst_QByteArray::truncate() QVERIFY(a.isEmpty()); } +void tst_QByteArray::trimmed_data() +{ + QTest::addColumn<QByteArray>("full" ); + QTest::addColumn<QByteArray>("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); @@ -2531,45 +2743,99 @@ void tst_QByteArray::simplified_data() void tst_QByteArray::left() { QByteArray a; + QCOMPARE(QByteArray().left(0), QByteArray()); + QCOMPARE(QByteArray().left(10), QByteArray()); QCOMPARE(a.left(0), QByteArray()); QCOMPARE(a.left(10), QByteArray()); QVERIFY(!a.isDetached()); + QCOMPARE(QByteArray(a).left(0), QByteArray()); + QCOMPARE(QByteArray(a).left(10), QByteArray()); + QCOMPARE(detached(a).left(0), QByteArray()); + QCOMPARE(detached(a).left(10), QByteArray()); a = QByteArray("abcdefgh"); const char *ptr = a.constData(); + + // lvalue QCOMPARE(a.left(5), QByteArray("abcde")); QCOMPARE(a.left(20), a); QCOMPARE(a.left(-5), QByteArray()); // calling left() does not modify the source array QCOMPARE(a.constData(), ptr); + + // rvalue, not detached + QCOMPARE(QByteArray(a).left(5), QByteArray("abcde")); + QCOMPARE(QByteArray(a).left(20), a); + QCOMPARE(QByteArray(a).left(-5), QByteArray()); + // calling left() does not modify the source array + QCOMPARE(a.constData(), ptr); + + // rvalue, detached + QCOMPARE(detached(a).left(5), QByteArray("abcde")); + QCOMPARE(detached(a).left(20), a); + QCOMPARE(detached(a).left(-5), QByteArray()); + // calling left() does not modify the source array + QCOMPARE(a.constData(), ptr); } void tst_QByteArray::right() { QByteArray a; + QCOMPARE(QByteArray().right(0), QByteArray()); + QCOMPARE(QByteArray().right(10), QByteArray()); QCOMPARE(a.right(0), QByteArray()); QCOMPARE(a.right(10), QByteArray()); QVERIFY(!a.isDetached()); + QCOMPARE(QByteArray(a).right(0), QByteArray()); + QCOMPARE(QByteArray(a).right(10), QByteArray()); + QCOMPARE(detached(a).right(0), QByteArray()); + QCOMPARE(detached(a).right(10), QByteArray()); a = QByteArray("abcdefgh"); const char *ptr = a.constData(); + + // lvalue QCOMPARE(a.right(5), QByteArray("defgh")); QCOMPARE(a.right(20), a); QCOMPARE(a.right(-5), QByteArray()); // calling right() does not modify the source array QCOMPARE(a.constData(), ptr); + + // rvalue, not detached + QCOMPARE(QByteArray(a).right(5), QByteArray("defgh")); + QCOMPARE(QByteArray(a).right(20), a); + QCOMPARE(QByteArray(a).right(-5), QByteArray()); + // calling right() does not modify the source array + QCOMPARE(a.constData(), ptr); + + // rvalue, detached + QCOMPARE(detached(a).right(5), QByteArray("defgh")); + QCOMPARE(detached(a).right(20), a); + QCOMPARE(detached(a).right(-5), QByteArray()); + // calling right() does not modify the source array + QCOMPARE(a.constData(), ptr); } void tst_QByteArray::mid() { QByteArray a; + QCOMPARE(QByteArray().mid(0), QByteArray()); + QCOMPARE(a.mid(0, 10), QByteArray()); QCOMPARE(a.mid(0), QByteArray()); QCOMPARE(a.mid(0, 10), QByteArray()); QCOMPARE(a.mid(10), QByteArray()); QVERIFY(!a.isDetached()); + QCOMPARE(QByteArray(a).mid(0), QByteArray()); + QCOMPARE(QByteArray(a).mid(0, 10), QByteArray()); + QCOMPARE(QByteArray(a).mid(10), QByteArray()); + QCOMPARE(detached(a).mid(0), QByteArray()); + QCOMPARE(detached(a).mid(0, 10), QByteArray()); + QCOMPARE(detached(a).mid(10), QByteArray()); a = QByteArray("abcdefgh"); const char *ptr = a.constData(); + + // lvalue QCOMPARE(a.mid(2), QByteArray("cdefgh")); QCOMPARE(a.mid(2, 3), QByteArray("cde")); QCOMPARE(a.mid(20), QByteArray()); @@ -2577,6 +2843,24 @@ void tst_QByteArray::mid() QCOMPARE(a.mid(-5, 8), QByteArray("abc")); // calling mid() does not modify the source array QCOMPARE(a.constData(), ptr); + + // rvalue, not detached + QCOMPARE(QByteArray(a).mid(2), QByteArray("cdefgh")); + QCOMPARE(QByteArray(a).mid(2, 3), QByteArray("cde")); + QCOMPARE(QByteArray(a).mid(20), QByteArray()); + QCOMPARE(QByteArray(a).mid(-5), QByteArray("abcdefgh")); + QCOMPARE(QByteArray(a).mid(-5, 8), QByteArray("abc")); + // calling mid() does not modify the source array + QCOMPARE(a.constData(), ptr); + + // rvalue, detached + QCOMPARE(detached(a).mid(2), QByteArray("cdefgh")); + QCOMPARE(detached(a).mid(2, 3), QByteArray("cde")); + QCOMPARE(detached(a).mid(20), QByteArray()); + QCOMPARE(detached(a).mid(-5), QByteArray("abcdefgh")); + QCOMPARE(detached(a).mid(-5, 8), QByteArray("abc")); + // calling mid() does not modify the source array + QCOMPARE(a.constData(), ptr); } void tst_QByteArray::length() @@ -2584,12 +2868,12 @@ void tst_QByteArray::length() QFETCH(QByteArray, src); QFETCH(qsizetype, res); - QCOMPARE(src.length(), res); + QCOMPARE(src.size(), res); QCOMPARE(src.size(), res); #if QT_DEPRECATED_SINCE(6, 4) QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED - QCOMPARE(src.count(), res); + QCOMPARE(src.size(), res); QT_WARNING_POP #endif } @@ -2607,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" |