diff options
author | Alex Trotsenko <alex1973tr@gmail.com> | 2015-09-11 12:15:03 +0300 |
---|---|---|
committer | Alex Trotsenko <alex1973tr@gmail.com> | 2015-10-27 18:24:34 +0000 |
commit | 1e32ade79ced64597ef4148f16e0d8a329733c4e (patch) | |
tree | 443c995629abb8db88a9d53b00af365f72df218b /tests/auto | |
parent | 33486e0ae7d40045029f9e24bebdf7a6829f756c (diff) |
QIODevice: fix interleaving read() and write() in text mode under Windows
Skip the correct number of bytes in the read buffer when expanding '\n'
into "\r\n" upon writing.
Change-Id: I5b01fc47c330dee5c83001abf0acd7d63d790b96
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@theqtcompany.com>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp b/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp index 565ca18899..334f5aba05 100644 --- a/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp +++ b/tests/auto/corelib/io/qiodevice/tst_qiodevice.cpp @@ -59,6 +59,7 @@ private slots: void peekBug(); void readAllKeepPosition(); + void writeInTextMode(); }; void tst_QIODevice::initTestCase() @@ -628,5 +629,45 @@ void tst_QIODevice::readAllKeepPosition() QCOMPARE(resultArray, buffer.buffer()); } +class RandomAccessBuffer : public QIODevice +{ +public: + RandomAccessBuffer(const char *data) : QIODevice(), buf(data) { } + +protected: + qint64 readData(char *data, qint64 maxSize) Q_DECL_OVERRIDE + { + maxSize = qMin(maxSize, qint64(buf.size() - pos())); + memcpy(data, buf.constData() + pos(), maxSize); + return maxSize; + } + qint64 writeData(const char *data, qint64 maxSize) Q_DECL_OVERRIDE + { + maxSize = qMin(maxSize, qint64(buf.size() - pos())); + memcpy(buf.data() + pos(), data, maxSize); + return maxSize; + } + +private: + QByteArray buf; +}; + +// Test write() on skipping correct number of bytes in read buffer +void tst_QIODevice::writeInTextMode() +{ + // Unlike other platforms, Windows implementation expands '\n' into + // "\r\n" sequence in write(). Ensure that write() properly works with + // a read buffer on random-access devices. +#ifndef Q_OS_WIN + QSKIP("This is a Windows-only test"); +#else + RandomAccessBuffer buffer("one\r\ntwo\r\nthree\r\n"); + buffer.open(QBuffer::ReadWrite | QBuffer::Text); + QCOMPARE(buffer.readLine(), QByteArray("one\n")); + QCOMPARE(buffer.write("two\n"), 4); + QCOMPARE(buffer.readLine(), QByteArray("three\n")); +#endif +} + QTEST_MAIN(tst_QIODevice) #include "tst_qiodevice.moc" |