/tests/auto/qtconcurrentmap/tst_qtconcurrentmap.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 2445 lines · 1935 code · 370 blank · 140 comment · 29 complexity · e9a655d6e5d260378eae82bdec3fabe1 MD5 · raw file

Large files are truncated click here to view the full file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the test suite of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include <qtconcurrentmap.h>
  42. #include <qtconcurrentexception.h>
  43. #include <qdebug.h>
  44. #include <QThread>
  45. #include <QtTest/QtTest>
  46. #include "functions.h"
  47. #include "../qfuture/versioncheck.h"
  48. Q_DECLARE_METATYPE(QVector<int>);
  49. Q_DECLARE_METATYPE(QVector<double>);
  50. Q_DECLARE_METATYPE(QVector<QString>);
  51. Q_DECLARE_METATYPE(QList<int>);
  52. Q_DECLARE_METATYPE(QList<double>);
  53. Q_DECLARE_METATYPE(QList<QString>);
  54. class tst_QtConcurrentMap: public QObject
  55. {
  56. Q_OBJECT
  57. private slots:
  58. void map();
  59. void blocking_map();
  60. void mapped();
  61. void blocking_mapped();
  62. void mappedReduced();
  63. void blocking_mappedReduced();
  64. void assignResult();
  65. void functionOverloads();
  66. #ifndef QT_NO_EXCEPTIONS
  67. void exceptions();
  68. #endif
  69. void incrementalResults();
  70. void noDetatch();
  71. void stlContainers();
  72. void qFutureAssignmentLeak();
  73. void stressTest();
  74. public slots:
  75. void throttling();
  76. };
  77. #if !defined (QT_NO_CONCURRENT_TEST) && !defined(QT_NO_CONCURRENT_MAP)
  78. using namespace QtConcurrent;
  79. void multiplyBy2Immutable(int x)
  80. {
  81. x *= 2;
  82. }
  83. class MultiplyBy2Immutable
  84. {
  85. public:
  86. void operator()(int x)
  87. {
  88. x *= 2;
  89. }
  90. };
  91. void multiplyBy2InPlace(int &x)
  92. {
  93. x *= 2;
  94. }
  95. class MultiplyBy2InPlace
  96. {
  97. public:
  98. void operator()(int &x)
  99. {
  100. x *= 2;
  101. }
  102. };
  103. Q_DECLARE_METATYPE(QList<Number>);
  104. void tst_QtConcurrentMap::map()
  105. {
  106. // functors take arguments by reference, modifying the sequence in place
  107. {
  108. QList<int> list;
  109. list << 1 << 2 << 3;
  110. // functor
  111. QtConcurrent::map(list, MultiplyBy2InPlace()).waitForFinished();
  112. QCOMPARE(list, QList<int>() << 2 << 4 << 6);
  113. QtConcurrent::map(list.begin(), list.end(), MultiplyBy2InPlace()).waitForFinished();
  114. QCOMPARE(list, QList<int>() << 4 << 8 << 12);
  115. // function
  116. QtConcurrent::map(list, multiplyBy2InPlace).waitForFinished();
  117. QCOMPARE(list, QList<int>() << 8 << 16 << 24);
  118. QtConcurrent::map(list.begin(), list.end(), multiplyBy2InPlace).waitForFinished();
  119. QCOMPARE(list, QList<int>() << 16 << 32 << 48);
  120. // bound function
  121. QtConcurrent::map(list, multiplyBy2InPlace).waitForFinished();
  122. QCOMPARE(list, QList<int>() << 32 << 64 << 96);
  123. QtConcurrent::map(list.begin(), list.end(), multiplyBy2InPlace).waitForFinished();
  124. QCOMPARE(list, QList<int>() << 64 << 128 << 192);
  125. // member function
  126. QList<Number> numberList;
  127. numberList << 1 << 2 << 3;
  128. QtConcurrent::map(numberList, &Number::multiplyBy2).waitForFinished();
  129. QCOMPARE(numberList, QList<Number>() << 2 << 4 << 6);
  130. QtConcurrent::map(numberList.begin(), numberList.end(), &Number::multiplyBy2).waitForFinished();
  131. QCOMPARE(numberList, QList<Number>() << 4 << 8 << 12);
  132. #ifdef Q_COMPILER_LAMBDA
  133. // lambda
  134. QtConcurrent::map(list, [](int &x){x *= 2;}).waitForFinished();
  135. QCOMPARE(list, QList<int>() << 128 << 256 << 384);
  136. QtConcurrent::map(list.begin(), list.end(), [](int &x){x *= 2;}).waitForFinished();
  137. QCOMPARE(list, QList<int>() << 256 << 512 << 768);
  138. #endif
  139. }
  140. // functors don't take arguments by reference, making these no-ops
  141. {
  142. QList<int> list;
  143. list << 1 << 2 << 3;
  144. // functor
  145. QtConcurrent::map(list, MultiplyBy2Immutable()).waitForFinished();
  146. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  147. QtConcurrent::map(list.begin(), list.end(), MultiplyBy2Immutable()).waitForFinished();
  148. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  149. // function
  150. QtConcurrent::map(list, multiplyBy2Immutable).waitForFinished();
  151. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  152. QtConcurrent::map(list.begin(), list.end(), multiplyBy2Immutable).waitForFinished();
  153. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  154. // bound function
  155. QtConcurrent::map(list, multiplyBy2Immutable).waitForFinished();
  156. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  157. QtConcurrent::map(list.begin(), list.end(), multiplyBy2Immutable).waitForFinished();
  158. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  159. #ifdef Q_COMPILER_LAMBDA
  160. // lambda
  161. QtConcurrent::map(list, [](int x){x *= 2;}).waitForFinished();
  162. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  163. QtConcurrent::map(list.begin(), list.end(), [](int x){x *= 2;}).waitForFinished();
  164. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  165. #endif
  166. }
  167. // Linked lists and forward iterators
  168. {
  169. QLinkedList<int> list;
  170. list << 1 << 2 << 3;
  171. // functor
  172. QtConcurrent::map(list, MultiplyBy2InPlace()).waitForFinished();
  173. QCOMPARE(list, QLinkedList<int>() << 2 << 4 << 6);
  174. QtConcurrent::map(list.begin(), list.end(), MultiplyBy2InPlace()).waitForFinished();
  175. QCOMPARE(list, QLinkedList<int>() << 4 << 8 << 12);
  176. // function
  177. QtConcurrent::map(list, multiplyBy2InPlace).waitForFinished();
  178. QCOMPARE(list, QLinkedList<int>() << 8 << 16 << 24);
  179. QtConcurrent::map(list.begin(), list.end(), multiplyBy2InPlace).waitForFinished();
  180. QCOMPARE(list, QLinkedList<int>() << 16 << 32 << 48);
  181. // bound function
  182. QtConcurrent::map(list, multiplyBy2InPlace).waitForFinished();
  183. QCOMPARE(list, QLinkedList<int>() << 32 << 64 << 96);
  184. QtConcurrent::map(list.begin(), list.end(), multiplyBy2InPlace).waitForFinished();
  185. QCOMPARE(list, QLinkedList<int>() << 64 << 128 << 192);
  186. // member function
  187. QLinkedList<Number> numberList;
  188. numberList << 1 << 2 << 3;
  189. QtConcurrent::map(numberList, &Number::multiplyBy2).waitForFinished();
  190. QCOMPARE(numberList, QLinkedList<Number>() << 2 << 4 << 6);
  191. QtConcurrent::map(numberList.begin(), numberList.end(), &Number::multiplyBy2).waitForFinished();
  192. QCOMPARE(numberList, QLinkedList<Number>() << 4 << 8 << 12);
  193. }
  194. #if 0
  195. // not allowed: map() with immutable sequences makes no sense
  196. {
  197. const QList<int> list = QList<int>() << 1 << 2 << 3;
  198. QtConcurrent::map(list, MultiplyBy2Immutable());
  199. QtConcurrent::map(list, multiplyBy2Immutable);
  200. QtConcurrent::map(list, multiplyBy2Immutable);
  201. }
  202. #endif
  203. #if 0
  204. // not allowed: in place modification of a temp copy (since temp copy goes out of scope)
  205. {
  206. QList<int> list;
  207. list << 1 << 2 << 3;
  208. QtConcurrent::map(QList<int>(list), MultiplyBy2InPlace());
  209. QtConcurrent::map(QList<int>(list), multiplyBy2);
  210. QtConcurrent::map(QList<int>(list), multiplyBy2InPlace);
  211. QList<Number> numberList;
  212. numberList << 1 << 2 << 3;
  213. QtConcurrent::map(QList<Number>(numberList), &Number::multiplyBy2);
  214. }
  215. #endif
  216. #if 0
  217. // not allowed: map() on a const list, where functors try to modify the items in the list
  218. {
  219. const QList<int> list = QList<int>() << 1 << 2 << 3;;
  220. QtConcurrent::map(list, MultiplyBy2InPlace());
  221. QtConcurrent::map(list, multiplyBy2InPlace);
  222. QtConcurrent::map(list, multiplyBy2InPlace);
  223. const QList<Number> numberList = QList<Number>() << 1 << 2 << 3;
  224. QtConcurrent::map(numberList, &Number::multiplyBy2);
  225. }
  226. #endif
  227. }
  228. void tst_QtConcurrentMap::blocking_map()
  229. {
  230. // functors take arguments by reference, modifying the sequence in place
  231. {
  232. QList<int> list;
  233. list << 1 << 2 << 3;
  234. // functor
  235. QtConcurrent::blockingMap(list, MultiplyBy2InPlace());
  236. QCOMPARE(list, QList<int>() << 2 << 4 << 6);
  237. QtConcurrent::blockingMap(list.begin(), list.end(), MultiplyBy2InPlace());
  238. QCOMPARE(list, QList<int>() << 4 << 8 << 12);
  239. // function
  240. QtConcurrent::blockingMap(list, multiplyBy2InPlace);
  241. QCOMPARE(list, QList<int>() << 8 << 16 << 24);
  242. QtConcurrent::blockingMap(list.begin(), list.end(), multiplyBy2InPlace);
  243. QCOMPARE(list, QList<int>() << 16 << 32 << 48);
  244. // bound function
  245. QtConcurrent::blockingMap(list, multiplyBy2InPlace);
  246. QCOMPARE(list, QList<int>() << 32 << 64 << 96);
  247. QtConcurrent::blockingMap(list.begin(), list.end(), multiplyBy2InPlace);
  248. QCOMPARE(list, QList<int>() << 64 << 128 << 192);
  249. // member function
  250. QList<Number> numberList;
  251. numberList << 1 << 2 << 3;
  252. QtConcurrent::blockingMap(numberList, &Number::multiplyBy2);
  253. QCOMPARE(numberList, QList<Number>() << 2 << 4 << 6);
  254. QtConcurrent::blockingMap(numberList.begin(), numberList.end(), &Number::multiplyBy2);
  255. QCOMPARE(numberList, QList<Number>() << 4 << 8 << 12);
  256. }
  257. // functors don't take arguments by reference, making these no-ops
  258. {
  259. QList<int> list;
  260. list << 1 << 2 << 3;
  261. // functor
  262. QtConcurrent::blockingMap(list, MultiplyBy2Immutable());
  263. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  264. QtConcurrent::blockingMap(list.begin(), list.end(), MultiplyBy2Immutable());
  265. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  266. // function
  267. QtConcurrent::blockingMap(list, multiplyBy2Immutable);
  268. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  269. QtConcurrent::blockingMap(list.begin(), list.end(), multiplyBy2Immutable);
  270. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  271. // bound function
  272. QtConcurrent::blockingMap(list, multiplyBy2Immutable);
  273. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  274. QtConcurrent::blockingMap(list.begin(), list.end(), multiplyBy2Immutable);
  275. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  276. }
  277. // Linked lists and forward iterators
  278. {
  279. QLinkedList<int> list;
  280. list << 1 << 2 << 3;
  281. // functor
  282. QtConcurrent::blockingMap(list, MultiplyBy2InPlace());
  283. QCOMPARE(list, QLinkedList<int>() << 2 << 4 << 6);
  284. QtConcurrent::blockingMap(list.begin(), list.end(), MultiplyBy2InPlace());
  285. QCOMPARE(list, QLinkedList<int>() << 4 << 8 << 12);
  286. // function
  287. QtConcurrent::blockingMap(list, multiplyBy2InPlace);
  288. QCOMPARE(list, QLinkedList<int>() << 8 << 16 << 24);
  289. QtConcurrent::blockingMap(list.begin(), list.end(), multiplyBy2InPlace);
  290. QCOMPARE(list, QLinkedList<int>() << 16 << 32 << 48);
  291. // bound function
  292. QtConcurrent::blockingMap(list, multiplyBy2InPlace);
  293. QCOMPARE(list, QLinkedList<int>() << 32 << 64 << 96);
  294. QtConcurrent::blockingMap(list.begin(), list.end(), multiplyBy2InPlace);
  295. QCOMPARE(list, QLinkedList<int>() << 64 << 128 << 192);
  296. // member function
  297. QLinkedList<Number> numberList;
  298. numberList << 1 << 2 << 3;
  299. QtConcurrent::blockingMap(numberList, &Number::multiplyBy2);
  300. QCOMPARE(numberList, QLinkedList<Number>() << 2 << 4 << 6);
  301. QtConcurrent::blockingMap(numberList.begin(), numberList.end(), &Number::multiplyBy2);
  302. QCOMPARE(numberList, QLinkedList<Number>() << 4 << 8 << 12);
  303. }
  304. #if 0
  305. // not allowed: map() with immutable sequences makes no sense
  306. {
  307. const QList<int> list = QList<int>() << 1 << 2 << 3;
  308. QtConcurrent::blockingMap(list, MultiplyBy2Immutable());
  309. QtConcurrent::blockkng::map(list, multiplyBy2Immutable);
  310. QtConcurrent::blockingMap(list, multiplyBy2Immutable);
  311. }
  312. #endif
  313. #if 0
  314. // not allowed: in place modification of a temp copy (since temp copy goes out of scope)
  315. {
  316. QList<int> list;
  317. list << 1 << 2 << 3;
  318. QtConcurrent::blockingMap(QList<int>(list), MultiplyBy2InPlace());
  319. QtConcurrent::blockingMap(QList<int>(list), multiplyBy2);
  320. QtConcurrent::blockingMap(QList<int>(list), multiplyBy2InPlace);
  321. QList<Number> numberList;
  322. numberList << 1 << 2 << 3;
  323. QtConcurrent::blockingMap(QList<Number>(numberList), &Number::multiplyBy2);
  324. }
  325. #endif
  326. #if 0
  327. // not allowed: map() on a const list, where functors try to modify the items in the list
  328. {
  329. const QList<int> list = QList<int>() << 1 << 2 << 3;;
  330. QtConcurrent::blockingMap(list, MultiplyBy2InPlace());
  331. QtConcurrent::blockingMap(list, multiplyBy2InPlace);
  332. QtConcurrent::blockingMap(list, multiplyBy2InPlace);
  333. const QList<Number> numberList = QList<Number>() << 1 << 2 << 3;
  334. QtConcurrent::blockingMap(numberList, &Number::multiplyBy2);
  335. }
  336. #endif
  337. }
  338. int multiplyBy2(int x)
  339. {
  340. int y = x * 2;
  341. return y;
  342. }
  343. class MultiplyBy2
  344. {
  345. public:
  346. typedef int result_type;
  347. int operator()(int x) const
  348. {
  349. int y = x * 2;
  350. return y;
  351. }
  352. };
  353. double intToDouble(int x)
  354. {
  355. return double(x);
  356. }
  357. class IntToDouble
  358. {
  359. public:
  360. typedef double result_type;
  361. double operator()(int x) const
  362. {
  363. return double(x);
  364. }
  365. };
  366. int stringToInt(const QString &string)
  367. {
  368. return string.toInt();
  369. }
  370. class StringToInt
  371. {
  372. public:
  373. typedef int result_type;
  374. int operator()(const QString &string) const
  375. {
  376. return string.toInt();
  377. }
  378. };
  379. void tst_QtConcurrentMap::mapped()
  380. {
  381. QList<int> list;
  382. list << 1 << 2 << 3;
  383. QLinkedList<int> linkedList;
  384. linkedList << 1 << 2 << 3;
  385. QList<Number> numberList;
  386. numberList << 1 << 2 << 3;
  387. QLinkedList<Number> numberLinkedList;
  388. numberLinkedList << 1 << 2 << 3;
  389. // functor
  390. {
  391. QList<int> list2 = QtConcurrent::mapped(list, MultiplyBy2()).results();
  392. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  393. QCOMPARE(list2, QList<int>() << 2 << 4 << 6);
  394. QList<int> list3 = QtConcurrent::mapped(list.constBegin(),
  395. list.constEnd(),
  396. MultiplyBy2()).results();
  397. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  398. QCOMPARE(list3, QList<int>() << 2 << 4 << 6);
  399. QList<int> list4 = QtConcurrent::mapped(QList<int>(list), MultiplyBy2()).results();
  400. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  401. QCOMPARE(list4, QList<int>() << 2 << 4 << 6);
  402. }
  403. {
  404. QList<int> list2 = QtConcurrent::mapped(linkedList, MultiplyBy2()).results();
  405. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  406. QCOMPARE(list2, QList<int>() << 2 << 4 << 6);
  407. QList<int> list3 = QtConcurrent::mapped(linkedList.constBegin(),
  408. linkedList.constEnd(),
  409. MultiplyBy2()).results();
  410. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  411. QCOMPARE(list3, QList<int>() << 2 << 4 << 6);
  412. QList<int> list4 =
  413. QtConcurrent::mapped(QLinkedList<int>(linkedList), MultiplyBy2()).results();
  414. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  415. QCOMPARE(list4, QList<int>() << 2 << 4 << 6);
  416. }
  417. // function
  418. {
  419. QList<int> list2 = QtConcurrent::mapped(list, multiplyBy2).results();
  420. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  421. QCOMPARE(list2, QList<int>() << 2 << 4 << 6);
  422. QList<int> list3 = QtConcurrent::mapped(list.constBegin(),
  423. list.constEnd(),
  424. multiplyBy2).results();
  425. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  426. QCOMPARE(list3, QList<int>() << 2 << 4 << 6);
  427. QList<int> list4 = QtConcurrent::mapped(QList<int>(list), multiplyBy2).results();
  428. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  429. QCOMPARE(list4, QList<int>() << 2 << 4 << 6);
  430. }
  431. {
  432. QList<int> list2 = QtConcurrent::mapped(linkedList, multiplyBy2).results();
  433. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  434. QCOMPARE(list2, QList<int>() << 2 << 4 << 6);
  435. QList<int> list3 = QtConcurrent::mapped(linkedList.constBegin(),
  436. linkedList.constEnd(),
  437. multiplyBy2).results();
  438. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  439. QCOMPARE(list3, QList<int>() << 2 << 4 << 6);
  440. QList<int> list4 =
  441. QtConcurrent::mapped(QLinkedList<int>(linkedList), multiplyBy2).results();
  442. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  443. QCOMPARE(list4, QList<int>() << 2 << 4 << 6);
  444. }
  445. // bound function
  446. {
  447. QList<int> list2 = QtConcurrent::mapped(list, multiplyBy2).results();
  448. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  449. QCOMPARE(list2, QList<int>() << 2 << 4 << 6);
  450. QList<int> list3 = QtConcurrent::mapped(list.constBegin(),
  451. list.constEnd(),
  452. multiplyBy2).results();
  453. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  454. QCOMPARE(list3, QList<int>() << 2 << 4 << 6);
  455. QList<int> list4 = QtConcurrent::mapped(QList<int>(list), multiplyBy2).results();
  456. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  457. QCOMPARE(list4, QList<int>() << 2 << 4 << 6);
  458. }
  459. {
  460. QList<int> list2 = QtConcurrent::mapped(linkedList, multiplyBy2).results();
  461. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  462. QCOMPARE(list2, QList<int>() << 2 << 4 << 6);
  463. QList<int> list3 = QtConcurrent::mapped(linkedList.constBegin(),
  464. linkedList.constEnd(),
  465. multiplyBy2)
  466. .results();
  467. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  468. QCOMPARE(list3, QList<int>() << 2 << 4 << 6);
  469. QList<int> list4 = QtConcurrent::mapped(QLinkedList<int>(linkedList), multiplyBy2)
  470. .results();
  471. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  472. QCOMPARE(list4, QList<int>() << 2 << 4 << 6);
  473. }
  474. // const member function
  475. {
  476. QList<Number> numberList2 = QtConcurrent::mapped(numberList, &Number::multipliedBy2)
  477. .results();
  478. QCOMPARE(numberList, QList<Number>() << 1 << 2 << 3);
  479. QCOMPARE(numberList2, QList<Number>() << 2 << 4 << 6);
  480. QList<Number> numberList3 = QtConcurrent::mapped(numberList.constBegin(),
  481. numberList.constEnd(),
  482. &Number::multipliedBy2)
  483. .results();
  484. QCOMPARE(numberList, QList<Number>() << 1 << 2 << 3);
  485. QCOMPARE(numberList3, QList<Number>() << 2 << 4 << 6);
  486. QList<Number> numberList4 = QtConcurrent::mapped(QList<Number>(numberList),
  487. &Number::multipliedBy2)
  488. .results();
  489. QCOMPARE(numberList, QList<Number>() << 1 << 2 << 3);
  490. QCOMPARE(numberList4, QList<Number>() << 2 << 4 << 6);
  491. }
  492. {
  493. QList<Number> numberList2 = QtConcurrent::mapped(numberLinkedList, &Number::multipliedBy2)
  494. .results();
  495. QCOMPARE(numberLinkedList, QLinkedList<Number>() << 1 << 2 << 3);
  496. QCOMPARE(numberList2, QList<Number>() << 2 << 4 << 6);
  497. QList<Number> numberList3 = QtConcurrent::mapped(numberLinkedList.constBegin(),
  498. numberLinkedList.constEnd(),
  499. &Number::multipliedBy2)
  500. .results();
  501. QCOMPARE(numberLinkedList, QLinkedList<Number>() << 1 << 2 << 3);
  502. QCOMPARE(numberList3, QList<Number>() << 2 << 4 << 6);
  503. QList<Number> numberList4 = QtConcurrent::mapped(QLinkedList<Number>(numberLinkedList),
  504. &Number::multipliedBy2)
  505. .results();
  506. QCOMPARE(numberLinkedList, QLinkedList<Number>() << 1 << 2 << 3);
  507. QCOMPARE(numberList4, QList<Number>() << 2 << 4 << 6);
  508. }
  509. // change the value_type, same container
  510. // functor
  511. {
  512. QList<double> list2 = QtConcurrent::mapped(list, IntToDouble()).results();
  513. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  514. QCOMPARE(list2, QList<double>() << 1.0 << 2.0 << 3.0);
  515. QList<double> list3 = QtConcurrent::mapped(list.constBegin(),
  516. list.constEnd(),
  517. IntToDouble())
  518. .results();
  519. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  520. QCOMPARE(list3, QList<double>() << 1.0 << 2.0 << 3.0);
  521. QList<double> list4 = QtConcurrent::mapped(QList<int>(list),
  522. IntToDouble())
  523. .results();
  524. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  525. QCOMPARE(list4, QList<double>() << 1.0 << 2.0 << 3.0);
  526. }
  527. {
  528. QList<double> list2 = QtConcurrent::mapped(linkedList, IntToDouble()).results();
  529. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  530. QCOMPARE(list2, QList<double>() << 1.0 << 2.0 << 3.0);
  531. QList<double> list3 = QtConcurrent::mapped(linkedList.constBegin(),
  532. linkedList.constEnd(),
  533. IntToDouble())
  534. .results();
  535. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  536. QCOMPARE(list3, QList<double>() << 1.0 << 2.0 << 3.0);
  537. QList<double> list4 = QtConcurrent::mapped(QLinkedList<int>(linkedList),
  538. IntToDouble())
  539. .results();
  540. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  541. QCOMPARE(list4, QList<double>() << 1.0 << 2.0 << 3.0);
  542. }
  543. // function
  544. {
  545. QList<double> list2 = QtConcurrent::mapped(list, intToDouble).results();
  546. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  547. QCOMPARE(list2, QList<double>() << 1.0 << 2.0 << 3.0);
  548. QList<double> list3 = QtConcurrent::mapped(list.constBegin(),
  549. list.constEnd(),
  550. intToDouble)
  551. .results();
  552. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  553. QCOMPARE(list3, QList<double>() << 1.0 << 2.0 << 3.0);
  554. QList<double> list4 = QtConcurrent::mapped(QList<int>(list), intToDouble).results();
  555. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  556. QCOMPARE(list4, QList<double>() << 1.0 << 2.0 << 3.0);
  557. }
  558. {
  559. QList<double> list2 = QtConcurrent::mapped(linkedList, intToDouble).results();
  560. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  561. QCOMPARE(list2, QList<double>() << 1.0 << 2.0 << 3.0);
  562. QList<double> list3 = QtConcurrent::mapped(linkedList.constBegin(),
  563. linkedList.constEnd(),
  564. intToDouble)
  565. .results();
  566. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  567. QCOMPARE(list3, QList<double>() << 1.0 << 2.0 << 3.0);
  568. QList<double> list4 = QtConcurrent::mapped(QLinkedList<int>(linkedList), intToDouble)
  569. .results();
  570. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  571. QCOMPARE(list4, QList<double>() << 1.0 << 2.0 << 3.0);
  572. }
  573. // bound function
  574. {
  575. QList<double> list2 = QtConcurrent::mapped(list, intToDouble).results();
  576. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  577. QCOMPARE(list2, QList<double>() << 1.0 << 2.0 << 3.0);
  578. QList<double> list3 = QtConcurrent::mapped(list.constBegin(),
  579. list.constEnd(),
  580. intToDouble)
  581. .results();
  582. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  583. QCOMPARE(list3, QList<double>() << 1.0 << 2.0 << 3.0);
  584. QList<double> list4 = QtConcurrent::mapped(QList<int>(list),
  585. intToDouble)
  586. .results();
  587. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  588. QCOMPARE(list4, QList<double>() << 1.0 << 2.0 << 3.0);
  589. }
  590. {
  591. QList<double> list2 = QtConcurrent::mapped(linkedList, intToDouble).results();
  592. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  593. QCOMPARE(list2, QList<double>() << 1.0 << 2.0 << 3.0);
  594. QList<double> list3 = QtConcurrent::mapped(linkedList.constBegin(),
  595. linkedList.constEnd(),
  596. intToDouble)
  597. .results();
  598. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  599. QCOMPARE(list3, QList<double>() << 1.0 << 2.0 << 3.0);
  600. QList<double> list4 = QtConcurrent::mapped(QLinkedList<int>(linkedList),
  601. intToDouble)
  602. .results();
  603. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  604. QCOMPARE(list4, QList<double>() << 1.0 << 2.0 << 3.0);
  605. }
  606. // const member function
  607. {
  608. QList<QString> list2 = QtConcurrent::mapped(numberList, &Number::toString).results();
  609. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  610. QCOMPARE(list2, QList<QString>() << "1" << "2" << "3");
  611. QList<QString> list3 = QtConcurrent::mapped(numberList.constBegin(),
  612. numberList.constEnd(),
  613. &Number::toString)
  614. .results();
  615. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  616. QCOMPARE(list3, QList<QString>() << "1" << "2" << "3");
  617. QList<QString> list4 = QtConcurrent::mapped(QList<Number>(numberList), &Number::toString)
  618. .results();
  619. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  620. QCOMPARE(list4, QList<QString>() << "1" << "2" << "3");
  621. }
  622. {
  623. QList<QString> list2 = QtConcurrent::mapped(numberLinkedList, &Number::toString).results();
  624. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  625. QCOMPARE(list2, QList<QString>() << "1" << "2" << "3");
  626. QList<QString> list3 = QtConcurrent::mapped(numberLinkedList.constBegin(),
  627. numberLinkedList.constEnd(),
  628. &Number::toString)
  629. .results();
  630. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  631. QCOMPARE(list3, QList<QString>() << "1" << "2" << "3");
  632. QList<QString> list4 = QtConcurrent::mapped(QLinkedList<Number>(numberLinkedList),
  633. &Number::toString)
  634. .results();
  635. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  636. QCOMPARE(list4, QList<QString>() << "1" << "2" << "3");
  637. }
  638. // change the value_type
  639. {
  640. QList<QString> strings = QStringList() << "1" << "2" << "3";
  641. QList<int> list = QtConcurrent::mapped(strings, StringToInt()).results();
  642. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  643. QList<int> list2 = QtConcurrent::mapped(strings.constBegin(),
  644. strings.constEnd(),
  645. StringToInt())
  646. .results();
  647. QCOMPARE(list2, QList<int>() << 1 << 2 << 3);
  648. }
  649. {
  650. QList<QString> strings = QStringList() << "1" << "2" << "3";
  651. QList<int> list = QtConcurrent::mapped(strings, stringToInt).results();
  652. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  653. QList<int> list2 = QtConcurrent::mapped(strings.constBegin(),
  654. strings.constEnd(),
  655. stringToInt).results();
  656. QCOMPARE(list2, QList<int>() << 1 << 2 << 3);
  657. }
  658. {
  659. QList<int> numberList2 = QtConcurrent::mapped(numberList, &Number::toInt).results();
  660. QCOMPARE(numberList2, QList<int>() << 1 << 2 << 3);
  661. QList<int> numberList3 = QtConcurrent::mapped(numberList.constBegin(),
  662. numberList.constEnd(),
  663. &Number::toInt)
  664. .results();
  665. QCOMPARE(numberList3, QList<int>() << 1 << 2 << 3);
  666. }
  667. // change the value_type from QStringList
  668. {
  669. QStringList strings = QStringList() << "1" << "2" << "3";
  670. QList<int> list = QtConcurrent::mapped(strings, StringToInt()).results();
  671. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  672. QList<int> list2 = QtConcurrent::mapped(strings.constBegin(),
  673. strings.constEnd(),
  674. StringToInt())
  675. .results();
  676. QCOMPARE(list2, QList<int>() << 1 << 2 << 3);
  677. }
  678. {
  679. QStringList strings = QStringList() << "1" << "2" << "3";
  680. QList<int> list = QtConcurrent::mapped(strings, stringToInt).results();
  681. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  682. QList<int> list2 = QtConcurrent::mapped(strings.constBegin(),
  683. strings.constEnd(),
  684. stringToInt)
  685. .results();
  686. QCOMPARE(list2, QList<int>() << 1 << 2 << 3);
  687. }
  688. }
  689. void tst_QtConcurrentMap::blocking_mapped()
  690. {
  691. QList<int> list;
  692. list << 1 << 2 << 3;
  693. QLinkedList<int> linkedList;
  694. linkedList << 1 << 2 << 3;
  695. QList<Number> numberList;
  696. numberList << 1 << 2 << 3;
  697. QLinkedList<Number> numberLinkedList;
  698. numberLinkedList << 1 << 2 << 3;
  699. // functor
  700. {
  701. QList<int> list2 = QtConcurrent::blockingMapped(list, MultiplyBy2());
  702. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  703. QCOMPARE(list2, QList<int>() << 2 << 4 << 6);
  704. QList<int> list3 = QtConcurrent::blockingMapped<QList<int> >(list.constBegin(),
  705. list.constEnd(),
  706. MultiplyBy2());
  707. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  708. QCOMPARE(list3, QList<int>() << 2 << 4 << 6);
  709. QList<int> list4 = QtConcurrent::blockingMapped(QList<int>(list), MultiplyBy2());
  710. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  711. QCOMPARE(list4, QList<int>() << 2 << 4 << 6);
  712. }
  713. {
  714. QLinkedList<int> linkedList2 = QtConcurrent::blockingMapped(linkedList, MultiplyBy2());
  715. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  716. QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4 << 6);
  717. QLinkedList<int> linkedList3 = QtConcurrent::blockingMapped<QLinkedList<int> >(linkedList.constBegin(),
  718. linkedList.constEnd(),
  719. MultiplyBy2());
  720. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  721. QCOMPARE(linkedList3, QLinkedList<int>() << 2 << 4 << 6);
  722. QLinkedList<int> linkedList4 = QtConcurrent::blockingMapped(QLinkedList<int>(linkedList), MultiplyBy2());
  723. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  724. QCOMPARE(linkedList4, QLinkedList<int>() << 2 << 4 << 6);
  725. }
  726. // function
  727. {
  728. QList<int> list2 = QtConcurrent::blockingMapped(list, multiplyBy2);
  729. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  730. QCOMPARE(list2, QList<int>() << 2 << 4 << 6);
  731. QList<int> list3 = QtConcurrent::blockingMapped<QList<int> >(list.constBegin(),
  732. list.constEnd(),
  733. multiplyBy2);
  734. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  735. QCOMPARE(list3, QList<int>() << 2 << 4 << 6);
  736. QList<int> list4 = QtConcurrent::blockingMapped(QList<int>(list), multiplyBy2);
  737. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  738. QCOMPARE(list4, QList<int>() << 2 << 4 << 6);
  739. }
  740. {
  741. QLinkedList<int> linkedList2 = QtConcurrent::blockingMapped(linkedList, multiplyBy2);
  742. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  743. QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4 << 6);
  744. QLinkedList<int> linkedList3 = QtConcurrent::blockingMapped<QLinkedList<int> >(linkedList.constBegin(),
  745. linkedList.constEnd(),
  746. multiplyBy2);
  747. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  748. QCOMPARE(linkedList3, QLinkedList<int>() << 2 << 4 << 6);
  749. QLinkedList<int> linkedList4 = QtConcurrent::blockingMapped(QLinkedList<int>(linkedList), multiplyBy2);
  750. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  751. QCOMPARE(linkedList4, QLinkedList<int>() << 2 << 4 << 6);
  752. }
  753. // bound function
  754. {
  755. QList<int> list2 = QtConcurrent::blockingMapped(list, multiplyBy2);
  756. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  757. QCOMPARE(list2, QList<int>() << 2 << 4 << 6);
  758. QList<int> list3 = QtConcurrent::blockingMapped<QList<int> >(list.constBegin(),
  759. list.constEnd(),
  760. multiplyBy2);
  761. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  762. QCOMPARE(list3, QList<int>() << 2 << 4 << 6);
  763. QList<int> list4 = QtConcurrent::blockingMapped(QList<int>(list), multiplyBy2);
  764. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  765. QCOMPARE(list4, QList<int>() << 2 << 4 << 6);
  766. }
  767. {
  768. QLinkedList<int> linkedList2 = QtConcurrent::blockingMapped(linkedList, multiplyBy2);
  769. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  770. QCOMPARE(linkedList2, QLinkedList<int>() << 2 << 4 << 6);
  771. QLinkedList<int> linkedList3 = QtConcurrent::blockingMapped<QLinkedList<int> >(linkedList.constBegin(),
  772. linkedList.constEnd(),
  773. multiplyBy2);
  774. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  775. QCOMPARE(linkedList3, QLinkedList<int>() << 2 << 4 << 6);
  776. QLinkedList<int> linkedList4 = QtConcurrent::blockingMapped(QLinkedList<int>(linkedList), multiplyBy2);
  777. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  778. QCOMPARE(linkedList4, QLinkedList<int>() << 2 << 4 << 6);
  779. }
  780. // const member function
  781. {
  782. QList<Number> numberList2 = QtConcurrent::blockingMapped(numberList, &Number::multipliedBy2);
  783. QCOMPARE(numberList, QList<Number>() << 1 << 2 << 3);
  784. QCOMPARE(numberList2, QList<Number>() << 2 << 4 << 6);
  785. QList<Number> numberList3 = QtConcurrent::blockingMapped<QList<Number> >(numberList.constBegin(),
  786. numberList.constEnd(),
  787. &Number::multipliedBy2);
  788. QCOMPARE(numberList, QList<Number>() << 1 << 2 << 3);
  789. QCOMPARE(numberList3, QList<Number>() << 2 << 4 << 6);
  790. QList<Number> numberList4 = QtConcurrent::blockingMapped(QList<Number>(numberList),
  791. &Number::multipliedBy2);
  792. QCOMPARE(numberList, QList<Number>() << 1 << 2 << 3);
  793. QCOMPARE(numberList4, QList<Number>() << 2 << 4 << 6);
  794. }
  795. {
  796. QLinkedList<Number> numberLinkedList2 = QtConcurrent::blockingMapped(numberLinkedList, &Number::multipliedBy2);
  797. QCOMPARE(numberLinkedList, QLinkedList<Number>() << 1 << 2 << 3);
  798. QCOMPARE(numberLinkedList2, QLinkedList<Number>() << 2 << 4 << 6);
  799. QLinkedList<Number> numberLinkedList3 = QtConcurrent::blockingMapped<QLinkedList<Number> >(numberLinkedList.constBegin(),
  800. numberLinkedList.constEnd(),
  801. &Number::multipliedBy2);
  802. QCOMPARE(numberLinkedList, QLinkedList<Number>() << 1 << 2 << 3);
  803. QCOMPARE(numberLinkedList3, QLinkedList<Number>() << 2 << 4 << 6);
  804. QLinkedList<Number> numberLinkedList4 = QtConcurrent::blockingMapped(QLinkedList<Number>(numberLinkedList),
  805. &Number::multipliedBy2);
  806. QCOMPARE(numberLinkedList, QLinkedList<Number>() << 1 << 2 << 3);
  807. QCOMPARE(numberLinkedList4, QLinkedList<Number>() << 2 << 4 << 6);
  808. }
  809. // change the value_type, same container
  810. // functor
  811. {
  812. QList<double> list2 = QtConcurrent::blockingMapped<QList<double> >(list, IntToDouble());
  813. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  814. QCOMPARE(list2, QList<double>() << 1.0 << 2.0 << 3.0);
  815. QList<double> list3 = QtConcurrent::blockingMapped<QList<double> >(list.constBegin(),
  816. list.constEnd(),
  817. IntToDouble());
  818. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  819. QCOMPARE(list3, QList<double>() << 1.0 << 2.0 << 3.0);
  820. QList<double> list4 = QtConcurrent::blockingMapped<QList<double> >(QList<int>(list),
  821. IntToDouble());
  822. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  823. QCOMPARE(list4, QList<double>() << 1.0 << 2.0 << 3.0);
  824. }
  825. {
  826. QLinkedList<double> linkedList2 = QtConcurrent::blockingMapped<QLinkedList<double> >(linkedList, IntToDouble());
  827. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  828. QCOMPARE(linkedList2, QLinkedList<double>() << 1.0 << 2.0 << 3.0);
  829. QLinkedList<double> linkedList3 = QtConcurrent::blockingMapped<QLinkedList<double> >(linkedList.constBegin(),
  830. linkedList.constEnd(),
  831. IntToDouble());
  832. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  833. QCOMPARE(linkedList3, QLinkedList<double>() << 1.0 << 2.0 << 3.0);
  834. QLinkedList<double> linkedList4 = QtConcurrent::blockingMapped<QLinkedList<double> >(QLinkedList<int>(linkedList),
  835. IntToDouble());
  836. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  837. QCOMPARE(linkedList4, QLinkedList<double>() << 1.0 << 2.0 << 3.0);
  838. }
  839. // function
  840. {
  841. QList<double> list2 = QtConcurrent::blockingMapped<QList<double> >(list, intToDouble);
  842. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  843. QCOMPARE(list2, QList<double>() << 1.0 << 2.0 << 3.0);
  844. QList<double> list3 = QtConcurrent::blockingMapped<QList<double> >(list.constBegin(),
  845. list.constEnd(),
  846. intToDouble);
  847. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  848. QCOMPARE(list3, QList<double>() << 1.0 << 2.0 << 3.0);
  849. QList<double> list4 = QtConcurrent::blockingMapped<QList<double> >(QList<int>(list), intToDouble);
  850. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  851. QCOMPARE(list4, QList<double>() << 1.0 << 2.0 << 3.0);
  852. }
  853. {
  854. QLinkedList<double> linkedList2 = QtConcurrent::blockingMapped<QLinkedList<double> >(linkedList, intToDouble);
  855. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  856. QCOMPARE(linkedList2, QLinkedList<double>() << 1.0 << 2.0 << 3.0);
  857. QLinkedList<double> linkedList3 = QtConcurrent::blockingMapped<QLinkedList<double> >(linkedList.constBegin(),
  858. linkedList.constEnd(),
  859. intToDouble);
  860. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  861. QCOMPARE(linkedList3, QLinkedList<double>() << 1.0 << 2.0 << 3.0);
  862. QLinkedList<double> linkedList4 = QtConcurrent::blockingMapped<QLinkedList<double> >(QLinkedList<int>(linkedList), intToDouble);
  863. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  864. QCOMPARE(linkedList4, QLinkedList<double>() << 1.0 << 2.0 << 3.0);
  865. }
  866. // bound function
  867. {
  868. QList<double> list2 = QtConcurrent::blockingMapped<QList<double> >(list, intToDouble);
  869. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  870. QCOMPARE(list2, QList<double>() << 1.0 << 2.0 << 3.0);
  871. QList<double> list3 = QtConcurrent::blockingMapped<QList<double> >(list.constBegin(),
  872. list.constEnd(),
  873. intToDouble);
  874. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  875. QCOMPARE(list3, QList<double>() << 1.0 << 2.0 << 3.0);
  876. QList<double> list4 = QtConcurrent::blockingMapped<QList<double> >(QList<int>(list),
  877. intToDouble);
  878. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  879. QCOMPARE(list4, QList<double>() << 1.0 << 2.0 << 3.0);
  880. }
  881. {
  882. QLinkedList<double> linkedList2 = QtConcurrent::blockingMapped<QLinkedList<double> >(linkedList, intToDouble);
  883. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  884. QCOMPARE(linkedList2, QLinkedList<double>() << 1.0 << 2.0 << 3.0);
  885. QLinkedList<double> linkedList3 = QtConcurrent::blockingMapped<QLinkedList<double> >(linkedList.constBegin(),
  886. linkedList.constEnd(),
  887. intToDouble);
  888. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  889. QCOMPARE(linkedList3, QLinkedList<double>() << 1.0 << 2.0 << 3.0);
  890. QLinkedList<double> linkedList4 = QtConcurrent::blockingMapped<QLinkedList<double> >(QLinkedList<int>(linkedList),
  891. intToDouble);
  892. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  893. QCOMPARE(linkedList4, QLinkedList<double>() << 1.0 << 2.0 << 3.0);
  894. }
  895. // const member function
  896. {
  897. QList<QString> list2 =
  898. QtConcurrent::blockingMapped<QList<QString> >(numberList, &Number::toString);
  899. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  900. QCOMPARE(list2, QList<QString>() << "1" << "2" << "3");
  901. QList<QString> list3 = QtConcurrent::blockingMapped<QList<QString> >(numberList.constBegin(),
  902. numberList.constEnd()
  903. , &Number::toString);
  904. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  905. QCOMPARE(list3, QList<QString>() << "1" << "2" << "3");
  906. QList<QString> list4 =
  907. QtConcurrent::blockingMapped<QList<QString> >(QList<Number>(numberList), &Number::toString);
  908. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  909. QCOMPARE(list4, QList<QString>() << "1" << "2" << "3");
  910. }
  911. {
  912. QLinkedList<QString> linkedList2 =
  913. QtConcurrent::blockingMapped<QLinkedList<QString> >(numberLinkedList, &Number::toString);
  914. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  915. QCOMPARE(linkedList2, QLinkedList<QString>() << "1" << "2" << "3");
  916. QLinkedList<QString> linkedList3 = QtConcurrent::blockingMapped<QLinkedList<QString> >(numberLinkedList.constBegin(),
  917. numberLinkedList.constEnd()
  918. , &Number::toString);
  919. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  920. QCOMPARE(linkedList3, QLinkedList<QString>() << "1" << "2" << "3");
  921. QLinkedList<QString> linkedList4 =
  922. QtConcurrent::blockingMapped<QLinkedList<QString> >(QLinkedList<Number>(numberLinkedList), &Number::toString);
  923. QCOMPARE(linkedList, QLinkedList<int>() << 1 << 2 << 3);
  924. QCOMPARE(linkedList4, QLinkedList<QString>() << "1" << "2" << "3");
  925. }
  926. // change the value_type
  927. {
  928. QList<QString> strings = QStringList() << "1" << "2" << "3";
  929. QList<int> list = QtConcurrent::blockingMapped(strings, StringToInt());
  930. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  931. QList<int> list2 = QtConcurrent::blockingMapped<QList<int> >(strings.constBegin(),
  932. strings.constEnd(),
  933. StringToInt());
  934. QCOMPARE(list2, QList<int>() << 1 << 2 << 3);
  935. }
  936. {
  937. QList<QString> strings = QStringList() << "1" << "2" << "3";
  938. QList<int> list = QtConcurrent::blockingMapped(strings, stringToInt);
  939. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  940. QList<int> list2 = QtConcurrent::blockingMapped<QList<int> >(strings.constBegin(),
  941. strings.constEnd(),
  942. stringToInt);
  943. QCOMPARE(list2, QList<int>() << 1 << 2 << 3);
  944. }
  945. {
  946. QList<int> numberList2 = QtConcurrent::blockingMapped(numberList, &Number::toInt);
  947. QCOMPARE(numberList2, QList<int>() << 1 << 2 << 3);
  948. QList<int> numberList3 = QtConcurrent::blockingMapped<QList<int> >(numberList.constBegin(),
  949. numberList.constEnd(),
  950. &Number::toInt);
  951. QCOMPARE(numberList3, QList<int>() << 1 << 2 << 3);
  952. }
  953. // change the value_type from QStringList
  954. {
  955. QStringList strings = QStringList() << "1" << "2" << "3";
  956. QList<int> list = QtConcurrent::blockingMapped(strings, StringToInt());
  957. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  958. QList<int> list2 = QtConcurrent::blockingMapped<QList<int> >(strings.constBegin(),
  959. strings.constEnd(),
  960. StringToInt());
  961. QCOMPARE(list2, QList<int>() << 1 << 2 << 3);
  962. }
  963. {
  964. QStringList strings = QStringList() << "1" << "2" << "3";
  965. QList<int> list = QtConcurrent::blockingMapped(strings, stringToInt);
  966. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  967. QList<int> list2 = QtConcurrent::blockingMapped<QList<int> >(strings.constBegin(),
  968. strings.constEnd(),
  969. stringToInt);
  970. QCOMPARE(list2, QList<int>() << 1 << 2 << 3);
  971. }
  972. // functor
  973. {
  974. QVector<double> list2 = QtConcurrent::blockingMapped<QVector<double> >(list, IntToDouble());
  975. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  976. QCOMPARE(list2, QVector<double>() << 1.0 << 2.0 << 3.0);
  977. QVector<double> list3 = QtConcurrent::blockingMapped<QVector<double> >(list.constBegin(),
  978. list.constEnd(),
  979. IntToDouble());
  980. QCOMPARE(list, QList<int>() << 1 << 2 << 3);
  981. QCOMPA