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