1 /****************************************************************************
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 ** This file is part of the test suite of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
10 ** This file contains pre-release code and may not be distributed.
11 ** You may use this file in accordance with the terms and conditions
12 ** contained in the Technology Preview License Agreement accompanying
15 ** GNU Lesser General Public License Usage
16 ** Alternatively, this file may be used under the terms of the GNU Lesser
17 ** General Public License version 2.1 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.LGPL included in the
19 ** packaging of this file. Please review the following information to
20 ** ensure the GNU Lesser General Public License version 2.1 requirements
21 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23 ** In addition, as a special exception, Nokia gives you certain
24 ** additional rights. These rights are described in the Nokia Qt LGPL
25 ** Exception version 1.1, included in the file LGPL_EXCEPTION.txt in this
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
41 #include <qtconcurrentmap.h>
42 #include <qtconcurrentexception.h>
47 #include <QtTest/QtTest>
49 #include "functions.h"
50 #include "../qfuture/versioncheck.h"
52 Q_DECLARE_METATYPE(QVector
<int>);
53 Q_DECLARE_METATYPE(QVector
<double>);
54 Q_DECLARE_METATYPE(QVector
<QString
>);
55 Q_DECLARE_METATYPE(QList
<int>);
56 Q_DECLARE_METATYPE(QList
<double>);
57 Q_DECLARE_METATYPE(QList
<QString
>);
59 class tst_map
: public QObject
66 void blocking_mapped();
68 void blocking_mappedReduced();
70 void functionOverloads();
71 #ifndef QT_NO_EXCEPTIONS
74 void incrementalResults();
77 void qFutureAssignmentLeak();
83 #if !defined (QT_NO_CONCURRENT_TEST) && !defined(QT_NO_CONCURRENT_MAP)
85 using namespace QtConcurrent
;
87 void multiplyBy2Immutable(int x
)
92 class MultiplyBy2Immutable
95 void operator()(int x
)
101 void multiplyBy2InPlace(int &x
)
106 class MultiplyBy2InPlace
109 void operator()(int &x
)
115 Q_DECLARE_METATYPE(QList
<Number
>);
119 // functors take arguments by reference, modifying the sequence in place
125 QtConcurrent::map(list
, MultiplyBy2InPlace()).waitForFinished();
126 QCOMPARE(list
, QList
<int>() << 2 << 4 << 6);
127 QtConcurrent::map(list
.begin(), list
.end(), MultiplyBy2InPlace()).waitForFinished();
128 QCOMPARE(list
, QList
<int>() << 4 << 8 << 12);
131 QtConcurrent::map(list
, multiplyBy2InPlace
).waitForFinished();
132 QCOMPARE(list
, QList
<int>() << 8 << 16 << 24);
133 QtConcurrent::map(list
.begin(), list
.end(), multiplyBy2InPlace
).waitForFinished();
134 QCOMPARE(list
, QList
<int>() << 16 << 32 << 48);
137 QtConcurrent::map(list
, multiplyBy2InPlace
).waitForFinished();
138 QCOMPARE(list
, QList
<int>() << 32 << 64 << 96);
139 QtConcurrent::map(list
.begin(), list
.end(), multiplyBy2InPlace
).waitForFinished();
140 QCOMPARE(list
, QList
<int>() << 64 << 128 << 192);
143 QList
<Number
> numberList
;
144 numberList
<< 1 << 2 << 3;
145 QtConcurrent::map(numberList
, &Number::multiplyBy2
).waitForFinished();
146 QCOMPARE(numberList
, QList
<Number
>() << 2 << 4 << 6);
147 QtConcurrent::map(numberList
.begin(), numberList
.end(), &Number::multiplyBy2
).waitForFinished();
148 QCOMPARE(numberList
, QList
<Number
>() << 4 << 8 << 12);
151 // functors don't take arguments by reference, making these no-ops
157 QtConcurrent::map(list
, MultiplyBy2Immutable()).waitForFinished();
158 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
159 QtConcurrent::map(list
.begin(), list
.end(), MultiplyBy2Immutable()).waitForFinished();
160 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
163 QtConcurrent::map(list
, multiplyBy2Immutable
).waitForFinished();
164 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
165 QtConcurrent::map(list
.begin(), list
.end(), multiplyBy2Immutable
).waitForFinished();
166 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
169 QtConcurrent::map(list
, multiplyBy2Immutable
).waitForFinished();
170 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
171 QtConcurrent::map(list
.begin(), list
.end(), multiplyBy2Immutable
).waitForFinished();
172 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
175 // Linked lists and forward iterators
177 QLinkedList
<int> list
;
181 QtConcurrent::map(list
, MultiplyBy2InPlace()).waitForFinished();
182 QCOMPARE(list
, QLinkedList
<int>() << 2 << 4 << 6);
183 QtConcurrent::map(list
.begin(), list
.end(), MultiplyBy2InPlace()).waitForFinished();
184 QCOMPARE(list
, QLinkedList
<int>() << 4 << 8 << 12);
187 QtConcurrent::map(list
, multiplyBy2InPlace
).waitForFinished();
188 QCOMPARE(list
, QLinkedList
<int>() << 8 << 16 << 24);
189 QtConcurrent::map(list
.begin(), list
.end(), multiplyBy2InPlace
).waitForFinished();
190 QCOMPARE(list
, QLinkedList
<int>() << 16 << 32 << 48);
193 QtConcurrent::map(list
, multiplyBy2InPlace
).waitForFinished();
194 QCOMPARE(list
, QLinkedList
<int>() << 32 << 64 << 96);
195 QtConcurrent::map(list
.begin(), list
.end(), multiplyBy2InPlace
).waitForFinished();
196 QCOMPARE(list
, QLinkedList
<int>() << 64 << 128 << 192);
199 QLinkedList
<Number
> numberList
;
200 numberList
<< 1 << 2 << 3;
201 QtConcurrent::map(numberList
, &Number::multiplyBy2
).waitForFinished();
202 QCOMPARE(numberList
, QLinkedList
<Number
>() << 2 << 4 << 6);
203 QtConcurrent::map(numberList
.begin(), numberList
.end(), &Number::multiplyBy2
).waitForFinished();
204 QCOMPARE(numberList
, QLinkedList
<Number
>() << 4 << 8 << 12);
208 // not allowed: map() with immutable sequences makes no sense
210 const QList
<int> list
= QList
<int>() << 1 << 2 << 3;
212 QtConcurrent::map(list
, MultiplyBy2Immutable());
213 QtConcurrent::map(list
, multiplyBy2Immutable
);
214 QtConcurrent::map(list
, multiplyBy2Immutable
);
219 // not allowed: in place modification of a temp copy (since temp copy goes out of scope)
224 QtConcurrent::map(QList
<int>(list
), MultiplyBy2InPlace());
225 QtConcurrent::map(QList
<int>(list
), multiplyBy2
);
226 QtConcurrent::map(QList
<int>(list
), multiplyBy2InPlace
);
228 QList
<Number
> numberList
;
229 numberList
<< 1 << 2 << 3;
230 QtConcurrent::map(QList
<Number
>(numberList
), &Number::multiplyBy2
);
235 // not allowed: map() on a const list, where functors try to modify the items in the list
237 const QList
<int> list
= QList
<int>() << 1 << 2 << 3;;
239 QtConcurrent::map(list
, MultiplyBy2InPlace());
240 QtConcurrent::map(list
, multiplyBy2InPlace
);
241 QtConcurrent::map(list
, multiplyBy2InPlace
);
243 const QList
<Number
> numberList
= QList
<Number
>() << 1 << 2 << 3;
244 QtConcurrent::map(numberList
, &Number::multiplyBy2
);
249 void tst_map::blocking_map()
251 // functors take arguments by reference, modifying the sequence in place
257 QtConcurrent::blockingMap(list
, MultiplyBy2InPlace());
258 QCOMPARE(list
, QList
<int>() << 2 << 4 << 6);
259 QtConcurrent::blockingMap(list
.begin(), list
.end(), MultiplyBy2InPlace());
260 QCOMPARE(list
, QList
<int>() << 4 << 8 << 12);
263 QtConcurrent::blockingMap(list
, multiplyBy2InPlace
);
264 QCOMPARE(list
, QList
<int>() << 8 << 16 << 24);
265 QtConcurrent::blockingMap(list
.begin(), list
.end(), multiplyBy2InPlace
);
266 QCOMPARE(list
, QList
<int>() << 16 << 32 << 48);
269 QtConcurrent::blockingMap(list
, multiplyBy2InPlace
);
270 QCOMPARE(list
, QList
<int>() << 32 << 64 << 96);
271 QtConcurrent::blockingMap(list
.begin(), list
.end(), multiplyBy2InPlace
);
272 QCOMPARE(list
, QList
<int>() << 64 << 128 << 192);
275 QList
<Number
> numberList
;
276 numberList
<< 1 << 2 << 3;
277 QtConcurrent::blockingMap(numberList
, &Number::multiplyBy2
);
278 QCOMPARE(numberList
, QList
<Number
>() << 2 << 4 << 6);
279 QtConcurrent::blockingMap(numberList
.begin(), numberList
.end(), &Number::multiplyBy2
);
280 QCOMPARE(numberList
, QList
<Number
>() << 4 << 8 << 12);
283 // functors don't take arguments by reference, making these no-ops
289 QtConcurrent::blockingMap(list
, MultiplyBy2Immutable());
290 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
291 QtConcurrent::blockingMap(list
.begin(), list
.end(), MultiplyBy2Immutable());
292 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
295 QtConcurrent::blockingMap(list
, multiplyBy2Immutable
);
296 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
297 QtConcurrent::blockingMap(list
.begin(), list
.end(), multiplyBy2Immutable
);
298 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
301 QtConcurrent::blockingMap(list
, multiplyBy2Immutable
);
302 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
303 QtConcurrent::blockingMap(list
.begin(), list
.end(), multiplyBy2Immutable
);
304 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
307 // Linked lists and forward iterators
309 QLinkedList
<int> list
;
313 QtConcurrent::blockingMap(list
, MultiplyBy2InPlace());
314 QCOMPARE(list
, QLinkedList
<int>() << 2 << 4 << 6);
315 QtConcurrent::blockingMap(list
.begin(), list
.end(), MultiplyBy2InPlace());
316 QCOMPARE(list
, QLinkedList
<int>() << 4 << 8 << 12);
319 QtConcurrent::blockingMap(list
, multiplyBy2InPlace
);
320 QCOMPARE(list
, QLinkedList
<int>() << 8 << 16 << 24);
321 QtConcurrent::blockingMap(list
.begin(), list
.end(), multiplyBy2InPlace
);
322 QCOMPARE(list
, QLinkedList
<int>() << 16 << 32 << 48);
325 QtConcurrent::blockingMap(list
, multiplyBy2InPlace
);
326 QCOMPARE(list
, QLinkedList
<int>() << 32 << 64 << 96);
327 QtConcurrent::blockingMap(list
.begin(), list
.end(), multiplyBy2InPlace
);
328 QCOMPARE(list
, QLinkedList
<int>() << 64 << 128 << 192);
331 QLinkedList
<Number
> numberList
;
332 numberList
<< 1 << 2 << 3;
333 QtConcurrent::blockingMap(numberList
, &Number::multiplyBy2
);
334 QCOMPARE(numberList
, QLinkedList
<Number
>() << 2 << 4 << 6);
335 QtConcurrent::blockingMap(numberList
.begin(), numberList
.end(), &Number::multiplyBy2
);
336 QCOMPARE(numberList
, QLinkedList
<Number
>() << 4 << 8 << 12);
340 // not allowed: map() with immutable sequences makes no sense
342 const QList
<int> list
= QList
<int>() << 1 << 2 << 3;
344 QtConcurrent::blockingMap(list
, MultiplyBy2Immutable());
345 QtConcurrent::blockkng::map(list
, multiplyBy2Immutable
);
346 QtConcurrent::blockingMap(list
, multiplyBy2Immutable
);
351 // not allowed: in place modification of a temp copy (since temp copy goes out of scope)
356 QtConcurrent::blockingMap(QList
<int>(list
), MultiplyBy2InPlace());
357 QtConcurrent::blockingMap(QList
<int>(list
), multiplyBy2
);
358 QtConcurrent::blockingMap(QList
<int>(list
), multiplyBy2InPlace
);
360 QList
<Number
> numberList
;
361 numberList
<< 1 << 2 << 3;
362 QtConcurrent::blockingMap(QList
<Number
>(numberList
), &Number::multiplyBy2
);
367 // not allowed: map() on a const list, where functors try to modify the items in the list
369 const QList
<int> list
= QList
<int>() << 1 << 2 << 3;;
371 QtConcurrent::blockingMap(list
, MultiplyBy2InPlace());
372 QtConcurrent::blockingMap(list
, multiplyBy2InPlace
);
373 QtConcurrent::blockingMap(list
, multiplyBy2InPlace
);
375 const QList
<Number
> numberList
= QList
<Number
>() << 1 << 2 << 3;
376 QtConcurrent::blockingMap(numberList
, &Number::multiplyBy2
);
381 int multiplyBy2(int x
)
390 typedef int result_type
;
392 int operator()(int x
) const
399 double intToDouble(int x
)
407 typedef double result_type
;
409 double operator()(int x
) const
415 int stringToInt(const QString
&string
)
417 return string
.toInt();
423 typedef int result_type
;
425 int operator()(const QString
&string
) const
427 return string
.toInt();
431 void tst_map::mapped()
435 QLinkedList
<int> linkedList
;
436 linkedList
<< 1 << 2 << 3;
437 QList
<Number
> numberList
;
438 numberList
<< 1 << 2 << 3;
439 QLinkedList
<Number
> numberLinkedList
;
440 numberLinkedList
<< 1 << 2 << 3;
444 QList
<int> list2
= QtConcurrent::mapped(list
, MultiplyBy2()).results();
445 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
446 QCOMPARE(list2
, QList
<int>() << 2 << 4 << 6);
448 QList
<int> list3
= QtConcurrent::mapped(list
.constBegin(),
450 MultiplyBy2()).results();
451 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
452 QCOMPARE(list3
, QList
<int>() << 2 << 4 << 6);
454 QList
<int> list4
= QtConcurrent::mapped(QList
<int>(list
), MultiplyBy2()).results();
455 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
456 QCOMPARE(list4
, QList
<int>() << 2 << 4 << 6);
459 QList
<int> list2
= QtConcurrent::mapped(linkedList
, MultiplyBy2()).results();
460 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
461 QCOMPARE(list2
, QList
<int>() << 2 << 4 << 6);
463 QList
<int> list3
= QtConcurrent::mapped(linkedList
.constBegin(),
464 linkedList
.constEnd(),
465 MultiplyBy2()).results();
466 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
467 QCOMPARE(list3
, QList
<int>() << 2 << 4 << 6);
470 QtConcurrent::mapped(QLinkedList
<int>(linkedList
), MultiplyBy2()).results();
471 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
472 QCOMPARE(list4
, QList
<int>() << 2 << 4 << 6);
477 QList
<int> list2
= QtConcurrent::mapped(list
, multiplyBy2
).results();
478 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
479 QCOMPARE(list2
, QList
<int>() << 2 << 4 << 6);
481 QList
<int> list3
= QtConcurrent::mapped(list
.constBegin(),
483 multiplyBy2
).results();
484 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
485 QCOMPARE(list3
, QList
<int>() << 2 << 4 << 6);
487 QList
<int> list4
= QtConcurrent::mapped(QList
<int>(list
), multiplyBy2
).results();
488 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
489 QCOMPARE(list4
, QList
<int>() << 2 << 4 << 6);
492 QList
<int> list2
= QtConcurrent::mapped(linkedList
, multiplyBy2
).results();
493 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
494 QCOMPARE(list2
, QList
<int>() << 2 << 4 << 6);
496 QList
<int> list3
= QtConcurrent::mapped(linkedList
.constBegin(),
497 linkedList
.constEnd(),
498 multiplyBy2
).results();
499 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
500 QCOMPARE(list3
, QList
<int>() << 2 << 4 << 6);
503 QtConcurrent::mapped(QLinkedList
<int>(linkedList
), multiplyBy2
).results();
504 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
505 QCOMPARE(list4
, QList
<int>() << 2 << 4 << 6);
510 QList
<int> list2
= QtConcurrent::mapped(list
, multiplyBy2
).results();
511 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
512 QCOMPARE(list2
, QList
<int>() << 2 << 4 << 6);
514 QList
<int> list3
= QtConcurrent::mapped(list
.constBegin(),
516 multiplyBy2
).results();
517 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
518 QCOMPARE(list3
, QList
<int>() << 2 << 4 << 6);
520 QList
<int> list4
= QtConcurrent::mapped(QList
<int>(list
), multiplyBy2
).results();
521 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
522 QCOMPARE(list4
, QList
<int>() << 2 << 4 << 6);
525 QList
<int> list2
= QtConcurrent::mapped(linkedList
, multiplyBy2
).results();
526 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
527 QCOMPARE(list2
, QList
<int>() << 2 << 4 << 6);
529 QList
<int> list3
= QtConcurrent::mapped(linkedList
.constBegin(),
530 linkedList
.constEnd(),
533 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
534 QCOMPARE(list3
, QList
<int>() << 2 << 4 << 6);
536 QList
<int> list4
= QtConcurrent::mapped(QLinkedList
<int>(linkedList
), multiplyBy2
)
538 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
539 QCOMPARE(list4
, QList
<int>() << 2 << 4 << 6);
542 // const member function
544 QList
<Number
> numberList2
= QtConcurrent::mapped(numberList
, &Number::multipliedBy2
)
546 QCOMPARE(numberList
, QList
<Number
>() << 1 << 2 << 3);
547 QCOMPARE(numberList2
, QList
<Number
>() << 2 << 4 << 6);
549 QList
<Number
> numberList3
= QtConcurrent::mapped(numberList
.constBegin(),
550 numberList
.constEnd(),
551 &Number::multipliedBy2
)
553 QCOMPARE(numberList
, QList
<Number
>() << 1 << 2 << 3);
554 QCOMPARE(numberList3
, QList
<Number
>() << 2 << 4 << 6);
556 QList
<Number
> numberList4
= QtConcurrent::mapped(QList
<Number
>(numberList
),
557 &Number::multipliedBy2
)
559 QCOMPARE(numberList
, QList
<Number
>() << 1 << 2 << 3);
560 QCOMPARE(numberList4
, QList
<Number
>() << 2 << 4 << 6);
563 QList
<Number
> numberList2
= QtConcurrent::mapped(numberLinkedList
, &Number::multipliedBy2
)
565 QCOMPARE(numberLinkedList
, QLinkedList
<Number
>() << 1 << 2 << 3);
566 QCOMPARE(numberList2
, QList
<Number
>() << 2 << 4 << 6);
568 QList
<Number
> numberList3
= QtConcurrent::mapped(numberLinkedList
.constBegin(),
569 numberLinkedList
.constEnd(),
570 &Number::multipliedBy2
)
572 QCOMPARE(numberLinkedList
, QLinkedList
<Number
>() << 1 << 2 << 3);
573 QCOMPARE(numberList3
, QList
<Number
>() << 2 << 4 << 6);
575 QList
<Number
> numberList4
= QtConcurrent::mapped(QLinkedList
<Number
>(numberLinkedList
),
576 &Number::multipliedBy2
)
578 QCOMPARE(numberLinkedList
, QLinkedList
<Number
>() << 1 << 2 << 3);
579 QCOMPARE(numberList4
, QList
<Number
>() << 2 << 4 << 6);
582 // change the value_type, same container
586 QList
<double> list2
= QtConcurrent::mapped(list
, IntToDouble()).results();
587 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
588 QCOMPARE(list2
, QList
<double>() << 1.0 << 2.0 << 3.0);
590 QList
<double> list3
= QtConcurrent::mapped(list
.constBegin(),
594 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
595 QCOMPARE(list3
, QList
<double>() << 1.0 << 2.0 << 3.0);
597 QList
<double> list4
= QtConcurrent::mapped(QList
<int>(list
),
600 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
601 QCOMPARE(list4
, QList
<double>() << 1.0 << 2.0 << 3.0);
604 QList
<double> list2
= QtConcurrent::mapped(linkedList
, IntToDouble()).results();
605 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
606 QCOMPARE(list2
, QList
<double>() << 1.0 << 2.0 << 3.0);
608 QList
<double> list3
= QtConcurrent::mapped(linkedList
.constBegin(),
609 linkedList
.constEnd(),
612 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
613 QCOMPARE(list3
, QList
<double>() << 1.0 << 2.0 << 3.0);
615 QList
<double> list4
= QtConcurrent::mapped(QLinkedList
<int>(linkedList
),
618 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
619 QCOMPARE(list4
, QList
<double>() << 1.0 << 2.0 << 3.0);
624 QList
<double> list2
= QtConcurrent::mapped(list
, intToDouble
).results();
625 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
626 QCOMPARE(list2
, QList
<double>() << 1.0 << 2.0 << 3.0);
628 QList
<double> list3
= QtConcurrent::mapped(list
.constBegin(),
632 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
633 QCOMPARE(list3
, QList
<double>() << 1.0 << 2.0 << 3.0);
635 QList
<double> list4
= QtConcurrent::mapped(QList
<int>(list
), intToDouble
).results();
636 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
637 QCOMPARE(list4
, QList
<double>() << 1.0 << 2.0 << 3.0);
640 QList
<double> list2
= QtConcurrent::mapped(linkedList
, intToDouble
).results();
641 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
642 QCOMPARE(list2
, QList
<double>() << 1.0 << 2.0 << 3.0);
644 QList
<double> list3
= QtConcurrent::mapped(linkedList
.constBegin(),
645 linkedList
.constEnd(),
648 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
649 QCOMPARE(list3
, QList
<double>() << 1.0 << 2.0 << 3.0);
651 QList
<double> list4
= QtConcurrent::mapped(QLinkedList
<int>(linkedList
), intToDouble
)
653 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
654 QCOMPARE(list4
, QList
<double>() << 1.0 << 2.0 << 3.0);
659 QList
<double> list2
= QtConcurrent::mapped(list
, intToDouble
).results();
660 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
661 QCOMPARE(list2
, QList
<double>() << 1.0 << 2.0 << 3.0);
663 QList
<double> list3
= QtConcurrent::mapped(list
.constBegin(),
667 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
668 QCOMPARE(list3
, QList
<double>() << 1.0 << 2.0 << 3.0);
671 QList
<double> list4
= QtConcurrent::mapped(QList
<int>(list
),
674 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
675 QCOMPARE(list4
, QList
<double>() << 1.0 << 2.0 << 3.0);
678 QList
<double> list2
= QtConcurrent::mapped(linkedList
, intToDouble
).results();
679 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
680 QCOMPARE(list2
, QList
<double>() << 1.0 << 2.0 << 3.0);
682 QList
<double> list3
= QtConcurrent::mapped(linkedList
.constBegin(),
683 linkedList
.constEnd(),
686 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
687 QCOMPARE(list3
, QList
<double>() << 1.0 << 2.0 << 3.0);
690 QList
<double> list4
= QtConcurrent::mapped(QLinkedList
<int>(linkedList
),
693 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
694 QCOMPARE(list4
, QList
<double>() << 1.0 << 2.0 << 3.0);
697 // const member function
699 QList
<QString
> list2
= QtConcurrent::mapped(numberList
, &Number::toString
).results();
700 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
701 QCOMPARE(list2
, QList
<QString
>() << "1" << "2" << "3");
703 QList
<QString
> list3
= QtConcurrent::mapped(numberList
.constBegin(),
704 numberList
.constEnd(),
707 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
708 QCOMPARE(list3
, QList
<QString
>() << "1" << "2" << "3");
710 QList
<QString
> list4
= QtConcurrent::mapped(QList
<Number
>(numberList
), &Number::toString
)
712 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
713 QCOMPARE(list4
, QList
<QString
>() << "1" << "2" << "3");
716 QList
<QString
> list2
= QtConcurrent::mapped(numberLinkedList
, &Number::toString
).results();
717 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
718 QCOMPARE(list2
, QList
<QString
>() << "1" << "2" << "3");
720 QList
<QString
> list3
= QtConcurrent::mapped(numberLinkedList
.constBegin(),
721 numberLinkedList
.constEnd(),
724 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
725 QCOMPARE(list3
, QList
<QString
>() << "1" << "2" << "3");
727 QList
<QString
> list4
= QtConcurrent::mapped(QLinkedList
<Number
>(numberLinkedList
),
730 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
731 QCOMPARE(list4
, QList
<QString
>() << "1" << "2" << "3");
734 // change the value_type
736 QList
<QString
> strings
= QStringList() << "1" << "2" << "3";
737 QList
<int> list
= QtConcurrent::mapped(strings
, StringToInt()).results();
738 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
740 QList
<int> list2
= QtConcurrent::mapped(strings
.constBegin(),
744 QCOMPARE(list2
, QList
<int>() << 1 << 2 << 3);
747 QList
<QString
> strings
= QStringList() << "1" << "2" << "3";
748 QList
<int> list
= QtConcurrent::mapped(strings
, stringToInt
).results();
749 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
751 QList
<int> list2
= QtConcurrent::mapped(strings
.constBegin(),
753 stringToInt
).results();
754 QCOMPARE(list2
, QList
<int>() << 1 << 2 << 3);
758 QList
<int> numberList2
= QtConcurrent::mapped(numberList
, &Number::toInt
).results();
759 QCOMPARE(numberList2
, QList
<int>() << 1 << 2 << 3);
761 QList
<int> numberList3
= QtConcurrent::mapped(numberList
.constBegin(),
762 numberList
.constEnd(),
765 QCOMPARE(numberList3
, QList
<int>() << 1 << 2 << 3);
768 // change the value_type from QStringList
770 QStringList strings
= QStringList() << "1" << "2" << "3";
771 QList
<int> list
= QtConcurrent::mapped(strings
, StringToInt()).results();
772 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
774 QList
<int> list2
= QtConcurrent::mapped(strings
.constBegin(),
778 QCOMPARE(list2
, QList
<int>() << 1 << 2 << 3);
781 QStringList strings
= QStringList() << "1" << "2" << "3";
782 QList
<int> list
= QtConcurrent::mapped(strings
, stringToInt
).results();
783 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
785 QList
<int> list2
= QtConcurrent::mapped(strings
.constBegin(),
789 QCOMPARE(list2
, QList
<int>() << 1 << 2 << 3);
793 void tst_map::blocking_mapped()
797 QLinkedList
<int> linkedList
;
798 linkedList
<< 1 << 2 << 3;
799 QList
<Number
> numberList
;
800 numberList
<< 1 << 2 << 3;
801 QLinkedList
<Number
> numberLinkedList
;
802 numberLinkedList
<< 1 << 2 << 3;
806 QList
<int> list2
= QtConcurrent::blockingMapped(list
, MultiplyBy2());
807 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
808 QCOMPARE(list2
, QList
<int>() << 2 << 4 << 6);
810 QList
<int> list3
= QtConcurrent::blockingMapped
<QList
<int> >(list
.constBegin(),
813 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
814 QCOMPARE(list3
, QList
<int>() << 2 << 4 << 6);
816 QList
<int> list4
= QtConcurrent::blockingMapped(QList
<int>(list
), MultiplyBy2());
817 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
818 QCOMPARE(list4
, QList
<int>() << 2 << 4 << 6);
821 QLinkedList
<int> linkedList2
= QtConcurrent::blockingMapped(linkedList
, MultiplyBy2());
822 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
823 QCOMPARE(linkedList2
, QLinkedList
<int>() << 2 << 4 << 6);
825 QLinkedList
<int> linkedList3
= QtConcurrent::blockingMapped
<QLinkedList
<int> >(linkedList
.constBegin(),
826 linkedList
.constEnd(),
828 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
829 QCOMPARE(linkedList3
, QLinkedList
<int>() << 2 << 4 << 6);
831 QLinkedList
<int> linkedList4
= QtConcurrent::blockingMapped(QLinkedList
<int>(linkedList
), MultiplyBy2());
832 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
833 QCOMPARE(linkedList4
, QLinkedList
<int>() << 2 << 4 << 6);
838 QList
<int> list2
= QtConcurrent::blockingMapped(list
, multiplyBy2
);
839 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
840 QCOMPARE(list2
, QList
<int>() << 2 << 4 << 6);
842 QList
<int> list3
= QtConcurrent::blockingMapped
<QList
<int> >(list
.constBegin(),
845 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
846 QCOMPARE(list3
, QList
<int>() << 2 << 4 << 6);
848 QList
<int> list4
= QtConcurrent::blockingMapped(QList
<int>(list
), multiplyBy2
);
849 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
850 QCOMPARE(list4
, QList
<int>() << 2 << 4 << 6);
853 QLinkedList
<int> linkedList2
= QtConcurrent::blockingMapped(linkedList
, multiplyBy2
);
854 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
855 QCOMPARE(linkedList2
, QLinkedList
<int>() << 2 << 4 << 6);
857 QLinkedList
<int> linkedList3
= QtConcurrent::blockingMapped
<QLinkedList
<int> >(linkedList
.constBegin(),
858 linkedList
.constEnd(),
860 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
861 QCOMPARE(linkedList3
, QLinkedList
<int>() << 2 << 4 << 6);
863 QLinkedList
<int> linkedList4
= QtConcurrent::blockingMapped(QLinkedList
<int>(linkedList
), multiplyBy2
);
864 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
865 QCOMPARE(linkedList4
, QLinkedList
<int>() << 2 << 4 << 6);
870 QList
<int> list2
= QtConcurrent::blockingMapped(list
, multiplyBy2
);
871 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
872 QCOMPARE(list2
, QList
<int>() << 2 << 4 << 6);
874 QList
<int> list3
= QtConcurrent::blockingMapped
<QList
<int> >(list
.constBegin(),
877 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
878 QCOMPARE(list3
, QList
<int>() << 2 << 4 << 6);
880 QList
<int> list4
= QtConcurrent::blockingMapped(QList
<int>(list
), multiplyBy2
);
881 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
882 QCOMPARE(list4
, QList
<int>() << 2 << 4 << 6);
885 QLinkedList
<int> linkedList2
= QtConcurrent::blockingMapped(linkedList
, multiplyBy2
);
886 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
887 QCOMPARE(linkedList2
, QLinkedList
<int>() << 2 << 4 << 6);
889 QLinkedList
<int> linkedList3
= QtConcurrent::blockingMapped
<QLinkedList
<int> >(linkedList
.constBegin(),
890 linkedList
.constEnd(),
892 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
893 QCOMPARE(linkedList3
, QLinkedList
<int>() << 2 << 4 << 6);
895 QLinkedList
<int> linkedList4
= QtConcurrent::blockingMapped(QLinkedList
<int>(linkedList
), multiplyBy2
);
896 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
897 QCOMPARE(linkedList4
, QLinkedList
<int>() << 2 << 4 << 6);
900 // const member function
902 QList
<Number
> numberList2
= QtConcurrent::blockingMapped(numberList
, &Number::multipliedBy2
);
903 QCOMPARE(numberList
, QList
<Number
>() << 1 << 2 << 3);
904 QCOMPARE(numberList2
, QList
<Number
>() << 2 << 4 << 6);
906 QList
<Number
> numberList3
= QtConcurrent::blockingMapped
<QList
<Number
> >(numberList
.constBegin(),
907 numberList
.constEnd(),
908 &Number::multipliedBy2
);
909 QCOMPARE(numberList
, QList
<Number
>() << 1 << 2 << 3);
910 QCOMPARE(numberList3
, QList
<Number
>() << 2 << 4 << 6);
912 QList
<Number
> numberList4
= QtConcurrent::blockingMapped(QList
<Number
>(numberList
),
913 &Number::multipliedBy2
);
914 QCOMPARE(numberList
, QList
<Number
>() << 1 << 2 << 3);
915 QCOMPARE(numberList4
, QList
<Number
>() << 2 << 4 << 6);
918 QLinkedList
<Number
> numberLinkedList2
= QtConcurrent::blockingMapped(numberLinkedList
, &Number::multipliedBy2
);
919 QCOMPARE(numberLinkedList
, QLinkedList
<Number
>() << 1 << 2 << 3);
920 QCOMPARE(numberLinkedList2
, QLinkedList
<Number
>() << 2 << 4 << 6);
922 QLinkedList
<Number
> numberLinkedList3
= QtConcurrent::blockingMapped
<QLinkedList
<Number
> >(numberLinkedList
.constBegin(),
923 numberLinkedList
.constEnd(),
924 &Number::multipliedBy2
);
925 QCOMPARE(numberLinkedList
, QLinkedList
<Number
>() << 1 << 2 << 3);
926 QCOMPARE(numberLinkedList3
, QLinkedList
<Number
>() << 2 << 4 << 6);
928 QLinkedList
<Number
> numberLinkedList4
= QtConcurrent::blockingMapped(QLinkedList
<Number
>(numberLinkedList
),
929 &Number::multipliedBy2
);
930 QCOMPARE(numberLinkedList
, QLinkedList
<Number
>() << 1 << 2 << 3);
931 QCOMPARE(numberLinkedList4
, QLinkedList
<Number
>() << 2 << 4 << 6);
934 // change the value_type, same container
938 QList
<double> list2
= QtConcurrent::blockingMapped
<QList
<double> >(list
, IntToDouble());
939 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
940 QCOMPARE(list2
, QList
<double>() << 1.0 << 2.0 << 3.0);
942 QList
<double> list3
= QtConcurrent::blockingMapped
<QList
<double> >(list
.constBegin(),
945 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
946 QCOMPARE(list3
, QList
<double>() << 1.0 << 2.0 << 3.0);
948 QList
<double> list4
= QtConcurrent::blockingMapped
<QList
<double> >(QList
<int>(list
),
950 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
951 QCOMPARE(list4
, QList
<double>() << 1.0 << 2.0 << 3.0);
954 QLinkedList
<double> linkedList2
= QtConcurrent::blockingMapped
<QLinkedList
<double> >(linkedList
, IntToDouble());
955 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
956 QCOMPARE(linkedList2
, QLinkedList
<double>() << 1.0 << 2.0 << 3.0);
958 QLinkedList
<double> linkedList3
= QtConcurrent::blockingMapped
<QLinkedList
<double> >(linkedList
.constBegin(),
959 linkedList
.constEnd(),
961 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
962 QCOMPARE(linkedList3
, QLinkedList
<double>() << 1.0 << 2.0 << 3.0);
964 QLinkedList
<double> linkedList4
= QtConcurrent::blockingMapped
<QLinkedList
<double> >(QLinkedList
<int>(linkedList
),
966 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
967 QCOMPARE(linkedList4
, QLinkedList
<double>() << 1.0 << 2.0 << 3.0);
972 QList
<double> list2
= QtConcurrent::blockingMapped
<QList
<double> >(list
, intToDouble
);
973 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
974 QCOMPARE(list2
, QList
<double>() << 1.0 << 2.0 << 3.0);
976 QList
<double> list3
= QtConcurrent::blockingMapped
<QList
<double> >(list
.constBegin(),
979 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
980 QCOMPARE(list3
, QList
<double>() << 1.0 << 2.0 << 3.0);
982 QList
<double> list4
= QtConcurrent::blockingMapped
<QList
<double> >(QList
<int>(list
), intToDouble
);
983 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
984 QCOMPARE(list4
, QList
<double>() << 1.0 << 2.0 << 3.0);
987 QLinkedList
<double> linkedList2
= QtConcurrent::blockingMapped
<QLinkedList
<double> >(linkedList
, intToDouble
);
988 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
989 QCOMPARE(linkedList2
, QLinkedList
<double>() << 1.0 << 2.0 << 3.0);
991 QLinkedList
<double> linkedList3
= QtConcurrent::blockingMapped
<QLinkedList
<double> >(linkedList
.constBegin(),
992 linkedList
.constEnd(),
994 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
995 QCOMPARE(linkedList3
, QLinkedList
<double>() << 1.0 << 2.0 << 3.0);
997 QLinkedList
<double> linkedList4
= QtConcurrent::blockingMapped
<QLinkedList
<double> >(QLinkedList
<int>(linkedList
), intToDouble
);
998 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
999 QCOMPARE(linkedList4
, QLinkedList
<double>() << 1.0 << 2.0 << 3.0);
1004 QList
<double> list2
= QtConcurrent::blockingMapped
<QList
<double> >(list
, intToDouble
);
1005 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1006 QCOMPARE(list2
, QList
<double>() << 1.0 << 2.0 << 3.0);
1008 QList
<double> list3
= QtConcurrent::blockingMapped
<QList
<double> >(list
.constBegin(),
1011 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1012 QCOMPARE(list3
, QList
<double>() << 1.0 << 2.0 << 3.0);
1015 QList
<double> list4
= QtConcurrent::blockingMapped
<QList
<double> >(QList
<int>(list
),
1017 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1018 QCOMPARE(list4
, QList
<double>() << 1.0 << 2.0 << 3.0);
1021 QLinkedList
<double> linkedList2
= QtConcurrent::blockingMapped
<QLinkedList
<double> >(linkedList
, intToDouble
);
1022 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1023 QCOMPARE(linkedList2
, QLinkedList
<double>() << 1.0 << 2.0 << 3.0);
1025 QLinkedList
<double> linkedList3
= QtConcurrent::blockingMapped
<QLinkedList
<double> >(linkedList
.constBegin(),
1026 linkedList
.constEnd(),
1028 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1029 QCOMPARE(linkedList3
, QLinkedList
<double>() << 1.0 << 2.0 << 3.0);
1032 QLinkedList
<double> linkedList4
= QtConcurrent::blockingMapped
<QLinkedList
<double> >(QLinkedList
<int>(linkedList
),
1034 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1035 QCOMPARE(linkedList4
, QLinkedList
<double>() << 1.0 << 2.0 << 3.0);
1038 // const member function
1040 QList
<QString
> list2
=
1041 QtConcurrent::blockingMapped
<QList
<QString
> >(numberList
, &Number::toString
);
1042 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1043 QCOMPARE(list2
, QList
<QString
>() << "1" << "2" << "3");
1045 QList
<QString
> list3
= QtConcurrent::blockingMapped
<QList
<QString
> >(numberList
.constBegin(),
1046 numberList
.constEnd()
1047 , &Number::toString
);
1048 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1049 QCOMPARE(list3
, QList
<QString
>() << "1" << "2" << "3");
1051 QList
<QString
> list4
=
1052 QtConcurrent::blockingMapped
<QList
<QString
> >(QList
<Number
>(numberList
), &Number::toString
);
1053 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1054 QCOMPARE(list4
, QList
<QString
>() << "1" << "2" << "3");
1057 QLinkedList
<QString
> linkedList2
=
1058 QtConcurrent::blockingMapped
<QLinkedList
<QString
> >(numberLinkedList
, &Number::toString
);
1059 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1060 QCOMPARE(linkedList2
, QLinkedList
<QString
>() << "1" << "2" << "3");
1062 QLinkedList
<QString
> linkedList3
= QtConcurrent::blockingMapped
<QLinkedList
<QString
> >(numberLinkedList
.constBegin(),
1063 numberLinkedList
.constEnd()
1064 , &Number::toString
);
1065 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1066 QCOMPARE(linkedList3
, QLinkedList
<QString
>() << "1" << "2" << "3");
1068 QLinkedList
<QString
> linkedList4
=
1069 QtConcurrent::blockingMapped
<QLinkedList
<QString
> >(QLinkedList
<Number
>(numberLinkedList
), &Number::toString
);
1070 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1071 QCOMPARE(linkedList4
, QLinkedList
<QString
>() << "1" << "2" << "3");
1074 // change the value_type
1076 QList
<QString
> strings
= QStringList() << "1" << "2" << "3";
1077 QList
<int> list
= QtConcurrent::blockingMapped(strings
, StringToInt());
1078 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1080 QList
<int> list2
= QtConcurrent::blockingMapped
<QList
<int> >(strings
.constBegin(),
1083 QCOMPARE(list2
, QList
<int>() << 1 << 2 << 3);
1086 QList
<QString
> strings
= QStringList() << "1" << "2" << "3";
1087 QList
<int> list
= QtConcurrent::blockingMapped(strings
, stringToInt
);
1088 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1090 QList
<int> list2
= QtConcurrent::blockingMapped
<QList
<int> >(strings
.constBegin(),
1093 QCOMPARE(list2
, QList
<int>() << 1 << 2 << 3);
1097 QList
<int> numberList2
= QtConcurrent::blockingMapped(numberList
, &Number::toInt
);
1098 QCOMPARE(numberList2
, QList
<int>() << 1 << 2 << 3);
1100 QList
<int> numberList3
= QtConcurrent::blockingMapped
<QList
<int> >(numberList
.constBegin(),
1101 numberList
.constEnd(),
1103 QCOMPARE(numberList3
, QList
<int>() << 1 << 2 << 3);
1106 // change the value_type from QStringList
1108 QStringList strings
= QStringList() << "1" << "2" << "3";
1109 QList
<int> list
= QtConcurrent::blockingMapped(strings
, StringToInt());
1110 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1112 QList
<int> list2
= QtConcurrent::blockingMapped
<QList
<int> >(strings
.constBegin(),
1115 QCOMPARE(list2
, QList
<int>() << 1 << 2 << 3);
1118 QStringList strings
= QStringList() << "1" << "2" << "3";
1119 QList
<int> list
= QtConcurrent::blockingMapped(strings
, stringToInt
);
1120 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1122 QList
<int> list2
= QtConcurrent::blockingMapped
<QList
<int> >(strings
.constBegin(),
1125 QCOMPARE(list2
, QList
<int>() << 1 << 2 << 3);
1130 QVector
<double> list2
= QtConcurrent::blockingMapped
<QVector
<double> >(list
, IntToDouble());
1131 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1132 QCOMPARE(list2
, QVector
<double>() << 1.0 << 2.0 << 3.0);
1134 QVector
<double> list3
= QtConcurrent::blockingMapped
<QVector
<double> >(list
.constBegin(),
1137 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1138 QCOMPARE(list3
, QVector
<double>() << 1.0 << 2.0 << 3.0);
1140 QVector
<double> list4
= QtConcurrent::blockingMapped
<QVector
<double> >(QList
<int>(list
),
1142 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1143 QCOMPARE(list4
, QVector
<double>() << 1.0 << 2.0 << 3.0);
1145 QStringList strings
= QStringList() << "1" << "2" << "3";
1146 QVector
<int> list5
= QtConcurrent::blockingMapped
<QVector
<int> >(strings
, StringToInt());
1147 QCOMPARE(list5
, QVector
<int>() << 1 << 2 << 3);
1149 QVector
<int> list6
= QtConcurrent::blockingMapped
<QVector
<int> >(strings
.constBegin(),
1152 QCOMPARE(list6
, QVector
<int>() << 1 << 2 << 3);
1154 QVector
<int> list7
= QtConcurrent::blockingMapped
<QVector
<int> >(QStringList(strings
),
1156 QCOMPARE(list7
, QVector
<int>() << 1 << 2 << 3);
1161 QVector
<double> list2
= QtConcurrent::blockingMapped
<QVector
<double> >(list
, intToDouble
);
1162 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1163 QCOMPARE(list2
, QVector
<double>() << 1.0 << 2.0 << 3.0);
1165 QVector
<double> list3
= QtConcurrent::blockingMapped
<QVector
<double> >(list
.constBegin(),
1168 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1169 QCOMPARE(list3
, QVector
<double>() << 1.0 << 2.0 << 3.0);
1171 QVector
<double> list4
= QtConcurrent::blockingMapped
<QVector
<double> >(QList
<int>(list
),
1173 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1174 QCOMPARE(list4
, QVector
<double>() << 1.0 << 2.0 << 3.0);
1176 QStringList strings
= QStringList() << "1" << "2" << "3";
1177 QVector
<int> list5
= QtConcurrent::blockingMapped
<QVector
<int> >(strings
, stringToInt
);
1178 QCOMPARE(list5
, QVector
<int>() << 1 << 2 << 3);
1180 QVector
<int> list6
= QtConcurrent::blockingMapped
<QVector
<int> >(strings
.constBegin(),
1183 QCOMPARE(list6
, QVector
<int>() << 1 << 2 << 3);
1185 QVector
<int> list7
= QtConcurrent::blockingMapped
<QVector
<int> >(QStringList(strings
),
1187 QCOMPARE(list7
, QVector
<int>() << 1 << 2 << 3);
1192 QVector
<double> list2
= QtConcurrent::blockingMapped
<QVector
<double> >(list
, intToDouble
);
1193 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1194 QCOMPARE(list2
, QVector
<double>() << 1.0 << 2.0 << 3.0);
1196 QVector
<double> list3
= QtConcurrent::blockingMapped
<QVector
<double> >(QList
<int>(list
), intToDouble
);
1197 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1198 QCOMPARE(list3
, QVector
<double>() << 1.0 << 2.0 << 3.0);
1200 QStringList strings
= QStringList() << "1" << "2" << "3";
1201 QVector
<int> list4
= QtConcurrent::blockingMapped
<QVector
<int> >(strings
, stringToInt
);
1202 QCOMPARE(list4
, QVector
<int>() << 1 << 2 << 3);
1204 QVector
<int> list5
= QtConcurrent::blockingMapped
<QVector
<int> >(QStringList(strings
), stringToInt
);
1205 QCOMPARE(list5
, QVector
<int>() << 1 << 2 << 3);
1208 // const member function
1210 QVector
<QString
> list2
= QtConcurrent::blockingMapped
<QVector
<QString
> >(numberList
, &Number::toString
);
1211 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1212 QCOMPARE(list2
, QVector
<QString
>() << "1" << "2" << "3");
1214 QVector
<QString
> list3
=
1215 QtConcurrent::blockingMapped
<QVector
<QString
> >(QList
<Number
>(numberList
), &Number::toString
);
1216 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1217 QCOMPARE(list3
, QVector
<QString
>() << "1" << "2" << "3");
1219 // not allowed: const member function where all arguments have default values
1221 QStringList strings
= QStringList() << "1" << "2" << "3";
1222 QVector
<int> list4
= QtConcurrent::blockingMapped
<QVector
<int> >(strings
, &QString::toInt
);
1223 QCOMPARE(list4
, QVector
<int>() << 1 << 2 << 3);
1225 QVector
<int> list5
= QtConcurrent::blockingMapped
<QVector
<int> >(QStringList(strings
), &QString::toInt
);
1226 QCOMPARE(list5
, QVector
<int>() << 1 << 2 << 3);
1231 int intSquare(int x
)
1239 typedef int result_type
;
1241 int operator()(int x
)
1247 void tst_map::mappedReduced()
1250 list
<< 1 << 2 << 3;
1251 QLinkedList
<int> linkedList
;
1252 linkedList
<< 1 << 2 << 3;
1253 QList
<Number
> numberList
;
1254 numberList
<< 1 << 2 << 3;
1255 QLinkedList
<Number
> numberLinkedList
;
1256 numberLinkedList
<< 1 << 2 << 3;
1258 // test Q_DECLARE_OPERATORS_FOR_FLAGS
1259 QtConcurrent::ReduceOptions opt
= (QtConcurrent::UnorderedReduce
| QtConcurrent::SequentialReduce
);
1263 int sum
= QtConcurrent::mappedReduced
<int>(list
, IntSquare(), IntSumReduce());
1265 int sum2
= QtConcurrent::mappedReduced
<int>(list
.constBegin(),
1271 int sum3
= QtConcurrent::mappedReduced
<int>(QList
<int>(list
), IntSquare(), IntSumReduce());
1274 int sum4
= QtConcurrent::mappedReduced
<int>(list
, intSquare
, intSumReduce
);
1276 int sum5
= QtConcurrent::mappedReduced
<int>(list
.constBegin(),
1282 int sum6
= QtConcurrent::mappedReduced
<int>(QList
<int>(list
),
1288 int sum
= QtConcurrent::mappedReduced
<int>(linkedList
, IntSquare(), IntSumReduce());
1290 int sum2
= QtConcurrent::mappedReduced
<int>(linkedList
.constBegin(),
1291 linkedList
.constEnd(),
1296 int sum3
= QtConcurrent::mappedReduced
<int>(QLinkedList
<int>(linkedList
), IntSquare(), IntSumReduce());
1299 int sum4
= QtConcurrent::mappedReduced
<int>(linkedList
, intSquare
, intSumReduce
);
1301 int sum5
= QtConcurrent::mappedReduced
<int>(linkedList
.constBegin(),
1302 linkedList
.constEnd(),
1307 int sum6
= QtConcurrent::mappedReduced
<int>(QLinkedList
<int>(linkedList
),
1315 int sum
= QtConcurrent::mappedReduced
<int>(list
, intSquare
, IntSumReduce());
1317 int sum2
= QtConcurrent::mappedReduced
<int>(list
.constBegin(),
1323 int sum3
= QtConcurrent::mappedReduced
<int>(QList
<int>(list
), intSquare
, IntSumReduce());
1327 int sum
= QtConcurrent::mappedReduced
<int>(linkedList
, intSquare
, IntSumReduce());
1329 int sum2
= QtConcurrent::mappedReduced
<int>(linkedList
.constBegin(),
1330 linkedList
.constEnd(),
1335 int sum3
= QtConcurrent::mappedReduced
<int>(QLinkedList
<int>(linkedList
), intSquare
, IntSumReduce());
1341 int sum
= QtConcurrent::mappedReduced(list
, IntSquare(), intSumReduce
);
1343 int sum2
= QtConcurrent::mappedReduced(list
.constBegin(),
1349 int sum3
= QtConcurrent::mappedReduced(QList
<int>(list
), IntSquare(), intSumReduce
);
1353 int sum
= QtConcurrent::mappedReduced(linkedList
, IntSquare(), intSumReduce
);
1355 int sum2
= QtConcurrent::mappedReduced(linkedList
.constBegin(),
1356 linkedList
.constEnd(),
1361 int sum3
= QtConcurrent::mappedReduced(QLinkedList
<int>(linkedList
), IntSquare(), intSumReduce
);
1365 // function-function
1367 int sum
= QtConcurrent::mappedReduced(list
, intSquare
, intSumReduce
);
1369 int sum2
= QtConcurrent::mappedReduced(list
.constBegin(),
1375 int sum3
= QtConcurrent::mappedReduced(QList
<int>(list
), intSquare
, intSumReduce
);
1379 int sum
= QtConcurrent::mappedReduced(linkedList
, intSquare
, intSumReduce
);
1381 int sum2
= QtConcurrent::mappedReduced(linkedList
.constBegin(),
1382 linkedList
.constEnd(),
1387 int sum3
= QtConcurrent::mappedReduced(QLinkedList
<int>(linkedList
), intSquare
, intSumReduce
);
1393 QList
<int> list2
= QtConcurrent::mappedReduced(list
,
1395 &QList
<int>::push_back
,
1397 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1398 QCOMPARE(list2
, QList
<int>() << 1 << 4 << 9);
1400 QList
<int> list3
= QtConcurrent::mappedReduced(list
.constBegin(),
1403 &QList
<int>::push_back
,
1405 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1406 QCOMPARE(list3
, QList
<int>() << 1 << 4 << 9);
1408 QList
<int> list4
= QtConcurrent::mappedReduced(QList
<int>(list
),
1410 &QList
<int>::push_back
,
1412 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1413 QCOMPARE(list4
, QList
<int>() << 1 << 4 << 9);
1416 QLinkedList
<int> linkedList2
= QtConcurrent::mappedReduced(linkedList
,
1418 &QLinkedList
<int>::push_back
,
1420 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1421 QCOMPARE(linkedList2
, QLinkedList
<int>() << 1 << 4 << 9);
1423 QLinkedList
<int> linkedList3
= QtConcurrent::mappedReduced(linkedList
.constBegin(),
1424 linkedList
.constEnd(),
1426 &QLinkedList
<int>::push_back
,
1428 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1429 QCOMPARE(linkedList3
, QLinkedList
<int>() << 1 << 4 << 9);
1431 QLinkedList
<int> linkedList4
= QtConcurrent::mappedReduced(QLinkedList
<int>(linkedList
),
1433 &QLinkedList
<int>::push_back
,
1435 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1436 QCOMPARE(linkedList4
, QLinkedList
<int>() << 1 << 4 << 9);
1441 int sum
= QtConcurrent::mappedReduced
<int>(numberList
, &Number::toInt
, IntSumReduce());
1443 int sum2
= QtConcurrent::mappedReduced
<int>(numberList
.constBegin(),
1444 numberList
.constEnd(),
1449 int sum3
= QtConcurrent::mappedReduced
<int>(QList
<Number
>(numberList
),
1455 int sum
= QtConcurrent::mappedReduced
<int>(numberLinkedList
, &Number::toInt
, IntSumReduce());
1457 int sum2
= QtConcurrent::mappedReduced
<int>(numberLinkedList
.constBegin(),
1458 numberLinkedList
.constEnd(),
1463 int sum3
= QtConcurrent::mappedReduced
<int>(QLinkedList
<Number
>(numberLinkedList
),
1471 QList
<int> list2
= QtConcurrent::mappedReduced(numberList
,
1473 &QList
<int>::push_back
,
1475 QCOMPARE(list2
, QList
<int>() << 1 << 2 << 3);
1477 QList
<int> list3
= QtConcurrent::mappedReduced(numberList
.constBegin(),
1478 numberList
.constEnd(),
1480 &QList
<int>::push_back
,
1482 QCOMPARE(list3
, QList
<int>() << 1 << 2 << 3);
1484 QList
<int> list4
= QtConcurrent::mappedReduced(QList
<Number
>(numberList
),
1486 &QList
<int>::push_back
, OrderedReduce
);
1487 QCOMPARE(list4
, QList
<int>() << 1 << 2 << 3);
1490 QLinkedList
<int> linkedList2
= QtConcurrent::mappedReduced(numberLinkedList
,
1492 &QLinkedList
<int>::push_back
,
1494 QCOMPARE(linkedList2
, QLinkedList
<int>() << 1 << 2 << 3);
1496 QLinkedList
<int> linkedList3
= QtConcurrent::mappedReduced(numberLinkedList
.constBegin(),
1497 numberLinkedList
.constEnd(),
1499 &QLinkedList
<int>::push_back
,
1501 QCOMPARE(linkedList3
, QLinkedList
<int>() << 1 << 2 << 3);
1503 QLinkedList
<int> linkedList4
= QtConcurrent::mappedReduced(QLinkedList
<Number
>(numberLinkedList
),
1505 &QLinkedList
<int>::push_back
, OrderedReduce
);
1506 QCOMPARE(linkedList4
, QLinkedList
<int>() << 1 << 2 << 3);
1511 QList
<int> list2
= QtConcurrent::mappedReduced(list
,
1513 &QList
<int>::push_back
,
1515 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1516 QCOMPARE(list2
, QList
<int>() << 1 << 4 << 9);
1518 QList
<int> list3
= QtConcurrent::mappedReduced(list
.constBegin(),
1521 &QList
<int>::push_back
,
1523 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1524 QCOMPARE(list3
, QList
<int>() << 1 << 4 << 9);
1526 QList
<int> list4
= QtConcurrent::mappedReduced(QList
<int>(list
),
1528 &QList
<int>::push_back
,
1530 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1531 QCOMPARE(list4
, QList
<int>() << 1 << 4 << 9);
1534 QLinkedList
<int> linkedList2
= QtConcurrent::mappedReduced(linkedList
,
1536 &QLinkedList
<int>::append
,
1538 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1539 QCOMPARE(linkedList2
, QLinkedList
<int>() << 1 << 4 << 9);
1541 QLinkedList
<int> linkedList3
= QtConcurrent::mappedReduced(linkedList
.constBegin(),
1542 linkedList
.constEnd(),
1544 &QLinkedList
<int>::append
,
1546 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1547 QCOMPARE(linkedList3
, QLinkedList
<int>() << 1 << 4 << 9);
1549 QLinkedList
<int> linkedList4
= QtConcurrent::mappedReduced(QLinkedList
<int>(linkedList
),
1551 &QLinkedList
<int>::append
,
1553 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1554 QCOMPARE(linkedList4
, QLinkedList
<int>() << 1 << 4 << 9);
1559 int sum
= QtConcurrent::mappedReduced(numberList
,
1563 int sum2
= QtConcurrent::mappedReduced(numberList
.constBegin(),
1564 numberList
.constEnd(),
1569 int sum3
= QtConcurrent::mappedReduced(QList
<Number
>(numberList
),
1575 int sum
= QtConcurrent::mappedReduced(numberLinkedList
,
1579 int sum2
= QtConcurrent::mappedReduced(numberLinkedList
.constBegin(),
1580 numberLinkedList
.constEnd(),
1585 int sum3
= QtConcurrent::mappedReduced(QLinkedList
<Number
>(numberLinkedList
),
1594 QLinkedList
<int> list
;
1595 list
<< 1 << 2 << 3;
1597 QLinkedList
<Number
> numberList
;
1598 numberList
<< 1 << 2 << 3;
1600 int sum
= QtConcurrent::mappedReduced
<int>(list
, IntSquare(), IntSumReduce());
1602 int sum2
= QtConcurrent::mappedReduced
<int>(list
.constBegin(),
1608 int sum3
= QtConcurrent::mappedReduced
<int>(QLinkedList
<int>(list
), IntSquare(), IntSumReduce());
1611 int sum4
= QtConcurrent::mappedReduced
<int>(list
, intSquare
, intSumReduce
);
1613 int sum5
= QtConcurrent::mappedReduced
<int>(list
.constBegin(),
1619 int sum6
= QtConcurrent::mappedReduced
<int>(QLinkedList
<int>(list
),
1625 // ### the same as above, with an initial result value
1628 void tst_map::blocking_mappedReduced()
1631 list
<< 1 << 2 << 3;
1632 QLinkedList
<int> linkedList
;
1633 linkedList
<< 1 << 2 << 3;
1634 QList
<Number
> numberList
;
1635 numberList
<< 1 << 2 << 3;
1636 QLinkedList
<Number
> numberLinkedList
;
1637 numberLinkedList
<< 1 << 2 << 3;
1641 int sum
= QtConcurrent::blockingMappedReduced
<int>(list
, IntSquare(), IntSumReduce());
1643 int sum2
= QtConcurrent::blockingMappedReduced
<int>(list
.constBegin(),
1649 int sum3
= QtConcurrent::blockingMappedReduced
<int>(QList
<int>(list
), IntSquare(), IntSumReduce());
1652 int sum4
= QtConcurrent::blockingMappedReduced
<int>(list
, intSquare
, intSumReduce
);
1654 int sum5
= QtConcurrent::blockingMappedReduced
<int>(list
.constBegin(),
1660 int sum6
= QtConcurrent::blockingMappedReduced
<int>(QList
<int>(list
),
1666 int sum
= QtConcurrent::blockingMappedReduced
<int>(linkedList
, IntSquare(), IntSumReduce());
1668 int sum2
= QtConcurrent::blockingMappedReduced
<int>(linkedList
.constBegin(),
1669 linkedList
.constEnd(),
1674 int sum3
= QtConcurrent::blockingMappedReduced
<int>(QLinkedList
<int>(linkedList
), IntSquare(), IntSumReduce());
1677 int sum4
= QtConcurrent::blockingMappedReduced
<int>(linkedList
, intSquare
, intSumReduce
);
1679 int sum5
= QtConcurrent::blockingMappedReduced
<int>(linkedList
.constBegin(),
1680 linkedList
.constEnd(),
1685 int sum6
= QtConcurrent::blockingMappedReduced
<int>(QLinkedList
<int>(linkedList
),
1693 int sum
= QtConcurrent::blockingMappedReduced
<int>(list
, intSquare
, IntSumReduce());
1695 int sum2
= QtConcurrent::blockingMappedReduced
<int>(list
.constBegin(),
1701 int sum3
= QtConcurrent::blockingMappedReduced
<int>(QList
<int>(list
), intSquare
, IntSumReduce());
1705 int sum
= QtConcurrent::blockingMappedReduced
<int>(linkedList
, intSquare
, IntSumReduce());
1707 int sum2
= QtConcurrent::blockingMappedReduced
<int>(linkedList
.constBegin(),
1708 linkedList
.constEnd(),
1713 int sum3
= QtConcurrent::blockingMappedReduced
<int>(QLinkedList
<int>(linkedList
), intSquare
, IntSumReduce());
1719 int sum
= QtConcurrent::blockingMappedReduced(list
, IntSquare(), intSumReduce
);
1721 int sum2
= QtConcurrent::blockingMappedReduced(list
.constBegin(),
1727 int sum3
= QtConcurrent::blockingMappedReduced(QList
<int>(list
), IntSquare(), intSumReduce
);
1731 int sum
= QtConcurrent::blockingMappedReduced(linkedList
, IntSquare(), intSumReduce
);
1733 int sum2
= QtConcurrent::blockingMappedReduced(linkedList
.constBegin(),
1734 linkedList
.constEnd(),
1739 int sum3
= QtConcurrent::blockingMappedReduced(QLinkedList
<int>(linkedList
), IntSquare(), intSumReduce
);
1743 // function-function
1745 int sum
= QtConcurrent::blockingMappedReduced(list
, intSquare
, intSumReduce
);
1747 int sum2
= QtConcurrent::blockingMappedReduced(list
.constBegin(),
1753 int sum3
= QtConcurrent::blockingMappedReduced(QList
<int>(list
), intSquare
, intSumReduce
);
1757 int sum
= QtConcurrent::blockingMappedReduced(linkedList
, intSquare
, intSumReduce
);
1759 int sum2
= QtConcurrent::blockingMappedReduced(linkedList
.constBegin(),
1760 linkedList
.constEnd(),
1765 int sum3
= QtConcurrent::blockingMappedReduced(QLinkedList
<int>(linkedList
), intSquare
, intSumReduce
);
1771 QList
<int> list2
= QtConcurrent::blockingMappedReduced(list
,
1773 &QList
<int>::push_back
,
1775 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1776 QCOMPARE(list2
, QList
<int>() << 1 << 4 << 9);
1778 QList
<int> list3
= QtConcurrent::blockingMappedReduced(list
.constBegin(),
1781 &QList
<int>::push_back
,
1783 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1784 QCOMPARE(list3
, QList
<int>() << 1 << 4 << 9);
1786 QList
<int> list4
= QtConcurrent::blockingMappedReduced(QList
<int>(list
),
1788 &QList
<int>::push_back
,
1790 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1791 QCOMPARE(list4
, QList
<int>() << 1 << 4 << 9);
1794 QLinkedList
<int> linkedList2
= QtConcurrent::blockingMappedReduced(linkedList
,
1796 &QLinkedList
<int>::append
,
1798 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1799 QCOMPARE(linkedList2
, QLinkedList
<int>() << 1 << 4 << 9);
1801 QLinkedList
<int> linkedList3
= QtConcurrent::blockingMappedReduced(linkedList
.constBegin(),
1802 linkedList
.constEnd(),
1804 &QLinkedList
<int>::append
,
1806 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1807 QCOMPARE(linkedList3
, QLinkedList
<int>() << 1 << 4 << 9);
1809 QLinkedList
<int> linkedList4
= QtConcurrent::blockingMappedReduced(QLinkedList
<int>(linkedList
),
1811 &QLinkedList
<int>::append
,
1813 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1814 QCOMPARE(linkedList4
, QLinkedList
<int>() << 1 << 4 << 9);
1819 int sum
= QtConcurrent::blockingMappedReduced
<int>(numberList
, &Number::toInt
,
1822 int sum2
= QtConcurrent::blockingMappedReduced
<int>(numberList
.constBegin(),
1823 numberList
.constEnd(),
1828 int sum3
= QtConcurrent::blockingMappedReduced
<int>(QList
<Number
>(numberList
),
1834 int sum
= QtConcurrent::blockingMappedReduced
<int>(numberLinkedList
, &Number::toInt
, IntSumReduce());
1836 int sum2
= QtConcurrent::blockingMappedReduced
<int>(numberLinkedList
.constBegin(),
1837 numberLinkedList
.constEnd(),
1842 int sum3
= QtConcurrent::blockingMappedReduced
<int>(QLinkedList
<Number
>(numberLinkedList
),
1850 QList
<int> list2
= QtConcurrent::blockingMappedReduced(numberList
,
1852 &QList
<int>::push_back
,
1854 QCOMPARE(list2
, QList
<int>() << 1 << 2 << 3);
1856 QList
<int> list3
= QtConcurrent::blockingMappedReduced(numberList
.constBegin(),
1857 numberList
.constEnd(),
1859 &QList
<int>::push_back
,
1861 QCOMPARE(list3
, QList
<int>() << 1 << 2 << 3);
1863 QList
<int> list4
= QtConcurrent::blockingMappedReduced(QList
<Number
>(numberList
),
1865 &QList
<int>::push_back
, OrderedReduce
);
1866 QCOMPARE(list4
, QList
<int>() << 1 << 2 << 3);
1869 QLinkedList
<int> linkedList2
= QtConcurrent::blockingMappedReduced(numberLinkedList
,
1871 &QLinkedList
<int>::append
,
1873 QCOMPARE(linkedList2
, QLinkedList
<int>() << 1 << 2 << 3);
1875 QLinkedList
<int> linkedList3
= QtConcurrent::blockingMappedReduced(numberLinkedList
.constBegin(),
1876 numberLinkedList
.constEnd(),
1878 &QLinkedList
<int>::append
,
1880 QCOMPARE(linkedList3
, QLinkedList
<int>() << 1 << 2 << 3);
1882 QLinkedList
<int> linkedList4
= QtConcurrent::blockingMappedReduced(QLinkedList
<Number
>(numberLinkedList
),
1884 &QLinkedList
<int>::append
, OrderedReduce
);
1885 QCOMPARE(linkedList4
, QLinkedList
<int>() << 1 << 2 << 3);
1890 QList
<int> list2
= QtConcurrent::blockingMappedReduced(list
,
1892 &QList
<int>::push_back
,
1894 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1895 QCOMPARE(list2
, QList
<int>() << 1 << 4 << 9);
1897 QList
<int> list3
= QtConcurrent::blockingMappedReduced(list
.constBegin(),
1900 &QList
<int>::push_back
,
1902 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1903 QCOMPARE(list3
, QList
<int>() << 1 << 4 << 9);
1905 QList
<int> list4
= QtConcurrent::blockingMappedReduced(QList
<int>(list
),
1907 &QList
<int>::push_back
,
1909 QCOMPARE(list
, QList
<int>() << 1 << 2 << 3);
1910 QCOMPARE(list4
, QList
<int>() << 1 << 4 << 9);
1913 QLinkedList
<int> linkedList2
= QtConcurrent::blockingMappedReduced(linkedList
,
1915 &QLinkedList
<int>::append
,
1917 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1918 QCOMPARE(linkedList2
, QLinkedList
<int>() << 1 << 4 << 9);
1920 QLinkedList
<int> linkedList3
= QtConcurrent::blockingMappedReduced(linkedList
.constBegin(),
1921 linkedList
.constEnd(),
1923 &QLinkedList
<int>::append
,
1925 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1926 QCOMPARE(linkedList3
, QLinkedList
<int>() << 1 << 4 << 9);
1928 QLinkedList
<int> linkedList4
= QtConcurrent::blockingMappedReduced(QLinkedList
<int>(linkedList
),
1930 &QLinkedList
<int>::append
,
1932 QCOMPARE(linkedList
, QLinkedList
<int>() << 1 << 2 << 3);
1933 QCOMPARE(linkedList4
, QLinkedList
<int>() << 1 << 4 << 9);
1938 int sum
= QtConcurrent::blockingMappedReduced(numberList
,
1942 int sum2
= QtConcurrent::blockingMappedReduced(numberList
.constBegin(),
1943 numberList
.constEnd(),
1948 int sum3
= QtConcurrent::blockingMappedReduced(QList
<Number
>(numberList
),
1954 int sum
= QtConcurrent::blockingMappedReduced(numberLinkedList
,
1958 int sum2
= QtConcurrent::blockingMappedReduced(numberLinkedList
.constBegin(),
1959 numberLinkedList
.constEnd(),
1964 int sum3
= QtConcurrent::blockingMappedReduced(QLinkedList
<Number
>(numberLinkedList
),
1973 QLinkedList
<int> list
;
1974 list
<< 1 << 2 << 3;
1976 QLinkedList
<Number
> numberList
;
1977 numberList
<< 1 << 2 << 3;
1979 int sum
= QtConcurrent::blockingMappedReduced
<int>(list
, IntSquare(), IntSumReduce());
1981 int sum2
= QtConcurrent::blockingMappedReduced
<int>(list
.constBegin(),
1987 int sum3
= QtConcurrent::blockingMappedReduced
<int>(QLinkedList
<int>(list
), IntSquare(), IntSumReduce());
1990 int sum4
= QtConcurrent::blockingMappedReduced
<int>(list
, intSquare
, intSumReduce
);
1992 int sum5
= QtConcurrent::blockingMappedReduced
<int>(list
.constBegin(),
1998 int sum6
= QtConcurrent::blockingMappedReduced
<int>(QLinkedList
<int>(list
),
2004 // ### the same as above, with an initial result value
2007 int sleeper(int val
)
2013 void tst_map::assignResult()
2015 const QList
<int> startList
= QList
<int>() << 0 << 1 << 2;
2016 QList
<int> list
= QtConcurrent::blockingMapped(startList
, sleeper
);
2017 QCOMPARE(list
.at(0), 0);
2018 QCOMPARE(list
.at(1), 1);
2021 int fnConst(const int &i
)
2031 QString
changeTypeConst(const int &)
2036 QString
changeType(int &)
2041 int changeTypeQStringListConst(const QStringList
&)
2046 int changeTypeQStringList(QStringList
&)
2058 return MemFnTester();
2061 MemFnTester
fnConst() const
2063 return MemFnTester();
2066 QString
changeType()
2071 QString
changeTypeConst() const
2077 Q_DECLARE_METATYPE(QVector
<MemFnTester
>);
2078 Q_DECLARE_METATYPE(QList
<MemFnTester
>);
2080 void tst_map::functionOverloads()
2083 const QList
<int> constIntList
;
2084 QList
<MemFnTester
> classList
;
2085 const QList
<MemFnTester
> constMemFnTesterList
;
2087 QtConcurrent::mapped(intList
, fnConst
);
2088 QtConcurrent::mapped(constIntList
, fnConst
);
2089 QtConcurrent::mapped(classList
, &MemFnTester::fnConst
);
2090 QtConcurrent::mapped(constMemFnTesterList
, &MemFnTester::fnConst
);
2092 QtConcurrent::blockingMapped
<QVector
<int> >(intList
, fnConst
);
2093 QtConcurrent::blockingMapped
<QVector
<int> >(constIntList
, fnConst
);
2094 QtConcurrent::blockingMapped
<QVector
<MemFnTester
> >(classList
, &MemFnTester::fnConst
);
2095 QtConcurrent::blockingMapped
<QVector
<MemFnTester
> >(constMemFnTesterList
, &MemFnTester::fnConst
);
2097 QtConcurrent::blockingMapped
<QList
<QString
> >(intList
, changeTypeConst
);
2098 QtConcurrent::blockingMapped
<QList
<QString
> >(constIntList
, changeTypeConst
);
2099 QtConcurrent::blockingMapped
<QList
<QString
> >(classList
, &MemFnTester::changeTypeConst
);
2100 QtConcurrent::blockingMapped
<QList
<QString
> >(constMemFnTesterList
, &MemFnTester::changeTypeConst
);
2102 QStringList stringList
;
2103 const QStringList constStringList
;
2104 // QtConcurrent::map(stringList, changeTypeQStringListConst);
2105 // QtConcurrent::map(intList, changeTypeNonConst);
2106 // QList<QString>(QtConcurrent::map(constStringList, changeTypeQStringList));
2107 // QtConcurrent::map(classList, &MemFnTester::changeType);
2108 // QtConcurrent::map(classList, &MemFnTester::changeTypeConst);
2109 // QtConcurrent::map(constMemFnTesterList, &MemFnTester::changeTypeConst);
2112 QAtomicInt currentInstanceCount
;
2113 QAtomicInt peakInstanceCount
;
2114 class InstanceCounter
2117 inline InstanceCounter()
2118 { currentInstanceCount
.fetchAndAddRelaxed(1); updatePeak(); }
2119 inline ~InstanceCounter()
2120 { currentInstanceCount
.fetchAndAddRelaxed(-1);}
2121 inline InstanceCounter(const InstanceCounter
&)
2122 { currentInstanceCount
.fetchAndAddRelaxed(1); updatePeak(); }
2127 const int localPeak
= peakInstanceCount
;
2128 const int localCurrent
= currentInstanceCount
;
2129 if (localCurrent
<= localPeak
)
2131 if (peakInstanceCount
.testAndSetOrdered(localPeak
, localCurrent
))
2137 InstanceCounter
slowMap(const InstanceCounter
&in
)
2143 InstanceCounter
fastMap(const InstanceCounter
&in
)
2145 QTest::qSleep(rand() % 2 + 1);
2146 // qDebug() << "map " << QThread::currentThread();
2150 void slowReduce(int &result
, const InstanceCounter
&)
2152 QTest::qSleep(rand() % 4 + 1);
2153 // qDebug() << "reduce" << QThread::currentThread();
2157 void fastReduce(int &result
, const InstanceCounter
&)
2162 void tst_map::throttling()
2164 const int itemcount
= 100;
2165 const int allowedTemporaries
= QThread::idealThreadCount() * 40;
2168 currentInstanceCount
= 0;
2169 peakInstanceCount
= 0;
2171 QList
<InstanceCounter
> instances
;
2172 for (int i
= 0; i
< itemcount
; ++i
)
2173 instances
.append(InstanceCounter());
2175 QCOMPARE((int)currentInstanceCount
, itemcount
);
2177 int results
= QtConcurrent::blockingMappedReduced(instances
, slowMap
, fastReduce
);
2178 QCOMPARE(results
, itemcount
);
2179 qDebug() << (int)currentInstanceCount
;
2180 qDebug() << (int)peakInstanceCount
;
2181 QCOMPARE(int(currentInstanceCount
), itemcount
);
2182 QVERIFY(int(peakInstanceCount
) < itemcount
+ allowedTemporaries
);
2186 QCOMPARE(int(currentInstanceCount
), 0);
2187 peakInstanceCount
= 0;
2189 QList
<InstanceCounter
> instances
;
2190 for (int i
= 0; i
< itemcount
; ++i
)
2191 instances
.append(InstanceCounter());
2193 QCOMPARE(int(currentInstanceCount
), itemcount
);
2194 int results
= QtConcurrent::blockingMappedReduced(instances
, fastMap
, slowReduce
);
2196 QCOMPARE(results
, itemcount
);
2197 qDebug() << (int)currentInstanceCount
;
2198 qDebug() << (int)peakInstanceCount
;
2199 QCOMPARE((int)currentInstanceCount
, itemcount
);
2200 QVERIFY(int(peakInstanceCount
) < itemcount
+ allowedTemporaries
);
2204 #ifndef QT_NO_EXCEPTIONS
2205 void throwMapper(int &e
)
2208 throw QtConcurrent::Exception();
2211 void tst_map::exceptions()
2213 bool caught
= false;
2215 QList
<int> list
= QList
<int>() << 1 << 2 << 3;
2216 QtConcurrent::map(list
, throwMapper
).waitForFinished();
2217 } catch (Exception
&e
) {
2221 QFAIL("did not get exception");
2225 int mapper(const int &i
)
2231 void tst_map::incrementalResults()
2233 const int count
= 200;
2235 for (int i
=0; i
< count
; ++i
)
2238 QFuture
<int> future
= QtConcurrent::mapped(ints
, mapper
);
2242 while (future
.isFinished() == false) {
2243 for (int i
= 0; i
< future
.resultCount(); ++i
) {
2244 results
+= future
.resultAt(i
);
2250 QCOMPARE(future
.isFinished(), true);
2251 QCOMPARE(future
.resultCount(), count
);
2252 QCOMPARE(future
.results().count(), count
);
2256 Test that mapped does not cause deep copies when holding
2257 references to Qt containers.
2259 void tst_map::noDetatch()
2262 QList
<int> l
= QList
<int>() << 1;
2263 QVERIFY(l
.isDetached());
2266 QVERIFY(l
.isDetached() == false);
2268 QtConcurrent::mapped(l
, mapper
).waitForFinished();
2270 QVERIFY(l
.isDetached() == false);
2271 QVERIFY(ll
.isDetached() == false);
2273 QtConcurrent::mappedReduced(l
, mapper
, intSumReduce
).waitForFinished();
2275 QVERIFY(l
.isDetached() == false);
2276 QVERIFY(ll
.isDetached() == false);
2278 QtConcurrent::map(l
, multiplyBy2Immutable
).waitForFinished();
2279 QVERIFY(l
.isDetached() == true);
2280 QVERIFY(ll
.isDetached() == true);
2283 const QList
<int> l
= QList
<int>() << 1;
2284 QVERIFY(l
.isDetached());
2286 const QList
<int> ll
= l
;
2287 QVERIFY(l
.isDetached() == false);
2289 QtConcurrent::mapped(l
, mapper
).waitForFinished();
2291 QVERIFY(l
.isDetached() == false);
2292 QVERIFY(ll
.isDetached() == false);
2294 QtConcurrent::mappedReduced(l
, mapper
, intSumReduce
).waitForFinished();
2296 QVERIFY(l
.isDetached() == false);
2297 QVERIFY(ll
.isDetached() == false);
2302 void tst_map::stlContainers()
2305 QSKIP("Qt compiled without STL support", SkipAll
);
2307 std::vector
<int> vector
;
2308 vector
.push_back(1);
2309 vector
.push_back(2);
2311 std::vector
<int> vector2
= QtConcurrent::blockingMapped
<std::vector
<int> >(vector
, mapper
);
2312 QCOMPARE(vector2
.size(), (std::vector
<int>::size_type
)(2));
2314 std::list
<int> list
;
2318 std::list
<int> list2
= QtConcurrent::blockingMapped
<std::list
<int> >(list
, mapper
);
2319 QCOMPARE(list2
.size(), (std::vector
<int>::size_type
)(2));
2321 QtConcurrent::mapped(list
, mapper
).waitForFinished();
2323 QtConcurrent::blockingMap(list
, multiplyBy2Immutable
);
2327 InstanceCounter
ic_fn(const InstanceCounter
& ic
)
2329 return InstanceCounter(ic
);
2332 // Verify that held results are deleted when a future is
2333 // assigned over with operator ==
2334 void tst_map::qFutureAssignmentLeak()
2336 currentInstanceCount
= 0;
2337 peakInstanceCount
= 0;
2338 QFuture
<InstanceCounter
> future
;
2340 QList
<InstanceCounter
> list
;
2341 for (int i
=0;i
<1000;++i
)
2342 list
+= InstanceCounter();
2343 future
= QtConcurrent::mapped(list
, ic_fn
);
2344 future
.waitForFinished();
2346 future
= QtConcurrent::mapped(list
, ic_fn
);
2347 future
.waitForFinished();
2349 future
= QtConcurrent::mapped(list
, ic_fn
);
2350 future
.waitForFinished();
2353 QCOMPARE(int(currentInstanceCount
), 1000);
2354 future
= QFuture
<InstanceCounter
>();
2355 QCOMPARE(int(currentInstanceCount
), 0);
2358 inline void increment(int &num
)
2363 inline int echo(const int &num
)
2368 void add(int &result
, const int &sum
)
2373 void tst_map::stressTest()
2375 const int listSize
= 1000;
2376 const int sum
= (listSize
- 1) * (listSize
/ 2);
2380 for (int i
= 0; i
< listSize
; ++i
) {
2384 for (int i
=0 ; i
< 100; ++i
) {
2385 QList
<int> result
= QtConcurrent::blockingMapped(list
, echo
);
2386 for (int j
= 0; j
< listSize
; ++j
)
2387 QCOMPARE(result
.at(j
), j
);
2390 for (int i
= 0 ; i
< 100; ++i
) {
2391 int result
= QtConcurrent::blockingMappedReduced(list
, echo
, add
);
2392 QCOMPARE(result
, sum
);
2395 for (int i
= 0 ; i
< 100; ++i
) {
2396 QtConcurrent::map(list
, increment
).waitForFinished();
2397 for (int j
= 0; j
< listSize
; ++j
)
2398 QCOMPARE(list
.at(j
), i
+ j
+ 1);
2406 void tst_map::map() {}
2407 void tst_map::blocking_map() {}
2408 void tst_map::mapped() {}
2409 void tst_map::blocking_mapped() {}
2410 void tst_map::mappedReduced() {}
2411 void tst_map::blocking_mappedReduced() {}
2412 void tst_map::assignResult() {}
2413 void tst_map::functionOverloads() {}
2414 #ifndef QT_NO_EXCEPTIONS
2415 void tst_map::exceptions() {}
2417 void tst_map::incrementalResults() {}
2418 void tst_map::stressTest() {}
2419 void tst_map::throttling() {}
2420 void tst_map::stlContainers() {}
2421 void tst_map::noDetatch() {}
2427 #include "tst_qtconcurrentmap.moc"