summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorJoão Abecasis <joao@abecasis.name>2009-11-04 13:42:08 +0100
committerJoão Abecasis <joao@abecasis.name>2009-11-04 20:20:56 +0100
commitd1ffc7422e71e42a329f7a9c78b6e584109169f3 (patch)
tree2d1d06b936f6a5c99654fbe0ce4078a9ff193b7a /tests
parent030b19f36e82cc005d21fab56e26c8b76c811ae7 (diff)
Extending tst_QFile::writeLargeDataBlock test
To test not only native, but fd and FILE* backends as well. Moved expensive generation of large block into a separate function that caches the result. Reviewed-by: Peter Hartmann
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/qfile/tst_qfile.cpp164
1 files changed, 134 insertions, 30 deletions
diff --git a/tests/auto/qfile/tst_qfile.cpp b/tests/auto/qfile/tst_qfile.cpp
index 19fbecd239..338ab9c573 100644
--- a/tests/auto/qfile/tst_qfile.cpp
+++ b/tests/auto/qfile/tst_qfile.cpp
@@ -79,6 +79,10 @@
# define SRCDIR ""
#endif
+#ifndef QT_OPEN_BINARY
+#define QT_OPEN_BINARY 0
+#endif
+
Q_DECLARE_METATYPE(QFile::FileError)
//TESTED_CLASS=
@@ -196,6 +200,81 @@ public:
// disabled this test for the moment... it hangs
void invalidFile_data();
void invalidFile();
+
+private:
+ enum FileType { OpenQFile, OpenFd, OpenStream };
+
+ bool openFd(QFile &file, QIODevice::OpenMode mode)
+ {
+ int fdMode = QT_OPEN_LARGEFILE | QT_OPEN_BINARY;
+
+ // File will be truncated if in Write mode.
+ if (mode & QIODevice::WriteOnly)
+ fdMode |= QT_OPEN_WRONLY | QT_OPEN_TRUNC;
+ if (mode & QIODevice::ReadOnly)
+ fdMode |= QT_OPEN_RDONLY;
+
+ fd_ = QT_OPEN(qPrintable(file.fileName()), fdMode);
+
+ return (-1 != fd_) && file.open(fd_, mode);
+ }
+
+ bool openStream(QFile &file, QIODevice::OpenMode mode)
+ {
+ char const *streamMode = "";
+
+ // File will be truncated if in Write mode.
+ if (mode & QIODevice::WriteOnly)
+ streamMode = "wb+";
+ else if (mode & QIODevice::ReadOnly)
+ streamMode = "rb";
+
+ stream_ = QT_FOPEN(qPrintable(file.fileName()), streamMode);
+
+ return stream_ && file.open(stream_, mode);
+ }
+
+ bool openFile(QFile &file, QIODevice::OpenMode mode, FileType type = OpenQFile)
+ {
+ if (mode & QIODevice::WriteOnly && !file.exists())
+ {
+ // Make sure the file exists
+ QFile createFile(file.fileName());
+ if (!createFile.open(QIODevice::ReadWrite))
+ return false;
+ }
+
+ // Note: openFd and openStream will truncate the file if write mode.
+ switch (type)
+ {
+ case OpenQFile:
+ return file.open(mode);
+
+ case OpenFd:
+ return openFd(file, mode);
+
+ case OpenStream:
+ return openStream(file, mode);
+ }
+
+ return false;
+ }
+
+ void closeFile(QFile &file)
+ {
+ file.close();
+
+ if (-1 != fd_)
+ QT_CLOSE(fd_);
+ if (stream_)
+ ::fclose(stream_);
+
+ fd_ = -1;
+ stream_ = 0;
+ }
+
+ int fd_;
+ FILE *stream_;
};
tst_QFile::tst_QFile()
@@ -211,6 +290,8 @@ void tst_QFile::init()
{
// TODO: Add initialization code here.
// This will be executed immediately before each test is run.
+ fd_ = -1;
+ stream_ = 0;
}
void tst_QFile::cleanup()
@@ -239,6 +320,11 @@ void tst_QFile::cleanup()
QFile::remove("existing-file.txt");
QFile::remove("file-renamed-once.txt");
QFile::remove("file-renamed-twice.txt");
+
+ if (-1 != fd_)
+ QT_CLOSE(fd_);
+ if (stream_)
+ ::fclose(stream_);
}
void tst_QFile::initTestCase()
@@ -1909,53 +1995,71 @@ void tst_QFile::fullDisk()
void tst_QFile::writeLargeDataBlock_data()
{
QTest::addColumn<QString>("fileName");
+ QTest::addColumn<int>("type");
+
+ QTest::newRow("localfile-QFile") << "./largeblockfile.txt" << (int)OpenQFile;
+ QTest::newRow("localfile-Fd") << "./largeblockfile.txt" << (int)OpenFd;
+ QTest::newRow("localfile-Stream") << "./largeblockfile.txt" << (int)OpenStream;
- QTest::newRow("localfile") << QString("./largeblockfile.txt");
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE)
// Some semi-randomness to avoid collisions.
QTest::newRow("unc file")
<< QString("//" + QtNetworkSettings::winServerName() + "/TESTSHAREWRITABLE/largefile-%1-%2.txt")
.arg(QHostInfo::localHostName())
- .arg(QTime::currentTime().msec());
+ .arg(QTime::currentTime().msec()) << (int)OpenQFile;
#endif
}
-void tst_QFile::writeLargeDataBlock()
+static QByteArray getLargeDataBlock()
{
- QFETCH(QString, fileName);
+ static QByteArray array;
- // Generate a 64MB array with well defined contents.
- QByteArray array;
+ if (array.isNull())
+ {
#if defined(Q_OS_WINCE) || defined(Q_OS_SYMBIAN)
- int resizeSize = 1024 * 1024; // WinCE and Symbian do not have much space
+ int resizeSize = 1024 * 1024; // WinCE and Symbian do not have much space
#else
- int resizeSize = 64 * 1024 * 1024;
+ int resizeSize = 64 * 1024 * 1024;
#endif
- array.resize(resizeSize);
- for (int i = 0; i < array.size(); ++i)
- array[i] = uchar(i);
+ array.resize(resizeSize);
+ for (int i = 0; i < array.size(); ++i)
+ array[i] = uchar(i);
+ }
- // Remove and open the target file
- QFile file(fileName);
- file.remove();
- if (file.open(QFile::WriteOnly)) {
- QCOMPARE(file.write(array), qint64(array.size()));
- file.close();
- QVERIFY(file.open(QFile::ReadOnly));
- array.clear();
- array = file.readAll();
- file.remove();
- } else {
- QFAIL(qPrintable(QString("Couldn't open file for writing: [%1]").arg(fileName)));
+ return array;
+}
+
+void tst_QFile::writeLargeDataBlock()
+{
+ QFETCH(QString, fileName);
+ QFETCH( int, type );
+
+ QByteArray const originalData = getLargeDataBlock();
+
+ {
+ QFile file(fileName);
+
+ QVERIFY2( openFile(file, QIODevice::WriteOnly, (FileType)type),
+ qPrintable(QString("Couldn't open file for writing: [%1]").arg(fileName)) );
+ QCOMPARE( file.write(originalData), (qint64)originalData.size() );
+ QVERIFY( file.flush() );
+
+ closeFile(file);
}
- // Check that we got the right content
- QCOMPARE(array.size(), resizeSize);
- for (int i = 0; i < array.size(); ++i) {
- if (array[i] != char(i)) {
- QFAIL(qPrintable(QString("Wrong contents! Char at %1 = %2, expected %3")
- .arg(i).arg(int(uchar(array[i]))).arg(int(uchar(i)))));
- }
+
+ QByteArray readData;
+
+ {
+ QFile file(fileName);
+
+ QVERIFY2( openFile(file, QIODevice::ReadOnly, (FileType)type),
+ qPrintable(QString("Couldn't open file for reading: [%1]").arg(fileName)) );
+ readData = file.readAll();
+ closeFile(file);
}
+
+ QCOMPARE( readData, originalData );
+ QVERIFY( QFile::remove(fileName) );
}
void tst_QFile::readFromWriteOnlyFile()