Improve QBuffer autotest.

This commit fixes several issues found in the readLineBoundaries() test
function.

First, the test performed some test actions but did not perform any
verification steps to check that the outcome of those actions was
acceptable.

Second, the test didn't need to write the buffered data to a file to
verify line-by-line reading.

Third, the get/unget action was unrelated to the readLineBoundaries()
test and has been moved to a separate test function.  That test function
now tests that get/unget works at every position in a buffer, not just
at position 0.

Change-Id: Icad52ed598e94b6e05a194b9ab301d099bfc094c
Reviewed-by: Rohan McGovern <rohan.mcgovern@nokia.com>
This commit is contained in:
Jason McDonald 2011-11-17 16:11:59 +10:00 committed by Qt by Nokia
parent ae4c28d8dc
commit 93d2519d99

View File

@ -71,6 +71,7 @@ private slots:
void canReadLine();
void atEnd();
void readLineBoundaries();
void getAndUngetChar();
void writeAfterQByteArrayResize();
void read_null();
@ -520,6 +521,8 @@ void tst_QBuffer::atEnd()
QCOMPARE(buffer.read(&c, 1), qint64(0));
}
// Test that reading data out of a QBuffer a line at a time gives the same
// result as reading the whole buffer at once.
void tst_QBuffer::readLineBoundaries()
{
QByteArray line = "This is a line\n";
@ -528,26 +531,50 @@ void tst_QBuffer::readLineBoundaries()
while (buffer.size() < 16384)
buffer.write(line);
/*
buffer.seek(0);
QFile out1("out1.txt");
out1.open(QFile::WriteOnly);
out1.write(buffer.readAll());
out1.close();
*/
buffer.seek(0);
char c;
buffer.getChar(&c);
buffer.ungetChar(c);
QFile out2("out2.txt");
out2.open(QFile::WriteOnly);
QByteArray lineByLine;
while (!buffer.atEnd())
out2.write(buffer.readLine());
out2.close();
out2.remove();
lineByLine.append(buffer.readLine());
buffer.seek(0);
QCOMPARE(lineByLine, buffer.readAll());
}
// Test that any character in a buffer can be read and pushed back.
void tst_QBuffer::getAndUngetChar()
{
// Create some data in a buffer
QByteArray line = "This is a line\n";
QBuffer buffer;
buffer.open(QIODevice::ReadWrite);
while (buffer.size() < 16384)
buffer.write(line);
// Take a copy of the data held in the buffer
buffer.seek(0);
QByteArray data = buffer.readAll();
// Get and unget each character in order
for (qint64 i = 0; i < buffer.size(); ++i) {
buffer.seek(i);
char c;
QVERIFY(buffer.getChar(&c));
QCOMPARE(c, data.at((uint)i));
buffer.ungetChar(c);
}
// Get and unget each character in reverse order
for (qint64 i = buffer.size() - 1; i >= 0; --i) {
buffer.seek(i);
char c;
QVERIFY(buffer.getChar(&c));
QCOMPARE(c, data.at((uint)i));
buffer.ungetChar(c);
}
// Verify that the state of the buffer still matches the original data.
buffer.seek(0);
QCOMPARE(buffer.readAll(), data);
}
void tst_QBuffer::writeAfterQByteArrayResize()