summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/tools/qringbuffer_p.h46
-rw-r--r--tests/auto/auto.pro1
-rw-r--r--tests/auto/qringbuffer/qringbuffer.pro6
-rw-r--r--tests/auto/qringbuffer/tst_qringbuffer.cpp200
4 files changed, 253 insertions, 0 deletions
diff --git a/src/corelib/tools/qringbuffer_p.h b/src/corelib/tools/qringbuffer_p.h
index eed4ba9..02cc497 100644
--- a/src/corelib/tools/qringbuffer_p.h
+++ b/src/corelib/tools/qringbuffer_p.h
@@ -74,6 +74,52 @@ public:
return buffers.isEmpty() ? 0 : (buffers.first().constData() + head);
}
+ // access the bytes at a specified position
+ // the out-variable length will contain the amount of bytes readable
+ // from there, e.g. the amount still the same QByteArray
+ inline const char *readPointerAtPosition(qint64 pos, qint64 &length) const {
+ if (buffers.isEmpty()) {
+ length = 0;
+ return 0;
+ }
+
+ if (pos >= bufferSize) {
+ length = 0;
+ return 0;
+ }
+
+ // special case: it is in the first buffer
+ int nextDataBlockSizeValue = nextDataBlockSize();
+ if (pos - head < nextDataBlockSizeValue) {
+ length = nextDataBlockSizeValue - pos;
+ return buffers.at(0).constData() + head + pos;
+ }
+
+ // special case: we only had one buffer and tried to read over it
+ if (buffers.length() == 1) {
+ length = 0;
+ return 0;
+ }
+
+ // skip the first
+ pos -= nextDataBlockSizeValue;
+
+ // normal case: it is somewhere in the second to the-one-before-the-tailBuffer
+ for (int i = 1; i < tailBuffer; i++) {
+ if (pos >= buffers[i].size()) {
+ pos -= buffers[i].size();
+ continue;
+ }
+
+ length = buffers[i].length() - pos;
+ return buffers[i].constData() + pos;
+ }
+
+ // it is in the tail buffer
+ length = tail - pos;
+ return buffers[tailBuffer].constData() + pos;
+ }
+
inline void free(int bytes) {
bufferSize -= bytes;
if (bufferSize < 0)
diff --git a/tests/auto/auto.pro b/tests/auto/auto.pro
index 443ee7e..a3a6916 100644
--- a/tests/auto/auto.pro
+++ b/tests/auto/auto.pro
@@ -238,6 +238,7 @@ SUBDIRS += bic \
qregexpvalidator \
qregion \
qresourceengine \
+ qringbuffer \
qscriptable \
qscriptclass \
qscriptcontext \
diff --git a/tests/auto/qringbuffer/qringbuffer.pro b/tests/auto/qringbuffer/qringbuffer.pro
new file mode 100644
index 0000000..91fb0a0
--- /dev/null
+++ b/tests/auto/qringbuffer/qringbuffer.pro
@@ -0,0 +1,6 @@
+load(qttest_p4)
+SOURCES += tst_qringbuffer.cpp
+
+QT = core
+
+
diff --git a/tests/auto/qringbuffer/tst_qringbuffer.cpp b/tests/auto/qringbuffer/tst_qringbuffer.cpp
new file mode 100644
index 0000000..c741c2e
--- /dev/null
+++ b/tests/auto/qringbuffer/tst_qringbuffer.cpp
@@ -0,0 +1,200 @@
+/****************************************************************************
+**
+** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: Qt Software Information (qt-info@nokia.com)
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the either Technology Preview License Agreement or the
+** Beta Release License Agreement.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain
+** additional rights. These rights are described in the Nokia Qt LGPL
+** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
+** package.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3.0 as published by the Free Software
+** Foundation and appearing in the file LICENSE.GPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU General Public License version 3.0 requirements will be
+** met: http://www.gnu.org/copyleft/gpl.html.
+**
+** If you are unsure which license is appropriate for your use, please
+** contact the sales department at qt-sales@nokia.com.
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include <QtTest/QtTest>
+
+#include <private/qringbuffer_p.h>
+
+class tst_QRingBuffer : public QObject
+{
+ Q_OBJECT
+
+public:
+ tst_QRingBuffer();
+ virtual ~tst_QRingBuffer();
+public slots:
+ void initTestCase();
+ void cleanupTestCase();
+private slots:
+ void readPointerAtPositionWriteRead();
+ void readPointerAtPositionEmptyRead();
+ void readPointerAtPositionWithHead();
+ void readPointerAtPositionReadTooMuch();
+ void sizeWhenEmpty();
+ void sizeWhenReservedAndChopped();
+ void sizeWhenReserved();
+};
+
+tst_QRingBuffer::tst_QRingBuffer()
+{
+}
+
+tst_QRingBuffer::~tst_QRingBuffer()
+{
+}
+
+void tst_QRingBuffer::initTestCase()
+{
+}
+
+void tst_QRingBuffer::cleanupTestCase()
+{
+}
+
+void tst_QRingBuffer::sizeWhenReserved()
+{
+ QRingBuffer ringBuffer;
+ ringBuffer.reserve(5);
+
+ QCOMPARE(ringBuffer.size(), 5);
+}
+
+void tst_QRingBuffer::sizeWhenReservedAndChopped()
+{
+ QRingBuffer ringBuffer;
+ ringBuffer.reserve(31337);
+ ringBuffer.chop(31337);
+
+ QCOMPARE(ringBuffer.size(), 0);
+}
+
+void tst_QRingBuffer::sizeWhenEmpty()
+{
+ QRingBuffer ringBuffer;
+
+ QCOMPARE(ringBuffer.size(), 0);
+}
+
+void tst_QRingBuffer::readPointerAtPositionReadTooMuch()
+{
+ QRingBuffer ringBuffer;
+
+ qint64 length;
+ const char *buf = ringBuffer.readPointerAtPosition(42, length);
+ QVERIFY(buf == 0);
+ QVERIFY(length == 0);
+}
+
+void tst_QRingBuffer::readPointerAtPositionWithHead()
+{
+ QRingBuffer ringBuffer;
+ char *buf = ringBuffer.reserve(4);
+ memcpy (buf, "0123", 4);
+ ringBuffer.free(2);
+
+ // ringBuffer should have stayed the same except
+ // its head it had moved to position 2
+ qint64 length;
+ const char* buf2 = ringBuffer.readPointerAtPosition(0, length);
+
+ QCOMPARE(length, qint64(2));
+ QVERIFY(*buf2 == '2');
+ QVERIFY(*(buf2+1) == '3');
+
+ // advance 2 more, ringBuffer should be empty then
+ ringBuffer.free(2);
+ buf2 = ringBuffer.readPointerAtPosition(0, length);
+ QCOMPARE(length, qint64(0));
+ QVERIFY(buf2 == 0);
+}
+
+void tst_QRingBuffer::readPointerAtPositionEmptyRead()
+{
+ QRingBuffer ringBuffer;
+
+ qint64 length;
+ const char *buf = ringBuffer.readPointerAtPosition(0, length);
+ QVERIFY(buf == 0);
+ QVERIFY(length == 0);
+}
+
+void tst_QRingBuffer::readPointerAtPositionWriteRead()
+{
+ //create some data
+ QBuffer inData;
+ inData.open(QIODevice::ReadWrite);
+ inData.putChar(0x42);
+ inData.putChar(0x23);
+ inData.write("Qt rocks!");
+ for (int i = 0; i < 5000; i++)
+ inData.write(QString("Number %1").arg(i).toUtf8());
+ inData.reset();
+ QVERIFY(inData.size() > 0);
+
+ //put the inData in the QRingBuffer
+ QRingBuffer ringBuffer;
+ qint64 remaining = inData.size();
+ while (remaining > 0) {
+ // write in chunks of 50 bytes
+ // this ensures there will be multiple QByteArrays inside the QRingBuffer
+ // since QRingBuffer is then only using individual arrays of around 4000 bytes
+ qint64 thisWrite = qMin(remaining, qint64(50));
+ char *pos = ringBuffer.reserve(thisWrite);
+ inData.read(pos, thisWrite);
+ remaining -= thisWrite;
+ }
+ // was data put into it?
+ QVERIFY(ringBuffer.size() > 0);
+ QCOMPARE(qint64(ringBuffer.size()), inData.size());
+
+ //read from the QRingBuffer in loop, put back into another QBuffer
+ QBuffer outData;
+ outData.open(QIODevice::ReadWrite);
+ remaining = ringBuffer.size();
+ while (remaining > 0) {
+ qint64 thisRead;
+ // always try to read as much as possible
+ const char *buf = ringBuffer.readPointerAtPosition(ringBuffer.size() - remaining, thisRead);
+ outData.write(buf, thisRead);
+ remaining -= thisRead;
+ }
+ outData.reset();
+
+ QVERIFY(outData.size() > 0);
+
+ // was the data read from the QRingBuffer the same as the one written into it?
+ QCOMPARE(outData.size(), inData.size());
+ QVERIFY(outData.buffer().startsWith(inData.buffer()));
+}
+
+
+QTEST_APPLESS_MAIN(tst_QRingBuffer)
+#include "tst_qringbuffer.moc"