0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
|
|
42 |
#include <QtTest/QtTest>
|
|
43 |
|
|
44 |
#include <private/qringbuffer_p.h>
|
|
45 |
|
|
46 |
class tst_QRingBuffer : public QObject
|
|
47 |
{
|
|
48 |
Q_OBJECT
|
|
49 |
|
|
50 |
public:
|
|
51 |
tst_QRingBuffer();
|
|
52 |
virtual ~tst_QRingBuffer();
|
|
53 |
public slots:
|
|
54 |
void initTestCase();
|
|
55 |
void cleanupTestCase();
|
|
56 |
private slots:
|
|
57 |
void readPointerAtPositionWriteRead();
|
|
58 |
void readPointerAtPositionEmptyRead();
|
|
59 |
void readPointerAtPositionWithHead();
|
|
60 |
void readPointerAtPositionReadTooMuch();
|
|
61 |
void sizeWhenEmpty();
|
|
62 |
void sizeWhenReservedAndChopped();
|
|
63 |
void sizeWhenReserved();
|
|
64 |
};
|
|
65 |
|
|
66 |
tst_QRingBuffer::tst_QRingBuffer()
|
|
67 |
{
|
|
68 |
}
|
|
69 |
|
|
70 |
tst_QRingBuffer::~tst_QRingBuffer()
|
|
71 |
{
|
|
72 |
}
|
|
73 |
|
|
74 |
void tst_QRingBuffer::initTestCase()
|
|
75 |
{
|
|
76 |
}
|
|
77 |
|
|
78 |
void tst_QRingBuffer::cleanupTestCase()
|
|
79 |
{
|
|
80 |
}
|
|
81 |
|
|
82 |
void tst_QRingBuffer::sizeWhenReserved()
|
|
83 |
{
|
|
84 |
QRingBuffer ringBuffer;
|
|
85 |
ringBuffer.reserve(5);
|
|
86 |
|
|
87 |
QCOMPARE(ringBuffer.size(), 5);
|
|
88 |
}
|
|
89 |
|
|
90 |
void tst_QRingBuffer::sizeWhenReservedAndChopped()
|
|
91 |
{
|
|
92 |
QRingBuffer ringBuffer;
|
|
93 |
ringBuffer.reserve(31337);
|
|
94 |
ringBuffer.chop(31337);
|
|
95 |
|
|
96 |
QCOMPARE(ringBuffer.size(), 0);
|
|
97 |
}
|
|
98 |
|
|
99 |
void tst_QRingBuffer::sizeWhenEmpty()
|
|
100 |
{
|
|
101 |
QRingBuffer ringBuffer;
|
|
102 |
|
|
103 |
QCOMPARE(ringBuffer.size(), 0);
|
|
104 |
}
|
|
105 |
|
|
106 |
void tst_QRingBuffer::readPointerAtPositionReadTooMuch()
|
|
107 |
{
|
|
108 |
QRingBuffer ringBuffer;
|
|
109 |
|
|
110 |
qint64 length;
|
|
111 |
const char *buf = ringBuffer.readPointerAtPosition(42, length);
|
|
112 |
QVERIFY(buf == 0);
|
|
113 |
QVERIFY(length == 0);
|
|
114 |
}
|
|
115 |
|
|
116 |
void tst_QRingBuffer::readPointerAtPositionWithHead()
|
|
117 |
{
|
|
118 |
QRingBuffer ringBuffer;
|
|
119 |
char *buf = ringBuffer.reserve(4);
|
|
120 |
memcpy (buf, "0123", 4);
|
|
121 |
ringBuffer.free(2);
|
|
122 |
|
|
123 |
// ringBuffer should have stayed the same except
|
|
124 |
// its head it had moved to position 2
|
|
125 |
qint64 length;
|
|
126 |
const char* buf2 = ringBuffer.readPointerAtPosition(0, length);
|
|
127 |
|
|
128 |
QCOMPARE(length, qint64(2));
|
|
129 |
QVERIFY(*buf2 == '2');
|
|
130 |
QVERIFY(*(buf2+1) == '3');
|
|
131 |
|
|
132 |
// advance 2 more, ringBuffer should be empty then
|
|
133 |
ringBuffer.free(2);
|
|
134 |
buf2 = ringBuffer.readPointerAtPosition(0, length);
|
|
135 |
QCOMPARE(length, qint64(0));
|
|
136 |
QVERIFY(buf2 == 0);
|
|
137 |
}
|
|
138 |
|
|
139 |
void tst_QRingBuffer::readPointerAtPositionEmptyRead()
|
|
140 |
{
|
|
141 |
QRingBuffer ringBuffer;
|
|
142 |
|
|
143 |
qint64 length;
|
|
144 |
const char *buf = ringBuffer.readPointerAtPosition(0, length);
|
|
145 |
QVERIFY(buf == 0);
|
|
146 |
QVERIFY(length == 0);
|
|
147 |
}
|
|
148 |
|
|
149 |
void tst_QRingBuffer::readPointerAtPositionWriteRead()
|
|
150 |
{
|
|
151 |
//create some data
|
|
152 |
QBuffer inData;
|
|
153 |
inData.open(QIODevice::ReadWrite);
|
|
154 |
inData.putChar(0x42);
|
|
155 |
inData.putChar(0x23);
|
|
156 |
inData.write("Qt rocks!");
|
|
157 |
for (int i = 0; i < 5000; i++)
|
|
158 |
inData.write(QString("Number %1").arg(i).toUtf8());
|
|
159 |
inData.reset();
|
|
160 |
QVERIFY(inData.size() > 0);
|
|
161 |
|
|
162 |
//put the inData in the QRingBuffer
|
|
163 |
QRingBuffer ringBuffer;
|
|
164 |
qint64 remaining = inData.size();
|
|
165 |
while (remaining > 0) {
|
|
166 |
// write in chunks of 50 bytes
|
|
167 |
// this ensures there will be multiple QByteArrays inside the QRingBuffer
|
|
168 |
// since QRingBuffer is then only using individual arrays of around 4000 bytes
|
|
169 |
qint64 thisWrite = qMin(remaining, qint64(50));
|
|
170 |
char *pos = ringBuffer.reserve(thisWrite);
|
|
171 |
inData.read(pos, thisWrite);
|
|
172 |
remaining -= thisWrite;
|
|
173 |
}
|
|
174 |
// was data put into it?
|
|
175 |
QVERIFY(ringBuffer.size() > 0);
|
|
176 |
QCOMPARE(qint64(ringBuffer.size()), inData.size());
|
|
177 |
|
|
178 |
//read from the QRingBuffer in loop, put back into another QBuffer
|
|
179 |
QBuffer outData;
|
|
180 |
outData.open(QIODevice::ReadWrite);
|
|
181 |
remaining = ringBuffer.size();
|
|
182 |
while (remaining > 0) {
|
|
183 |
qint64 thisRead;
|
|
184 |
// always try to read as much as possible
|
|
185 |
const char *buf = ringBuffer.readPointerAtPosition(ringBuffer.size() - remaining, thisRead);
|
|
186 |
outData.write(buf, thisRead);
|
|
187 |
remaining -= thisRead;
|
|
188 |
}
|
|
189 |
outData.reset();
|
|
190 |
|
|
191 |
QVERIFY(outData.size() > 0);
|
|
192 |
|
|
193 |
// was the data read from the QRingBuffer the same as the one written into it?
|
|
194 |
QCOMPARE(outData.size(), inData.size());
|
|
195 |
QVERIFY(outData.buffer().startsWith(inData.buffer()));
|
|
196 |
}
|
|
197 |
|
|
198 |
|
|
199 |
QTEST_APPLESS_MAIN(tst_QRingBuffer)
|
|
200 |
#include "tst_qringbuffer.moc"
|