1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the test suite of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
21 | ** included in the packaging of this file. Please review the following |
22 | ** information to ensure the GNU General Public License requirements will |
23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
24 | ** |
25 | ** $QT_END_LICENSE$ |
26 | ** |
27 | ****************************************************************************/ |
28 | #include <qtconcurrentfilter.h> |
29 | #include <QCoreApplication> |
30 | #include <QList> |
31 | #include <QLinkedList> |
32 | #include <QtTest/QtTest> |
33 | |
34 | #include "../qtconcurrentmap/functions.h" |
35 | |
36 | class tst_QtConcurrentFilter : public QObject |
37 | { |
38 | Q_OBJECT |
39 | |
40 | private slots: |
41 | void filter(); |
42 | void filtered(); |
43 | void filteredReduced(); |
44 | void resultAt(); |
45 | void incrementalResults(); |
46 | void noDetach(); |
47 | void stlContainers(); |
48 | }; |
49 | |
50 | void tst_QtConcurrentFilter::filter() |
51 | { |
52 | // functor |
53 | { |
54 | QList<int> list; |
55 | list << 1 << 2 << 3 << 4; |
56 | QtConcurrent::filter(sequence&: list, keep: KeepEvenIntegers()).waitForFinished(); |
57 | QCOMPARE(list, QList<int>() << 2 << 4); |
58 | } |
59 | { |
60 | QList<int> list; |
61 | list << 1 << 2 << 3 << 4; |
62 | QtConcurrent::blockingFilter(sequence&: list, keep: KeepEvenIntegers()); |
63 | QCOMPARE(list, QList<int>() << 2 << 4); |
64 | } |
65 | { |
66 | QVector<int> vector; |
67 | vector << 1 << 2 << 3 << 4; |
68 | QtConcurrent::filter(sequence&: vector, keep: KeepEvenIntegers()).waitForFinished(); |
69 | QCOMPARE(vector, QVector<int>() << 2 << 4); |
70 | } |
71 | { |
72 | QVector<int> vector; |
73 | vector << 1 << 2 << 3 << 4; |
74 | QtConcurrent::blockingFilter(sequence&: vector, keep: KeepEvenIntegers()); |
75 | QCOMPARE(vector, QVector<int>() << 2 << 4); |
76 | } |
77 | |
78 | // function |
79 | { |
80 | QList<int> list; |
81 | list << 1 << 2 << 3 << 4; |
82 | QtConcurrent::filter(sequence&: list, keep: keepEvenIntegers).waitForFinished(); |
83 | QCOMPARE(list, QList<int>() << 2 << 4); |
84 | } |
85 | { |
86 | QList<int> list; |
87 | list << 1 << 2 << 3 << 4; |
88 | QtConcurrent::blockingFilter(sequence&: list, keep: keepEvenIntegers); |
89 | QCOMPARE(list, QList<int>() << 2 << 4); |
90 | } |
91 | |
92 | // bound function |
93 | { |
94 | QList<int> list; |
95 | list << 1 << 2 << 3 << 4; |
96 | QtConcurrent::filter(sequence&: list, keep: keepEvenIntegers).waitForFinished(); |
97 | QCOMPARE(list, QList<int>() << 2 << 4); |
98 | } |
99 | { |
100 | QList<int> list; |
101 | list << 1 << 2 << 3 << 4; |
102 | QtConcurrent::blockingFilter(sequence&: list, keep: keepEvenIntegers); |
103 | QCOMPARE(list, QList<int>() << 2 << 4); |
104 | } |
105 | |
106 | // member |
107 | { |
108 | QList<Number> list; |
109 | list << 1 << 2 << 3 << 4; |
110 | QtConcurrent::filter(sequence&: list, keep: &Number::isEven).waitForFinished(); |
111 | QCOMPARE(list, QList<Number>() << 2 << 4); |
112 | } |
113 | { |
114 | QList<Number> list; |
115 | list << 1 << 2 << 3 << 4; |
116 | QtConcurrent::blockingFilter(sequence&: list, keep: &Number::isEven); |
117 | QCOMPARE(list, QList<Number>() << 2 << 4); |
118 | } |
119 | |
120 | #if QT_DEPRECATED_SINCE(5, 15) |
121 | QT_WARNING_PUSH |
122 | QT_WARNING_DISABLE_DEPRECATED |
123 | |
124 | // functor |
125 | { |
126 | QLinkedList<int> linkedList; |
127 | linkedList << 1 << 2 << 3 << 4; |
128 | QtConcurrent::filter(sequence&: linkedList, keep: KeepEvenIntegers()).waitForFinished(); |
129 | QCOMPARE(linkedList, QLinkedList<int>() << 2 << 4); |
130 | } |
131 | { |
132 | QLinkedList<int> linkedList; |
133 | linkedList << 1 << 2 << 3 << 4; |
134 | QtConcurrent::blockingFilter(sequence&: linkedList, keep: KeepEvenIntegers()); |
135 | QCOMPARE(linkedList, QLinkedList<int>() << 2 << 4); |
136 | } |
137 | |
138 | // function |
139 | { |
140 | QLinkedList<int> linkedList; |
141 | linkedList << 1 << 2 << 3 << 4; |
142 | QtConcurrent::filter(sequence&: linkedList, keep: keepEvenIntegers).waitForFinished(); |
143 | QCOMPARE(linkedList, QLinkedList<int>() << 2 << 4); |
144 | } |
145 | { |
146 | QLinkedList<int> linkedList; |
147 | linkedList << 1 << 2 << 3 << 4; |
148 | QtConcurrent::blockingFilter(sequence&: linkedList, keep: keepEvenIntegers); |
149 | QCOMPARE(linkedList, QLinkedList<int>() << 2 << 4); |
150 | } |
151 | |
152 | // bound function |
153 | { |
154 | QLinkedList<int> linkedList; |
155 | linkedList << 1 << 2 << 3 << 4; |
156 | QtConcurrent::filter(sequence&: linkedList, keep: keepEvenIntegers).waitForFinished(); |
157 | QCOMPARE(linkedList, QLinkedList<int>() << 2 << 4); |
158 | } |
159 | { |
160 | QLinkedList<int> linkedList; |
161 | linkedList << 1 << 2 << 3 << 4; |
162 | QtConcurrent::blockingFilter(sequence&: linkedList, keep: keepEvenIntegers); |
163 | QCOMPARE(linkedList, QLinkedList<int>() << 2 << 4); |
164 | } |
165 | |
166 | // member |
167 | { |
168 | QLinkedList<Number> linkedList; |
169 | linkedList << 1 << 2 << 3 << 4; |
170 | QtConcurrent::filter(sequence&: linkedList, keep: &Number::isEven).waitForFinished(); |
171 | QCOMPARE(linkedList, QLinkedList<Number>() << 2 << 4); |
172 | } |
173 | { |
174 | QLinkedList<Number> linkedList; |
175 | linkedList << 1 << 2 << 3 << 4; |
176 | QtConcurrent::blockingFilter(sequence&: linkedList, keep: &Number::isEven); |
177 | QCOMPARE(linkedList, QLinkedList<Number>() << 2 << 4); |
178 | } |
179 | |
180 | QT_WARNING_POP |
181 | #endif |
182 | } |
183 | |
184 | void tst_QtConcurrentFilter::filtered() |
185 | { |
186 | QList<int> list; |
187 | list << 1 << 2 << 3 << 4; |
188 | |
189 | // functor |
190 | { |
191 | QFuture<int> f = QtConcurrent::filtered(sequence: list, keep: KeepEvenIntegers()); |
192 | QList<int> list2 = f.results(); |
193 | QCOMPARE(list2, QList<int>() << 2 << 4); |
194 | } |
195 | { |
196 | QFuture<int> f = QtConcurrent::filtered(begin: list.begin(), end: list.end(), keep: KeepEvenIntegers()); |
197 | QList<int> list2 = f.results(); |
198 | QCOMPARE(list2, QList<int>() << 2 << 4); |
199 | } |
200 | { |
201 | QFuture<int> f = QtConcurrent::filtered(begin: list.constBegin(), |
202 | end: list.constEnd(), |
203 | keep: KeepEvenIntegers()); |
204 | QList<int> list2 = f.results(); |
205 | QCOMPARE(list2, QList<int>() << 2 << 4); |
206 | } |
207 | { |
208 | QList<int> list2 = QtConcurrent::blockingFiltered(sequence: list, keep: KeepEvenIntegers()); |
209 | QCOMPARE(list2, QList<int>() << 2 << 4); |
210 | } |
211 | { |
212 | QList<int> list2 = QtConcurrent::blockingFiltered<QList<int> >(begin: list.begin(), |
213 | end: list.end(), |
214 | keep: KeepEvenIntegers()); |
215 | QCOMPARE(list2, QList<int>() << 2 << 4); |
216 | } |
217 | { |
218 | QList<int> list2 = QtConcurrent::blockingFiltered<QList<int> >(begin: list.constBegin(), |
219 | end: list.constEnd(), |
220 | keep: KeepEvenIntegers()); |
221 | QCOMPARE(list2, QList<int>() << 2 << 4); |
222 | } |
223 | |
224 | { |
225 | QVector<int> vector; |
226 | vector << 1 << 2 << 3 << 4; |
227 | QVector<int> vector2 = QtConcurrent::blockingFiltered(sequence: vector, keep: KeepEvenIntegers()); |
228 | QCOMPARE(vector2, QVector<int>() << 2 << 4); |
229 | } |
230 | { |
231 | QVector<int> vector; |
232 | vector << 1 << 2 << 3 << 4; |
233 | QFuture<int> f = QtConcurrent::filtered(sequence: vector, keep: KeepEvenIntegers()); |
234 | QCOMPARE(f.results(), QList<int>() << 2 << 4); |
235 | } |
236 | |
237 | // function |
238 | { |
239 | QFuture<int> f = QtConcurrent::filtered(sequence: list, keep: keepEvenIntegers); |
240 | QList<int> list2 = f.results(); |
241 | QCOMPARE(list2, QList<int>() << 2 << 4); |
242 | } |
243 | { |
244 | QFuture<int> f = QtConcurrent::filtered(begin: list.begin(), end: list.end(), keep: keepEvenIntegers); |
245 | QList<int> list2 = f.results(); |
246 | QCOMPARE(list2, QList<int>() << 2 << 4); |
247 | } |
248 | { |
249 | QFuture<int> f = QtConcurrent::filtered(begin: list.constBegin(), |
250 | end: list.constEnd(), |
251 | keep: keepEvenIntegers); |
252 | QList<int> list2 = f.results(); |
253 | QCOMPARE(list2, QList<int>() << 2 << 4); |
254 | } |
255 | { |
256 | QList<int> list2 = QtConcurrent::blockingFiltered(sequence: list, keep: keepEvenIntegers); |
257 | QCOMPARE(list2, QList<int>() << 2 << 4); |
258 | } |
259 | { |
260 | QList<int> list2 = QtConcurrent::blockingFiltered<QList<int> >(begin: list.begin(), |
261 | end: list.end(), |
262 | keep: keepEvenIntegers); |
263 | QCOMPARE(list2, QList<int>() << 2 << 4); |
264 | } |
265 | { |
266 | QList<int> list2 = QtConcurrent::blockingFiltered<QList<int> >(begin: list.constBegin(), |
267 | end: list.constEnd(), |
268 | keep: keepEvenIntegers); |
269 | QCOMPARE(list2, QList<int>() << 2 << 4); |
270 | } |
271 | |
272 | // bound function |
273 | { |
274 | QFuture<int> f = QtConcurrent::filtered(sequence: list, keep: keepEvenIntegers); |
275 | QList<int> list2 = f.results(); |
276 | QCOMPARE(list2, QList<int>() << 2 << 4); |
277 | } |
278 | { |
279 | QFuture<int> f = QtConcurrent::filtered(begin: list.begin(), end: list.end(), keep: keepEvenIntegers); |
280 | QList<int> list2 = f.results(); |
281 | QCOMPARE(list2, QList<int>() << 2 << 4); |
282 | } |
283 | { |
284 | QFuture<int> f = QtConcurrent::filtered(begin: list.constBegin(), |
285 | end: list.constEnd(), |
286 | keep: keepEvenIntegers); |
287 | QList<int> list2 = f.results(); |
288 | QCOMPARE(list2, QList<int>() << 2 << 4); |
289 | } |
290 | { |
291 | QList<int> list2 = QtConcurrent::blockingFiltered(sequence: list, keep: keepEvenIntegers); |
292 | QCOMPARE(list2, QList<int>() << 2 << 4); |
293 | } |
294 | { |
295 | QList<int> list2 = QtConcurrent::blockingFiltered<QList<int> >(begin: list.begin(), |
296 | end: list.end(), |
297 | keep: keepEvenIntegers); |
298 | QCOMPARE(list2, QList<int>() << 2 << 4); |
299 | } |
300 | { |
301 | QList<int> list2 = QtConcurrent::blockingFiltered<QList<int> >(begin: list.constBegin(), |
302 | end: list.constEnd(), |
303 | keep: keepEvenIntegers); |
304 | QCOMPARE(list2, QList<int>() << 2 << 4); |
305 | } |
306 | |
307 | // const member function |
308 | { |
309 | QList<Number> integers; |
310 | integers << 1 << 2 << 3 << 4; |
311 | QFuture<Number> f = QtConcurrent::filtered(sequence: integers, keep: &Number::isEven); |
312 | QList<Number> list2 = f.results(); |
313 | QCOMPARE(list2, QList<Number>() << 2 << 4); |
314 | } |
315 | { |
316 | QList<Number> integers; |
317 | integers << 1 << 2 << 3 << 4; |
318 | QFuture<Number> f = QtConcurrent::filtered(begin: integers.begin(), |
319 | end: integers.end(), |
320 | keep: &Number::isEven); |
321 | QList<Number> list2 = f.results(); |
322 | QCOMPARE(list2, QList<Number>() << 2 << 4); |
323 | } |
324 | { |
325 | QList<Number> integers; |
326 | integers << 1 << 2 << 3 << 4; |
327 | QFuture<Number> f = QtConcurrent::filtered(begin: integers.constBegin(), |
328 | end: integers.constEnd(), |
329 | keep: &Number::isEven); |
330 | QList<Number> list2 = f.results(); |
331 | QCOMPARE(list2, QList<Number>() << 2 << 4); |
332 | } |
333 | { |
334 | QList<Number> integers; |
335 | integers << 1 << 2 << 3 << 4; |
336 | QList<Number> list2 = QtConcurrent::blockingFiltered(sequence: integers, keep: &Number::isEven); |
337 | QCOMPARE(list2, QList<Number>() << 2 << 4); |
338 | } |
339 | { |
340 | QList<Number> integers; |
341 | integers << 1 << 2 << 3 << 4; |
342 | QList<Number> list2 = QtConcurrent::blockingFiltered<QList<Number> >(begin: integers.begin(), |
343 | end: integers.end(), |
344 | keep: &Number::isEven); |
345 | QCOMPARE(list2, QList<Number>() << 2 << 4); |
346 | } |
347 | { |
348 | QList<Number> integers; |
349 | integers << 1 << 2 << 3 << 4; |
350 | QList<Number> list2 = |
351 | QtConcurrent::blockingFiltered<QList<Number> >(begin: integers.constBegin(), |
352 | end: integers.constEnd(), |
353 | keep: &Number::isEven); |
354 | QCOMPARE(list2, QList<Number>() << 2 << 4); |
355 | } |
356 | |
357 | #if QT_DEPRECATED_SINCE(5, 15) |
358 | QT_WARNING_PUSH |
359 | QT_WARNING_DISABLE_DEPRECATED |
360 | |
361 | // same thing on linked lists |
362 | |
363 | QLinkedList<int> linkedList; |
364 | linkedList << 1 << 2 << 3 << 4; |
365 | |
366 | // functor |
367 | { |
368 | QFuture<int> f = QtConcurrent::filtered(sequence: linkedList, keep: KeepEvenIntegers()); |
369 | QList<int> linkedList2 = f.results(); |
370 | QCOMPARE(linkedList2, QList<int>() << 2 << 4); |
371 | } |
372 | { |
373 | QFuture<int> f = QtConcurrent::filtered(begin: linkedList.begin(), |
374 | end: linkedList.end(), |
375 | keep: KeepEvenIntegers()); |
376 | QList<int> linkedList2 = f.results(); |
377 | QCOMPARE(linkedList2, QList<int>() << 2 << 4); |
378 | } |
379 | { |
380 | QFuture<int> f = QtConcurrent::filtered(begin: linkedList.constBegin(), |
381 | end: linkedList.constEnd(), |
382 | keep: KeepEvenIntegers()); |
383 | QList<int> linkedList2 = f.results(); |
384 | QCOMPARE(linkedList2, QList<int>() << 2 << 4); |
385 | } |
386 | { |
387 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFiltered(sequence: linkedList, keep: KeepEvenIntegers()); |
388 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
389 | } |
390 | { |
391 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFiltered<QLinkedList<int> >(begin: linkedList.begin(), |
392 | end: linkedList.end(), |
393 | keep: KeepEvenIntegers()); |
394 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
395 | } |
396 | { |
397 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFiltered<QLinkedList<int> >(begin: linkedList.constBegin(), |
398 | end: linkedList.constEnd(), |
399 | keep: KeepEvenIntegers()); |
400 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
401 | } |
402 | |
403 | // function |
404 | { |
405 | QFuture<int> f = QtConcurrent::filtered(sequence: linkedList, keep: keepEvenIntegers); |
406 | QList<int> linkedList2 = f.results(); |
407 | QCOMPARE(linkedList2, QList<int>() << 2 << 4); |
408 | } |
409 | { |
410 | QFuture<int> f = QtConcurrent::filtered(begin: linkedList.begin(), |
411 | end: linkedList.end(), |
412 | keep: keepEvenIntegers); |
413 | QList<int> linkedList2 = f.results(); |
414 | QCOMPARE(linkedList2, QList<int>() << 2 << 4); |
415 | } |
416 | { |
417 | QFuture<int> f = QtConcurrent::filtered(begin: linkedList.constBegin(), |
418 | end: linkedList.constEnd(), |
419 | keep: keepEvenIntegers); |
420 | QList<int> linkedList2 = f.results(); |
421 | QCOMPARE(linkedList2, QList<int>() << 2 << 4); |
422 | } |
423 | { |
424 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFiltered(sequence: linkedList, keep: keepEvenIntegers); |
425 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
426 | } |
427 | { |
428 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFiltered<QLinkedList<int> >(begin: linkedList.begin(), |
429 | end: linkedList.end(), |
430 | keep: keepEvenIntegers); |
431 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
432 | } |
433 | { |
434 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFiltered<QLinkedList<int> >(begin: linkedList.constBegin(), |
435 | end: linkedList.constEnd(), |
436 | keep: keepEvenIntegers); |
437 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
438 | } |
439 | |
440 | // bound function |
441 | { |
442 | QFuture<int> f = QtConcurrent::filtered(sequence: linkedList, keep: keepEvenIntegers); |
443 | QList<int> linkedList2 = f.results(); |
444 | QCOMPARE(linkedList2, QList<int>() << 2 << 4); |
445 | } |
446 | { |
447 | QFuture<int> f = QtConcurrent::filtered(begin: linkedList.begin(), |
448 | end: linkedList.end(), |
449 | keep: keepEvenIntegers); |
450 | QList<int> linkedList2 = f.results(); |
451 | QCOMPARE(linkedList2, QList<int>() << 2 << 4); |
452 | } |
453 | { |
454 | QFuture<int> f = QtConcurrent::filtered(begin: linkedList.constBegin(), |
455 | end: linkedList.constEnd(), |
456 | keep: keepEvenIntegers); |
457 | QList<int> linkedList2 = f.results(); |
458 | QCOMPARE(linkedList2, QList<int>() << 2 << 4); |
459 | } |
460 | { |
461 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFiltered(sequence: linkedList, keep: keepEvenIntegers); |
462 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
463 | } |
464 | { |
465 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFiltered<QLinkedList<int> >(begin: linkedList.begin(), |
466 | end: linkedList.end(), |
467 | keep: keepEvenIntegers); |
468 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
469 | } |
470 | { |
471 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFiltered<QLinkedList<int> >(begin: linkedList.constBegin(), |
472 | end: linkedList.constEnd(), |
473 | keep: keepEvenIntegers); |
474 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
475 | } |
476 | |
477 | // const member function |
478 | { |
479 | QLinkedList<Number> integers; |
480 | integers << 1 << 2 << 3 << 4; |
481 | QFuture<Number> f = QtConcurrent::filtered(sequence: integers, keep: &Number::isEven); |
482 | QList<Number> linkedList2 = f.results(); |
483 | QCOMPARE(linkedList2, QList<Number>() << 2 << 4); |
484 | } |
485 | { |
486 | QLinkedList<Number> integers; |
487 | integers << 1 << 2 << 3 << 4; |
488 | QFuture<Number> f = QtConcurrent::filtered(begin: integers.begin(), |
489 | end: integers.end(), |
490 | keep: &Number::isEven); |
491 | QList<Number> linkedList2 = f.results(); |
492 | QCOMPARE(linkedList2, QList<Number>() << 2 << 4); |
493 | } |
494 | { |
495 | QLinkedList<Number> integers; |
496 | integers << 1 << 2 << 3 << 4; |
497 | QFuture<Number> f = QtConcurrent::filtered(begin: integers.constBegin(), |
498 | end: integers.constEnd(), |
499 | keep: &Number::isEven); |
500 | QList<Number> linkedList2 = f.results(); |
501 | QCOMPARE(linkedList2, QList<Number>() << 2 << 4); |
502 | } |
503 | { |
504 | QLinkedList<Number> integers; |
505 | integers << 1 << 2 << 3 << 4; |
506 | QLinkedList<Number> linkedList2 = QtConcurrent::blockingFiltered(sequence: integers, keep: &Number::isEven); |
507 | QCOMPARE(linkedList2, QLinkedList<Number>() << 2 << 4); |
508 | } |
509 | { |
510 | QLinkedList<Number> integers; |
511 | integers << 1 << 2 << 3 << 4; |
512 | QLinkedList<Number> linkedList2 = QtConcurrent::blockingFiltered<QLinkedList<Number> >(begin: integers.begin(), |
513 | end: integers.end(), |
514 | keep: &Number::isEven); |
515 | QCOMPARE(linkedList2, QLinkedList<Number>() << 2 << 4); |
516 | } |
517 | { |
518 | QLinkedList<Number> integers; |
519 | integers << 1 << 2 << 3 << 4; |
520 | QLinkedList<Number> linkedList2 = |
521 | QtConcurrent::blockingFiltered<QLinkedList<Number> >(begin: integers.constBegin(), |
522 | end: integers.constEnd(), |
523 | keep: &Number::isEven); |
524 | QCOMPARE(linkedList2, QLinkedList<Number>() << 2 << 4); |
525 | } |
526 | |
527 | QT_WARNING_POP |
528 | #endif |
529 | } |
530 | |
531 | void tst_QtConcurrentFilter::filteredReduced() |
532 | { |
533 | QList<int> list; |
534 | list << 1 << 2 << 3 << 4; |
535 | QList<Number> numberList; |
536 | numberList << 1 << 2 << 3 << 4; |
537 | |
538 | // functor-functor |
539 | { |
540 | int sum = QtConcurrent::filteredReduced<int>(sequence: list, keep: KeepEvenIntegers(), reduce: IntSumReduce()); |
541 | QCOMPARE(sum, 6); |
542 | |
543 | int sum2 = QtConcurrent::filteredReduced<int>(sequence: list, keep: keepEvenIntegers, reduce: intSumReduce); |
544 | QCOMPARE(sum2, 6); |
545 | } |
546 | { |
547 | QVector<int> vector; |
548 | vector << 1 << 2 << 3 << 4; |
549 | int sum = QtConcurrent::filteredReduced<int>(sequence: vector, keep: KeepEvenIntegers(), reduce: IntSumReduce()); |
550 | QCOMPARE(sum, 6); |
551 | } |
552 | { |
553 | int sum = QtConcurrent::filteredReduced<int>(begin: list.begin(), |
554 | end: list.end(), |
555 | keep: KeepEvenIntegers(), |
556 | reduce: IntSumReduce()); |
557 | QCOMPARE(sum, 6); |
558 | |
559 | int sum2 = QtConcurrent::filteredReduced<int>(begin: list.begin(), |
560 | end: list.end(), |
561 | keep: keepEvenIntegers, |
562 | reduce: intSumReduce); |
563 | QCOMPARE(sum2, 6); |
564 | } |
565 | { |
566 | int sum = QtConcurrent::filteredReduced<int>(begin: list.constBegin(), |
567 | end: list.constEnd(), |
568 | keep: KeepEvenIntegers(), |
569 | reduce: IntSumReduce()); |
570 | QCOMPARE(sum, 6); |
571 | |
572 | int sum2 = QtConcurrent::filteredReduced<int>(begin: list.constBegin(), |
573 | end: list.constEnd(), |
574 | keep: keepEvenIntegers, |
575 | reduce: intSumReduce); |
576 | QCOMPARE(sum2, 6); |
577 | } |
578 | { |
579 | int sum = QtConcurrent::blockingFilteredReduced<int>(sequence: list, keep: KeepEvenIntegers(), reduce: IntSumReduce()); |
580 | QCOMPARE(sum, 6); |
581 | |
582 | int sum2 = QtConcurrent::blockingFilteredReduced<int>(sequence: list, keep: keepEvenIntegers, reduce: intSumReduce); |
583 | QCOMPARE(sum2, 6); |
584 | } |
585 | { |
586 | int sum = QtConcurrent::blockingFilteredReduced<int>(begin: list.begin(), |
587 | end: list.end(), |
588 | keep: KeepEvenIntegers(), |
589 | reduce: IntSumReduce()); |
590 | QCOMPARE(sum, 6); |
591 | |
592 | int sum2 = QtConcurrent::blockingFilteredReduced<int>(begin: list.begin(), |
593 | end: list.end(), |
594 | keep: keepEvenIntegers, |
595 | reduce: intSumReduce); |
596 | QCOMPARE(sum2, 6); |
597 | } |
598 | { |
599 | int sum = QtConcurrent::blockingFilteredReduced<int>(begin: list.constBegin(), |
600 | end: list.constEnd(), |
601 | keep: KeepEvenIntegers(), |
602 | reduce: IntSumReduce()); |
603 | QCOMPARE(sum, 6); |
604 | |
605 | int sum2 = QtConcurrent::blockingFilteredReduced<int>(begin: list.constBegin(), |
606 | end: list.constEnd(), |
607 | keep: keepEvenIntegers, |
608 | reduce: intSumReduce); |
609 | QCOMPARE(sum2, 6); |
610 | } |
611 | |
612 | // function-functor |
613 | { |
614 | int sum = QtConcurrent::filteredReduced<int>(sequence: list, keep: keepEvenIntegers, reduce: IntSumReduce()); |
615 | QCOMPARE(sum, 6); |
616 | } |
617 | { |
618 | int sum = QtConcurrent::filteredReduced<int>(begin: list.begin(), |
619 | end: list.end(), |
620 | keep: keepEvenIntegers, |
621 | reduce: IntSumReduce()); |
622 | QCOMPARE(sum, 6); |
623 | } |
624 | { |
625 | int sum = QtConcurrent::filteredReduced<int>(begin: list.constBegin(), |
626 | end: list.constEnd(), |
627 | keep: keepEvenIntegers, |
628 | reduce: IntSumReduce()); |
629 | QCOMPARE(sum, 6); |
630 | } |
631 | { |
632 | int sum = QtConcurrent::blockingFilteredReduced<int>(sequence: list, keep: keepEvenIntegers, reduce: IntSumReduce()); |
633 | QCOMPARE(sum, 6); |
634 | } |
635 | { |
636 | int sum = QtConcurrent::blockingFilteredReduced<int>(begin: list.begin(), |
637 | end: list.end(), |
638 | keep: keepEvenIntegers, |
639 | reduce: IntSumReduce()); |
640 | QCOMPARE(sum, 6); |
641 | } |
642 | { |
643 | int sum = QtConcurrent::blockingFilteredReduced<int>(begin: list.constBegin(), |
644 | end: list.constEnd(), |
645 | keep: keepEvenIntegers, |
646 | reduce: IntSumReduce()); |
647 | QCOMPARE(sum, 6); |
648 | } |
649 | |
650 | // functor-function |
651 | { |
652 | int sum = QtConcurrent::filteredReduced(sequence: list, keep: KeepEvenIntegers(), reduce: intSumReduce); |
653 | QCOMPARE(sum, 6); |
654 | } |
655 | { |
656 | int sum = QtConcurrent::filteredReduced(begin: list.begin(), |
657 | end: list.end(), |
658 | keep: KeepEvenIntegers(), |
659 | reduce: intSumReduce); |
660 | QCOMPARE(sum, 6); |
661 | } |
662 | { |
663 | int sum = QtConcurrent::filteredReduced(begin: list.constBegin(), |
664 | end: list.constEnd(), |
665 | keep: KeepEvenIntegers(), |
666 | reduce: intSumReduce); |
667 | QCOMPARE(sum, 6); |
668 | } |
669 | { |
670 | int sum = QtConcurrent::blockingFilteredReduced(sequence: list, keep: KeepEvenIntegers(), reduce: intSumReduce); |
671 | QCOMPARE(sum, 6); |
672 | } |
673 | { |
674 | int sum = QtConcurrent::blockingFilteredReduced(begin: list.begin(), |
675 | end: list.end(), |
676 | keep: KeepEvenIntegers(), |
677 | reduce: intSumReduce); |
678 | QCOMPARE(sum, 6); |
679 | } |
680 | { |
681 | int sum = QtConcurrent::blockingFilteredReduced(begin: list.constBegin(), |
682 | end: list.constEnd(), |
683 | keep: KeepEvenIntegers(), |
684 | reduce: intSumReduce); |
685 | QCOMPARE(sum, 6); |
686 | } |
687 | |
688 | // function-function |
689 | { |
690 | int sum = QtConcurrent::filteredReduced(sequence: list, keep: keepEvenIntegers, reduce: intSumReduce); |
691 | QCOMPARE(sum, 6); |
692 | } |
693 | { |
694 | int sum = QtConcurrent::filteredReduced(begin: list.begin(), |
695 | end: list.end(), |
696 | keep: keepEvenIntegers, |
697 | reduce: intSumReduce); |
698 | QCOMPARE(sum, 6); |
699 | } |
700 | { |
701 | int sum = QtConcurrent::filteredReduced(begin: list.constBegin(), |
702 | end: list.constEnd(), |
703 | keep: keepEvenIntegers, |
704 | reduce: intSumReduce); |
705 | QCOMPARE(sum, 6); |
706 | } |
707 | { |
708 | int sum = QtConcurrent::blockingFilteredReduced(sequence: list, keep: keepEvenIntegers, reduce: intSumReduce); |
709 | QCOMPARE(sum, 6); |
710 | } |
711 | { |
712 | int sum = QtConcurrent::blockingFilteredReduced(begin: list.begin(), |
713 | end: list.end(), |
714 | keep: keepEvenIntegers, |
715 | reduce: intSumReduce); |
716 | QCOMPARE(sum, 6); |
717 | } |
718 | { |
719 | int sum = QtConcurrent::blockingFilteredReduced(begin: list.constBegin(), |
720 | end: list.constEnd(), |
721 | keep: keepEvenIntegers, |
722 | reduce: intSumReduce); |
723 | QCOMPARE(sum, 6); |
724 | } |
725 | |
726 | // functor-member |
727 | { |
728 | QList<int> list2 = QtConcurrent::filteredReduced(sequence: list, keep: KeepEvenIntegers(), reduce: &QList<int>::push_back, options: QtConcurrent::OrderedReduce); |
729 | QCOMPARE(list2, QList<int>() << 2 << 4); |
730 | } |
731 | { |
732 | QList<int> list2 = QtConcurrent::filteredReduced(begin: list.begin(), |
733 | end: list.end(), |
734 | keep: KeepEvenIntegers(), |
735 | reduce: &QList<int>::push_back, |
736 | options: QtConcurrent::OrderedReduce); |
737 | QCOMPARE(list2, QList<int>() << 2 << 4); |
738 | } |
739 | { |
740 | QList<int> list2 = QtConcurrent::filteredReduced(begin: list.constBegin(), |
741 | end: list.constEnd(), |
742 | keep: KeepEvenIntegers(), |
743 | reduce: &QList<int>::push_back, |
744 | options: QtConcurrent::OrderedReduce); |
745 | QCOMPARE(list2, QList<int>() << 2 << 4); |
746 | } |
747 | { |
748 | QList<int> list2 = QtConcurrent::blockingFilteredReduced(sequence: list, keep: KeepEvenIntegers(), reduce: &QList<int>::push_back, options: QtConcurrent::OrderedReduce); |
749 | QCOMPARE(list2, QList<int>() << 2 << 4); |
750 | } |
751 | { |
752 | QList<int> list2 = QtConcurrent::blockingFilteredReduced(begin: list.begin(), |
753 | end: list.end(), |
754 | keep: KeepEvenIntegers(), |
755 | reduce: &QList<int>::push_back, |
756 | options: QtConcurrent::OrderedReduce); |
757 | QCOMPARE(list2, QList<int>() << 2 << 4); |
758 | } |
759 | { |
760 | QList<int> list2 = QtConcurrent::blockingFilteredReduced(begin: list.constBegin(), |
761 | end: list.constEnd(), |
762 | keep: KeepEvenIntegers(), |
763 | reduce: &QList<int>::push_back, |
764 | options: QtConcurrent::OrderedReduce); |
765 | QCOMPARE(list2, QList<int>() << 2 << 4); |
766 | } |
767 | |
768 | // member-functor |
769 | { |
770 | int sum = QtConcurrent::filteredReduced<int>(sequence: numberList, keep: &Number::isEven, reduce: NumberSumReduce()); |
771 | QCOMPARE(sum, 6); |
772 | |
773 | int sum2 = QtConcurrent::filteredReduced<int>(sequence: QList<Number>(numberList), |
774 | keep: &Number::isEven, |
775 | reduce: NumberSumReduce()); |
776 | QCOMPARE(sum2, 6); |
777 | } |
778 | { |
779 | int sum = QtConcurrent::filteredReduced<int>(begin: numberList.begin(), |
780 | end: numberList.end(), |
781 | keep: &Number::isEven, |
782 | reduce: NumberSumReduce()); |
783 | QCOMPARE(sum, 6); |
784 | } |
785 | { |
786 | int sum = QtConcurrent::filteredReduced<int>(begin: numberList.constBegin(), |
787 | end: numberList.constEnd(), |
788 | keep: &Number::isEven, |
789 | reduce: NumberSumReduce()); |
790 | QCOMPARE(sum, 6); |
791 | } |
792 | { |
793 | int sum = QtConcurrent::blockingFilteredReduced<int>(sequence: numberList, keep: &Number::isEven, reduce: NumberSumReduce()); |
794 | QCOMPARE(sum, 6); |
795 | |
796 | int sum2 = QtConcurrent::blockingFilteredReduced<int>(sequence: QList<Number>(numberList), |
797 | keep: &Number::isEven, |
798 | reduce: NumberSumReduce()); |
799 | QCOMPARE(sum2, 6); |
800 | } |
801 | { |
802 | int sum = QtConcurrent::blockingFilteredReduced<int>(begin: numberList.begin(), |
803 | end: numberList.end(), |
804 | keep: &Number::isEven, |
805 | reduce: NumberSumReduce()); |
806 | QCOMPARE(sum, 6); |
807 | } |
808 | { |
809 | int sum = QtConcurrent::blockingFilteredReduced<int>(begin: numberList.constBegin(), |
810 | end: numberList.constEnd(), |
811 | keep: &Number::isEven, |
812 | reduce: NumberSumReduce()); |
813 | QCOMPARE(sum, 6); |
814 | } |
815 | |
816 | // member-member |
817 | { |
818 | QList<Number> numbers; |
819 | numbers << 1 << 2 << 3 << 4; |
820 | QList<Number> list2 = QtConcurrent::filteredReduced(sequence: numbers, |
821 | keep: &Number::isEven, |
822 | reduce: &QList<Number>::push_back, options: QtConcurrent::OrderedReduce); |
823 | QCOMPARE(list2, QList<Number>() << 2 << 4); |
824 | } |
825 | { |
826 | QList<Number> numbers; |
827 | numbers << 1 << 2 << 3 << 4; |
828 | QList<Number> list2 = QtConcurrent::filteredReduced(begin: numbers.begin(), |
829 | end: numbers.end(), |
830 | keep: &Number::isEven, |
831 | reduce: &QList<Number>::push_back, |
832 | options: QtConcurrent::OrderedReduce); |
833 | QCOMPARE(list2, QList<Number>() << 2 << 4); |
834 | } |
835 | { |
836 | QList<Number> numbers; |
837 | numbers << 1 << 2 << 3 << 4; |
838 | QList<Number> list2 = QtConcurrent::filteredReduced(begin: numbers.constBegin(), |
839 | end: numbers.constEnd(), |
840 | keep: &Number::isEven, |
841 | reduce: &QList<Number>::push_back, |
842 | options: QtConcurrent::OrderedReduce); |
843 | QCOMPARE(list2, QList<Number>() << 2 << 4); |
844 | } |
845 | { |
846 | QList<Number> numbers; |
847 | numbers << 1 << 2 << 3 << 4; |
848 | QList<Number> list2 = QtConcurrent::blockingFilteredReduced(sequence: numbers, |
849 | keep: &Number::isEven, |
850 | reduce: &QList<Number>::push_back, options: QtConcurrent::OrderedReduce); |
851 | QCOMPARE(list2, QList<Number>() << 2 << 4); |
852 | } |
853 | { |
854 | QList<Number> numbers; |
855 | numbers << 1 << 2 << 3 << 4; |
856 | QList<Number> list2 = QtConcurrent::blockingFilteredReduced(begin: numbers.begin(), |
857 | end: numbers.end(), |
858 | keep: &Number::isEven, |
859 | reduce: &QList<Number>::push_back, |
860 | options: QtConcurrent::OrderedReduce); |
861 | QCOMPARE(list2, QList<Number>() << 2 << 4); |
862 | } |
863 | { |
864 | QList<Number> numbers; |
865 | numbers << 1 << 2 << 3 << 4; |
866 | QList<Number> list2 = QtConcurrent::blockingFilteredReduced(begin: numbers.constBegin(), |
867 | end: numbers.constEnd(), |
868 | keep: &Number::isEven, |
869 | reduce: &QList<Number>::push_back, |
870 | options: QtConcurrent::OrderedReduce); |
871 | QCOMPARE(list2, QList<Number>() << 2 << 4); |
872 | } |
873 | |
874 | // function-member |
875 | { |
876 | QList<int> list2 = QtConcurrent::filteredReduced(sequence: list, keep: keepEvenIntegers, reduce: &QList<int>::push_back, options: QtConcurrent::OrderedReduce); |
877 | QCOMPARE(list2, QList<int>() << 2 << 4); |
878 | } |
879 | { |
880 | QList<int> list2 = QtConcurrent::filteredReduced(begin: list.begin(), |
881 | end: list.end(), |
882 | keep: keepEvenIntegers, |
883 | reduce: &QList<int>::push_back, |
884 | options: QtConcurrent::OrderedReduce); |
885 | QCOMPARE(list2, QList<int>() << 2 << 4); |
886 | } |
887 | { |
888 | QList<int> list2 = QtConcurrent::filteredReduced(begin: list.constBegin(), |
889 | end: list.constEnd(), |
890 | keep: keepEvenIntegers, |
891 | reduce: &QList<int>::push_back, |
892 | options: QtConcurrent::OrderedReduce); |
893 | QCOMPARE(list2, QList<int>() << 2 << 4); |
894 | } |
895 | { |
896 | QList<int> list2 = QtConcurrent::blockingFilteredReduced(sequence: list, keep: keepEvenIntegers, reduce: &QList<int>::push_back, options: QtConcurrent::OrderedReduce); |
897 | QCOMPARE(list2, QList<int>() << 2 << 4); |
898 | } |
899 | { |
900 | QList<int> list2 = QtConcurrent::blockingFilteredReduced(begin: list.begin(), |
901 | end: list.end(), |
902 | keep: keepEvenIntegers, |
903 | reduce: &QList<int>::push_back, |
904 | options: QtConcurrent::OrderedReduce); |
905 | QCOMPARE(list2, QList<int>() << 2 << 4); |
906 | } |
907 | { |
908 | QList<int> list2 = QtConcurrent::blockingFilteredReduced(begin: list.constBegin(), |
909 | end: list.constEnd(), |
910 | keep: keepEvenIntegers, |
911 | reduce: &QList<int>::push_back, |
912 | options: QtConcurrent::OrderedReduce); |
913 | QCOMPARE(list2, QList<int>() << 2 << 4); |
914 | } |
915 | |
916 | // member-function |
917 | { |
918 | int sum = QtConcurrent::filteredReduced(sequence: numberList, keep: &Number::isEven, reduce: numberSumReduce); |
919 | QCOMPARE(sum, 6); |
920 | |
921 | int sum2 = QtConcurrent::filteredReduced(sequence: QList<Number>(numberList), |
922 | keep: &Number::isEven, |
923 | reduce: numberSumReduce); |
924 | QCOMPARE(sum2, 6); |
925 | } |
926 | { |
927 | int sum = QtConcurrent::filteredReduced(begin: numberList.begin(), |
928 | end: numberList.end(), |
929 | keep: &Number::isEven, |
930 | reduce: numberSumReduce); |
931 | QCOMPARE(sum, 6); |
932 | } |
933 | { |
934 | int sum = QtConcurrent::filteredReduced(begin: numberList.constBegin(), |
935 | end: numberList.constEnd(), |
936 | keep: &Number::isEven, |
937 | reduce: numberSumReduce); |
938 | QCOMPARE(sum, 6); |
939 | } |
940 | { |
941 | int sum = QtConcurrent::blockingFilteredReduced(sequence: numberList, keep: &Number::isEven, reduce: numberSumReduce); |
942 | QCOMPARE(sum, 6); |
943 | |
944 | int sum2 = QtConcurrent::blockingFilteredReduced(sequence: QList<Number>(numberList), |
945 | keep: &Number::isEven, |
946 | reduce: numberSumReduce); |
947 | QCOMPARE(sum2, 6); |
948 | } |
949 | { |
950 | int sum = QtConcurrent::blockingFilteredReduced(begin: numberList.begin(), |
951 | end: numberList.end(), |
952 | keep: &Number::isEven, |
953 | reduce: numberSumReduce); |
954 | QCOMPARE(sum, 6); |
955 | } |
956 | { |
957 | int sum = QtConcurrent::blockingFilteredReduced(begin: numberList.constBegin(), |
958 | end: numberList.constEnd(), |
959 | keep: &Number::isEven, |
960 | reduce: numberSumReduce); |
961 | QCOMPARE(sum, 6); |
962 | } |
963 | |
964 | #if QT_DEPRECATED_SINCE(5, 15) |
965 | QT_WARNING_PUSH |
966 | QT_WARNING_DISABLE_DEPRECATED |
967 | |
968 | // same as above on linked lists |
969 | QLinkedList<int> linkedList; |
970 | linkedList << 1 << 2 << 3 << 4; |
971 | QLinkedList<Number> numberLinkedList; |
972 | numberLinkedList << 1 << 2 << 3 << 4; |
973 | |
974 | // functor-functor |
975 | { |
976 | int sum = QtConcurrent::filteredReduced<int>(sequence: linkedList, keep: KeepEvenIntegers(), reduce: IntSumReduce()); |
977 | QCOMPARE(sum, 6); |
978 | |
979 | int sum2 = QtConcurrent::filteredReduced<int>(sequence: linkedList, keep: keepEvenIntegers, reduce: intSumReduce); |
980 | QCOMPARE(sum2, 6); |
981 | } |
982 | { |
983 | int sum = QtConcurrent::filteredReduced<int>(begin: linkedList.begin(), |
984 | end: linkedList.end(), |
985 | keep: KeepEvenIntegers(), |
986 | reduce: IntSumReduce()); |
987 | QCOMPARE(sum, 6); |
988 | |
989 | int sum2 = QtConcurrent::filteredReduced<int>(begin: linkedList.begin(), |
990 | end: linkedList.end(), |
991 | keep: keepEvenIntegers, |
992 | reduce: intSumReduce); |
993 | QCOMPARE(sum2, 6); |
994 | } |
995 | { |
996 | int sum = QtConcurrent::filteredReduced<int>(begin: linkedList.constBegin(), |
997 | end: linkedList.constEnd(), |
998 | keep: KeepEvenIntegers(), |
999 | reduce: IntSumReduce()); |
1000 | QCOMPARE(sum, 6); |
1001 | |
1002 | int sum2 = QtConcurrent::filteredReduced<int>(begin: linkedList.constBegin(), |
1003 | end: linkedList.constEnd(), |
1004 | keep: keepEvenIntegers, |
1005 | reduce: intSumReduce); |
1006 | QCOMPARE(sum2, 6); |
1007 | } |
1008 | { |
1009 | int sum = QtConcurrent::blockingFilteredReduced<int>(sequence: linkedList, keep: KeepEvenIntegers(), reduce: IntSumReduce()); |
1010 | QCOMPARE(sum, 6); |
1011 | |
1012 | int sum2 = QtConcurrent::blockingFilteredReduced<int>(sequence: linkedList, keep: keepEvenIntegers, reduce: intSumReduce); |
1013 | QCOMPARE(sum2, 6); |
1014 | } |
1015 | { |
1016 | int sum = QtConcurrent::blockingFilteredReduced<int>(begin: linkedList.begin(), |
1017 | end: linkedList.end(), |
1018 | keep: KeepEvenIntegers(), |
1019 | reduce: IntSumReduce()); |
1020 | QCOMPARE(sum, 6); |
1021 | |
1022 | int sum2 = QtConcurrent::blockingFilteredReduced<int>(begin: linkedList.begin(), |
1023 | end: linkedList.end(), |
1024 | keep: keepEvenIntegers, |
1025 | reduce: intSumReduce); |
1026 | QCOMPARE(sum2, 6); |
1027 | } |
1028 | { |
1029 | int sum = QtConcurrent::blockingFilteredReduced<int>(begin: linkedList.constBegin(), |
1030 | end: linkedList.constEnd(), |
1031 | keep: KeepEvenIntegers(), |
1032 | reduce: IntSumReduce()); |
1033 | QCOMPARE(sum, 6); |
1034 | |
1035 | int sum2 = QtConcurrent::blockingFilteredReduced<int>(begin: linkedList.constBegin(), |
1036 | end: linkedList.constEnd(), |
1037 | keep: keepEvenIntegers, |
1038 | reduce: intSumReduce); |
1039 | QCOMPARE(sum2, 6); |
1040 | } |
1041 | |
1042 | // function-functor |
1043 | { |
1044 | int sum = QtConcurrent::filteredReduced<int>(sequence: linkedList, keep: keepEvenIntegers, reduce: IntSumReduce()); |
1045 | QCOMPARE(sum, 6); |
1046 | } |
1047 | { |
1048 | int sum = QtConcurrent::filteredReduced<int>(begin: linkedList.begin(), |
1049 | end: linkedList.end(), |
1050 | keep: keepEvenIntegers, |
1051 | reduce: IntSumReduce()); |
1052 | QCOMPARE(sum, 6); |
1053 | } |
1054 | { |
1055 | int sum = QtConcurrent::filteredReduced<int>(begin: linkedList.constBegin(), |
1056 | end: linkedList.constEnd(), |
1057 | keep: keepEvenIntegers, |
1058 | reduce: IntSumReduce()); |
1059 | QCOMPARE(sum, 6); |
1060 | } |
1061 | { |
1062 | int sum = QtConcurrent::blockingFilteredReduced<int>(sequence: linkedList, keep: keepEvenIntegers, reduce: IntSumReduce()); |
1063 | QCOMPARE(sum, 6); |
1064 | } |
1065 | { |
1066 | int sum = QtConcurrent::blockingFilteredReduced<int>(begin: linkedList.begin(), |
1067 | end: linkedList.end(), |
1068 | keep: keepEvenIntegers, |
1069 | reduce: IntSumReduce()); |
1070 | QCOMPARE(sum, 6); |
1071 | } |
1072 | { |
1073 | int sum = QtConcurrent::blockingFilteredReduced<int>(begin: linkedList.constBegin(), |
1074 | end: linkedList.constEnd(), |
1075 | keep: keepEvenIntegers, |
1076 | reduce: IntSumReduce()); |
1077 | QCOMPARE(sum, 6); |
1078 | } |
1079 | |
1080 | // functor-function |
1081 | { |
1082 | int sum = QtConcurrent::filteredReduced(sequence: linkedList, keep: KeepEvenIntegers(), reduce: intSumReduce); |
1083 | QCOMPARE(sum, 6); |
1084 | } |
1085 | { |
1086 | int sum = QtConcurrent::filteredReduced(begin: linkedList.begin(), |
1087 | end: linkedList.end(), |
1088 | keep: KeepEvenIntegers(), |
1089 | reduce: intSumReduce); |
1090 | QCOMPARE(sum, 6); |
1091 | } |
1092 | { |
1093 | int sum = QtConcurrent::filteredReduced(begin: linkedList.constBegin(), |
1094 | end: linkedList.constEnd(), |
1095 | keep: KeepEvenIntegers(), |
1096 | reduce: intSumReduce); |
1097 | QCOMPARE(sum, 6); |
1098 | } |
1099 | { |
1100 | int sum = QtConcurrent::blockingFilteredReduced(sequence: linkedList, keep: KeepEvenIntegers(), reduce: intSumReduce); |
1101 | QCOMPARE(sum, 6); |
1102 | } |
1103 | { |
1104 | int sum = QtConcurrent::blockingFilteredReduced(begin: linkedList.begin(), |
1105 | end: linkedList.end(), |
1106 | keep: KeepEvenIntegers(), |
1107 | reduce: intSumReduce); |
1108 | QCOMPARE(sum, 6); |
1109 | } |
1110 | { |
1111 | int sum = QtConcurrent::blockingFilteredReduced(begin: linkedList.constBegin(), |
1112 | end: linkedList.constEnd(), |
1113 | keep: KeepEvenIntegers(), |
1114 | reduce: intSumReduce); |
1115 | QCOMPARE(sum, 6); |
1116 | } |
1117 | |
1118 | // function-function |
1119 | { |
1120 | int sum = QtConcurrent::filteredReduced(sequence: linkedList, keep: keepEvenIntegers, reduce: intSumReduce); |
1121 | QCOMPARE(sum, 6); |
1122 | } |
1123 | { |
1124 | int sum = QtConcurrent::filteredReduced(begin: linkedList.begin(), |
1125 | end: linkedList.end(), |
1126 | keep: keepEvenIntegers, |
1127 | reduce: intSumReduce); |
1128 | QCOMPARE(sum, 6); |
1129 | } |
1130 | { |
1131 | int sum = QtConcurrent::filteredReduced(begin: linkedList.constBegin(), |
1132 | end: linkedList.constEnd(), |
1133 | keep: keepEvenIntegers, |
1134 | reduce: intSumReduce); |
1135 | QCOMPARE(sum, 6); |
1136 | } |
1137 | { |
1138 | int sum = QtConcurrent::blockingFilteredReduced(sequence: linkedList, keep: keepEvenIntegers, reduce: intSumReduce); |
1139 | QCOMPARE(sum, 6); |
1140 | } |
1141 | { |
1142 | int sum = QtConcurrent::blockingFilteredReduced(begin: linkedList.begin(), |
1143 | end: linkedList.end(), |
1144 | keep: keepEvenIntegers, |
1145 | reduce: intSumReduce); |
1146 | QCOMPARE(sum, 6); |
1147 | } |
1148 | { |
1149 | int sum = QtConcurrent::blockingFilteredReduced(begin: linkedList.constBegin(), |
1150 | end: linkedList.constEnd(), |
1151 | keep: keepEvenIntegers, |
1152 | reduce: intSumReduce); |
1153 | QCOMPARE(sum, 6); |
1154 | } |
1155 | |
1156 | // functor-member |
1157 | { |
1158 | QLinkedList<int> linkedList2 = QtConcurrent::filteredReduced(sequence: linkedList, keep: KeepEvenIntegers(), reduce: &QLinkedList<int>::append, options: QtConcurrent::OrderedReduce); |
1159 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
1160 | } |
1161 | { |
1162 | QLinkedList<int> linkedList2 = QtConcurrent::filteredReduced(begin: linkedList.begin(), |
1163 | end: linkedList.end(), |
1164 | keep: KeepEvenIntegers(), |
1165 | reduce: &QLinkedList<int>::append, |
1166 | options: QtConcurrent::OrderedReduce); |
1167 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
1168 | } |
1169 | { |
1170 | QLinkedList<int> linkedList2 = QtConcurrent::filteredReduced(begin: linkedList.constBegin(), |
1171 | end: linkedList.constEnd(), |
1172 | keep: KeepEvenIntegers(), |
1173 | reduce: &QLinkedList<int>::append, |
1174 | options: QtConcurrent::OrderedReduce); |
1175 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
1176 | } |
1177 | { |
1178 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFilteredReduced(sequence: linkedList, keep: KeepEvenIntegers(), reduce: &QLinkedList<int>::append, options: QtConcurrent::OrderedReduce); |
1179 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
1180 | } |
1181 | { |
1182 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFilteredReduced(begin: linkedList.begin(), |
1183 | end: linkedList.end(), |
1184 | keep: KeepEvenIntegers(), |
1185 | reduce: &QLinkedList<int>::append, |
1186 | options: QtConcurrent::OrderedReduce); |
1187 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
1188 | } |
1189 | { |
1190 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFilteredReduced(begin: linkedList.constBegin(), |
1191 | end: linkedList.constEnd(), |
1192 | keep: KeepEvenIntegers(), |
1193 | reduce: &QLinkedList<int>::append, |
1194 | options: QtConcurrent::OrderedReduce); |
1195 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
1196 | } |
1197 | |
1198 | // member-functor |
1199 | { |
1200 | int sum = QtConcurrent::filteredReduced<int>(sequence: numberLinkedList, keep: &Number::isEven, reduce: NumberSumReduce()); |
1201 | QCOMPARE(sum, 6); |
1202 | |
1203 | int sum2 = QtConcurrent::filteredReduced<int>(sequence: QLinkedList<Number>(numberLinkedList), |
1204 | keep: &Number::isEven, |
1205 | reduce: NumberSumReduce()); |
1206 | QCOMPARE(sum2, 6); |
1207 | } |
1208 | { |
1209 | int sum = QtConcurrent::filteredReduced<int>(begin: numberLinkedList.begin(), |
1210 | end: numberLinkedList.end(), |
1211 | keep: &Number::isEven, |
1212 | reduce: NumberSumReduce()); |
1213 | QCOMPARE(sum, 6); |
1214 | } |
1215 | { |
1216 | int sum = QtConcurrent::filteredReduced<int>(begin: numberLinkedList.constBegin(), |
1217 | end: numberLinkedList.constEnd(), |
1218 | keep: &Number::isEven, |
1219 | reduce: NumberSumReduce()); |
1220 | QCOMPARE(sum, 6); |
1221 | } |
1222 | { |
1223 | int sum = QtConcurrent::blockingFilteredReduced<int>(sequence: numberLinkedList, keep: &Number::isEven, reduce: NumberSumReduce()); |
1224 | QCOMPARE(sum, 6); |
1225 | |
1226 | int sum2 = QtConcurrent::blockingFilteredReduced<int>(sequence: QLinkedList<Number>(numberLinkedList), |
1227 | keep: &Number::isEven, |
1228 | reduce: NumberSumReduce()); |
1229 | QCOMPARE(sum2, 6); |
1230 | } |
1231 | { |
1232 | int sum = QtConcurrent::blockingFilteredReduced<int>(begin: numberLinkedList.begin(), |
1233 | end: numberLinkedList.end(), |
1234 | keep: &Number::isEven, |
1235 | reduce: NumberSumReduce()); |
1236 | QCOMPARE(sum, 6); |
1237 | } |
1238 | { |
1239 | int sum = QtConcurrent::blockingFilteredReduced<int>(begin: numberLinkedList.constBegin(), |
1240 | end: numberLinkedList.constEnd(), |
1241 | keep: &Number::isEven, |
1242 | reduce: NumberSumReduce()); |
1243 | QCOMPARE(sum, 6); |
1244 | } |
1245 | |
1246 | // member-member |
1247 | { |
1248 | QLinkedList<Number> numbers; |
1249 | numbers << 1 << 2 << 3 << 4; |
1250 | QLinkedList<Number> linkedList2 = QtConcurrent::filteredReduced(sequence: numbers, |
1251 | keep: &Number::isEven, |
1252 | reduce: &QLinkedList<Number>::append, options: QtConcurrent::OrderedReduce); |
1253 | QCOMPARE(linkedList2, QLinkedList<Number>() << 2 << 4); |
1254 | } |
1255 | { |
1256 | QLinkedList<Number> numbers; |
1257 | numbers << 1 << 2 << 3 << 4; |
1258 | QLinkedList<Number> linkedList2 = QtConcurrent::filteredReduced(begin: numbers.begin(), |
1259 | end: numbers.end(), |
1260 | keep: &Number::isEven, |
1261 | reduce: &QLinkedList<Number>::append, |
1262 | options: QtConcurrent::OrderedReduce); |
1263 | QCOMPARE(linkedList2, QLinkedList<Number>() << 2 << 4); |
1264 | } |
1265 | { |
1266 | QLinkedList<Number> numbers; |
1267 | numbers << 1 << 2 << 3 << 4; |
1268 | QLinkedList<Number> linkedList2 = QtConcurrent::filteredReduced(begin: numbers.constBegin(), |
1269 | end: numbers.constEnd(), |
1270 | keep: &Number::isEven, |
1271 | reduce: &QLinkedList<Number>::append, |
1272 | options: QtConcurrent::OrderedReduce); |
1273 | QCOMPARE(linkedList2, QLinkedList<Number>() << 2 << 4); |
1274 | } |
1275 | { |
1276 | QLinkedList<Number> numbers; |
1277 | numbers << 1 << 2 << 3 << 4; |
1278 | QLinkedList<Number> linkedList2 = QtConcurrent::blockingFilteredReduced(sequence: numbers, |
1279 | keep: &Number::isEven, |
1280 | reduce: &QLinkedList<Number>::append, options: QtConcurrent::OrderedReduce); |
1281 | QCOMPARE(linkedList2, QLinkedList<Number>() << 2 << 4); |
1282 | } |
1283 | { |
1284 | QLinkedList<Number> numbers; |
1285 | numbers << 1 << 2 << 3 << 4; |
1286 | QLinkedList<Number> linkedList2 = QtConcurrent::blockingFilteredReduced(begin: numbers.begin(), |
1287 | end: numbers.end(), |
1288 | keep: &Number::isEven, |
1289 | reduce: &QLinkedList<Number>::append, |
1290 | options: QtConcurrent::OrderedReduce); |
1291 | QCOMPARE(linkedList2, QLinkedList<Number>() << 2 << 4); |
1292 | } |
1293 | { |
1294 | QLinkedList<Number> numbers; |
1295 | numbers << 1 << 2 << 3 << 4; |
1296 | QLinkedList<Number> linkedList2 = QtConcurrent::blockingFilteredReduced(begin: numbers.constBegin(), |
1297 | end: numbers.constEnd(), |
1298 | keep: &Number::isEven, |
1299 | reduce: &QLinkedList<Number>::append, |
1300 | options: QtConcurrent::OrderedReduce); |
1301 | QCOMPARE(linkedList2, QLinkedList<Number>() << 2 << 4); |
1302 | } |
1303 | |
1304 | // function-member |
1305 | { |
1306 | QLinkedList<int> linkedList2 = QtConcurrent::filteredReduced(sequence: linkedList, keep: keepEvenIntegers, reduce: &QLinkedList<int>::append, options: QtConcurrent::OrderedReduce); |
1307 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
1308 | } |
1309 | { |
1310 | QLinkedList<int> linkedList2 = QtConcurrent::filteredReduced(begin: linkedList.begin(), |
1311 | end: linkedList.end(), |
1312 | keep: keepEvenIntegers, |
1313 | reduce: &QLinkedList<int>::append, |
1314 | options: QtConcurrent::OrderedReduce); |
1315 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
1316 | } |
1317 | { |
1318 | QLinkedList<int> linkedList2 = QtConcurrent::filteredReduced(begin: linkedList.constBegin(), |
1319 | end: linkedList.constEnd(), |
1320 | keep: keepEvenIntegers, |
1321 | reduce: &QLinkedList<int>::append, |
1322 | options: QtConcurrent::OrderedReduce); |
1323 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
1324 | } |
1325 | { |
1326 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFilteredReduced(sequence: linkedList, keep: keepEvenIntegers, reduce: &QLinkedList<int>::append, options: QtConcurrent::OrderedReduce); |
1327 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
1328 | } |
1329 | { |
1330 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFilteredReduced(begin: linkedList.begin(), |
1331 | end: linkedList.end(), |
1332 | keep: keepEvenIntegers, |
1333 | reduce: &QLinkedList<int>::append, |
1334 | options: QtConcurrent::OrderedReduce); |
1335 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
1336 | } |
1337 | { |
1338 | QLinkedList<int> linkedList2 = QtConcurrent::blockingFilteredReduced(begin: linkedList.constBegin(), |
1339 | end: linkedList.constEnd(), |
1340 | keep: keepEvenIntegers, |
1341 | reduce: &QLinkedList<int>::append, |
1342 | options: QtConcurrent::OrderedReduce); |
1343 | QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4); |
1344 | } |
1345 | |
1346 | // member-function |
1347 | { |
1348 | int sum = QtConcurrent::filteredReduced(sequence: numberLinkedList, keep: &Number::isEven, reduce: numberSumReduce); |
1349 | QCOMPARE(sum, 6); |
1350 | |
1351 | int sum2 = QtConcurrent::filteredReduced(sequence: QLinkedList<Number>(numberLinkedList), |
1352 | keep: &Number::isEven, |
1353 | reduce: numberSumReduce); |
1354 | QCOMPARE(sum2, 6); |
1355 | } |
1356 | { |
1357 | int sum = QtConcurrent::filteredReduced(begin: numberLinkedList.begin(), |
1358 | end: numberLinkedList.end(), |
1359 | keep: &Number::isEven, |
1360 | reduce: numberSumReduce); |
1361 | QCOMPARE(sum, 6); |
1362 | } |
1363 | { |
1364 | int sum = QtConcurrent::filteredReduced(begin: numberLinkedList.constBegin(), |
1365 | end: numberLinkedList.constEnd(), |
1366 | keep: &Number::isEven, |
1367 | reduce: numberSumReduce); |
1368 | QCOMPARE(sum, 6); |
1369 | } |
1370 | { |
1371 | int sum = QtConcurrent::blockingFilteredReduced(sequence: numberLinkedList, keep: &Number::isEven, reduce: numberSumReduce); |
1372 | QCOMPARE(sum, 6); |
1373 | |
1374 | int sum2 = QtConcurrent::blockingFilteredReduced(sequence: QLinkedList<Number>(numberLinkedList), |
1375 | keep: &Number::isEven, |
1376 | reduce: numberSumReduce); |
1377 | QCOMPARE(sum2, 6); |
1378 | } |
1379 | { |
1380 | int sum = QtConcurrent::blockingFilteredReduced(begin: numberLinkedList.begin(), |
1381 | end: numberLinkedList.end(), |
1382 | keep: &Number::isEven, |
1383 | reduce: numberSumReduce); |
1384 | QCOMPARE(sum, 6); |
1385 | } |
1386 | { |
1387 | int sum = QtConcurrent::blockingFilteredReduced(begin: numberLinkedList.constBegin(), |
1388 | end: numberLinkedList.constEnd(), |
1389 | keep: &Number::isEven, |
1390 | reduce: numberSumReduce); |
1391 | QCOMPARE(sum, 6); |
1392 | } |
1393 | |
1394 | QT_WARNING_POP |
1395 | #endif |
1396 | |
1397 | // ### the same as above, with an initial result value |
1398 | } |
1399 | |
1400 | bool filterfn(int i) |
1401 | { |
1402 | return (i % 2); |
1403 | } |
1404 | |
1405 | void tst_QtConcurrentFilter::resultAt() |
1406 | { |
1407 | |
1408 | QList<int> ints; |
1409 | for (int i=0; i < 1000; ++i) |
1410 | ints << i; |
1411 | |
1412 | QFuture<int> future = QtConcurrent::filtered(sequence: ints, keep: filterfn); |
1413 | future.waitForFinished(); |
1414 | |
1415 | for (int i = 0; i < future.resultCount(); ++i) { |
1416 | QCOMPARE(future.resultAt(i), ints.at(i * 2 + 1)); |
1417 | } |
1418 | |
1419 | } |
1420 | |
1421 | bool waitFilterfn(const int &i) |
1422 | { |
1423 | QTest::qWait(ms: 1); |
1424 | return (i % 2); |
1425 | } |
1426 | |
1427 | void tst_QtConcurrentFilter::incrementalResults() |
1428 | { |
1429 | const int count = 200; |
1430 | QList<int> ints; |
1431 | for (int i=0; i < count; ++i) |
1432 | ints << i; |
1433 | |
1434 | QFuture<int> future = QtConcurrent::filtered(sequence: ints, keep: waitFilterfn); |
1435 | |
1436 | QList<int> results; |
1437 | |
1438 | while (future.isFinished() == false) { |
1439 | for (int i = 0; i < future.resultCount(); ++i) { |
1440 | results += future.resultAt(index: i); |
1441 | } |
1442 | QTest::qWait(ms: 1); |
1443 | } |
1444 | |
1445 | QCOMPARE(future.isFinished(), true); |
1446 | QCOMPARE(future.resultCount(), count / 2); |
1447 | QCOMPARE(future.results().count(), count / 2); |
1448 | } |
1449 | |
1450 | void tst_QtConcurrentFilter::noDetach() |
1451 | { |
1452 | { |
1453 | QList<int> l = QList<int>() << 1; |
1454 | QVERIFY(l.isDetached()); |
1455 | |
1456 | QList<int> ll = l; |
1457 | QVERIFY(!l.isDetached()); |
1458 | |
1459 | QtConcurrent::filtered(sequence: l, keep: waitFilterfn).waitForFinished(); |
1460 | |
1461 | QVERIFY(!l.isDetached()); |
1462 | QVERIFY(!ll.isDetached()); |
1463 | |
1464 | QtConcurrent::blockingFiltered(sequence: l, keep: waitFilterfn); |
1465 | |
1466 | QVERIFY(!l.isDetached()); |
1467 | QVERIFY(!ll.isDetached()); |
1468 | |
1469 | QtConcurrent::filteredReduced(sequence: l, keep: waitFilterfn, reduce: intSumReduce).waitForFinished(); |
1470 | |
1471 | QVERIFY(!l.isDetached()); |
1472 | QVERIFY(!ll.isDetached()); |
1473 | |
1474 | QtConcurrent::filter(sequence&: l, keep: waitFilterfn).waitForFinished(); |
1475 | if (!l.isDetached()) |
1476 | QEXPECT_FAIL("" , "QTBUG-20688: Known unstable failure" , Abort); |
1477 | QVERIFY(l.isDetached()); |
1478 | QVERIFY(ll.isDetached()); |
1479 | } |
1480 | { |
1481 | const QList<int> l = QList<int>() << 1; |
1482 | QVERIFY(l.isDetached()); |
1483 | |
1484 | const QList<int> ll = l; |
1485 | QVERIFY(!l.isDetached()); |
1486 | |
1487 | QtConcurrent::filtered(sequence: l, keep: waitFilterfn).waitForFinished(); |
1488 | |
1489 | QVERIFY(!l.isDetached()); |
1490 | QVERIFY(!ll.isDetached()); |
1491 | |
1492 | QtConcurrent::filteredReduced(sequence: l, keep: waitFilterfn, reduce: intSumReduce).waitForFinished(); |
1493 | |
1494 | QVERIFY(!l.isDetached()); |
1495 | QVERIFY(!ll.isDetached()); |
1496 | } |
1497 | } |
1498 | |
1499 | void tst_QtConcurrentFilter::stlContainers() |
1500 | { |
1501 | std::vector<int> vector; |
1502 | vector.push_back(x: 1); |
1503 | vector.push_back(x: 2); |
1504 | |
1505 | std::vector<int> vector2 = QtConcurrent::blockingFiltered(sequence: vector, keep: waitFilterfn); |
1506 | QCOMPARE(vector2.size(), (std::vector<int>::size_type)(1)); |
1507 | QCOMPARE(vector2[0], 1); |
1508 | |
1509 | std::list<int> list; |
1510 | list.push_back(x: 1); |
1511 | list.push_back(x: 2); |
1512 | |
1513 | std::list<int> list2 = QtConcurrent::blockingFiltered(sequence: list, keep: waitFilterfn); |
1514 | QCOMPARE(list2.size(), (std::list<int>::size_type)(1)); |
1515 | QCOMPARE(*list2.begin(), 1); |
1516 | |
1517 | QtConcurrent::filtered(sequence: list, keep: waitFilterfn).waitForFinished(); |
1518 | QtConcurrent::filtered(sequence: vector, keep: waitFilterfn).waitForFinished(); |
1519 | QtConcurrent::filtered(begin: vector.begin(), end: vector.end(), keep: waitFilterfn).waitForFinished(); |
1520 | |
1521 | QtConcurrent::blockingFilter(sequence&: list, keep: waitFilterfn); |
1522 | QCOMPARE(list2.size(), (std::list<int>::size_type)(1)); |
1523 | QCOMPARE(*list2.begin(), 1); |
1524 | } |
1525 | |
1526 | QTEST_MAIN(tst_QtConcurrentFilter) |
1527 | #include "tst_qtconcurrentfilter.moc" |
1528 | |