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 |
|
|
43 |
#include <QtTest/QtTest>
|
|
44 |
|
|
45 |
|
|
46 |
#include <QBuffer>
|
|
47 |
#include <QByteArray>
|
|
48 |
|
|
49 |
//TESTED_CLASS=
|
|
50 |
//TESTED_FILES=
|
|
51 |
|
|
52 |
class tst_QBuffer : public QObject
|
|
53 |
{
|
|
54 |
Q_OBJECT
|
|
55 |
public:
|
|
56 |
tst_QBuffer();
|
|
57 |
|
|
58 |
private slots:
|
|
59 |
void getSetCheck();
|
|
60 |
void readBlock();
|
|
61 |
void readBlockPastEnd();
|
|
62 |
void writeBlock_data();
|
|
63 |
void writeBlock();
|
|
64 |
void seek();
|
|
65 |
void seekTest_data();
|
|
66 |
void seekTest();
|
|
67 |
void read_rawdata();
|
|
68 |
void isSequential();
|
|
69 |
void signalTest_data();
|
|
70 |
void signalTest();
|
|
71 |
void isClosedAfterClose();
|
|
72 |
void readLine_data();
|
|
73 |
void readLine();
|
|
74 |
void canReadLine_data();
|
|
75 |
void canReadLine();
|
|
76 |
void atEnd();
|
|
77 |
void readLineBoundaries();
|
|
78 |
void writeAfterQByteArrayResize();
|
|
79 |
|
|
80 |
protected slots:
|
|
81 |
void readyReadSlot();
|
|
82 |
void bytesWrittenSlot(qint64 written);
|
|
83 |
|
|
84 |
private:
|
|
85 |
qint64 totalBytesWritten;
|
|
86 |
bool gotReadyRead;
|
|
87 |
};
|
|
88 |
|
|
89 |
// Testing get/set functions
|
|
90 |
void tst_QBuffer::getSetCheck()
|
|
91 |
{
|
|
92 |
QBuffer obj1;
|
|
93 |
// const QByteArray & QBuffer::data()
|
|
94 |
// void QBuffer::setData(const QByteArray &)
|
|
95 |
QByteArray var1("Bogus data");
|
|
96 |
obj1.setData(var1);
|
|
97 |
QCOMPARE(var1, obj1.data());
|
|
98 |
obj1.setData(QByteArray());
|
|
99 |
QCOMPARE(QByteArray(), obj1.data());
|
|
100 |
}
|
|
101 |
|
|
102 |
tst_QBuffer::tst_QBuffer()
|
|
103 |
{
|
|
104 |
}
|
|
105 |
|
|
106 |
// some status() tests, too
|
|
107 |
void tst_QBuffer::readBlock()
|
|
108 |
{
|
|
109 |
// QTest::ignoreMessage(QtWarningMsg, "QIODevice::read: File not open");
|
|
110 |
// QTest::ignoreMessage(QtWarningMsg, "QIODevice::read: Read operation not permitted");
|
|
111 |
|
|
112 |
const int arraySize = 10;
|
|
113 |
char a[arraySize];
|
|
114 |
QBuffer b;
|
|
115 |
QCOMPARE(b.read(a, arraySize), (qint64) -1); // not opened
|
|
116 |
QVERIFY(b.atEnd());
|
|
117 |
|
|
118 |
QByteArray ba;
|
|
119 |
ba.resize(arraySize);
|
|
120 |
b.setBuffer(&ba);
|
|
121 |
b.open(QIODevice::WriteOnly);
|
|
122 |
QTest::ignoreMessage(QtWarningMsg, "QIODevice::read: WriteOnly device");
|
|
123 |
QCOMPARE(b.read(a, arraySize), (qint64) -1); // no read access
|
|
124 |
b.close();
|
|
125 |
|
|
126 |
b.open(QIODevice::ReadOnly);
|
|
127 |
QCOMPARE(b.read(a, arraySize), (qint64) arraySize);
|
|
128 |
QVERIFY(b.atEnd());
|
|
129 |
|
|
130 |
// up to 3.0.x reading beyond the end was an error while ok
|
|
131 |
// this has been made consistent with other QIODevice sub classes in 3.1
|
|
132 |
QCOMPARE(b.read(a, 1), qint64(0));
|
|
133 |
QVERIFY(b.atEnd());
|
|
134 |
|
|
135 |
// read in two chunks
|
|
136 |
b.close();
|
|
137 |
b.open(QIODevice::ReadOnly);
|
|
138 |
QCOMPARE(b.read(a, arraySize/2), (qint64) arraySize/2);
|
|
139 |
QCOMPARE(b.read(a + arraySize/2, arraySize - arraySize/2),
|
|
140 |
(qint64)(arraySize - arraySize/2));
|
|
141 |
QVERIFY(b.atEnd());
|
|
142 |
}
|
|
143 |
|
|
144 |
void tst_QBuffer::readBlockPastEnd()
|
|
145 |
{
|
|
146 |
QByteArray arr(4096 + 3616, 'd');
|
|
147 |
QBuffer buf(&arr);
|
|
148 |
|
|
149 |
buf.open(QIODevice::ReadOnly);
|
|
150 |
char dummy[4096];
|
|
151 |
|
|
152 |
buf.read(1);
|
|
153 |
|
|
154 |
QCOMPARE(buf.read(dummy, 4096), qint64(4096));
|
|
155 |
QCOMPARE(buf.read(dummy, 4096), qint64(3615));
|
|
156 |
QVERIFY(buf.atEnd());
|
|
157 |
}
|
|
158 |
|
|
159 |
void tst_QBuffer::writeBlock_data()
|
|
160 |
{
|
|
161 |
QTest::addColumn<QString>("str");
|
|
162 |
|
|
163 |
QTest::newRow( "small_bytearray" ) << QString("Test");
|
|
164 |
QTest::newRow( "large_bytearray" ) << QString("The QBuffer class is an I/O device that operates on a QByteArray.\n"
|
|
165 |
"QBuffer is used to read and write to a memory buffer. It is normally "
|
|
166 |
"used with a QTextStream or a QDataStream. QBuffer has an associated "
|
|
167 |
"QByteArray which holds the buffer data. The size() of the buffer is "
|
|
168 |
"automatically adjusted as data is written.\n"
|
|
169 |
"The constructor QBuffer(QByteArray) creates a QBuffer using an existing "
|
|
170 |
"byte array. The byte array can also be set with setBuffer(). Writing to "
|
|
171 |
"the QBuffer will modify the original byte array because QByteArray is "
|
|
172 |
"explicitly shared.\n"
|
|
173 |
"Use open() to open the buffer before use and to set the mode (read-only, "
|
|
174 |
"write-only, etc.). close() closes the buffer. The buffer must be closed "
|
|
175 |
"before reopening or calling setBuffer().\n"
|
|
176 |
"A common way to use QBuffer is through QDataStream or QTextStream, which "
|
|
177 |
"have constructors that take a QBuffer parameter. For convenience, there "
|
|
178 |
"are also QDataStream and QTextStream constructors that take a QByteArray "
|
|
179 |
"parameter. These constructors create and open an internal QBuffer.\n"
|
|
180 |
"Note that QTextStream can also operate on a QString (a Unicode string); a "
|
|
181 |
"QBuffer cannot.\n"
|
|
182 |
"You can also use QBuffer directly through the standard QIODevice functions "
|
|
183 |
"readBlock(), writeBlock() readLine(), at(), getch(), putch() and ungetch().\n"
|
|
184 |
"See also QFile, QDataStream, QTextStream, QByteArray, Shared Classes, Collection "
|
|
185 |
"Classes and Input/Output and Networking.\n\n"
|
|
186 |
"The QBuffer class is an I/O device that operates on a QByteArray.\n"
|
|
187 |
"QBuffer is used to read and write to a memory buffer. It is normally "
|
|
188 |
"used with a QTextStream or a QDataStream. QBuffer has an associated "
|
|
189 |
"QByteArray which holds the buffer data. The size() of the buffer is "
|
|
190 |
"automatically adjusted as data is written.\n"
|
|
191 |
"The constructor QBuffer(QByteArray) creates a QBuffer using an existing "
|
|
192 |
"byte array. The byte array can also be set with setBuffer(). Writing to "
|
|
193 |
"the QBuffer will modify the original byte array because QByteArray is "
|
|
194 |
"explicitly shared.\n"
|
|
195 |
"Use open() to open the buffer before use and to set the mode (read-only, "
|
|
196 |
"write-only, etc.). close() closes the buffer. The buffer must be closed "
|
|
197 |
"before reopening or calling setBuffer().\n"
|
|
198 |
"A common way to use QBuffer is through QDataStream or QTextStream, which "
|
|
199 |
"have constructors that take a QBuffer parameter. For convenience, there "
|
|
200 |
"are also QDataStream and QTextStream constructors that take a QByteArray "
|
|
201 |
"parameter. These constructors create and open an internal QBuffer.\n"
|
|
202 |
"Note that QTextStream can also operate on a QString (a Unicode string); a "
|
|
203 |
"QBuffer cannot.\n"
|
|
204 |
"You can also use QBuffer directly through the standard QIODevice functions "
|
|
205 |
"readBlock(), writeBlock() readLine(), at(), getch(), putch() and ungetch().\n"
|
|
206 |
"See also QFile, QDataStream, QTextStream, QByteArray, Shared Classes, Collection "
|
|
207 |
"Classes and Input/Output and Networking.\n\n"
|
|
208 |
"The QBuffer class is an I/O device that operates on a QByteArray.\n"
|
|
209 |
"QBuffer is used to read and write to a memory buffer. It is normally "
|
|
210 |
"used with a QTextStream or a QDataStream. QBuffer has an associated "
|
|
211 |
"QByteArray which holds the buffer data. The size() of the buffer is "
|
|
212 |
"automatically adjusted as data is written.\n"
|
|
213 |
"The constructor QBuffer(QByteArray) creates a QBuffer using an existing "
|
|
214 |
"byte array. The byte array can also be set with setBuffer(). Writing to "
|
|
215 |
"the QBuffer will modify the original byte array because QByteArray is "
|
|
216 |
"explicitly shared.\n"
|
|
217 |
"Use open() to open the buffer before use and to set the mode (read-only, "
|
|
218 |
"write-only, etc.). close() closes the buffer. The buffer must be closed "
|
|
219 |
"before reopening or calling setBuffer().\n"
|
|
220 |
"A common way to use QBuffer is through QDataStream or QTextStream, which "
|
|
221 |
"have constructors that take a QBuffer parameter. For convenience, there "
|
|
222 |
"are also QDataStream and QTextStream constructors that take a QByteArray "
|
|
223 |
"parameter. These constructors create and open an internal QBuffer.\n"
|
|
224 |
"Note that QTextStream can also operate on a QString (a Unicode string); a "
|
|
225 |
"QBuffer cannot.\n"
|
|
226 |
"You can also use QBuffer directly through the standard QIODevice functions "
|
|
227 |
"readBlock(), writeBlock() readLine(), at(), getch(), putch() and ungetch().\n"
|
|
228 |
"See also QFile, QDataStream, QTextStream, QByteArray, Shared Classes, Collection "
|
|
229 |
"Classes and Input/Output and Networking.");
|
|
230 |
}
|
|
231 |
|
|
232 |
void tst_QBuffer::writeBlock()
|
|
233 |
{
|
|
234 |
QFETCH( QString, str );
|
|
235 |
|
|
236 |
QByteArray ba;
|
|
237 |
QBuffer buf( &ba );
|
|
238 |
buf.open(QIODevice::ReadWrite);
|
|
239 |
QByteArray data = str.toLatin1();
|
|
240 |
QCOMPARE(buf.write( data.constData(), data.size() ), qint64(data.size()));
|
|
241 |
|
|
242 |
QCOMPARE(buf.data(), str.toLatin1());
|
|
243 |
}
|
|
244 |
|
|
245 |
void tst_QBuffer::seek()
|
|
246 |
{
|
|
247 |
QBuffer buffer;
|
|
248 |
buffer.open(QIODevice::WriteOnly);
|
|
249 |
QCOMPARE(buffer.size(), qint64(0));
|
|
250 |
QCOMPARE(buffer.pos(), qint64(0));
|
|
251 |
const qint64 pos = 10;
|
|
252 |
QVERIFY(buffer.seek(pos));
|
|
253 |
QCOMPARE(buffer.size(), pos);
|
|
254 |
}
|
|
255 |
|
|
256 |
void tst_QBuffer::seekTest_data()
|
|
257 |
{
|
|
258 |
writeBlock_data();
|
|
259 |
}
|
|
260 |
|
|
261 |
#define DO_VALID_SEEK(position) { \
|
|
262 |
char c; \
|
|
263 |
QVERIFY(buf.seek(qint64(position))); \
|
|
264 |
QCOMPARE(buf.pos(), qint64(position)); \
|
|
265 |
QVERIFY(buf.getChar(&c)); \
|
|
266 |
QCOMPARE(QChar(c), str.at(qint64(position))); \
|
|
267 |
}
|
|
268 |
#define DO_INVALID_SEEK(position) { \
|
|
269 |
qint64 prev_pos = buf.pos(); \
|
|
270 |
QVERIFY(!buf.seek(qint64(position))); \
|
|
271 |
QCOMPARE(buf.pos(), prev_pos); /* position should not be changed */ \
|
|
272 |
}
|
|
273 |
|
|
274 |
void tst_QBuffer::seekTest()
|
|
275 |
{
|
|
276 |
QFETCH(QString, str);
|
|
277 |
|
|
278 |
QByteArray ba;
|
|
279 |
QBuffer buf(&ba);
|
|
280 |
#if 0
|
|
281 |
QCOMPARE(buf.pos(), qint64(-1));
|
|
282 |
#endif
|
|
283 |
buf.open(QIODevice::ReadWrite);
|
|
284 |
QCOMPARE(buf.pos(), qint64(0));
|
|
285 |
|
|
286 |
QByteArray data = str.toLatin1();
|
|
287 |
QCOMPARE(buf.write( data.constData(), data.size() ), qint64(data.size()));
|
|
288 |
|
|
289 |
QTest::ignoreMessage(QtWarningMsg, "QBuffer::seek: Invalid pos: -1");
|
|
290 |
DO_INVALID_SEEK(-1);
|
|
291 |
|
|
292 |
DO_VALID_SEEK(0);
|
|
293 |
DO_VALID_SEEK(str.size() - 1);
|
|
294 |
QVERIFY(buf.atEnd());
|
|
295 |
DO_VALID_SEEK(str.size() / 2);
|
|
296 |
|
|
297 |
// Special case: valid to seek one position past the buffer.
|
|
298 |
// Its then legal to write, but not read.
|
|
299 |
{
|
|
300 |
char c = 'a';
|
|
301 |
QVERIFY(buf.seek(qint64(str.size())));
|
|
302 |
QCOMPARE(buf.read(&c, qint64(1)), qint64(0));
|
|
303 |
QCOMPARE(c, 'a');
|
|
304 |
QCOMPARE(buf.write(&c, qint64(1)), qint64(1));
|
|
305 |
}
|
|
306 |
|
|
307 |
// Special case 2: seeking to an arbitrary position beyond the buffer auto-expands it
|
|
308 |
// (see Task 184730)
|
|
309 |
{
|
|
310 |
char c;
|
|
311 |
const int offset = 1;
|
|
312 |
Q_ASSERT(offset > 0); // any positive integer will do
|
|
313 |
const qint64 pos = buf.size() + offset;
|
|
314 |
QVERIFY(buf.seek(pos));
|
|
315 |
QCOMPARE(buf.pos(), pos);
|
|
316 |
QVERIFY(!buf.getChar(&c));
|
|
317 |
QVERIFY(buf.seek(pos - 1));
|
|
318 |
QVERIFY(buf.getChar(&c));
|
|
319 |
QCOMPARE(c, buf.data().at(pos - 1));
|
|
320 |
QVERIFY(buf.seek(pos));
|
|
321 |
QVERIFY(buf.putChar(c));
|
|
322 |
}
|
|
323 |
}
|
|
324 |
|
|
325 |
void tst_QBuffer::read_rawdata()
|
|
326 |
{
|
|
327 |
static const unsigned char mydata[] = {
|
|
328 |
0x01, 0x00, 0x03, 0x84, 0x78, 0x9c, 0x3b, 0x76,
|
|
329 |
0xec, 0x18, 0xc3, 0x31, 0x0a, 0xf1, 0xcc, 0x99,
|
|
330 |
0x6d, 0x5b
|
|
331 |
};
|
|
332 |
|
|
333 |
QByteArray data = QByteArray::fromRawData((const char *)mydata, sizeof(mydata));
|
|
334 |
QBuffer buffer(&data);
|
|
335 |
buffer.open(QIODevice::ReadOnly);
|
|
336 |
QDataStream in(&buffer);
|
|
337 |
quint8 ch;
|
|
338 |
for (int i = 0; i < (int)sizeof(mydata); ++i) {
|
|
339 |
QVERIFY(!buffer.atEnd());
|
|
340 |
in >> ch;
|
|
341 |
QVERIFY(ch == (quint8)mydata[i]);
|
|
342 |
}
|
|
343 |
QVERIFY(buffer.atEnd());
|
|
344 |
}
|
|
345 |
|
|
346 |
void tst_QBuffer::isSequential()
|
|
347 |
{
|
|
348 |
QBuffer buf;
|
|
349 |
QVERIFY(!buf.isSequential());
|
|
350 |
}
|
|
351 |
|
|
352 |
void tst_QBuffer::signalTest_data()
|
|
353 |
{
|
|
354 |
QTest::addColumn<QByteArray>("sample");
|
|
355 |
|
|
356 |
QTest::newRow("empty") << QByteArray();
|
|
357 |
QTest::newRow("size 1") << QByteArray("1");
|
|
358 |
QTest::newRow("size 2") << QByteArray("11");
|
|
359 |
QTest::newRow("size 100") << QByteArray(100, '1');
|
|
360 |
}
|
|
361 |
|
|
362 |
void tst_QBuffer::signalTest()
|
|
363 |
{
|
|
364 |
QFETCH(QByteArray, sample);
|
|
365 |
|
|
366 |
totalBytesWritten = 0;
|
|
367 |
|
|
368 |
QBuffer buf;
|
|
369 |
buf.open(QIODevice::WriteOnly);
|
|
370 |
|
|
371 |
buf.buffer().resize(sample.size() * 10);
|
|
372 |
connect(&buf, SIGNAL(readyRead()), this, SLOT(readyReadSlot()));
|
|
373 |
connect(&buf, SIGNAL(bytesWritten(qint64)), this, SLOT(bytesWrittenSlot(qint64)));
|
|
374 |
|
|
375 |
for (int i = 0; i < 10; ++i) {
|
|
376 |
gotReadyRead = false;
|
|
377 |
QCOMPARE(buf.write(sample), qint64(sample.size()));
|
|
378 |
if (sample.size() > 0) {
|
|
379 |
QTestEventLoop::instance().enterLoop(5);
|
|
380 |
if (QTestEventLoop::instance().timeout())
|
|
381 |
QFAIL("Timed out when waiting for readyRead()");
|
|
382 |
QCOMPARE(totalBytesWritten, qint64(sample.size() * (i + 1)));
|
|
383 |
QVERIFY(gotReadyRead);
|
|
384 |
} else {
|
|
385 |
QCOMPARE(totalBytesWritten, qint64(0));
|
|
386 |
QVERIFY(!gotReadyRead);
|
|
387 |
}
|
|
388 |
}
|
|
389 |
}
|
|
390 |
|
|
391 |
void tst_QBuffer::readyReadSlot()
|
|
392 |
{
|
|
393 |
gotReadyRead = true;
|
|
394 |
QTestEventLoop::instance().exitLoop();
|
|
395 |
}
|
|
396 |
|
|
397 |
void tst_QBuffer::bytesWrittenSlot(qint64 written)
|
|
398 |
{
|
|
399 |
totalBytesWritten += written;
|
|
400 |
}
|
|
401 |
|
|
402 |
void tst_QBuffer::isClosedAfterClose()
|
|
403 |
{
|
|
404 |
QBuffer buffer;
|
|
405 |
buffer.open(QBuffer::ReadOnly);
|
|
406 |
QVERIFY(buffer.isOpen());
|
|
407 |
buffer.close();
|
|
408 |
QVERIFY(!buffer.isOpen());
|
|
409 |
}
|
|
410 |
|
|
411 |
void tst_QBuffer::readLine_data()
|
|
412 |
{
|
|
413 |
QTest::addColumn<QByteArray>("src");
|
|
414 |
QTest::addColumn<int>("maxlen");
|
|
415 |
QTest::addColumn<QByteArray>("expected");
|
|
416 |
|
|
417 |
QTest::newRow("1") << QByteArray("line1\nline2\n") << 1024
|
|
418 |
<< QByteArray("line1\n");
|
|
419 |
QTest::newRow("2") << QByteArray("hi there") << 1024
|
|
420 |
<< QByteArray("hi there");
|
|
421 |
QTest::newRow("3") << QByteArray("l\n") << 3 << QByteArray("l\n");
|
|
422 |
QTest::newRow("4") << QByteArray("l\n") << 2 << QByteArray("l");
|
|
423 |
}
|
|
424 |
|
|
425 |
void tst_QBuffer::readLine()
|
|
426 |
{
|
|
427 |
QFETCH(QByteArray, src);
|
|
428 |
QFETCH(int, maxlen);
|
|
429 |
QFETCH(QByteArray, expected);
|
|
430 |
|
|
431 |
QBuffer buf;
|
|
432 |
buf.setBuffer(&src);
|
|
433 |
char *result = new char[maxlen + 1];
|
|
434 |
result[maxlen] = '\0';
|
|
435 |
|
|
436 |
QVERIFY(buf.open(QIODevice::ReadOnly));
|
|
437 |
|
|
438 |
qint64 bytes_read = buf.readLine(result, maxlen);
|
|
439 |
|
|
440 |
QCOMPARE(bytes_read, qint64(expected.size()));
|
|
441 |
QCOMPARE(QByteArray(result), expected);
|
|
442 |
|
|
443 |
buf.close();
|
|
444 |
delete[] result;
|
|
445 |
|
|
446 |
}
|
|
447 |
|
|
448 |
void tst_QBuffer::canReadLine_data()
|
|
449 |
{
|
|
450 |
QTest::addColumn<QByteArray>("src");
|
|
451 |
QTest::addColumn<bool>("expected");
|
|
452 |
|
|
453 |
QTest::newRow("1") << QByteArray("no newline") << false;
|
|
454 |
QTest::newRow("2") << QByteArray("two \n lines\n") << true;
|
|
455 |
QTest::newRow("3") << QByteArray("\n") << true;
|
|
456 |
QTest::newRow("4") << QByteArray() << false;
|
|
457 |
}
|
|
458 |
|
|
459 |
void tst_QBuffer::canReadLine()
|
|
460 |
{
|
|
461 |
QFETCH(QByteArray, src);
|
|
462 |
QFETCH(bool, expected);
|
|
463 |
|
|
464 |
QBuffer buf;
|
|
465 |
buf.setBuffer(&src);
|
|
466 |
QVERIFY(!buf.canReadLine());
|
|
467 |
QVERIFY(buf.open(QIODevice::ReadOnly));
|
|
468 |
QCOMPARE(buf.canReadLine(), expected);
|
|
469 |
}
|
|
470 |
|
|
471 |
void tst_QBuffer::atEnd()
|
|
472 |
{
|
|
473 |
QBuffer buffer;
|
|
474 |
buffer.open(QBuffer::Append);
|
|
475 |
buffer.write("heisann");
|
|
476 |
buffer.close();
|
|
477 |
|
|
478 |
buffer.open(QBuffer::ReadOnly);
|
|
479 |
buffer.seek(buffer.size());
|
|
480 |
char c;
|
|
481 |
QVERIFY(!buffer.getChar(&c));
|
|
482 |
QCOMPARE(buffer.read(&c, 1), qint64(0));
|
|
483 |
}
|
|
484 |
|
|
485 |
void tst_QBuffer::readLineBoundaries()
|
|
486 |
{
|
|
487 |
QByteArray line = "This is a line\n";
|
|
488 |
QBuffer buffer;
|
|
489 |
buffer.open(QIODevice::ReadWrite);
|
|
490 |
while (buffer.size() < 16384)
|
|
491 |
buffer.write(line);
|
|
492 |
|
|
493 |
/*
|
|
494 |
buffer.seek(0);
|
|
495 |
QFile out1("out1.txt");
|
|
496 |
out1.open(QFile::WriteOnly);
|
|
497 |
out1.write(buffer.readAll());
|
|
498 |
out1.close();
|
|
499 |
*/
|
|
500 |
buffer.seek(0);
|
|
501 |
|
|
502 |
char c;
|
|
503 |
buffer.getChar(&c);
|
|
504 |
buffer.ungetChar(c);
|
|
505 |
|
|
506 |
QFile out2("out2.txt");
|
|
507 |
out2.open(QFile::WriteOnly);
|
|
508 |
while (!buffer.atEnd())
|
|
509 |
out2.write(buffer.readLine());
|
|
510 |
|
|
511 |
out2.close();
|
|
512 |
out2.remove();
|
|
513 |
}
|
|
514 |
|
|
515 |
void tst_QBuffer::writeAfterQByteArrayResize()
|
|
516 |
{
|
|
517 |
QBuffer buffer;
|
|
518 |
QVERIFY(buffer.open(QIODevice::WriteOnly));
|
|
519 |
|
|
520 |
buffer.write(QByteArray().fill('a', 1000));
|
|
521 |
QCOMPARE(buffer.buffer().size(), 1000);
|
|
522 |
|
|
523 |
// resize the QByteArray behind QBuffer's back
|
|
524 |
buffer.buffer().clear();
|
|
525 |
buffer.seek(0);
|
|
526 |
QCOMPARE(buffer.buffer().size(), 0);
|
|
527 |
|
|
528 |
buffer.write(QByteArray().fill('b', 1000));
|
|
529 |
QCOMPARE(buffer.buffer().size(), 1000);
|
|
530 |
}
|
|
531 |
|
|
532 |
QTEST_MAIN(tst_QBuffer)
|
|
533 |
#include "tst_qbuffer.moc"
|