diff options
author | Alex Trotsenko <alex1973tr@gmail.com> | 2015-03-28 16:47:41 +0200 |
---|---|---|
committer | Alex Trotsenko <alex1973tr@gmail.com> | 2015-04-04 08:40:01 +0000 |
commit | f40cf77b0fa1cd5353ca866a7a5799da9f303081 (patch) | |
tree | c78a9f09ded0afaa66e289edd1db644fa57f8091 /tests | |
parent | 6dcbaa487d222440c2aeeb5f0ad3bc6337d52f5d (diff) |
QIODevice: do not change the 'pos' member for sequential devices
Concept of 'current position' exists only for random-access devices.
As documented, for sequential devices QIODevice::pos() must always
return 0. Prevent a modification of the internal 'pos' member in
QIODevice::readAll() method to follow this rule.
Change-Id: Ida2ee6a629ccfc3068d62f95ab1064ada13fdda5
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp b/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp index 30c1f2be59..f756588e80 100644 --- a/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp +++ b/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp @@ -58,6 +58,7 @@ private slots: void readLine2(); void peekBug(); + void readAllKeepPosition(); }; void tst_QIODevice::initTestCase() @@ -584,5 +585,48 @@ void tst_QIODevice::peekBug() } +class SequentialReadBuffer : public QIODevice +{ +public: + SequentialReadBuffer(const char *data) : QIODevice(), buf(data), offset(0) { } + + bool isSequential() const Q_DECL_OVERRIDE { return true; } + const QByteArray &buffer() const { return buf; } + +protected: + qint64 readData(char *data, qint64 maxSize) Q_DECL_OVERRIDE + { + maxSize = qMin(maxSize, qint64(buf.size() - offset)); + memcpy(data, buf.constData() + offset, maxSize); + offset += maxSize; + return maxSize; + } + qint64 writeData(const char * /* data */, qint64 /* maxSize */) Q_DECL_OVERRIDE + { + return -1; + } + +private: + QByteArray buf; + int offset; +}; + +// Test readAll() on position change for sequential device +void tst_QIODevice::readAllKeepPosition() +{ + SequentialReadBuffer buffer("Hello world!"); + buffer.open(QIODevice::ReadOnly); + char c; + + QVERIFY(buffer.getChar(&c)); + QCOMPARE(buffer.pos(), qint64(0)); + buffer.ungetChar(c); + QCOMPARE(buffer.pos(), qint64(0)); + + QByteArray resultArray = buffer.readAll(); + QCOMPARE(buffer.pos(), qint64(0)); + QCOMPARE(resultArray, buffer.buffer()); +} + QTEST_MAIN(tst_QIODevice) #include "tst_qiodevice.moc" |