35 #include <QRandomGenerator>
37 #include "../testhelper_functions.h"
47 void mappedThreadPool();
48 void mappedWithMoveOnlyCallable();
50 void mappedReducedThreadPool();
51 void mappedReducedWithMoveOnlyCallable();
52 void mappedReducedDifferentType();
53 void mappedReducedInitialValue();
54 void mappedReducedInitialValueThreadPool();
55 void mappedReducedInitialValueWithMoveOnlyCallable();
56 void mappedReducedDifferentTypeInitialValue();
57 void mappedReduceOptionConvertableToResultType();
59 void functionOverloads();
60 void noExceptFunctionOverloads();
61 #ifndef QT_NO_EXCEPTIONS
64 void incrementalResults();
67 void stlContainersLambda();
68 void qFutureAssignmentLeak();
70 void persistentResultTest();
120 void tst_QtConcurrentMap::map()
147 numberList << 1 << 2 << 3;
234 numberList << 1 << 2 << 3;
254 void tst_QtConcurrentMap::blockingMap()
281 numberList << 1 << 2 << 3;
371 numberList << 1 << 2 << 3;
391 void tst_QtConcurrentMap::mapOnRvalue()
406 const QList expected { 1, 4, 6, 4 };
488 return string.toInt();
496 return string.toInt();
500 #define CHECK_FAIL(message) \
502 if (QTest::currentTestFailed())\
503 QFAIL("failed one line above on " message);\
506 template <
typename SourceObject,
typename ResultObject,
typename MapObject>
510 sourceObjectList, mapObject).results();
518 sourceObjectList, mapObject);
526 void tst_QtConcurrentMap::mapped()
533 const QList<int> intListMultipiedBy2 {2, 4, 6};
536 auto lambdaMultiplyBy2 = [](
int x) {
539 auto lambdaIntToDouble = [](
int x) {
543 return string.toInt();
606 auto result = QtConcurrent::blockingMapped<std::vector<int>>(
625 const int count = workingThreads.size();
630 template <
typename SourceObject,
typename ResultObject,
typename MapObject>
637 sourceObjectList, mapObject).results();
647 sourceObjectList, mapObject);
673 void tst_QtConcurrentMap::mappedThreadPool()
676 const QList<int> intListMultipiedBy3 {3, 6, 9};
678 auto lambdaMultiplyBy3 = [](
int x) {
686 workingThreads.clear();
720 auto result = QtConcurrent::blockingMapped<std::vector<int>>(
726 void tst_QtConcurrentMap::mappedWithMoveOnlyCallable()
729 const QList<int> intListMultipiedBy2 { 2, 4, 6 };
786 return Number(
x.toInt() *
x.toInt());
794 return Number(
x.toInt() *
x.toInt());
798 template <
typename SourceObject,
typename ResultObject,
typename MapObject,
typename ReduceObject>
803 const ResultObject result1 = QtConcurrent::mappedReduced<ResultObject>(
804 sourceObjectList, mapObject, reduceObject).result();
807 const ResultObject result2 = QtConcurrent::mappedReduced<ResultObject>(
809 mapObject, reduceObject).result();
812 const ResultObject result3 = QtConcurrent::blockingMappedReduced<ResultObject>(
813 sourceObjectList, mapObject, reduceObject);
816 const ResultObject result4 = QtConcurrent::blockingMappedReduced<ResultObject>(
824 sourceObjectList, mapObject, reduceObject).
result();
829 mapObject, reduceObject).
result();
833 sourceObjectList, mapObject, reduceObject);
842 template <
typename SourceObject,
typename ResultObject,
typename MapObject,
typename ReduceObject>
846 sourceObjectList, mapObject, reduceObject, options).
result();
851 mapObject, reduceObject, options).
result();
855 sourceObjectList, mapObject, reduceObject, options);
859 sourceObjectList.
constBegin(), sourceObjectList.
constEnd(), mapObject, reduceObject, options);
863 void tst_QtConcurrentMap::mappedReduced()
869 const int sumOfSquares = 14;
873 auto lambdaSquare = [](
int x) {
876 auto lambdaSumReduce = [](
int &
sum,
int x) {
953 template <
typename SourceObject,
typename ResultObject,
typename MapObject,
typename ReduceObject>
958 ReduceObject reduceObject)
962 const ResultObject result1 = QtConcurrent::mappedReduced<ResultObject>(
963 pool, sourceObjectList, mapObject, reduceObject).result();
968 QtConcurrent::mappedReduced<ResultObject>(
pool, sourceObjectList.
constBegin(),
969 sourceObjectList.
constEnd(), mapObject,
970 reduceObject).result();
974 const ResultObject result3 = QtConcurrent::blockingMappedReduced<ResultObject>(
975 pool, sourceObjectList, mapObject, reduceObject);
979 const ResultObject result4 = QtConcurrent::blockingMappedReduced<ResultObject>(
981 mapObject, reduceObject);
989 pool, sourceObjectList, mapObject, reduceObject).
result();
995 sourceObjectList.
constEnd(), mapObject,
1001 pool, sourceObjectList, mapObject, reduceObject);
1007 mapObject, reduceObject);
1029 void tst_QtConcurrentMap::mappedReducedThreadPool()
1032 const int sumOfCubes = 36;
1034 auto lambdaCube = [](
int x) {
1037 auto lambdaSumReduce = [](
int &
sum,
int x) {
1044 workingThreads.clear();
1104 void tst_QtConcurrentMap::mappedReducedWithMoveOnlyCallable()
1107 const auto sum = 12;
1154 void tst_QtConcurrentMap::mappedReducedDifferentType()
1158 const int sumOfSquares = 14;
1160 auto lambdaSquare = [](
Number x) {
1161 return Number(
x.toInt() *
x.toInt());
1163 auto lambdaSumReduce = [](
int &
sum,
Number x) {
1204 template <
typename SourceObject,
typename ResultObject,
typename InitialObject,
typename MapObject,
typename ReduceObject>
1207 MapObject mapObject,
1208 ReduceObject reduceObject,
1209 InitialObject &&initialObject)
1214 QtConcurrent::mappedReduced<ResultObject>(sourceObjectList, mapObject, reduceObject,
1215 initialObject).result();
1218 const ResultObject result2 = QtConcurrent::mappedReduced<ResultObject>(
1220 mapObject, reduceObject, initialObject).result();
1223 const ResultObject result3 = QtConcurrent::blockingMappedReduced<ResultObject>(
1224 sourceObjectList, mapObject, reduceObject, initialObject);
1227 const ResultObject result4 = QtConcurrent::blockingMappedReduced<ResultObject>(
1229 mapObject, reduceObject, initialObject);
1242 mapObject, reduceObject, initialObject).
result();
1246 sourceObjectList, mapObject, reduceObject, initialObject);
1251 mapObject, reduceObject, initialObject);
1256 template <
typename SourceObject,
typename ResultObject,
typename InitialObject,
typename MapObject,
typename ReduceObject>
1259 MapObject mapObject,
1260 ReduceObject reduceObject,
1261 InitialObject &&initialObject,
1262 QtConcurrent::ReduceOptions options)
1265 sourceObjectList, mapObject, reduceObject, initialObject, options).
result();
1270 mapObject, reduceObject, initialObject, options).
result();
1274 sourceObjectList, mapObject, reduceObject, initialObject, options);
1278 sourceObjectList.
constBegin(), sourceObjectList.
constEnd(), mapObject, reduceObject, initialObject, options);
1282 void tst_QtConcurrentMap::mappedReducedInitialValue()
1288 const QList<int> intListAppended {10, 1, 2, 3};
1289 const QList<int> intListSquaresAppended {10, 1, 4, 9};
1292 const int sumOfSquares = 24;
1293 const int intInitial = 10;
1297 auto lambdaSquare = [](
int x) {
1300 auto lambdaSumReduce = [](
int &
sum,
int x) {
1379 template <
typename SourceObject,
typename ResultObject,
typename InitialObject,
typename MapObject,
typename ReduceObject>
1383 MapObject mapObject,
1384 ReduceObject reduceObject,
1385 InitialObject &&initialObject)
1389 const ResultObject result1 = QtConcurrent::mappedReduced<ResultObject>(
1390 pool, sourceObjectList, mapObject, reduceObject, initialObject).result();
1395 QtConcurrent::mappedReduced<ResultObject>(
pool, sourceObjectList.
constBegin(),
1396 sourceObjectList.
constEnd(), mapObject,
1397 reduceObject, initialObject).result();
1401 const ResultObject result3 = QtConcurrent::blockingMappedReduced<ResultObject>(
1402 pool, sourceObjectList, mapObject, reduceObject, initialObject);
1406 const ResultObject result4 = QtConcurrent::blockingMappedReduced<ResultObject>(
1408 mapObject, reduceObject, initialObject);
1416 pool, sourceObjectList, mapObject, reduceObject, initialObject).
result();
1422 sourceObjectList.
constEnd(), mapObject,
1423 reduceObject, initialObject).
result();
1428 pool, sourceObjectList, mapObject, reduceObject, initialObject);
1434 mapObject, reduceObject, initialObject);
1440 void tst_QtConcurrentMap::mappedReducedInitialValueThreadPool()
1445 const int sumOfCubes = 46;
1446 const int intInitial = 10;
1448 auto lambdaCube = [](
int x) {
1451 auto lambdaSumReduce = [](
int &
sum,
int x) {
1458 workingThreads.clear();
1468 lambdaSumReduce, intInitial);
1479 lambdaSumReduce, intInitial);
1490 lambdaSumReduce, intInitial);
1529 void tst_QtConcurrentMap::mappedReducedInitialValueWithMoveOnlyCallable()
1532 const auto initialValue = 10;
1533 const auto sum = 22;
1584 void tst_QtConcurrentMap::mappedReducedDifferentTypeInitialValue()
1590 const int sumOfSquares = 24;
1591 const int intInitial = 10;
1593 auto lambdaSquare = [](
Number x) {
1594 return Number(
x.toInt() *
x.toInt());
1596 auto lambdaSumReduce = [](
int &
sum,
Number x) {
1633 void tst_QtConcurrentMap::mappedReduceOptionConvertableToResultType()
1684 void tst_QtConcurrentMap::assignResult()
1800 void tst_QtConcurrentMap::functionOverloads()
1813 QtConcurrent::blockingMapped<QList<int>>(constIntList,
fnConst);
1818 QtConcurrent::blockingMapped<QList<QString> >(constIntList,
changeTypeConst);
1823 void tst_QtConcurrentMap::noExceptFunctionOverloads()
1836 QtConcurrent::blockingMapped<QList<int>>(constIntList,
fnConstNoExcept);
1838 QtConcurrent::blockingMapped<QList<MemFnTester>>(constMemFnTesterList,
1866 if (localCurrent <= localPeak)
1899 const int itemcount = 100;
1907 for (
int i = 0;
i < itemcount; ++
i)
1923 for (
int i = 0;
i < itemcount; ++
i)
1935 #ifndef QT_NO_EXCEPTIONS
1942 void tst_QtConcurrentMap::exceptions()
1944 bool caught =
false;
1952 QFAIL(
"did not get exception");
1962 void tst_QtConcurrentMap::incrementalResults()
1964 const int count = 200;
1990 void tst_QtConcurrentMap::noDetach()
2033 void tst_QtConcurrentMap::stlContainers()
2042 std::list<int>
list;
2054 void tst_QtConcurrentMap::stlContainersLambda()
2060 std::vector<int> vector2 =
2064 std::list<int>
list;
2068 std::list<int>
list2 =
2084 void tst_QtConcurrentMap::qFutureAssignmentLeak()
2091 for (
int i=0;
i<1000;++
i)
2126 void tst_QtConcurrentMap::stressTest()
2128 const int listSize = 1000;
2129 const int sum = (listSize - 1) * (listSize / 2);
2133 for (
int i = 0;
i < listSize; ++
i) {
2137 for (
int i =0 ;
i < 100; ++
i) {
2139 for (
int j = 0;
j < listSize; ++
j)
2143 for (
int i = 0 ;
i < 100; ++
i) {
2148 for (
int i = 0 ;
i < 100; ++
i) {
2150 for (
int j = 0;
j < listSize; ++
j)
2175 void tst_QtConcurrentMap::persistentResultTest()
2183 list << 1 << 2 << 3;
2196 #include "tst_qtconcurrentmap.moc"
small capitals from c petite p scientific i
[1]
InstanceCounter(const InstanceCounter &)
constexpr InstanceCounter & operator=(const InstanceCounter &) noexcept
double operator()(int x) const
QString changeTypeConstNoExcept() const noexcept
QString changeTypeConst() const
MemFnTester fnConst() const
MemFnTester fnNoExcept() noexcept
QString changeTypeNoExcept() noexcept
MemFnTester fnConstNoExcept() const noexcept
int operator()(int x) const
MultiplyBy2InPlaceMoveOnly & operator=(MultiplyBy2InPlaceMoveOnly &&other)=default
MultiplyBy2InPlaceMoveOnly(const MultiplyBy2InPlaceMoveOnly &)=delete
MultiplyBy2InPlaceMoveOnly & operator=(const MultiplyBy2InPlaceMoveOnly &)=delete
MultiplyBy2InPlaceMoveOnly()=default
MultiplyBy2InPlaceMoveOnly(MultiplyBy2InPlaceMoveOnly &&)=default
MultiplyBy2MoveOnly & operator=(MultiplyBy2MoveOnly &&other)=default
MultiplyBy2MoveOnly()=default
int operator()(int x) const
MultiplyBy2MoveOnly(MultiplyBy2MoveOnly &&)=default
MultiplyBy2MoveOnly(const MultiplyBy2MoveOnly &)=delete
MultiplyBy2MoveOnly & operator=(const MultiplyBy2MoveOnly &)=delete
int operator()(int x) const
Number multipliedBy2() const
Number operator()(Number x)
The QAtomicInt class provides platform-independent atomic operations on int.
bool testAndSetOrdered(T expectedValue, T newValue) noexcept
void storeRelaxed(T newValue) noexcept
T fetchAndAddRelaxed(T valueToAdd) noexcept
T loadRelaxed() const noexcept
The QException class provides a base class for exceptions that can be transferred across threads.
QList< T > results() const
T resultAt(int index) const
qsizetype size() const noexcept
bool isDetached() const noexcept
void push_back(parameter_type t)
const_iterator constBegin() const noexcept
void append(parameter_type t)
const_iterator constEnd() const noexcept
The QMutex class provides access serialization between threads.
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes.
static Q_DECL_CONST_FUNCTION QRandomGenerator * global()
The QSemaphore class provides a general counting semaphore.
The QString class provides a Unicode character string.
int toInt(bool *ok=nullptr, int base=10) const
The QStringList class provides a list of strings.
static int idealThreadCount() noexcept
static QThread * currentThread()
The QThreadPool class manages a collection of QThreads.
void setMaxThreadCount(int maxThreadCount)
int operator()(const QString &string) const
QMap< QString, QString > map
[6]
auto it unsigned count const
typename C::iterator iterator
Q_CORE_EXPORT void qSleep(int ms)
Q_CORE_EXPORT void qWait(int ms)
The QtConcurrent namespace provides high-level APIs that make it possible to write multi-threaded pro...
void blockingMap(QThreadPool *pool, Sequence &&sequence, MapFunctor map)
QFuture< void > map(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map)
QFuture< QtPrivate::MapResultType< Sequence, MapFunctor > > mapped(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map)
QFuture< ResultType > mappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options=ReduceOptions(UnorderedReduce|SequentialReduce))
ResultType blockingMappedReduced(QThreadPool *pool, Sequence &&sequence, MapFunctor &&map, ReduceFunctor &&reduce, ReduceOptions options=ReduceOptions(UnorderedReduce|SequentialReduce))
OutputSequence blockingMapped(QThreadPool *pool, InputSequence &&sequence, MapFunctor &&map)
GLboolean GLboolean GLboolean b
GLint GLint GLint GLint GLint x
[0]
GLenum GLenum GLsizei count
GLsizei const GLchar *const * string
[0]
QtPrivate::QRegularExpressionMatchIteratorRangeBasedForIterator begin(const QRegularExpressionMatchIterator &iterator)
#define QTEST_MAIN(TestObject)
#define QTRY_COMPARE(expr, expected)
#define QVERIFY(statement)
void intSumReduce(int &sum, int x)
[15]
QFuture< void > future
[5]
LockedCounter(QMutex *mutex, QAtomicInt *ai)
void numberSumReduce(int &sum, const Number &x)
void multiplyBy2Immutable(int x)
InstanceCounter slowMap(const InstanceCounter &in)
int changeTypeQStringListConstNoExcept(const QStringList &) noexcept
void slowReduce(int &result, const InstanceCounter &)
Q_DECLARE_METATYPE(QList< Number >)
QString changeTypeNoExcept(int &) noexcept
int changeTypeQStringListConst(const QStringList &)
int changeTypeQStringListNoExcept(QStringList &) noexcept
QAtomicInt peakInstanceCount
Number numberSquare(Number x)
int stringToInt(const QString &string)
int fnNoExcept(int &i) noexcept
QString changeTypeConst(const int &)
void testMappedThreadPool(QThreadPool *pool, const QList< SourceObject > &sourceObjectList, const QList< ResultObject > &expectedResult, MapObject mapObject)
double intToDouble(int x)
QString changeType(int &)
#define CHECK_FAIL(message)
QString changeTypeConstNoExcept(const int &) noexcept
void fastReduce(int &result, const InstanceCounter &)
InstanceCounter fastMap(const InstanceCounter &in)
int fnConst(const int &i)
void testMappedReduced(const QList< SourceObject > &sourceObjectList, const ResultObject &expectedResult, MapObject mapObject, ReduceObject reduceObject)
InstanceCounter ic_fn(const InstanceCounter &ic)
void add(int &result, const int &sum)
void storeCurrentThread()
QAtomicInt currentInstanceCount
void testMappedReducedInitialValue(const QList< SourceObject > &sourceObjectList, const ResultObject &expectedResult, MapObject mapObject, ReduceObject reduceObject, InitialObject &&initialObject)
void testMappedReducedInitialValueThreadPool(QThreadPool *pool, const QList< SourceObject > &sourceObjectList, const ResultObject &expectedResult, MapObject mapObject, ReduceObject reduceObject, InitialObject &&initialObject)
int changeTypeQStringList(QStringList &)
int fnConstNoExcept(const int &i) noexcept
void multiplyBy2InPlace(int &x)
void testMappedReducedThreadPool(QThreadPool *pool, const QList< SourceObject > &sourceObjectList, const ResultObject &expectedResult, MapObject mapObject, ReduceObject reduceObject)
void testMapped(const QList< SourceObject > &sourceObjectList, const QList< ResultObject > &expectedResult, MapObject mapObject)