diff options
author | Jason McDonald <jason.mcdonald@nokia.com> | 2011-11-17 16:11:59 +1000 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-11-18 04:51:09 +0100 |
commit | 93d2519d99d0ab29440a31f88dd978c3f031c08c (patch) | |
tree | dfc0e4086d39d66b1995ac75169d76ec19e444a8 /tests/auto/corelib | |
parent | ae4c28d8dc2a9e6c3c2dc83f75cf2b0e815459f3 (diff) |
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>
Diffstat (limited to 'tests/auto/corelib')
-rw-r--r-- | tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp | 59 |
1 files changed, 43 insertions, 16 deletions
diff --git a/tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp b/tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp index 6bdc160b27..f52a71b77f 100644 --- a/tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp +++ b/tests/auto/corelib/io/qbuffer/tst_qbuffer.cpp @@ -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(); -*/ + QByteArray lineByLine; + while (!buffer.atEnd()) + lineByLine.append(buffer.readLine()); + buffer.seek(0); + QCOMPARE(lineByLine, buffer.readAll()); +} - char c; - buffer.getChar(&c); - buffer.ungetChar(c); +// 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); - QFile out2("out2.txt"); - out2.open(QFile::WriteOnly); - while (!buffer.atEnd()) - out2.write(buffer.readLine()); - - out2.close(); - out2.remove(); + // 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() |