author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 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 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 |
#include <iostream> |
|
46 |
#include <iomanip> |
|
47 |
#include <sstream> |
|
48 |
#include <algorithm> |
|
49 |
#include <qalgorithms.h> |
|
50 |
#include "../../../src/qt3support/tools/q3tl.h" |
|
51 |
#include <QStringList> |
|
52 |
#include <QString> |
|
53 |
#include <QVector> |
|
54 |
||
55 |
#define Q_TEST_PERFORMANCE 0 |
|
56 |
||
57 |
using namespace std; |
|
58 |
||
59 |
//TESTED_FILES= |
|
60 |
||
61 |
class tst_QAlgorithms : public QObject |
|
62 |
{ |
|
63 |
Q_OBJECT |
|
64 |
||
65 |
public: |
|
66 |
tst_QAlgorithms(); |
|
67 |
~tst_QAlgorithms(); |
|
68 |
||
69 |
public slots: |
|
70 |
void init(); |
|
71 |
void cleanup(); |
|
72 |
||
73 |
private slots: |
|
74 |
void qBubbleSort(); |
|
75 |
void qHeapSort(); |
|
76 |
void test_qLowerBound_data(); |
|
77 |
void test_qLowerBound(); |
|
78 |
void test_qUpperBound_data(); |
|
79 |
void test_qUpperBound(); |
|
80 |
void test_qBinaryFind_data(); |
|
81 |
void test_qBinaryFind(); |
|
82 |
void qBinaryFindOneEntry(); |
|
83 |
void swap(); |
|
84 |
void sortEmptyList(); |
|
85 |
void sortedList(); |
|
86 |
void sortAPItest(); |
|
87 |
void stableSortTest(); |
|
88 |
void stableSortCorrectnessTest_data(); |
|
89 |
void stableSortCorrectnessTest(); |
|
90 |
void convenienceAPI(); |
|
91 |
void qCountIterators() const; |
|
92 |
void qCountContainer() const; |
|
93 |
void binaryFindOnLargeContainer() const; |
|
94 |
||
95 |
#if Q_TEST_PERFORMANCE |
|
96 |
private: |
|
97 |
void performance(); |
|
98 |
#endif |
|
99 |
}; |
|
100 |
||
101 |
tst_QAlgorithms::tst_QAlgorithms() |
|
102 |
||
103 |
{ |
|
104 |
} |
|
105 |
||
106 |
tst_QAlgorithms::~tst_QAlgorithms() |
|
107 |
{ |
|
108 |
||
109 |
} |
|
110 |
||
111 |
void tst_QAlgorithms::init() |
|
112 |
{ |
|
113 |
} |
|
114 |
||
115 |
void tst_QAlgorithms::cleanup() |
|
116 |
{ |
|
117 |
} |
|
118 |
||
119 |
||
120 |
class TestInt |
|
121 |
{ |
|
122 |
public: |
|
123 |
TestInt(int number) :m_number(number) {} ; |
|
124 |
TestInt() : m_number(0) {}; |
|
125 |
bool operator<(const TestInt &other) const { ++TestInt::lessThanRefCount; return (m_number < other.m_number); } |
|
126 |
int m_number; |
|
127 |
static long int lessThanRefCount; |
|
128 |
}; |
|
129 |
||
130 |
long int TestInt::lessThanRefCount; |
|
131 |
||
132 |
||
133 |
QStringList dataSetTypes = QStringList() << "Random" << "Ascending" |
|
134 |
<< "Descending" << "Equal" << "Duplicates" << "Almost Sorted" ; |
|
135 |
||
136 |
template <typename DataType> |
|
137 |
QVector<DataType> generateData(QString dataSetType, const int length) |
|
138 |
{ |
|
139 |
QVector<DataType> container; |
|
140 |
if (dataSetType == "Random") { |
|
141 |
for(int i=0; i < length; ++i) |
|
142 |
container.append(rand()); |
|
143 |
} |
|
144 |
else if (dataSetType == "Ascending") { |
|
145 |
for (int i=0; i < length; ++i) |
|
146 |
container.append(i); |
|
147 |
} |
|
148 |
else if (dataSetType == "Descending") { |
|
149 |
for (int i=0; i < length; ++i) |
|
150 |
container.append(length - i); |
|
151 |
} |
|
152 |
else if (dataSetType == "Equal") { |
|
153 |
for (int i=0; i < length; ++i) |
|
154 |
container.append(43); |
|
155 |
} |
|
156 |
else if (dataSetType == "Duplicates") { |
|
157 |
for (int i=0; i < length; ++i) |
|
158 |
container.append(i % 10); |
|
159 |
} |
|
160 |
else if (dataSetType == "Almost Sorted") { |
|
161 |
for (int i=0; i < length; ++i) |
|
162 |
container.append(i); |
|
163 |
for(int i = 0; i<= length / 10; ++i) { |
|
164 |
const int iswap = i * 9; |
|
165 |
DataType tmp = container.at(iswap); |
|
166 |
container[iswap] = container.at(iswap + 1); |
|
167 |
container[iswap + 1] = tmp; |
|
168 |
} |
|
169 |
} |
|
170 |
return container; |
|
171 |
} |
|
172 |
||
173 |
struct ResultSet |
|
174 |
{ |
|
175 |
int numSorts; |
|
176 |
long int lessThanRefCount; |
|
177 |
}; |
|
178 |
||
179 |
||
180 |
template <typename ContainerType, typename Algorithm> |
|
181 |
ResultSet testRun(ContainerType &container, Algorithm &algorithm, int millisecs) |
|
182 |
{ |
|
183 |
TestInt::lessThanRefCount = 0; |
|
184 |
int count = 0; |
|
185 |
QTime t; |
|
186 |
t.start(); |
|
187 |
while(t.elapsed() < millisecs) { |
|
188 |
++count; |
|
189 |
algorithm(container); |
|
190 |
} |
|
191 |
ResultSet result; |
|
192 |
result.numSorts = count; |
|
193 |
result.lessThanRefCount = TestInt::lessThanRefCount; |
|
194 |
return result; |
|
195 |
} |
|
196 |
||
197 |
template <typename ContainerType, typename LessThan> |
|
198 |
bool isSorted(ContainerType &container, LessThan lessThan) |
|
199 |
{ |
|
200 |
for (int i=0; i < container.count() - 1; ++i) |
|
201 |
if (lessThan(container.at(i+1), container.at(i))) { |
|
202 |
return false; |
|
203 |
} |
|
204 |
return true; |
|
205 |
} |
|
206 |
||
207 |
template <typename ContainerType> |
|
208 |
bool isSorted(ContainerType &container) |
|
209 |
{ |
|
210 |
return isSorted(container, qLess<Q_TYPENAME ContainerType::value_type>()); |
|
211 |
} |
|
212 |
||
213 |
||
214 |
#if Q_TEST_PERFORMANCE |
|
215 |
void printHeader(QStringList &headers) |
|
216 |
{ |
|
217 |
cout << setw(10) << setiosflags(ios_base::left) << " "; |
|
218 |
for (int h = 0; h < headers.count(); ++h) { |
|
219 |
cout << setw(20) << setiosflags(ios_base::left) << headers.at(h).toLatin1().constData(); |
|
220 |
} |
|
221 |
cout << endl; |
|
222 |
} |
|
223 |
||
224 |
template <typename ContainerType> |
|
225 |
void print(ContainerType testContainer) |
|
226 |
{ |
|
227 |
typedef typename ContainerType::value_type T; |
|
228 |
||
229 |
foreach(T value, testContainer) { |
|
230 |
cout << value << " "; |
|
231 |
} |
|
232 |
||
233 |
cout << endl; |
|
234 |
} |
|
235 |
||
236 |
template <typename Algorithm, typename DataType> |
|
237 |
QList<ResultSet> testAlgorithm(Algorithm &algorithm, QStringList dataSetTypes, int size, int time) |
|
238 |
{ |
|
239 |
QList<ResultSet> results; |
|
240 |
foreach(QString dataSetType, dataSetTypes) { |
|
241 |
QVector<DataType> container = generateData<DataType>(dataSetType, size); |
|
242 |
results.append(testRun(container, algorithm, time)); |
|
243 |
Q_ASSERT(isSorted(container)); |
|
244 |
} |
|
245 |
return results; |
|
246 |
} |
|
247 |
||
248 |
template <typename Algorithm, typename DataType> |
|
249 |
void testAlgorithm(Algorithm algorithm, QStringList &dataSetTypes) |
|
250 |
{ |
|
251 |
QList<int> sizes = QList<int>() << 5 << 15 << 35 << 70 << 200 << 1000 << 10000; |
|
252 |
printHeader(dataSetTypes); |
|
253 |
for (int s = 0; s < sizes.count(); ++s){ |
|
254 |
cout << setw(10) << setiosflags(ios_base::left)<< sizes.at(s); |
|
255 |
QList<ResultSet> results = |
|
256 |
testAlgorithm<Algorithm, DataType>(algorithm, dataSetTypes, sizes.at(s), 100); |
|
257 |
foreach(ResultSet result, results) { |
|
258 |
stringstream numSorts; |
|
259 |
numSorts << setiosflags(ios_base::left) << setw(10) << result.numSorts; |
|
260 |
stringstream lessThan; |
|
261 |
lessThan << setiosflags(ios_base::left) << setw(10) << result.lessThanRefCount / result.numSorts; |
|
262 |
cout << numSorts.str() << lessThan.str(); |
|
263 |
} |
|
264 |
cout << endl; |
|
265 |
} |
|
266 |
} |
|
267 |
#endif |
|
268 |
static bool userFunction1(char ch1, char ch2) |
|
269 |
{ |
|
270 |
return (ch1 ^ 1) < (ch2 ^ 1); |
|
271 |
} |
|
272 |
||
273 |
bool userFunction2(const char &ch1, char ch2) |
|
274 |
{ |
|
275 |
return (ch1 ^ 1) < (ch2 ^ 1); |
|
276 |
} |
|
277 |
||
278 |
static inline bool userFunction3(char ch1, const char &ch2) |
|
279 |
{ |
|
280 |
return (ch1 ^ 1) < (ch2 ^ 1); |
|
281 |
} |
|
282 |
||
283 |
inline bool userFunction4(const char &ch1, const char &ch2) |
|
284 |
{ |
|
285 |
return (ch1 ^ 1) < (ch2 ^ 1); |
|
286 |
} |
|
287 |
||
288 |
class UserFunctor1 |
|
289 |
{ |
|
290 |
public: |
|
291 |
UserFunctor1(int x = 1) : y(x) {} |
|
292 |
||
293 |
bool operator()(char ch1, char ch2) |
|
294 |
{ |
|
295 |
return (ch1 ^ y) < (ch2 ^ y); |
|
296 |
} |
|
297 |
||
298 |
char y; |
|
299 |
}; |
|
300 |
||
301 |
void tst_QAlgorithms::qHeapSort() |
|
302 |
{ |
|
303 |
char array1[] = "3141592"; |
|
304 |
::qHeapSort((char *)array1, array1 + strlen(array1)); |
|
305 |
QVERIFY(strcmp(array1, "1123459") == 0); |
|
306 |
||
307 |
::qHeapSort((char *)array1, array1 + strlen(array1), qGreater<char>()); |
|
308 |
QVERIFY(strcmp(array1, "9543211") == 0); |
|
309 |
||
310 |
::qHeapSort((char *)array1, array1 + strlen(array1), qLess<char>()); |
|
311 |
QVERIFY(strcmp(array1, "1123459") == 0); |
|
312 |
{ |
|
313 |
char array2[] = "0123456789@ABCDE"; |
|
314 |
::qHeapSort((char *)array2, array2 + strlen(array2), userFunction1); |
|
315 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
316 |
} |
|
317 |
||
318 |
{ |
|
319 |
char array2[] = "0123456789@ABCDE"; |
|
320 |
::qHeapSort((char *)array2, array2 + strlen(array2), userFunction2); |
|
321 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
322 |
} |
|
323 |
||
324 |
{ |
|
325 |
char array2[] = "0123456789@ABCDE"; |
|
326 |
::qHeapSort((char *)array2, array2 + strlen(array2), userFunction3); |
|
327 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
328 |
} |
|
329 |
||
330 |
{ |
|
331 |
char array2[] = "0123456789@ABCDE"; |
|
332 |
::qHeapSort((char *)array2, array2 + strlen(array2), userFunction4); |
|
333 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
334 |
} |
|
335 |
||
336 |
{ |
|
337 |
UserFunctor1 userFunctor1; |
|
338 |
char array2[] = "0123456789@ABCDE"; |
|
339 |
::qHeapSort((char *)array2, array2 + strlen(array2), userFunctor1); |
|
340 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
341 |
} |
|
342 |
||
343 |
{ |
|
344 |
char array2[] = "0123456789@ABCDE"; |
|
345 |
::qHeapSort((char *)array2, array2 + strlen(array2), UserFunctor1()); |
|
346 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
347 |
::qHeapSort((char *)array2, array2 + strlen(array2), UserFunctor1(1)); |
|
348 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
349 |
::qHeapSort((char *)array2, array2 + strlen(array2), UserFunctor1(3)); |
|
350 |
QVERIFY(strcmp(array2, "3210765498CBA@ED") == 0); |
|
351 |
::qHeapSort((char *)array2, array2 + strlen(array2), UserFunctor1(0)); |
|
352 |
QVERIFY(strcmp(array2, "0123456789@ABCDE") == 0); |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
void tst_QAlgorithms::qBubbleSort() |
|
357 |
{ |
|
358 |
char array1[] = "3141592"; |
|
359 |
::qBubbleSort((char *)array1, array1 + strlen(array1)); |
|
360 |
QVERIFY(strcmp(array1, "1123459") == 0); |
|
361 |
||
362 |
::qBubbleSort((char *)array1, array1 + strlen(array1), qGreater<char>()); |
|
363 |
QVERIFY(strcmp(array1, "9543211") == 0); |
|
364 |
||
365 |
::qBubbleSort((char *)array1, array1 + strlen(array1), qLess<char>()); |
|
366 |
QVERIFY(strcmp(array1, "1123459") == 0); |
|
367 |
||
368 |
{ |
|
369 |
char array2[] = "0123456789@ABCDE"; |
|
370 |
::qBubbleSort((char *)array2, array2 + strlen(array2), userFunction1); |
|
371 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
372 |
} |
|
373 |
||
374 |
{ |
|
375 |
char array2[] = "0123456789@ABCDE"; |
|
376 |
::qBubbleSort((char *)array2, array2 + strlen(array2), userFunction2); |
|
377 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
378 |
} |
|
379 |
||
380 |
{ |
|
381 |
char array2[] = "0123456789@ABCDE"; |
|
382 |
::qBubbleSort((char *)array2, array2 + strlen(array2), userFunction3); |
|
383 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
384 |
} |
|
385 |
||
386 |
{ |
|
387 |
char array2[] = "0123456789@ABCDE"; |
|
388 |
::qBubbleSort((char *)array2, array2 + strlen(array2), userFunction4); |
|
389 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
390 |
} |
|
391 |
||
392 |
{ |
|
393 |
UserFunctor1 userFunctor1; |
|
394 |
char array2[] = "0123456789@ABCDE"; |
|
395 |
::qBubbleSort((char *)array2, array2 + strlen(array2), userFunctor1); |
|
396 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
397 |
} |
|
398 |
||
399 |
{ |
|
400 |
char array2[] = "0123456789@ABCDE"; |
|
401 |
::qBubbleSort((char *)array2, array2 + strlen(array2), UserFunctor1()); |
|
402 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
403 |
::qBubbleSort((char *)array2, array2 + strlen(array2), UserFunctor1(1)); |
|
404 |
QVERIFY(strcmp(array2, "1032547698A@CBED") == 0); |
|
405 |
::qBubbleSort((char *)array2, array2 + strlen(array2), UserFunctor1(3)); |
|
406 |
QVERIFY(strcmp(array2, "3210765498CBA@ED") == 0); |
|
407 |
::qBubbleSort((char *)array2, array2 + strlen(array2), UserFunctor1(0)); |
|
408 |
QVERIFY(strcmp(array2, "0123456789@ABCDE") == 0); |
|
409 |
} |
|
410 |
} |
|
411 |
||
412 |
void tst_QAlgorithms::swap() |
|
413 |
{ |
|
414 |
{ |
|
415 |
int a = 1, b = 2; |
|
416 |
qSwap(a, b); |
|
417 |
QVERIFY(a == 2); |
|
418 |
QVERIFY(b == 1); |
|
419 |
||
420 |
qSwap(a, a); |
|
421 |
QVERIFY(a == 2); |
|
422 |
QVERIFY(b == 1); |
|
423 |
||
424 |
qSwap(b, b); |
|
425 |
QVERIFY(a == 2); |
|
426 |
QVERIFY(b == 1); |
|
427 |
||
428 |
qSwap(a, b); |
|
429 |
QVERIFY(a == 1); |
|
430 |
QVERIFY(b == 2); |
|
431 |
||
432 |
qSwap(b, a); |
|
433 |
QVERIFY(a == 2); |
|
434 |
QVERIFY(b == 1); |
|
435 |
} |
|
436 |
||
437 |
{ |
|
438 |
double a = 1.0, b = 2.0; |
|
439 |
qSwap(a, b); |
|
440 |
QVERIFY(a == 2.0); |
|
441 |
QVERIFY(b == 1.0); |
|
442 |
||
443 |
qSwap(a, a); |
|
444 |
QVERIFY(a == 2.0); |
|
445 |
QVERIFY(b == 1.0); |
|
446 |
||
447 |
qSwap(b, b); |
|
448 |
QVERIFY(a == 2.0); |
|
449 |
QVERIFY(b == 1.0); |
|
450 |
||
451 |
qSwap(a, b); |
|
452 |
QVERIFY(a == 1.0); |
|
453 |
QVERIFY(b == 2.0); |
|
454 |
||
455 |
qSwap(b, a); |
|
456 |
QVERIFY(a == 2.0); |
|
457 |
QVERIFY(b == 1.0); |
|
458 |
} |
|
459 |
||
460 |
{ |
|
461 |
QString a = "1", b = "2"; |
|
462 |
qSwap(a, b); |
|
463 |
QVERIFY(a == "2"); |
|
464 |
QVERIFY(b == "1"); |
|
465 |
||
466 |
qSwap(a, a); |
|
467 |
QVERIFY(a == "2"); |
|
468 |
QVERIFY(b == "1"); |
|
469 |
||
470 |
qSwap(b, b); |
|
471 |
QVERIFY(a == "2"); |
|
472 |
QVERIFY(b == "1"); |
|
473 |
||
474 |
qSwap(a, b); |
|
475 |
QVERIFY(a == "1"); |
|
476 |
QVERIFY(b == "2"); |
|
477 |
||
478 |
qSwap(b, a); |
|
479 |
QVERIFY(a == "2"); |
|
480 |
QVERIFY(b == "1"); |
|
481 |
} |
|
482 |
||
483 |
{ |
|
484 |
void *a = 0, *b = 0; |
|
485 |
qSwap(a, b); |
|
486 |
} |
|
487 |
||
488 |
{ |
|
489 |
const void *a = 0, *b = 0; |
|
490 |
qSwap(a, b); |
|
491 |
} |
|
492 |
||
493 |
{ |
|
494 |
QString *a = 0, *b = 0; |
|
495 |
qSwap(a, b); |
|
496 |
} |
|
497 |
||
498 |
{ |
|
499 |
const QString *a = 0, *b = 0; |
|
500 |
qSwap(a, b); |
|
501 |
} |
|
502 |
||
503 |
{ |
|
504 |
QString **a = 0, **b = 0; |
|
505 |
qSwap(a, b); |
|
506 |
} |
|
507 |
||
508 |
{ |
|
509 |
const QString **a = 0, **b = 0; |
|
510 |
qSwap(a, b); |
|
511 |
} |
|
512 |
||
513 |
{ |
|
514 |
QString * const *a = 0, * const *b = 0; |
|
515 |
qSwap(a, b); |
|
516 |
} |
|
517 |
||
518 |
{ |
|
519 |
const QString * const *a = 0, * const *b = 0; |
|
520 |
qSwap(a, b); |
|
521 |
} |
|
522 |
} |
|
523 |
||
524 |
void tst_QAlgorithms::sortEmptyList() |
|
525 |
{ |
|
526 |
// Only test if it crashes |
|
527 |
QStringList stringList; |
|
528 |
stringList.sort(); |
|
529 |
QVERIFY(true); |
|
530 |
} |
|
531 |
||
532 |
void tst_QAlgorithms::sortedList() |
|
533 |
{ |
|
534 |
QList<int> list; |
|
535 |
list << 4 << 3 << 6; |
|
536 |
||
537 |
::qHeapSort(list.begin(), list.end()); |
|
538 |
||
539 |
QCOMPARE(list.count(), 3); |
|
540 |
QCOMPARE(list.at(0), 3); |
|
541 |
QCOMPARE(list.at(1), 4); |
|
542 |
QCOMPARE(list.at(2), 6); |
|
543 |
||
544 |
list.insert(qUpperBound(list.begin(), list.end(), 5), 5); |
|
545 |
list.insert(qUpperBound(list.begin(), list.end(), 1), 1); |
|
546 |
list.insert(qUpperBound(list.begin(), list.end(), 8), 8); |
|
547 |
||
548 |
QCOMPARE(list.count(), 6); |
|
549 |
QCOMPARE(list.at(0), 1); |
|
550 |
QCOMPARE(list.at(1), 3); |
|
551 |
QCOMPARE(list.at(2), 4); |
|
552 |
QCOMPARE(list.at(3), 5); |
|
553 |
QCOMPARE(list.at(4), 6); |
|
554 |
QCOMPARE(list.at(5), 8); |
|
555 |
} |
|
556 |
||
557 |
Q_DECLARE_METATYPE(QList<int>) |
|
558 |
||
559 |
void tst_QAlgorithms::test_qLowerBound_data() |
|
560 |
{ |
|
561 |
QTest::addColumn<QList<int> >("data"); |
|
562 |
QTest::addColumn<int>("resultValue"); |
|
563 |
QTest::addColumn<int>("resultIndex"); |
|
564 |
||
565 |
QTest::newRow("sorted-duplicate") << (QList<int>() << 1 << 2 << 2 << 3) << 2 << 1; |
|
566 |
} |
|
567 |
||
568 |
void tst_QAlgorithms::test_qLowerBound() |
|
569 |
{ |
|
570 |
QFETCH(QList<int>, data); |
|
571 |
QFETCH(int, resultValue); |
|
572 |
QFETCH(int, resultIndex); |
|
573 |
||
574 |
||
575 |
QCOMPARE(qLowerBound(data.constBegin(), data.constEnd(), resultValue), data.constBegin() + resultIndex); |
|
576 |
QCOMPARE(qLowerBound(data.begin(), data.end(), resultValue), data.begin() + resultIndex); |
|
577 |
QCOMPARE(qLowerBound(data, resultValue), data.constBegin() + resultIndex); |
|
578 |
QCOMPARE(qLowerBound(data.constBegin(), data.constEnd(), resultValue, qLess<int>()), data.constBegin() + resultIndex); |
|
579 |
} |
|
580 |
||
581 |
void tst_QAlgorithms::test_qUpperBound_data() |
|
582 |
{ |
|
583 |
QTest::addColumn<QList<int> >("data"); |
|
584 |
QTest::addColumn<int>("resultValue"); |
|
585 |
QTest::addColumn<int>("resultIndex"); |
|
586 |
||
587 |
QTest::newRow("sorted-duplicate") << (QList<int>() << 1 << 2 << 2 << 3) << 2 << 3; |
|
588 |
} |
|
589 |
||
590 |
void tst_QAlgorithms::test_qUpperBound() |
|
591 |
{ |
|
592 |
QFETCH(QList<int>, data); |
|
593 |
QFETCH(int, resultValue); |
|
594 |
QFETCH(int, resultIndex); |
|
595 |
||
596 |
QCOMPARE(qUpperBound(data.constBegin(), data.constEnd(), resultValue), data.constBegin() + resultIndex); |
|
597 |
QCOMPARE(qUpperBound(data.begin(), data.end(), resultValue), data.begin() + resultIndex); |
|
598 |
QCOMPARE(qUpperBound(data, resultValue), data.constBegin() + resultIndex); |
|
599 |
QCOMPARE(qUpperBound(data.constBegin(), data.constEnd(), resultValue, qLess<int>()), data.constBegin() + resultIndex); |
|
600 |
} |
|
601 |
||
602 |
void tst_QAlgorithms::test_qBinaryFind_data() |
|
603 |
{ |
|
604 |
QTest::addColumn<QList<int> >("data"); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
605 |
QTest::addColumn<int>("resultValue"); // -42 means not found |
0 | 606 |
|
607 |
QTest::newRow("sorted-duplicate") << (QList<int>() << 1 << 2 << 2 << 3) << 2; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
608 |
QTest::newRow("sorted-end") << (QList<int>() << -5 << -2 << 0 << 8) << 8; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
609 |
QTest::newRow("sorted-beginning") << (QList<int>() << -5 << -2 << 0 << 8) << -5; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
610 |
QTest::newRow("sorted-duplicate-beginning") << (QList<int>() << -5 << -5 << -2 << 0 << 8) << -5; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
611 |
QTest::newRow("empty") << (QList<int>()) << -42; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
612 |
QTest::newRow("not found 1 ") << (QList<int>() << 1 << 5 << 8 << 65) << -42; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
613 |
QTest::newRow("not found 2 ") << (QList<int>() << -456 << -5 << 8 << 65) << -42; |
0 | 614 |
} |
615 |
||
616 |
void tst_QAlgorithms::test_qBinaryFind() |
|
617 |
{ |
|
618 |
QFETCH(QList<int>, data); |
|
619 |
QFETCH(int, resultValue); |
|
620 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
621 |
//-42 means not found |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
622 |
if (resultValue == -42) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
623 |
QVERIFY(qBinaryFind(data.constBegin(), data.constEnd(), resultValue) == data.constEnd()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
624 |
QVERIFY(qBinaryFind(data, resultValue) == data.constEnd()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
625 |
QVERIFY(qBinaryFind(data.begin(), data.end(), resultValue) == data.end()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
626 |
QVERIFY(qBinaryFind(data.begin(), data.end(), resultValue, qLess<int>()) == data.end()); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
627 |
return; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
628 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
629 |
|
0 | 630 |
QCOMPARE(*qBinaryFind(data.constBegin(), data.constEnd(), resultValue), resultValue); |
631 |
QCOMPARE(*qBinaryFind(data.begin(), data.end(), resultValue), resultValue); |
|
632 |
QCOMPARE(*qBinaryFind(data, resultValue), resultValue); |
|
633 |
QCOMPARE(*qBinaryFind(data.constBegin(), data.constEnd(), resultValue, qLess<int>()), resultValue); |
|
634 |
} |
|
635 |
||
636 |
void tst_QAlgorithms::qBinaryFindOneEntry() |
|
637 |
{ |
|
638 |
QList<int> list; |
|
639 |
list << 2; |
|
640 |
||
641 |
QVERIFY(::qBinaryFind(list.constBegin(), list.constEnd(), 2) != list.constEnd()); |
|
642 |
} |
|
643 |
||
644 |
||
645 |
void tst_QAlgorithms::sortAPItest() |
|
646 |
{ |
|
647 |
QVector<int> testVector = generateData<int>("Random", 101); |
|
648 |
qSort(testVector); |
|
649 |
QVERIFY(isSorted(testVector)); |
|
650 |
qSort(testVector.begin(), testVector.end()); |
|
651 |
QVERIFY(isSorted(testVector)); |
|
652 |
qSort(testVector.begin(), testVector.end(), qLess<int>()); |
|
653 |
QVERIFY(isSorted(testVector)); |
|
654 |
||
655 |
testVector = generateData<int>("Random", 71); |
|
656 |
qStableSort(testVector); |
|
657 |
QVERIFY(isSorted(testVector)); |
|
658 |
qStableSort(testVector.begin(), testVector.end()); |
|
659 |
QVERIFY(isSorted(testVector)); |
|
660 |
qStableSort(testVector.begin(), testVector.end(), qLess<int>()); |
|
661 |
QVERIFY(isSorted(testVector)); |
|
662 |
||
663 |
QList<int> testList = generateData<int>("Random", 101).toList(); |
|
664 |
qSort(testList); |
|
665 |
QVERIFY(isSorted(testList)); |
|
666 |
qSort(testList.begin(), testList.end()); |
|
667 |
QVERIFY(isSorted(testList)); |
|
668 |
qSort(testList.begin(), testList.end(), qLess<int>()); |
|
669 |
QVERIFY(isSorted(testList)); |
|
670 |
||
671 |
testList = generateData<int>("Random", 71).toList(); |
|
672 |
qStableSort(testList); |
|
673 |
QVERIFY(isSorted(testList)); |
|
674 |
qStableSort(testList.begin(), testList.end()); |
|
675 |
QVERIFY(isSorted(testList)); |
|
676 |
qStableSort(testList.begin(), testList.end(), qLess<int>()); |
|
677 |
QVERIFY(isSorted(testList)); |
|
678 |
} |
|
679 |
||
680 |
||
681 |
class StableSortTest |
|
682 |
{ |
|
683 |
public: |
|
684 |
StableSortTest(){}; |
|
685 |
StableSortTest(int Major, int Minor) : Major(Major), Minor(Minor) {} |
|
686 |
bool operator<(const StableSortTest &other) const {return (Major < other.Major); } |
|
687 |
bool testMinor(const StableSortTest &other) const {return Minor < other.Minor; } |
|
688 |
||
689 |
int Major; |
|
690 |
int Minor; |
|
691 |
}; |
|
692 |
||
693 |
ostream &operator<<(ostream &out, const StableSortTest& obj) { out << obj.Major << "-" << obj.Minor; return out; } |
|
694 |
||
695 |
QVector<StableSortTest> createStableTestVector() |
|
696 |
{ |
|
697 |
QVector<StableSortTest> stableTestVector; |
|
698 |
for (int i=500; i>=0; --i) { |
|
699 |
for (int j=0; j<10; ++j) { |
|
700 |
stableTestVector.append(StableSortTest(i, j)); |
|
701 |
} |
|
702 |
} |
|
703 |
return stableTestVector; |
|
704 |
} |
|
705 |
||
706 |
template <typename ContainerType, typename LessThan> |
|
707 |
bool isStableSorted(ContainerType &container, LessThan lessThan) |
|
708 |
{ |
|
709 |
for (int i=0; i < container.count() - 1; ++i) { |
|
710 |
//not sorted? |
|
711 |
if (lessThan(container.at(i + 1), container.at(i))) |
|
712 |
return false; |
|
713 |
// equal? |
|
714 |
if (lessThan(container.at(i), container.at(i + 1))) |
|
715 |
continue; |
|
716 |
// minor version? |
|
717 |
if(container.at(i + 1).testMinor(container.at(i))) |
|
718 |
return false; |
|
719 |
} |
|
720 |
return true; |
|
721 |
} |
|
722 |
||
723 |
void tst_QAlgorithms::stableSortTest() |
|
724 |
{ |
|
725 |
// Selftests: |
|
726 |
{ |
|
727 |
QVector<StableSortTest> stableTestVector = createStableTestVector(); |
|
728 |
qSort(stableTestVector.begin(), stableTestVector.end(), qLess<StableSortTest>()); |
|
729 |
QVERIFY(isSorted(stableTestVector, qLess<StableSortTest>())); |
|
730 |
QVERIFY(!isStableSorted(stableTestVector, qLess<StableSortTest>())); |
|
731 |
} |
|
732 |
{ |
|
733 |
QVector<StableSortTest> stableTestVector = createStableTestVector(); |
|
734 |
qSort(stableTestVector.begin(), stableTestVector.end(), qGreater<StableSortTest>()); |
|
735 |
QVERIFY(isSorted(stableTestVector, qGreater<StableSortTest>())); |
|
736 |
QVERIFY(!isStableSorted(stableTestVector, qGreater<StableSortTest>())); |
|
737 |
} |
|
738 |
{ |
|
739 |
QVector<StableSortTest> stableTestVector = createStableTestVector(); |
|
740 |
qSort(stableTestVector.begin(), stableTestVector.end(), qGreater<StableSortTest>()); |
|
741 |
QVERIFY(!isSorted(stableTestVector, qLess<StableSortTest>())); |
|
742 |
QVERIFY(!isStableSorted(stableTestVector, qGreater<StableSortTest>())); |
|
743 |
} |
|
744 |
||
745 |
||
746 |
// Stable sort with qLess |
|
747 |
{ |
|
748 |
QVector<StableSortTest> stableTestVector = createStableTestVector(); |
|
749 |
std::stable_sort(stableTestVector.begin(), stableTestVector.end(), qLess<StableSortTest>()); |
|
750 |
QVERIFY(isSorted(stableTestVector, qLess<StableSortTest>())); |
|
751 |
QVERIFY(isStableSorted(stableTestVector, qLess<StableSortTest>())); |
|
752 |
} |
|
753 |
{ |
|
754 |
QVector<StableSortTest> stableTestVector = createStableTestVector(); |
|
755 |
qStableSort(stableTestVector.begin(), stableTestVector.end(), qLess<StableSortTest>()); |
|
756 |
QVERIFY(isSorted(stableTestVector, qLess<StableSortTest>())); |
|
757 |
QVERIFY(isStableSorted(stableTestVector, qLess<StableSortTest>())); |
|
758 |
} |
|
759 |
||
760 |
// Stable sort with qGreater |
|
761 |
{ |
|
762 |
QVector<StableSortTest> stableTestVector = createStableTestVector(); |
|
763 |
std::stable_sort(stableTestVector.begin(), stableTestVector.end(), qGreater<StableSortTest>()); |
|
764 |
QVERIFY(isSorted(stableTestVector, qGreater<StableSortTest>())); |
|
765 |
QVERIFY(isStableSorted(stableTestVector, qGreater<StableSortTest>())); |
|
766 |
} |
|
767 |
||
768 |
{ |
|
769 |
QVector<StableSortTest> stableTestVector = createStableTestVector(); |
|
770 |
qStableSort(stableTestVector.begin(), stableTestVector.end(), qGreater<StableSortTest>()); |
|
771 |
QVERIFY(isSorted(stableTestVector, qGreater<StableSortTest>())); |
|
772 |
QVERIFY(isStableSorted(stableTestVector, qGreater<StableSortTest>())); |
|
773 |
} |
|
774 |
} |
|
775 |
||
776 |
Q_DECLARE_METATYPE(QVector<int>) |
|
777 |
||
778 |
void tst_QAlgorithms::stableSortCorrectnessTest_data() |
|
779 |
{ |
|
780 |
const int dataSize = 1000; |
|
781 |
QTest::addColumn<QVector<int> >("unsorted"); |
|
782 |
QTest::newRow("From documentation") << (QVector<int>() << 33 << 12 << 68 << 6 << 12); |
|
783 |
QTest::newRow("Equal") << (generateData<int>("Equal", dataSize)); |
|
784 |
QTest::newRow("Ascending") << (generateData<int>("Ascending", dataSize)); |
|
785 |
QTest::newRow("Descending") << (generateData<int>("Descending", dataSize)); |
|
786 |
QTest::newRow("Duplicates") << (generateData<int>("Duplicates", dataSize)); |
|
787 |
QTest::newRow("Almost Sorted") << (generateData<int>("Almost Sorted", dataSize)); |
|
788 |
QTest::newRow("Random") << (generateData<int>("Random", dataSize)); |
|
789 |
} |
|
790 |
||
791 |
void tst_QAlgorithms::stableSortCorrectnessTest() |
|
792 |
{ |
|
793 |
QFETCH(QVector<int>, unsorted); |
|
794 |
||
795 |
QVector<int> sorted = unsorted; |
|
796 |
qStableSort(sorted.begin(), sorted.end()); |
|
797 |
||
798 |
// Verify that sorted contains the same numbers as unsorted. |
|
799 |
foreach(int value, unsorted) { |
|
800 |
QVERIFY(sorted.contains(value)); |
|
801 |
int unsortedCount = 0; |
|
802 |
qCount(unsorted.begin(), unsorted.end(), value, unsortedCount); |
|
803 |
int sortedCount = 0; |
|
804 |
qCount(sorted.begin(), sorted.end(), value, sortedCount); |
|
805 |
QCOMPARE(sortedCount, unsortedCount); |
|
806 |
} |
|
807 |
||
808 |
QVERIFY(isSorted(sorted)); |
|
809 |
} |
|
810 |
||
811 |
void tst_QAlgorithms::convenienceAPI() |
|
812 |
{ |
|
813 |
// Compile-test for QAlgorithm convenience functions. |
|
814 |
QList<int> list, list2; |
|
815 |
||
816 |
qCopy(list.begin(), list.end(), list2.begin()); |
|
817 |
qCopyBackward(list.begin(), list.end(), list2.begin()); |
|
818 |
qEqual(list.begin(), list.end(), list2.begin()); |
|
819 |
||
820 |
qFill(list, 1); |
|
821 |
qFill(list.begin(), list.end(), 1); |
|
822 |
||
823 |
qFind(list, 1); |
|
824 |
qFind(list.begin(), list.end(), 1); |
|
825 |
||
826 |
int count1 = 0 , count2 = 0, count3 = 0; |
|
827 |
qCount(list, 1, count1); |
|
828 |
qCount(list.begin(), list.end(), 1, count2); |
|
829 |
QCOMPARE(count1, count2); |
|
830 |
QCOMPARE(count2, count3); |
|
831 |
||
832 |
qSort(list); |
|
833 |
qSort(list.begin(), list.end()); |
|
834 |
qSort(list.begin(), list.end(), qLess<int>()); |
|
835 |
||
836 |
qStableSort(list); |
|
837 |
qStableSort(list.begin(), list.end()); |
|
838 |
qStableSort(list.begin(), list.end(), qLess<int>()); |
|
839 |
||
840 |
qLowerBound(list, 1);; |
|
841 |
qLowerBound(list.begin(), list.end(), 1); |
|
842 |
qLowerBound(list.begin(), list.end(), 1, qLess<int>()); |
|
843 |
||
844 |
qUpperBound(list, 1); |
|
845 |
qUpperBound(list.begin(), list.end(), 1); |
|
846 |
qUpperBound(list.begin(), list.end(), 1, qLess<int>()); |
|
847 |
||
848 |
qBinaryFind(list, 1); |
|
849 |
qBinaryFind(list.begin(), list.end(), 1); |
|
850 |
qBinaryFind(list.begin(), list.end(), 1, qLess<int>()); |
|
851 |
||
852 |
QList<int *> pointerList; |
|
853 |
qDeleteAll(pointerList); |
|
854 |
qDeleteAll(pointerList.begin(), pointerList.end()); |
|
855 |
} |
|
856 |
||
857 |
template <typename DataType> |
|
858 |
class HeapSortHelper |
|
859 |
{ |
|
860 |
public: |
|
861 |
void operator()(QVector<DataType> list) |
|
862 |
{ |
|
863 |
::qHeapSort(list); |
|
864 |
} |
|
865 |
}; |
|
866 |
||
867 |
template <typename DataType> |
|
868 |
class BubbleSortHelper |
|
869 |
{ |
|
870 |
public: |
|
871 |
void operator()(QVector<DataType> list) |
|
872 |
{ |
|
873 |
::qBubbleSort(list); |
|
874 |
} |
|
875 |
}; |
|
876 |
||
877 |
template <typename DataType> |
|
878 |
class QuickSortHelper |
|
879 |
{ |
|
880 |
public: |
|
881 |
void operator()(QVector<DataType> list) |
|
882 |
{ |
|
883 |
::qSort(list); |
|
884 |
} |
|
885 |
}; |
|
886 |
||
887 |
template <typename DataType> |
|
888 |
class StableSortHelper |
|
889 |
{ |
|
890 |
public: |
|
891 |
void operator()(QVector<DataType> list) |
|
892 |
{ |
|
893 |
::qStableSort(list); |
|
894 |
} |
|
895 |
}; |
|
896 |
||
897 |
template <typename DataType> |
|
898 |
class StlSortHelper |
|
899 |
{ |
|
900 |
public: |
|
901 |
void operator()(QVector<DataType> list) |
|
902 |
{ |
|
903 |
std::sort(list.begin(), list.end()); |
|
904 |
} |
|
905 |
}; |
|
906 |
||
907 |
template <typename DataType> |
|
908 |
class StlStableSortHelper |
|
909 |
{ |
|
910 |
public: |
|
911 |
void operator()(QVector<DataType> list) |
|
912 |
{ |
|
913 |
std::stable_sort(list.begin(), list.end()); |
|
914 |
} |
|
915 |
}; |
|
916 |
||
917 |
#if Q_TEST_PERFORMANCE |
|
918 |
void tst_QAlgorithms::performance() |
|
919 |
{ |
|
920 |
cout << endl << "Quick sort" << endl; |
|
921 |
testAlgorithm<QuickSortHelper<TestInt>, TestInt>(QuickSortHelper<TestInt>(), dataSetTypes); |
|
922 |
cout << endl << "stable sort" << endl; |
|
923 |
testAlgorithm<StableSortHelper<TestInt>, TestInt>(StableSortHelper<TestInt>(), dataSetTypes); |
|
924 |
cout << endl << "std::sort" << endl; |
|
925 |
testAlgorithm<StlSortHelper<TestInt>, TestInt>(StlSortHelper<TestInt>(), dataSetTypes); |
|
926 |
cout << endl << "std::stable_sort" << endl; |
|
927 |
testAlgorithm<StlStableSortHelper<TestInt>, TestInt>(StlStableSortHelper<TestInt>(), dataSetTypes); |
|
928 |
cout << "Heap sort" << endl; |
|
929 |
testAlgorithm<HeapSortHelper<TestInt>, TestInt>(HeapSortHelper<TestInt>(), dataSetTypes); |
|
930 |
cout << endl << "Bubble sort" << endl; |
|
931 |
testAlgorithm<BubbleSortHelper<TestInt>, TestInt>(BubbleSortHelper<TestInt>(), dataSetTypes); |
|
932 |
/* |
|
933 |
cout << endl << "Sorting lists of ints" << endl; |
|
934 |
cout << "Heap sort" << endl; |
|
935 |
testAlgorithm<HeapSortHelper<int>, int>(HeapSortHelper<int>(), dataSetTypes); |
|
936 |
cout << endl << "Quick sort" << endl; |
|
937 |
testAlgorithm<QuickSortHelper<int>, int>(QuickSortHelper<int>(), dataSetTypes); |
|
938 |
cout << endl << "std::sort" << endl; |
|
939 |
testAlgorithm<StlSortHelper<int>, int>(StlSortHelper<int>(), dataSetTypes); |
|
940 |
cout << endl << "std::stable_sort" << endl; |
|
941 |
testAlgorithm<StlStableSortHelper<int>, int>(StlStableSortHelper<int>(), dataSetTypes); |
|
942 |
cout << endl << "Bubble sort" << endl; |
|
943 |
testAlgorithm<BubbleSortHelper<int>, int>(BubbleSortHelper<int>(), dataSetTypes); |
|
944 |
*/ |
|
945 |
} |
|
946 |
#endif |
|
947 |
||
948 |
void tst_QAlgorithms::qCountIterators() const |
|
949 |
{ |
|
950 |
QList<int> list; |
|
951 |
list << 3 << 3 << 6 << 6 << 6 << 8; |
|
952 |
||
953 |
{ |
|
954 |
int countOf7 = 0; |
|
955 |
::qCount(list.begin(), list.end(), 7, countOf7); |
|
956 |
QCOMPARE(countOf7, 0); |
|
957 |
} |
|
958 |
||
959 |
{ |
|
960 |
int countOf3 = 0; |
|
961 |
::qCount(list.begin(), list.end(), 3, countOf3); |
|
962 |
QCOMPARE(countOf3, 2); |
|
963 |
} |
|
964 |
||
965 |
{ |
|
966 |
int countOf6 = 0; |
|
967 |
::qCount(list.begin(), list.end(), 6, countOf6); |
|
968 |
QCOMPARE(countOf6, 3); |
|
969 |
} |
|
970 |
||
971 |
{ |
|
972 |
int countOf8 = 0; |
|
973 |
::qCount(list.begin(), list.end(), 8, countOf8); |
|
974 |
QCOMPARE(countOf8, 1); |
|
975 |
} |
|
976 |
||
977 |
/* Check that we add to the count, not set it. */ |
|
978 |
{ |
|
979 |
int countOf8 = 5; |
|
980 |
::qCount(list.begin(), list.end(), 8, countOf8); |
|
981 |
QCOMPARE(countOf8, 6); |
|
982 |
} |
|
983 |
} |
|
984 |
||
985 |
void tst_QAlgorithms::qCountContainer() const |
|
986 |
{ |
|
987 |
QList<int> list; |
|
988 |
list << 3 << 3 << 6 << 6 << 6 << 8; |
|
989 |
||
990 |
{ |
|
991 |
int countOf7 = 0; |
|
992 |
::qCount(list, 7, countOf7); |
|
993 |
QCOMPARE(countOf7, 0); |
|
994 |
} |
|
995 |
||
996 |
{ |
|
997 |
int countOf3 = 0; |
|
998 |
::qCount(list, 3, countOf3); |
|
999 |
QCOMPARE(countOf3, 2); |
|
1000 |
} |
|
1001 |
||
1002 |
{ |
|
1003 |
int countOf6 = 0; |
|
1004 |
::qCount(list, 6, countOf6); |
|
1005 |
QCOMPARE(countOf6, 3); |
|
1006 |
} |
|
1007 |
||
1008 |
{ |
|
1009 |
int countOf8 = 0; |
|
1010 |
::qCount(list, 8, countOf8); |
|
1011 |
QCOMPARE(countOf8, 1); |
|
1012 |
} |
|
1013 |
||
1014 |
/* Check that we add to the count, not set it. */ |
|
1015 |
{ |
|
1016 |
int countOf8 = 5; |
|
1017 |
::qCount(list, 8, countOf8); |
|
1018 |
QCOMPARE(countOf8, 6); |
|
1019 |
} |
|
1020 |
} |
|
1021 |
||
1022 |
class RAI |
|
1023 |
{ |
|
1024 |
public: |
|
1025 |
RAI(int searched = 5, int hidePos = 4, int len = 10) |
|
1026 |
: curPos_(0) |
|
1027 |
, length_(len) |
|
1028 |
, searchedVal_(searched) |
|
1029 |
, searchedValPos_(hidePos) |
|
1030 |
{ |
|
1031 |
} |
|
1032 |
||
1033 |
int at(int pos) const |
|
1034 |
{ |
|
1035 |
if (pos == searchedValPos_) { |
|
1036 |
return searchedVal_; |
|
1037 |
} |
|
1038 |
else if (pos < searchedValPos_) { |
|
1039 |
return searchedVal_ - 1; |
|
1040 |
} |
|
1041 |
||
1042 |
return searchedVal_ + 1; |
|
1043 |
} |
|
1044 |
||
1045 |
RAI begin() const |
|
1046 |
{ |
|
1047 |
RAI rai = *this; |
|
1048 |
rai.setCurPos(0); |
|
1049 |
return rai; |
|
1050 |
} |
|
1051 |
||
1052 |
RAI end() const |
|
1053 |
{ |
|
1054 |
RAI rai = *this; |
|
1055 |
rai.setCurPos(length_); |
|
1056 |
return rai; |
|
1057 |
} |
|
1058 |
||
1059 |
int pos() const |
|
1060 |
{ |
|
1061 |
return curPos(); |
|
1062 |
} |
|
1063 |
||
1064 |
int size() const |
|
1065 |
{ |
|
1066 |
return length_; |
|
1067 |
} |
|
1068 |
||
1069 |
RAI operator+(int i) const |
|
1070 |
{ |
|
1071 |
RAI rai = *this; |
|
1072 |
rai.setCurPos( rai.curPos() + i ); |
|
1073 |
if (rai.curPos() > length_) { |
|
1074 |
rai.setCurPos(length_); |
|
1075 |
} |
|
1076 |
return rai; |
|
1077 |
} |
|
1078 |
||
1079 |
RAI operator-(int i) const |
|
1080 |
{ |
|
1081 |
RAI rai = *this; |
|
1082 |
rai.setCurPos( rai.curPos() - i ); |
|
1083 |
if (rai.curPos() < 0) { |
|
1084 |
rai.setCurPos(0); |
|
1085 |
} |
|
1086 |
return rai; |
|
1087 |
} |
|
1088 |
||
1089 |
int operator-(const RAI& it) const |
|
1090 |
{ |
|
1091 |
return curPos() - it.curPos(); |
|
1092 |
} |
|
1093 |
||
1094 |
RAI& operator+=(int i) |
|
1095 |
{ |
|
1096 |
setCurPos( curPos() + i ); |
|
1097 |
if (curPos() > length_) { |
|
1098 |
setCurPos(length_); |
|
1099 |
} |
|
1100 |
return *this; |
|
1101 |
} |
|
1102 |
||
1103 |
RAI& operator-=(int i) |
|
1104 |
{ |
|
1105 |
setCurPos( curPos() - i); |
|
1106 |
if (curPos() < 0) { |
|
1107 |
setCurPos(0); |
|
1108 |
} |
|
1109 |
return *this; |
|
1110 |
} |
|
1111 |
||
1112 |
RAI& operator++() |
|
1113 |
{ |
|
1114 |
if (curPos() < length_) { |
|
1115 |
setCurPos( curPos() + 1 ); |
|
1116 |
} |
|
1117 |
return *this; |
|
1118 |
} |
|
1119 |
||
1120 |
RAI operator++(int) |
|
1121 |
{ |
|
1122 |
RAI rai = *this; |
|
1123 |
||
1124 |
if (curPos() < length_) { |
|
1125 |
setCurPos( curPos() + 1 ); |
|
1126 |
} |
|
1127 |
||
1128 |
return rai; |
|
1129 |
} |
|
1130 |
||
1131 |
RAI& operator--() |
|
1132 |
{ |
|
1133 |
if (curPos() > 0) { |
|
1134 |
setCurPos( curPos() - 1 ); |
|
1135 |
} |
|
1136 |
return *this; |
|
1137 |
} |
|
1138 |
||
1139 |
RAI operator--(int) |
|
1140 |
{ |
|
1141 |
RAI rai = *this; |
|
1142 |
||
1143 |
if (curPos() > 0) { |
|
1144 |
setCurPos( curPos() - 1 ); |
|
1145 |
} |
|
1146 |
||
1147 |
return rai; |
|
1148 |
} |
|
1149 |
||
1150 |
bool operator==(const RAI& rai) const |
|
1151 |
{ |
|
1152 |
return rai.curPos() == curPos(); |
|
1153 |
} |
|
1154 |
||
1155 |
bool operator!=(const RAI& rai) const |
|
1156 |
{ |
|
1157 |
return !operator==(rai); |
|
1158 |
} |
|
1159 |
||
1160 |
int operator*() const |
|
1161 |
{ |
|
1162 |
return at(curPos()); |
|
1163 |
} |
|
1164 |
||
1165 |
int operator[](int i) const |
|
1166 |
{ |
|
1167 |
return at(i); |
|
1168 |
} |
|
1169 |
||
1170 |
private: |
|
1171 |
||
1172 |
int curPos() const |
|
1173 |
{ |
|
1174 |
return curPos_; |
|
1175 |
} |
|
1176 |
||
1177 |
void setCurPos(int pos) |
|
1178 |
{ |
|
1179 |
curPos_ = pos; |
|
1180 |
} |
|
1181 |
||
1182 |
int curPos_; |
|
1183 |
int length_; |
|
1184 |
int searchedVal_; |
|
1185 |
int searchedValPos_; |
|
1186 |
}; |
|
1187 |
||
1188 |
void tst_QAlgorithms::binaryFindOnLargeContainer() const |
|
1189 |
{ |
|
1190 |
const int len = 2 * 1000 * 1000 * 537; |
|
1191 |
const int pos = len - 12345; |
|
1192 |
RAI rai(5, pos, len); |
|
1193 |
||
1194 |
RAI foundIt = qBinaryFind(rai.begin(), rai.end(), 5); |
|
1195 |
QCOMPARE(foundIt.pos(), 1073987655); |
|
1196 |
} |
|
1197 |
||
1198 |
QTEST_APPLESS_MAIN(tst_QAlgorithms) |
|
1199 |
#include "tst_qalgorithms.moc" |
|
1200 |