diff options
author | Alex Trotsenko <alex1973tr@gmail.com> | 2014-07-21 16:35:43 +0300 |
---|---|---|
committer | Alex Trotsenko <alex1973tr@gmail.com> | 2014-07-23 07:31:09 +0200 |
commit | edfac1a9edb9c9c76ca9a97a0f9d7a5bfd83143b (patch) | |
tree | 9f32564a4e0939104831e0cb860ddfcbd10da64e | |
parent | 3905c6f00d9d11e01d6e211565d5ed58a59fc2d8 (diff) |
Make QRingBuffer::append() not leave empty arrays in buffer list
Change-Id: I4c5af33488a70996299289ec2b953b7bf3b2c428
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@digia.com>
-rw-r--r-- | src/corelib/tools/qringbuffer_p.h | 10 | ||||
-rw-r--r-- | tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp | 15 |
2 files changed, 22 insertions, 3 deletions
diff --git a/src/corelib/tools/qringbuffer_p.h b/src/corelib/tools/qringbuffer_p.h index 2f208d21e3..afd15c85ea 100644 --- a/src/corelib/tools/qringbuffer_p.h +++ b/src/corelib/tools/qringbuffer_p.h @@ -369,9 +369,13 @@ public: // append a new buffer to the end inline void append(const QByteArray &qba) { - buffers[tailBuffer].resize(tail); - buffers << qba; - ++tailBuffer; + if (tail == 0) { + buffers.last() = qba; + } else { + buffers.last().resize(tail); + buffers << qba; + ++tailBuffer; + } tail = qba.length(); bufferSize += qba.length(); } diff --git a/tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp b/tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp index 285cc3042a..a2d686d5d8 100644 --- a/tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp +++ b/tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp @@ -54,6 +54,7 @@ private slots: void sizeWhenEmpty(); void sizeWhenReservedAndChopped(); void sizeWhenReserved(); + void appendAndRead(); }; void tst_QRingBuffer::sizeWhenReserved() @@ -179,6 +180,20 @@ void tst_QRingBuffer::readPointerAtPositionWriteRead() QVERIFY(outData.buffer().startsWith(inData.buffer())); } +void tst_QRingBuffer::appendAndRead() +{ + QRingBuffer ringBuffer; + QByteArray ba1("Hello world!"); + QByteArray ba2("Test string."); + QByteArray ba3("0123456789"); + ringBuffer.append(ba1); + ringBuffer.append(ba2); + ringBuffer.append(ba3); + + QVERIFY(ringBuffer.read() == ba1); + QVERIFY(ringBuffer.read() == ba2); + QVERIFY(ringBuffer.read() == ba3); +} QTEST_APPLESS_MAIN(tst_QRingBuffer) #include "tst_qringbuffer.moc" |