From c262a1805a062fd99abc890f58ea951da80ff0af Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Mon, 12 Dec 2022 15:09:25 +0100 Subject: [PATCH] QBuffer: test and document open() behavior QBuffer::open() was only documented as \reimp, so its behavior regarding WriteOnly was never actually described. Add a test and document the outcome. Pick-to: 6.5 6.4 6.2 5.15 Change-Id: I75c49cd3f6a1961bcaece4a92a4e479bb3300d36 Reviewed-by: Mate Barany Reviewed-by: Fabian Kosmale --- src/corelib/io/qbuffer.cpp | 4 ++++ tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/corelib/io/qbuffer.cpp b/src/corelib/io/qbuffer.cpp index ba84428275..ba1ce7463b 100644 --- a/src/corelib/io/qbuffer.cpp +++ b/src/corelib/io/qbuffer.cpp @@ -288,6 +288,10 @@ void QBuffer::setData(const char *data, qsizetype size) /*! \reimp + + Unlike QFile, opening a QBuffer QIODevice::WriteOnly does not truncate it. + However, pos() is set to 0. Use QIODevice::Append or QIODevice::Truncate to + change either behavior. */ bool QBuffer::open(OpenMode flags) { diff --git a/tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp b/tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp index 93e0d6fce8..6fe192f3ab 100644 --- a/tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp +++ b/tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp @@ -15,6 +15,7 @@ class tst_QBuffer : public QObject Q_OBJECT private slots: void open(); + void openWriteOnlyDoesNotTruncate(); void getSetCheck(); void readBlock(); void readBlockPastEnd(); @@ -111,6 +112,29 @@ void tst_QBuffer::open() b.close(); } +void tst_QBuffer::openWriteOnlyDoesNotTruncate() +{ + QBuffer b; + const auto data = QByteArrayLiteral("Hey, presto!"); + + { + QVERIFY(b.open(QIODevice::WriteOnly)); + b.write(data); + b.close(); + } + { + QVERIFY(b.open(QIODevice::ReadOnly)); + QCOMPARE(b.readAll(), data); + b.close(); + } + { + QVERIFY(b.open(QIODevice::WriteOnly)); + QCOMPARE(b.size(), data.size()); + QCOMPARE(b.pos(), 0); + b.close(); + } +} + // some status() tests, too void tst_QBuffer::readBlock() {