Fix boundary condition bugs in RawDataDevice.

bytesAvailable gets automatically calculated by QIODevice, so it doesn't
need to be reimplemented.

QIODevice doesn't necessarily buffer after a seek, and the underlying
device is unaware of peek/ungetChar, so canReadLine needs to check both
QIODevice and the device.
This commit is contained in:
sawinglogz 2021-05-24 19:59:58 -04:00
parent 0737ad9b10
commit c64376033d
3 changed files with 29 additions and 10 deletions

View File

@ -138,14 +138,9 @@ bool RawDataDevice::isSequential() const
return is_sequential;
}
qint64 RawDataDevice::bytesAvailable() const
{
return m_device.bytesAvailable();
}
bool RawDataDevice::canReadLine() const
{
return m_device.canReadLine();
return m_device.canReadLine() || QIODevice::canReadLine();
}
qint64 RawDataDevice::size() const

View File

@ -30,8 +30,6 @@ class RawDataDevice : public QIODevice
virtual qint64 size() const;
virtual bool seek(qint64 pos);
virtual qint64 bytesAvailable() const;
virtual bool canReadLine() const;
virtual bool waitForReadyRead(int msecs);

View File

@ -95,12 +95,24 @@ void RawDataTests::testQIODeviceInterface()
Q_ASSERT(ch == data[0]);
Q_ASSERT(raw.size() == qio.size());
Q_ASSERT(raw.atEnd() == qio.atEnd());
Q_ASSERT(raw.bytesAvailable() == qio.bytesAvailable());
Q_ASSERT(raw.seek(16) == true);
Q_ASSERT(raw.pos() == 16);
Q_ASSERT(raw.pos() == qio.pos());
Q_ASSERT(raw.atEnd() == qio.atEnd());
// Check boundary conditions at end of device.
Q_ASSERT(raw.seek(255) == true);
Q_ASSERT(raw.getChar(&ch) == true);
Q_ASSERT(raw.pos() == qio.pos());
Q_ASSERT(raw.atEnd() == true);
Q_ASSERT(raw.atEnd() == qio.atEnd());
Q_ASSERT(raw.bytesAvailable() == qio.bytesAvailable());
raw.ungetChar(ch);
Q_ASSERT(raw.atEnd() == false);
Q_ASSERT(raw.atEnd() != qio.atEnd());
Q_ASSERT(raw.bytesAvailable() == qio.bytesAvailable() + 1);
Q_ASSERT(raw.reset() == true);
Q_ASSERT(raw.pos() == 0);
@ -111,6 +123,20 @@ void RawDataTests::testQIODeviceInterface()
Q_ASSERT(raw.bytesAvailable() == qio.bytesAvailable());
// canReadLine
Q_ASSERT(raw.canReadLine() == qio.canReadLine());
raw.seek(255 - 0x0A);
Q_ASSERT(raw.canReadLine() == true);
Q_ASSERT(raw.canReadLine() == qio.canReadLine());
Q_ASSERT(raw.getChar(&ch) == true);
Q_ASSERT(ch == 0x0A);
Q_ASSERT(raw.canReadLine() == false);
Q_ASSERT(raw.canReadLine() == qio.canReadLine());
raw.ungetChar(ch);
Q_ASSERT(raw.canReadLine() == true);
Q_ASSERT(raw.canReadLine() != qio.canReadLine());
// readLine x2
Q_ASSERT(raw.reset() == true);
Q_ASSERT(raw.canReadLine() == qio.canReadLine());