|
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 module 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 #include <QtTest/QtTest> |
|
43 |
|
44 #ifdef Q_OS_WIN |
|
45 # include <windows.h> |
|
46 #else |
|
47 # include <sys/stat.h> |
|
48 # include <sys/types.h> |
|
49 # include <dirent.h> |
|
50 # include <unistd.h> |
|
51 #endif |
|
52 |
|
53 class Test : public QObject{ |
|
54 Q_OBJECT |
|
55 public slots: |
|
56 void initTestCase() { |
|
57 QDir testdir = QDir::tempPath(); |
|
58 |
|
59 const QString subfolder_name = QLatin1String("test_speed"); |
|
60 QVERIFY(testdir.mkdir(subfolder_name)); |
|
61 QVERIFY(testdir.cd(subfolder_name)); |
|
62 |
|
63 for (uint i=0; i<10000; ++i) { |
|
64 QFile file(testdir.absolutePath() + "/testfile_" + QString::number(i)); |
|
65 file.open(QIODevice::WriteOnly); |
|
66 } |
|
67 } |
|
68 void cleanupTestCase() { |
|
69 { |
|
70 QDir testdir(QDir::tempPath() + QLatin1String("/test_speed")); |
|
71 |
|
72 foreach (const QString &filename, testdir.entryList()) { |
|
73 testdir.remove(filename); |
|
74 } |
|
75 } |
|
76 const QDir temp = QDir(QDir::tempPath()); |
|
77 temp.rmdir(QLatin1String("test_speed")); |
|
78 } |
|
79 private slots: |
|
80 void sizeSpeed() { |
|
81 QDir testdir(QDir::tempPath() + QLatin1String("/test_speed")); |
|
82 QBENCHMARK { |
|
83 QFileInfoList fileInfoList = testdir.entryInfoList(QDir::Files, QDir::Unsorted); |
|
84 foreach (const QFileInfo &fileInfo, fileInfoList) { |
|
85 fileInfo.isDir(); |
|
86 fileInfo.size(); |
|
87 } |
|
88 } |
|
89 } |
|
90 void sizeSpeedWithoutFilter() { |
|
91 QDir testdir(QDir::tempPath() + QLatin1String("/test_speed")); |
|
92 QBENCHMARK { |
|
93 QFileInfoList fileInfoList = testdir.entryInfoList(QDir::NoFilter, QDir::Unsorted); |
|
94 foreach (const QFileInfo &fileInfo, fileInfoList) { |
|
95 fileInfo.size(); |
|
96 } |
|
97 } |
|
98 } |
|
99 void sizeSpeedWithoutFileInfoList() { |
|
100 QDir testdir(QDir::tempPath() + QLatin1String("/test_speed")); |
|
101 testdir.setSorting(QDir::Unsorted); |
|
102 QBENCHMARK { |
|
103 QStringList fileList = testdir.entryList(QDir::NoFilter, QDir::Unsorted); |
|
104 foreach (const QString &filename, fileList) { |
|
105 QFileInfo fileInfo(filename); |
|
106 fileInfo.size(); |
|
107 } |
|
108 } |
|
109 } |
|
110 void iDontWantAnyStat() { |
|
111 QDir testdir(QDir::tempPath() + QLatin1String("/test_speed")); |
|
112 testdir.setSorting(QDir::Unsorted); |
|
113 testdir.setFilter(QDir::AllEntries | QDir::System | QDir::Hidden); |
|
114 QBENCHMARK { |
|
115 QStringList fileList = testdir.entryList(QDir::NoFilter, QDir::Unsorted); |
|
116 foreach (const QString &filename, fileList) { |
|
117 |
|
118 } |
|
119 } |
|
120 } |
|
121 |
|
122 void testLowLevel() { |
|
123 #ifdef Q_OS_WIN |
|
124 const wchar_t *dirpath = (wchar_t*)testdir.absolutePath().utf16(); |
|
125 wchar_t appendedPath[MAX_PATH]; |
|
126 wcscpy(appendedPath, dirpath); |
|
127 wcscat(appendedPath, L"\\*"); |
|
128 |
|
129 WIN32_FIND_DATA fd; |
|
130 HANDLE hSearch = FindFirstFileW(appendedPath, &fd); |
|
131 QVERIFY(hSearch == INVALID_HANDLE_VALUE); |
|
132 |
|
133 QBENCHMARK { |
|
134 do { |
|
135 |
|
136 } while (FindNextFile(hSearch, &fd)); |
|
137 } |
|
138 FindClose(hSearch); |
|
139 #else |
|
140 QDir testdir(QDir::tempPath() + QLatin1String("/test_speed")); |
|
141 DIR *dir = opendir(qPrintable(testdir.absolutePath())); |
|
142 QVERIFY(dir); |
|
143 |
|
144 QVERIFY(!chdir(qPrintable(testdir.absolutePath()))); |
|
145 QBENCHMARK { |
|
146 struct dirent *item = readdir(dir); |
|
147 while (item) { |
|
148 char *fileName = item->d_name; |
|
149 |
|
150 struct stat fileStat; |
|
151 QVERIFY(!stat(fileName, &fileStat)); |
|
152 |
|
153 item = readdir(dir); |
|
154 } |
|
155 } |
|
156 closedir(dir); |
|
157 #endif |
|
158 } |
|
159 }; |
|
160 |
|
161 QTEST_MAIN(Test) |
|
162 #include "tst_qdir.moc" |