2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 12:36:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
#include <qtconcurrentmap.h>
|
2012-08-27 21:22:53 +00:00
|
|
|
#include <qexception.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <qdebug.h>
|
2020-11-26 16:31:50 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <QThread>
|
2012-10-25 10:32:52 +00:00
|
|
|
#include <QMutex>
|
2020-11-26 16:31:50 +00:00
|
|
|
#include <QTest>
|
|
|
|
#include <QRandomGenerator>
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-10-14 18:19:38 +00:00
|
|
|
#include "../testhelper_functions.h"
|
2011-10-11 04:15:52 +00:00
|
|
|
|
2020-03-26 14:47:04 +00:00
|
|
|
class tst_QtConcurrentMap : public QObject
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
private slots:
|
|
|
|
void map();
|
2020-09-24 09:07:32 +00:00
|
|
|
void blockingMap();
|
|
|
|
void mapOnRvalue();
|
2011-04-27 10:05:43 +00:00
|
|
|
void mapped();
|
2020-04-28 12:19:42 +00:00
|
|
|
void mappedThreadPool();
|
2020-10-09 16:21:17 +00:00
|
|
|
void mappedWithMoveOnlyCallable();
|
2011-04-27 10:05:43 +00:00
|
|
|
void mappedReduced();
|
2020-04-28 12:19:42 +00:00
|
|
|
void mappedReducedThreadPool();
|
2020-10-09 16:21:17 +00:00
|
|
|
void mappedReducedWithMoveOnlyCallable();
|
2020-04-17 09:54:40 +00:00
|
|
|
void mappedReducedDifferentType();
|
2020-04-14 06:14:08 +00:00
|
|
|
void mappedReducedInitialValue();
|
2020-04-28 12:19:42 +00:00
|
|
|
void mappedReducedInitialValueThreadPool();
|
2020-10-09 16:21:17 +00:00
|
|
|
void mappedReducedInitialValueWithMoveOnlyCallable();
|
2020-04-17 09:54:40 +00:00
|
|
|
void mappedReducedDifferentTypeInitialValue();
|
2011-04-27 10:05:43 +00:00
|
|
|
void assignResult();
|
|
|
|
void functionOverloads();
|
2017-01-16 11:22:43 +00:00
|
|
|
void noExceptFunctionOverloads();
|
2011-04-27 10:05:43 +00:00
|
|
|
#ifndef QT_NO_EXCEPTIONS
|
|
|
|
void exceptions();
|
|
|
|
#endif
|
|
|
|
void incrementalResults();
|
2011-12-29 01:38:45 +00:00
|
|
|
void noDetach();
|
2011-04-27 10:05:43 +00:00
|
|
|
void stlContainers();
|
2020-03-26 14:47:04 +00:00
|
|
|
void stlContainersLambda();
|
2011-04-27 10:05:43 +00:00
|
|
|
void qFutureAssignmentLeak();
|
|
|
|
void stressTest();
|
2012-10-25 10:32:52 +00:00
|
|
|
void persistentResultTest();
|
2011-04-27 10:05:43 +00:00
|
|
|
public slots:
|
|
|
|
void throttling();
|
|
|
|
};
|
|
|
|
|
|
|
|
using namespace QtConcurrent;
|
|
|
|
|
|
|
|
void multiplyBy2Immutable(int x)
|
|
|
|
{
|
|
|
|
x *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MultiplyBy2Immutable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void operator()(int x)
|
|
|
|
{
|
|
|
|
x *= 2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void multiplyBy2InPlace(int &x)
|
|
|
|
{
|
|
|
|
x *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MultiplyBy2InPlace
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void operator()(int &x)
|
|
|
|
{
|
|
|
|
x *= 2;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-09 16:21:17 +00:00
|
|
|
class MultiplyBy2InPlaceMoveOnly
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MultiplyBy2InPlaceMoveOnly() = default;
|
|
|
|
MultiplyBy2InPlaceMoveOnly(MultiplyBy2InPlaceMoveOnly &&) = default;
|
|
|
|
MultiplyBy2InPlaceMoveOnly &operator=(MultiplyBy2InPlaceMoveOnly &&other) = default;
|
|
|
|
|
|
|
|
MultiplyBy2InPlaceMoveOnly(const MultiplyBy2InPlaceMoveOnly &) = delete;
|
|
|
|
MultiplyBy2InPlaceMoveOnly &operator=(const MultiplyBy2InPlaceMoveOnly &) = delete;
|
|
|
|
|
|
|
|
void operator()(int &x) { x *= 2; }
|
|
|
|
};
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
Q_DECLARE_METATYPE(QList<Number>);
|
|
|
|
|
|
|
|
void tst_QtConcurrentMap::map()
|
|
|
|
{
|
|
|
|
// functors take arguments by reference, modifying the sequence in place
|
|
|
|
{
|
|
|
|
QList<int> list;
|
|
|
|
list << 1 << 2 << 3;
|
|
|
|
|
|
|
|
// functor
|
|
|
|
QtConcurrent::map(list, MultiplyBy2InPlace()).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 2 << 4 << 6);
|
|
|
|
QtConcurrent::map(list.begin(), list.end(), MultiplyBy2InPlace()).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 4 << 8 << 12);
|
|
|
|
|
|
|
|
// function
|
|
|
|
QtConcurrent::map(list, multiplyBy2InPlace).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 8 << 16 << 24);
|
|
|
|
QtConcurrent::map(list.begin(), list.end(), multiplyBy2InPlace).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 16 << 32 << 48);
|
|
|
|
|
|
|
|
// bound function
|
|
|
|
QtConcurrent::map(list, multiplyBy2InPlace).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 32 << 64 << 96);
|
|
|
|
QtConcurrent::map(list.begin(), list.end(), multiplyBy2InPlace).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 64 << 128 << 192);
|
|
|
|
|
|
|
|
// member function
|
|
|
|
QList<Number> numberList;
|
|
|
|
numberList << 1 << 2 << 3;
|
|
|
|
QtConcurrent::map(numberList, &Number::multiplyBy2).waitForFinished();
|
|
|
|
QCOMPARE(numberList, QList<Number>() << 2 << 4 << 6);
|
|
|
|
QtConcurrent::map(numberList.begin(), numberList.end(), &Number::multiplyBy2).waitForFinished();
|
|
|
|
QCOMPARE(numberList, QList<Number>() << 4 << 8 << 12);
|
2011-05-18 15:55:43 +00:00
|
|
|
|
|
|
|
// lambda
|
|
|
|
QtConcurrent::map(list, [](int &x){x *= 2;}).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 128 << 256 << 384);
|
|
|
|
QtConcurrent::map(list.begin(), list.end(), [](int &x){x *= 2;}).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 256 << 512 << 768);
|
2020-10-09 16:21:17 +00:00
|
|
|
|
|
|
|
// move-only functor
|
|
|
|
QtConcurrent::map(list, MultiplyBy2InPlaceMoveOnly()).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 512 << 1024 << 1536);
|
|
|
|
QtConcurrent::map(list.begin(), list.end(), MultiplyBy2InPlaceMoveOnly()).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 1024 << 2048 << 3072);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// functors don't take arguments by reference, making these no-ops
|
|
|
|
{
|
|
|
|
QList<int> list;
|
|
|
|
list << 1 << 2 << 3;
|
|
|
|
|
|
|
|
// functor
|
|
|
|
QtConcurrent::map(list, MultiplyBy2Immutable()).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
|
|
|
QtConcurrent::map(list.begin(), list.end(), MultiplyBy2Immutable()).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
|
|
|
|
|
|
|
// function
|
|
|
|
QtConcurrent::map(list, multiplyBy2Immutable).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
|
|
|
QtConcurrent::map(list.begin(), list.end(), multiplyBy2Immutable).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
|
|
|
|
|
|
|
// bound function
|
|
|
|
QtConcurrent::map(list, multiplyBy2Immutable).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
|
|
|
QtConcurrent::map(list.begin(), list.end(), multiplyBy2Immutable).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
2011-05-18 15:55:43 +00:00
|
|
|
|
|
|
|
// lambda
|
|
|
|
QtConcurrent::map(list, [](int x){x *= 2;}).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
|
|
|
QtConcurrent::map(list.begin(), list.end(), [](int x){x *= 2;}).waitForFinished();
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 08:30:59 +00:00
|
|
|
// functors take arguments by reference, modifying the move-only sequence in place
|
|
|
|
{
|
2020-10-08 22:02:00 +00:00
|
|
|
MoveOnlyVector<int> moveOnlyVector({ 1, 2, 3 });
|
2020-09-29 08:30:59 +00:00
|
|
|
|
|
|
|
// functor
|
|
|
|
QtConcurrent::map(moveOnlyVector, MultiplyBy2InPlace()).waitForFinished();
|
2020-10-08 22:02:00 +00:00
|
|
|
QCOMPARE(moveOnlyVector, MoveOnlyVector<int>({ 2, 4, 6 }));
|
2020-09-29 08:30:59 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
#if 0
|
|
|
|
// not allowed: map() with immutable sequences makes no sense
|
|
|
|
{
|
|
|
|
const QList<int> list = QList<int>() << 1 << 2 << 3;
|
|
|
|
|
|
|
|
QtConcurrent::map(list, MultiplyBy2Immutable());
|
|
|
|
QtConcurrent::map(list, multiplyBy2Immutable);
|
|
|
|
QtConcurrent::map(list, multiplyBy2Immutable);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// not allowed: in place modification of a temp copy (since temp copy goes out of scope)
|
|
|
|
{
|
|
|
|
QList<int> list;
|
|
|
|
list << 1 << 2 << 3;
|
|
|
|
|
|
|
|
QtConcurrent::map(QList<int>(list), MultiplyBy2InPlace());
|
|
|
|
QtConcurrent::map(QList<int>(list), multiplyBy2);
|
|
|
|
QtConcurrent::map(QList<int>(list), multiplyBy2InPlace);
|
|
|
|
|
|
|
|
QList<Number> numberList;
|
|
|
|
numberList << 1 << 2 << 3;
|
|
|
|
QtConcurrent::map(QList<Number>(numberList), &Number::multiplyBy2);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// not allowed: map() on a const list, where functors try to modify the items in the list
|
|
|
|
{
|
|
|
|
const QList<int> list = QList<int>() << 1 << 2 << 3;;
|
|
|
|
|
|
|
|
QtConcurrent::map(list, MultiplyBy2InPlace());
|
|
|
|
QtConcurrent::map(list, multiplyBy2InPlace);
|
|
|
|
QtConcurrent::map(list, multiplyBy2InPlace);
|
|
|
|
|
|
|
|
const QList<Number> numberList = QList<Number>() << 1 << 2 << 3;
|
|
|
|
QtConcurrent::map(numberList, &Number::multiplyBy2);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-09-24 09:07:32 +00:00
|
|
|
void tst_QtConcurrentMap::blockingMap()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
// functors take arguments by reference, modifying the sequence in place
|
|
|
|
{
|
|
|
|
QList<int> list;
|
|
|
|
list << 1 << 2 << 3;
|
|
|
|
|
|
|
|
// functor
|
|
|
|
QtConcurrent::blockingMap(list, MultiplyBy2InPlace());
|
|
|
|
QCOMPARE(list, QList<int>() << 2 << 4 << 6);
|
|
|
|
QtConcurrent::blockingMap(list.begin(), list.end(), MultiplyBy2InPlace());
|
|
|
|
QCOMPARE(list, QList<int>() << 4 << 8 << 12);
|
|
|
|
|
|
|
|
// function
|
|
|
|
QtConcurrent::blockingMap(list, multiplyBy2InPlace);
|
|
|
|
QCOMPARE(list, QList<int>() << 8 << 16 << 24);
|
|
|
|
QtConcurrent::blockingMap(list.begin(), list.end(), multiplyBy2InPlace);
|
|
|
|
QCOMPARE(list, QList<int>() << 16 << 32 << 48);
|
|
|
|
|
|
|
|
// bound function
|
|
|
|
QtConcurrent::blockingMap(list, multiplyBy2InPlace);
|
|
|
|
QCOMPARE(list, QList<int>() << 32 << 64 << 96);
|
|
|
|
QtConcurrent::blockingMap(list.begin(), list.end(), multiplyBy2InPlace);
|
|
|
|
QCOMPARE(list, QList<int>() << 64 << 128 << 192);
|
|
|
|
|
|
|
|
// member function
|
|
|
|
QList<Number> numberList;
|
|
|
|
numberList << 1 << 2 << 3;
|
|
|
|
QtConcurrent::blockingMap(numberList, &Number::multiplyBy2);
|
|
|
|
QCOMPARE(numberList, QList<Number>() << 2 << 4 << 6);
|
|
|
|
QtConcurrent::blockingMap(numberList.begin(), numberList.end(), &Number::multiplyBy2);
|
|
|
|
QCOMPARE(numberList, QList<Number>() << 4 << 8 << 12);
|
2020-03-26 14:47:04 +00:00
|
|
|
|
|
|
|
// lambda
|
|
|
|
QtConcurrent::blockingMap(list, [](int &x) { x *= 2; });
|
|
|
|
QCOMPARE(list, QList<int>() << 128 << 256 << 384);
|
|
|
|
QtConcurrent::blockingMap(list.begin(), list.end(), [](int &x) { x *= 2; });
|
|
|
|
QCOMPARE(list, QList<int>() << 256 << 512 << 768);
|
2020-10-09 16:21:17 +00:00
|
|
|
|
|
|
|
// move-only functor
|
|
|
|
QtConcurrent::blockingMap(list, MultiplyBy2InPlaceMoveOnly());
|
|
|
|
QCOMPARE(list, QList<int>() << 512 << 1024 << 1536);
|
|
|
|
QtConcurrent::blockingMap(list.begin(), list.end(), MultiplyBy2InPlaceMoveOnly());
|
|
|
|
QCOMPARE(list, QList<int>() << 1024 << 2048 << 3072);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2020-09-29 08:30:59 +00:00
|
|
|
// functors take arguments by reference, modifying the move-only sequence in place
|
|
|
|
{
|
2020-10-08 22:02:00 +00:00
|
|
|
MoveOnlyVector<int> moveOnlyVector({ 1, 2, 3 });
|
2020-09-29 08:30:59 +00:00
|
|
|
|
|
|
|
// functor
|
|
|
|
QtConcurrent::blockingMap(moveOnlyVector, MultiplyBy2InPlace());
|
2020-10-08 22:02:00 +00:00
|
|
|
QCOMPARE(moveOnlyVector, MoveOnlyVector<int>({ 2, 4, 6 }));
|
2020-09-29 08:30:59 +00:00
|
|
|
QtConcurrent::blockingMap(moveOnlyVector.begin(), moveOnlyVector.end(),
|
|
|
|
MultiplyBy2InPlace());
|
2020-10-08 22:02:00 +00:00
|
|
|
QCOMPARE(moveOnlyVector, MoveOnlyVector<int>({ 4, 8, 12 }));
|
2020-09-29 08:30:59 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
// functors don't take arguments by reference, making these no-ops
|
|
|
|
{
|
|
|
|
QList<int> list;
|
|
|
|
list << 1 << 2 << 3;
|
|
|
|
|
|
|
|
// functor
|
|
|
|
QtConcurrent::blockingMap(list, MultiplyBy2Immutable());
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
|
|
|
QtConcurrent::blockingMap(list.begin(), list.end(), MultiplyBy2Immutable());
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
|
|
|
|
|
|
|
// function
|
|
|
|
QtConcurrent::blockingMap(list, multiplyBy2Immutable);
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
|
|
|
QtConcurrent::blockingMap(list.begin(), list.end(), multiplyBy2Immutable);
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
|
|
|
|
|
|
|
// bound function
|
|
|
|
QtConcurrent::blockingMap(list, multiplyBy2Immutable);
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
|
|
|
QtConcurrent::blockingMap(list.begin(), list.end(), multiplyBy2Immutable);
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
2020-03-26 14:47:04 +00:00
|
|
|
|
|
|
|
// lambda
|
|
|
|
QtConcurrent::blockingMap(list, [](int x) { x *= 2; });
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
|
|
|
QtConcurrent::blockingMap(list.begin(), list.end(), [](int x) { x *= 2; });
|
|
|
|
QCOMPARE(list, QList<int>() << 1 << 2 << 3);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// not allowed: map() with immutable sequences makes no sense
|
|
|
|
{
|
|
|
|
const QList<int> list = QList<int>() << 1 << 2 << 3;
|
|
|
|
|
|
|
|
QtConcurrent::blockingMap(list, MultiplyBy2Immutable());
|
|
|
|
QtConcurrent::blockkng::map(list, multiplyBy2Immutable);
|
|
|
|
QtConcurrent::blockingMap(list, multiplyBy2Immutable);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// not allowed: in place modification of a temp copy (since temp copy goes out of scope)
|
|
|
|
{
|
|
|
|
QList<int> list;
|
|
|
|
list << 1 << 2 << 3;
|
|
|
|
|
|
|
|
QtConcurrent::blockingMap(QList<int>(list), MultiplyBy2InPlace());
|
|
|
|
QtConcurrent::blockingMap(QList<int>(list), multiplyBy2);
|
|
|
|
QtConcurrent::blockingMap(QList<int>(list), multiplyBy2InPlace);
|
|
|
|
|
|
|
|
QList<Number> numberList;
|
|
|
|
numberList << 1 << 2 << 3;
|
|
|
|
QtConcurrent::blockingMap(QList<Number>(numberList), &Number::multiplyBy2);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// not allowed: map() on a const list, where functors try to modify the items in the list
|
|
|
|
{
|
|
|
|
const QList<int> list = QList<int>() << 1 << 2 << 3;;
|
|
|
|
|
|
|
|
QtConcurrent::blockingMap(list, MultiplyBy2InPlace());
|
|
|
|
QtConcurrent::blockingMap(list, multiplyBy2InPlace);
|
|
|
|
QtConcurrent::blockingMap(list, multiplyBy2InPlace);
|
|
|
|
|
|
|
|
const QList<Number> numberList = QList<Number>() << 1 << 2 << 3;
|
|
|
|
QtConcurrent::blockingMap(numberList, &Number::multiplyBy2);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-09-24 09:07:32 +00:00
|
|
|
void tst_QtConcurrentMap::mapOnRvalue()
|
|
|
|
{
|
|
|
|
struct ListRange
|
|
|
|
{
|
|
|
|
using iterator = QList<int>::iterator;
|
|
|
|
ListRange(iterator b, iterator e) : m_begin(b), m_end(e) { }
|
|
|
|
|
|
|
|
iterator begin() const { return m_begin; }
|
|
|
|
iterator end() const { return m_end; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
iterator m_begin;
|
|
|
|
iterator m_end;
|
|
|
|
};
|
|
|
|
|
|
|
|
const QList expected { 1, 4, 6, 4 };
|
|
|
|
{
|
|
|
|
QList list { 1, 2, 3, 4 };
|
|
|
|
auto future =
|
|
|
|
QtConcurrent::map(ListRange(list.begin() + 1, list.end() - 1), multiplyBy2InPlace);
|
|
|
|
future.waitForFinished();
|
|
|
|
QCOMPARE(list, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QList list { 1, 2, 3, 4 };
|
|
|
|
QThreadPool pool;
|
|
|
|
auto future = QtConcurrent::map(&pool, ListRange(list.begin() + 1, list.end() - 1),
|
|
|
|
multiplyBy2InPlace);
|
|
|
|
future.waitForFinished();
|
|
|
|
QCOMPARE(list, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QList list { 1, 2, 3, 4 };
|
|
|
|
QtConcurrent::blockingMap(ListRange(list.begin() + 1, list.end() - 1), multiplyBy2InPlace);
|
|
|
|
QCOMPARE(list, expected);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QList list { 1, 2, 3, 4 };
|
|
|
|
QThreadPool pool;
|
|
|
|
QtConcurrent::blockingMap(&pool, ListRange(list.begin() + 1, list.end() - 1),
|
|
|
|
multiplyBy2InPlace);
|
|
|
|
QCOMPARE(list, expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
int multiplyBy2(int x)
|
|
|
|
{
|
|
|
|
int y = x * 2;
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MultiplyBy2
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int operator()(int x) const
|
|
|
|
{
|
|
|
|
int y = x * 2;
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-09 16:21:17 +00:00
|
|
|
class MultiplyBy2MoveOnly
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MultiplyBy2MoveOnly() = default;
|
|
|
|
MultiplyBy2MoveOnly(MultiplyBy2MoveOnly &&) = default;
|
|
|
|
MultiplyBy2MoveOnly &operator=(MultiplyBy2MoveOnly &&other) = default;
|
|
|
|
|
|
|
|
MultiplyBy2MoveOnly(const MultiplyBy2MoveOnly &) = delete;
|
|
|
|
MultiplyBy2MoveOnly &operator=(const MultiplyBy2MoveOnly &) = delete;
|
|
|
|
|
|
|
|
int operator()(int x) const
|
|
|
|
{
|
|
|
|
int y = x * 2;
|
|
|
|
return y;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
double intToDouble(int x)
|
|
|
|
{
|
|
|
|
return double(x);
|
|
|
|
}
|
|
|
|
|
|
|
|
class IntToDouble
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
double operator()(int x) const
|
|
|
|
{
|
|
|
|
return double(x);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int stringToInt(const QString &string)
|
|
|
|
{
|
|
|
|
return string.toInt();
|
|
|
|
}
|
|
|
|
|
|
|
|
class StringToInt
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int operator()(const QString &string) const
|
|
|
|
{
|
|
|
|
return string.toInt();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
#define CHECK_FAIL(message) \
|
|
|
|
do {\
|
|
|
|
if (QTest::currentTestFailed())\
|
|
|
|
QFAIL("failed one line above on " message);\
|
|
|
|
} while (false)
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
template <typename SourceObject, typename ResultObject, typename MapObject>
|
|
|
|
void testMapped(const QList<SourceObject> &sourceObjectList, const QList<ResultObject> &expectedResult, MapObject mapObject)
|
|
|
|
{
|
|
|
|
const QList<ResultObject> result1 = QtConcurrent::mapped(
|
|
|
|
sourceObjectList, mapObject).results();
|
|
|
|
QCOMPARE(result1, expectedResult);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
const QList<ResultObject> result2 = QtConcurrent::mapped(
|
|
|
|
sourceObjectList.constBegin(), sourceObjectList.constEnd(), mapObject).results();
|
|
|
|
QCOMPARE(result2, expectedResult);
|
2020-04-17 10:49:47 +00:00
|
|
|
|
|
|
|
const QList<ResultObject> result3 = QtConcurrent::blockingMapped(
|
|
|
|
sourceObjectList, mapObject);
|
|
|
|
QCOMPARE(result3, expectedResult);
|
|
|
|
|
|
|
|
const QList<ResultObject> result4 = QtConcurrent::blockingMapped<QList<ResultObject>>(
|
|
|
|
sourceObjectList.constBegin(), sourceObjectList.constEnd(), mapObject);
|
|
|
|
QCOMPARE(result4, expectedResult);
|
2020-04-14 06:14:08 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
void tst_QtConcurrentMap::mapped()
|
|
|
|
{
|
|
|
|
const QList<int> intList {1, 2, 3};
|
|
|
|
const QList<Number> numberList {1, 2, 3};
|
|
|
|
const QList<QString> stringList {"1", "2", "3"};
|
|
|
|
const QList<double> doubleList {1.0, 2.0, 3.0};
|
|
|
|
|
|
|
|
const QList<int> intListMultipiedBy2 {2, 4, 6};
|
|
|
|
const QList<Number> numberListMultipiedBy2 {2, 4, 6};
|
|
|
|
|
|
|
|
auto lambdaMultiplyBy2 = [](int x) {
|
|
|
|
return 2 * x;
|
|
|
|
};
|
|
|
|
auto lambdaIntToDouble = [](int x) {
|
|
|
|
return double(x);
|
|
|
|
};
|
|
|
|
auto lambdaStringToInt = [](const QString &string) {
|
|
|
|
return string.toInt();
|
|
|
|
};
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
testMapped(intList, intListMultipiedBy2, MultiplyBy2());
|
|
|
|
CHECK_FAIL("functor");
|
|
|
|
testMapped(intList, intListMultipiedBy2, multiplyBy2);
|
|
|
|
CHECK_FAIL("function");
|
|
|
|
testMapped(numberList, numberListMultipiedBy2, &Number::multipliedBy2);
|
|
|
|
CHECK_FAIL("member");
|
|
|
|
testMapped(intList, intListMultipiedBy2, lambdaMultiplyBy2);
|
|
|
|
CHECK_FAIL("lambda");
|
2020-03-26 14:47:04 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
// change the value_type, same container
|
2020-04-14 06:14:08 +00:00
|
|
|
testMapped(intList, doubleList, IntToDouble());
|
|
|
|
CHECK_FAIL("functor");
|
|
|
|
testMapped(intList, doubleList, intToDouble);
|
|
|
|
CHECK_FAIL("function");
|
|
|
|
testMapped(numberList, stringList, &Number::toString);
|
|
|
|
CHECK_FAIL("member");
|
|
|
|
testMapped(intList, doubleList, lambdaIntToDouble);
|
|
|
|
CHECK_FAIL("lambda");
|
2020-03-26 14:47:04 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
// change the value_type
|
2020-04-14 06:14:08 +00:00
|
|
|
testMapped(stringList, intList, StringToInt());
|
|
|
|
CHECK_FAIL("functor");
|
|
|
|
testMapped(stringList, intList, stringToInt);
|
|
|
|
CHECK_FAIL("function");
|
|
|
|
testMapped(numberList, intList, &Number::toInt);
|
|
|
|
CHECK_FAIL("member");
|
|
|
|
testMapped(stringList, intList, lambdaStringToInt);
|
|
|
|
CHECK_FAIL("lambda");
|
|
|
|
|
|
|
|
// not allowed: const member function where all arguments have default values
|
|
|
|
#if 0
|
|
|
|
testMapped(stringList, intList, &QString::toInt);
|
|
|
|
CHECK_FAIL("member");
|
|
|
|
#endif
|
2020-09-24 08:48:33 +00:00
|
|
|
|
2020-09-29 08:30:59 +00:00
|
|
|
{
|
|
|
|
// rvalue sequences
|
|
|
|
auto future = QtConcurrent::mapped(std::vector { 1, 2, 3 }, multiplyBy2);
|
|
|
|
QCOMPARE(future.results(), QList<int>({ 2, 4, 6 }));
|
2020-09-24 08:48:33 +00:00
|
|
|
|
2020-10-27 16:06:26 +00:00
|
|
|
auto result = QtConcurrent::blockingMapped(std::vector { 1, 2, 3 }, multiplyBy2);
|
2020-09-29 08:30:59 +00:00
|
|
|
QCOMPARE(result, std::vector<int>({ 2, 4, 6 }));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// move only sequences
|
2020-10-08 22:02:00 +00:00
|
|
|
auto future = QtConcurrent::mapped(MoveOnlyVector<int>({ 1, 2, 3 }), multiplyBy2);
|
2020-09-29 08:30:59 +00:00
|
|
|
QCOMPARE(future.results(), QList<int>({ 2, 4, 6 }));
|
|
|
|
|
2020-10-08 22:02:00 +00:00
|
|
|
auto result = QtConcurrent::blockingMapped<std::vector<int>>(
|
|
|
|
MoveOnlyVector<int>({ 1, 2, 3 }), multiplyBy2);
|
2020-09-29 08:30:59 +00:00
|
|
|
QCOMPARE(result, std::vector<int>({ 2, 4, 6 }));
|
|
|
|
}
|
2020-04-14 06:14:08 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-04-28 12:19:42 +00:00
|
|
|
static QSemaphore semaphore(1);
|
|
|
|
static QSet<QThread *> workingThreads;
|
|
|
|
|
|
|
|
void storeCurrentThread()
|
|
|
|
{
|
|
|
|
semaphore.acquire();
|
|
|
|
workingThreads.insert(QThread::currentThread());
|
|
|
|
semaphore.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
int threadCount()
|
|
|
|
{
|
|
|
|
semaphore.acquire();
|
|
|
|
const int count = workingThreads.size();
|
|
|
|
semaphore.release();
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename SourceObject, typename ResultObject, typename MapObject>
|
|
|
|
void testMappedThreadPool(QThreadPool *pool,
|
|
|
|
const QList<SourceObject> &sourceObjectList,
|
|
|
|
const QList<ResultObject> &expectedResult,
|
|
|
|
MapObject mapObject)
|
|
|
|
{
|
|
|
|
const QList<ResultObject> result1 = QtConcurrent::mapped(pool,
|
|
|
|
sourceObjectList, mapObject).results();
|
|
|
|
QCOMPARE(result1, expectedResult);
|
|
|
|
QCOMPARE(threadCount(), 1); // ensure the only one thread was working
|
|
|
|
|
|
|
|
const QList<ResultObject> result2 = QtConcurrent::mapped(pool,
|
|
|
|
sourceObjectList.constBegin(), sourceObjectList.constEnd(), mapObject).results();
|
|
|
|
QCOMPARE(result2, expectedResult);
|
|
|
|
QCOMPARE(threadCount(), 1); // ensure the only one thread was working
|
|
|
|
|
2020-05-14 16:46:15 +00:00
|
|
|
const QList<ResultObject> result3 = QtConcurrent::blockingMapped(pool,
|
|
|
|
sourceObjectList, mapObject);
|
|
|
|
QCOMPARE(result3, expectedResult);
|
|
|
|
QCOMPARE(threadCount(), 1); // ensure the only one thread was working
|
2020-04-28 12:19:42 +00:00
|
|
|
|
2020-05-14 16:46:15 +00:00
|
|
|
const QList<ResultObject> result4 = QtConcurrent::blockingMapped<QList<ResultObject>>(pool,
|
|
|
|
sourceObjectList.constBegin(), sourceObjectList.constEnd(), mapObject);
|
|
|
|
QCOMPARE(result4, expectedResult);
|
|
|
|
QCOMPARE(threadCount(), 1); // ensure the only one thread was working
|
2020-04-28 12:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int multiplyBy3(int x)
|
|
|
|
{
|
|
|
|
storeCurrentThread();
|
|
|
|
return x * 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
class MultiplyBy3
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int operator()(int x) const
|
|
|
|
{
|
|
|
|
storeCurrentThread();
|
|
|
|
return x * 3;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QtConcurrentMap::mappedThreadPool()
|
|
|
|
{
|
|
|
|
const QList<int> intList {1, 2, 3};
|
|
|
|
const QList<int> intListMultipiedBy3 {3, 6, 9};
|
|
|
|
|
|
|
|
auto lambdaMultiplyBy3 = [](int x) {
|
|
|
|
storeCurrentThread();
|
|
|
|
return x * 3;
|
|
|
|
};
|
|
|
|
|
|
|
|
QThreadPool pool;
|
|
|
|
pool.setMaxThreadCount(1);
|
|
|
|
QCOMPARE(semaphore.available(), 1);
|
|
|
|
workingThreads.clear();
|
|
|
|
|
|
|
|
testMappedThreadPool(&pool, intList, intListMultipiedBy3, MultiplyBy3());
|
|
|
|
CHECK_FAIL("functor");
|
|
|
|
testMappedThreadPool(&pool, intList, intListMultipiedBy3, multiplyBy3);
|
|
|
|
CHECK_FAIL("function");
|
|
|
|
testMappedThreadPool(&pool, intList, intListMultipiedBy3, lambdaMultiplyBy3);
|
|
|
|
CHECK_FAIL("lambda");
|
2020-09-24 08:48:33 +00:00
|
|
|
|
2020-09-29 08:30:59 +00:00
|
|
|
{
|
|
|
|
// rvalue sequences
|
|
|
|
auto future = QtConcurrent::mapped(&pool, std::vector { 1, 2, 3 }, multiplyBy2);
|
|
|
|
QCOMPARE(future.results(), QList<int>({ 2, 4, 6 }));
|
2020-09-24 08:48:33 +00:00
|
|
|
|
2020-09-29 08:30:59 +00:00
|
|
|
auto result = QtConcurrent::blockingMapped<std::vector<int>>(&pool, std::vector { 1, 2, 3 },
|
|
|
|
multiplyBy2);
|
|
|
|
QCOMPARE(result, std::vector<int>({ 2, 4, 6 }));
|
|
|
|
}
|
|
|
|
{
|
|
|
|
// move only sequences
|
2020-10-08 22:02:00 +00:00
|
|
|
auto future = QtConcurrent::mapped(&pool, MoveOnlyVector<int>({ 1, 2, 3 }), multiplyBy2);
|
2020-09-29 08:30:59 +00:00
|
|
|
QCOMPARE(future.results(), QList<int>({ 2, 4, 6 }));
|
|
|
|
|
|
|
|
auto result = QtConcurrent::blockingMapped<std::vector<int>>(
|
2020-10-08 22:02:00 +00:00
|
|
|
&pool, MoveOnlyVector<int>({ 1, 2, 3 }), multiplyBy2);
|
2020-09-29 08:30:59 +00:00
|
|
|
QCOMPARE(result, std::vector<int>({ 2, 4, 6 }));
|
|
|
|
}
|
2020-04-28 12:19:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 16:21:17 +00:00
|
|
|
void tst_QtConcurrentMap::mappedWithMoveOnlyCallable()
|
|
|
|
{
|
|
|
|
const QList<int> intList { 1, 2, 3 };
|
|
|
|
const QList<int> intListMultipiedBy2 { 2, 4, 6 };
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::mapped(intList, MultiplyBy2()).results();
|
|
|
|
QCOMPARE(result, intListMultipiedBy2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result =
|
|
|
|
QtConcurrent::mapped(intList.begin(), intList.end(), MultiplyBy2()).results();
|
|
|
|
QCOMPARE(result, intListMultipiedBy2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::blockingMapped(intList, MultiplyBy2());
|
|
|
|
QCOMPARE(result, intListMultipiedBy2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::blockingMapped<QList<int>>(intList.begin(), intList.end(),
|
|
|
|
MultiplyBy2());
|
|
|
|
QCOMPARE(result, intListMultipiedBy2);
|
|
|
|
}
|
|
|
|
|
|
|
|
QThreadPool pool;
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::mapped(&pool, intList, MultiplyBy2()).results();
|
|
|
|
QCOMPARE(result, intListMultipiedBy2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::mapped(
|
|
|
|
&pool, intList.begin(), intList.end(), MultiplyBy2()).results();
|
|
|
|
QCOMPARE(result, intListMultipiedBy2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::blockingMapped(&pool, intList, MultiplyBy2());
|
|
|
|
QCOMPARE(result, intListMultipiedBy2);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::blockingMapped<QList<int>>(&pool, intList.begin(),
|
|
|
|
intList.end(), MultiplyBy2());
|
|
|
|
QCOMPARE(result, intListMultipiedBy2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
int intSquare(int x)
|
|
|
|
{
|
|
|
|
return x * x;
|
|
|
|
}
|
|
|
|
|
|
|
|
class IntSquare
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int operator()(int x)
|
|
|
|
{
|
|
|
|
return x * x;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-17 09:54:40 +00:00
|
|
|
Number numberSquare(Number x)
|
|
|
|
{
|
|
|
|
return Number(x.toInt() * x.toInt());
|
|
|
|
}
|
|
|
|
|
|
|
|
class NumberSquare
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Number operator()(Number x)
|
|
|
|
{
|
|
|
|
return Number(x.toInt() * x.toInt());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
template <typename SourceObject, typename ResultObject, typename MapObject, typename ReduceObject>
|
|
|
|
void testMappedReduced(const QList<SourceObject> &sourceObjectList, const ResultObject &expectedResult, MapObject mapObject, ReduceObject reduceObject)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2020-04-14 06:14:08 +00:00
|
|
|
const ResultObject result1 = QtConcurrent::mappedReduced<ResultObject>(
|
2020-10-30 10:21:01 +00:00
|
|
|
sourceObjectList, mapObject, reduceObject).result();
|
2020-04-14 06:14:08 +00:00
|
|
|
QCOMPARE(result1, expectedResult);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
const ResultObject result2 = QtConcurrent::mappedReduced<ResultObject>(
|
2020-10-30 10:21:01 +00:00
|
|
|
sourceObjectList.constBegin(), sourceObjectList.constEnd(),
|
|
|
|
mapObject, reduceObject).result();
|
2020-04-14 06:14:08 +00:00
|
|
|
QCOMPARE(result2, expectedResult);
|
2020-04-17 10:49:47 +00:00
|
|
|
|
|
|
|
const ResultObject result3 = QtConcurrent::blockingMappedReduced<ResultObject>(
|
|
|
|
sourceObjectList, mapObject, reduceObject);
|
|
|
|
QCOMPARE(result3, expectedResult);
|
|
|
|
|
|
|
|
const ResultObject result4 = QtConcurrent::blockingMappedReduced<ResultObject>(
|
|
|
|
sourceObjectList.constBegin(), sourceObjectList.constEnd(), mapObject, reduceObject);
|
|
|
|
QCOMPARE(result4, expectedResult);
|
2020-04-14 06:14:08 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
template <typename SourceObject, typename ResultObject, typename MapObject, typename ReduceObject>
|
|
|
|
void testMappedReduced(const QList<SourceObject> &sourceObjectList, const ResultObject &expectedResult, MapObject mapObject, ReduceObject reduceObject, QtConcurrent::ReduceOptions options)
|
|
|
|
{
|
|
|
|
const ResultObject result1 = QtConcurrent::mappedReduced(
|
2020-10-30 10:21:01 +00:00
|
|
|
sourceObjectList, mapObject, reduceObject, options).result();
|
2020-04-14 06:14:08 +00:00
|
|
|
QCOMPARE(result1, expectedResult);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-10-30 10:21:01 +00:00
|
|
|
const ResultObject result2 =
|
|
|
|
QtConcurrent::mappedReduced(sourceObjectList.constBegin(), sourceObjectList.constEnd(),
|
|
|
|
mapObject, reduceObject, options).result();
|
2020-04-14 06:14:08 +00:00
|
|
|
QCOMPARE(result2, expectedResult);
|
2020-04-17 10:49:47 +00:00
|
|
|
|
|
|
|
const ResultObject result3 = QtConcurrent::blockingMappedReduced(
|
|
|
|
sourceObjectList, mapObject, reduceObject, options);
|
|
|
|
QCOMPARE(result3, expectedResult);
|
|
|
|
|
|
|
|
const ResultObject result4 = QtConcurrent::blockingMappedReduced(
|
|
|
|
sourceObjectList.constBegin(), sourceObjectList.constEnd(), mapObject, reduceObject, options);
|
|
|
|
QCOMPARE(result4, expectedResult);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
void tst_QtConcurrentMap::mappedReduced()
|
2020-03-26 14:47:04 +00:00
|
|
|
{
|
2020-04-14 06:14:08 +00:00
|
|
|
const QList<int> intList {1, 2, 3};
|
|
|
|
const QList<int> intListOfSquares {1, 4, 9};
|
|
|
|
const QList<Number> numberList {1, 2, 3};
|
|
|
|
const int sum = 6;
|
|
|
|
const int sumOfSquares = 14;
|
2020-03-26 14:47:04 +00:00
|
|
|
|
2020-10-12 14:29:27 +00:00
|
|
|
void (QList<int>::*push_back)(QList<int>::parameter_type) = &QList<int>::push_back;
|
2020-03-26 14:47:04 +00:00
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
auto lambdaSquare = [](int x) {
|
|
|
|
return x * x;
|
|
|
|
};
|
|
|
|
auto lambdaSumReduce = [](int &sum, int x) {
|
|
|
|
sum += x;
|
|
|
|
};
|
|
|
|
|
|
|
|
// FUNCTOR-other
|
|
|
|
testMappedReduced(intList, sumOfSquares, IntSquare(), IntSumReduce());
|
|
|
|
CHECK_FAIL("functor-functor");
|
|
|
|
testMappedReduced(intList, sumOfSquares, IntSquare(), intSumReduce);
|
|
|
|
CHECK_FAIL("functor-function");
|
|
|
|
testMappedReduced(intList, intListOfSquares, IntSquare(), push_back, OrderedReduce);
|
|
|
|
CHECK_FAIL("functor-member");
|
|
|
|
testMappedReduced(intList, sumOfSquares, IntSquare(), lambdaSumReduce);
|
|
|
|
CHECK_FAIL("functor-lambda");
|
|
|
|
|
|
|
|
// FUNCTION-other
|
|
|
|
testMappedReduced(intList, sumOfSquares, intSquare, IntSumReduce());
|
|
|
|
CHECK_FAIL("function-functor");
|
|
|
|
testMappedReduced(intList, sumOfSquares, intSquare, intSumReduce);
|
|
|
|
CHECK_FAIL("function-function");
|
|
|
|
testMappedReduced(intList, intListOfSquares, intSquare, push_back, OrderedReduce);
|
|
|
|
CHECK_FAIL("function-member");
|
|
|
|
testMappedReduced(intList, sumOfSquares, intSquare, lambdaSumReduce);
|
|
|
|
CHECK_FAIL("function-lambda");
|
|
|
|
|
|
|
|
// MEMBER-other
|
|
|
|
testMappedReduced(numberList, sum, &Number::toInt, IntSumReduce());
|
|
|
|
CHECK_FAIL("member-functor");
|
|
|
|
testMappedReduced(numberList, sum, &Number::toInt, intSumReduce);
|
|
|
|
CHECK_FAIL("member-function");
|
|
|
|
testMappedReduced(numberList, intList, &Number::toInt, push_back, OrderedReduce);
|
|
|
|
CHECK_FAIL("member-member");
|
|
|
|
testMappedReduced(numberList, sum, &Number::toInt, lambdaSumReduce);
|
|
|
|
CHECK_FAIL("member-lambda");
|
|
|
|
|
|
|
|
// LAMBDA-other
|
|
|
|
testMappedReduced(intList, sumOfSquares, lambdaSquare, IntSumReduce());
|
|
|
|
CHECK_FAIL("lambda-functor");
|
|
|
|
testMappedReduced(intList, sumOfSquares, lambdaSquare, intSumReduce);
|
|
|
|
CHECK_FAIL("lambda-function");
|
|
|
|
testMappedReduced(intList, intListOfSquares, lambdaSquare, push_back, OrderedReduce);
|
|
|
|
CHECK_FAIL("lambda-member");
|
|
|
|
testMappedReduced(intList, sumOfSquares, lambdaSquare, lambdaSumReduce);
|
|
|
|
CHECK_FAIL("lambda-lambda");
|
2020-09-24 08:48:33 +00:00
|
|
|
|
2020-09-29 08:30:59 +00:00
|
|
|
{
|
|
|
|
// rvalue sequences
|
|
|
|
auto future = QtConcurrent::mappedReduced(std::vector { 1, 2, 3 }, intSquare, intSumReduce);
|
2020-10-29 12:33:22 +00:00
|
|
|
QCOMPARE(future.result(), sumOfSquares);
|
2020-09-29 08:30:59 +00:00
|
|
|
|
|
|
|
auto result = QtConcurrent::blockingMappedReduced(std::vector { 1, 2, 3 }, intSquare,
|
|
|
|
intSumReduce);
|
|
|
|
QCOMPARE(result, sumOfSquares);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// move only sequences
|
|
|
|
auto future =
|
2020-10-08 22:02:00 +00:00
|
|
|
QtConcurrent::mappedReduced(MoveOnlyVector<int>({ 1, 2, 3 }), intSquare, intSumReduce);
|
2020-10-29 12:33:22 +00:00
|
|
|
QCOMPARE(future.result(), sumOfSquares);
|
2020-09-24 08:48:33 +00:00
|
|
|
|
2020-10-08 22:02:00 +00:00
|
|
|
auto result = QtConcurrent::blockingMappedReduced(MoveOnlyVector<int>({ 1, 2, 3 }),
|
|
|
|
intSquare, intSumReduce);
|
2020-09-29 08:30:59 +00:00
|
|
|
QCOMPARE(result, sumOfSquares);
|
|
|
|
}
|
2020-04-14 06:14:08 +00:00
|
|
|
}
|
2020-03-26 14:47:04 +00:00
|
|
|
|
2020-04-28 12:19:42 +00:00
|
|
|
template <typename SourceObject, typename ResultObject, typename MapObject, typename ReduceObject>
|
|
|
|
void testMappedReducedThreadPool(QThreadPool *pool,
|
|
|
|
const QList<SourceObject> &sourceObjectList,
|
|
|
|
const ResultObject &expectedResult,
|
|
|
|
MapObject mapObject,
|
|
|
|
ReduceObject reduceObject)
|
|
|
|
{
|
2020-10-30 10:21:01 +00:00
|
|
|
const ResultObject result1 = QtConcurrent::mappedReduced<ResultObject>(
|
|
|
|
pool, sourceObjectList, mapObject, reduceObject).result();
|
2020-04-28 12:19:42 +00:00
|
|
|
QCOMPARE(result1, expectedResult);
|
|
|
|
QCOMPARE(threadCount(), 1); // ensure the only one thread was working
|
|
|
|
|
2020-10-30 10:21:01 +00:00
|
|
|
const ResultObject result2 =
|
|
|
|
QtConcurrent::mappedReduced<ResultObject>(pool, sourceObjectList.constBegin(),
|
|
|
|
sourceObjectList.constEnd(), mapObject,
|
|
|
|
reduceObject).result();
|
2020-04-28 12:19:42 +00:00
|
|
|
QCOMPARE(result2, expectedResult);
|
|
|
|
QCOMPARE(threadCount(), 1); // ensure the only one thread was working
|
|
|
|
|
2020-05-14 16:46:15 +00:00
|
|
|
const ResultObject result3 = QtConcurrent::blockingMappedReduced<ResultObject>(pool,
|
|
|
|
sourceObjectList, mapObject, reduceObject);
|
|
|
|
QCOMPARE(result3, expectedResult);
|
|
|
|
QCOMPARE(threadCount(), 1); // ensure the only one thread was working
|
2020-04-28 12:19:42 +00:00
|
|
|
|
2020-05-14 16:46:15 +00:00
|
|
|
const ResultObject result4 = QtConcurrent::blockingMappedReduced<ResultObject>(pool,
|
|
|
|
sourceObjectList.constBegin(), sourceObjectList.constEnd(), mapObject, reduceObject);
|
|
|
|
QCOMPARE(result4, expectedResult);
|
|
|
|
QCOMPARE(threadCount(), 1); // ensure the only one thread was working
|
2020-04-28 12:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int intCube(int x)
|
|
|
|
{
|
|
|
|
storeCurrentThread();
|
|
|
|
return x * x * x;
|
|
|
|
}
|
|
|
|
|
|
|
|
class IntCube
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int operator()(int x)
|
|
|
|
{
|
|
|
|
storeCurrentThread();
|
|
|
|
return x * x * x;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void tst_QtConcurrentMap::mappedReducedThreadPool()
|
|
|
|
{
|
|
|
|
const QList<int> intList {1, 2, 3};
|
|
|
|
const int sumOfCubes = 36;
|
|
|
|
|
|
|
|
auto lambdaCube = [](int x) {
|
|
|
|
return x * x * x;
|
|
|
|
};
|
|
|
|
auto lambdaSumReduce = [](int &sum, int x) {
|
|
|
|
sum += x;
|
|
|
|
};
|
|
|
|
|
|
|
|
QThreadPool pool;
|
|
|
|
pool.setMaxThreadCount(1);
|
|
|
|
QCOMPARE(semaphore.available(), 1);
|
|
|
|
workingThreads.clear();
|
|
|
|
|
|
|
|
// FUNCTOR-other
|
|
|
|
testMappedReducedThreadPool(&pool, intList, sumOfCubes, IntCube(), IntSumReduce());
|
|
|
|
CHECK_FAIL("functor-functor");
|
|
|
|
testMappedReducedThreadPool(&pool, intList, sumOfCubes, IntCube(), intSumReduce);
|
|
|
|
CHECK_FAIL("functor-function");
|
|
|
|
testMappedReducedThreadPool(&pool, intList, sumOfCubes, IntCube(), lambdaSumReduce);
|
|
|
|
CHECK_FAIL("functor-lambda");
|
|
|
|
|
|
|
|
// FUNCTION-other
|
|
|
|
testMappedReducedThreadPool(&pool, intList, sumOfCubes, intCube, IntSumReduce());
|
|
|
|
CHECK_FAIL("function-functor");
|
|
|
|
testMappedReducedThreadPool(&pool, intList, sumOfCubes, intCube, intSumReduce);
|
|
|
|
CHECK_FAIL("function-function");
|
|
|
|
testMappedReducedThreadPool(&pool, intList, sumOfCubes, intCube, lambdaSumReduce);
|
|
|
|
CHECK_FAIL("function-lambda");
|
|
|
|
|
|
|
|
// LAMBDA-other
|
|
|
|
testMappedReducedThreadPool(&pool, intList, sumOfCubes, lambdaCube, IntSumReduce());
|
|
|
|
CHECK_FAIL("lambda-functor");
|
|
|
|
testMappedReducedThreadPool(&pool, intList, sumOfCubes, lambdaCube, intSumReduce);
|
|
|
|
CHECK_FAIL("lambda-function");
|
|
|
|
testMappedReducedThreadPool(&pool, intList, sumOfCubes, lambdaCube, lambdaSumReduce);
|
|
|
|
CHECK_FAIL("lambda-lambda");
|
2020-09-24 08:48:33 +00:00
|
|
|
|
2020-09-29 08:30:59 +00:00
|
|
|
{
|
|
|
|
// rvalue sequences
|
|
|
|
auto future =
|
|
|
|
QtConcurrent::mappedReduced(&pool, std::vector { 1, 2, 3 }, intCube, intSumReduce);
|
2020-10-29 12:33:22 +00:00
|
|
|
QCOMPARE(future.result(), sumOfCubes);
|
2020-09-29 08:30:59 +00:00
|
|
|
|
|
|
|
auto result = QtConcurrent::blockingMappedReduced(&pool, std::vector { 1, 2, 3 }, intCube,
|
|
|
|
intSumReduce);
|
|
|
|
QCOMPARE(result, sumOfCubes);
|
|
|
|
}
|
2020-09-24 08:48:33 +00:00
|
|
|
|
2020-09-29 08:30:59 +00:00
|
|
|
{
|
|
|
|
// move only sequences
|
2020-10-08 22:02:00 +00:00
|
|
|
auto future = QtConcurrent::mappedReduced(&pool, MoveOnlyVector<int>({ 1, 2, 3 }), intCube,
|
2020-09-29 08:30:59 +00:00
|
|
|
intSumReduce);
|
2020-10-29 12:33:22 +00:00
|
|
|
QCOMPARE(future.result(), sumOfCubes);
|
2020-09-29 08:30:59 +00:00
|
|
|
|
2020-10-08 22:02:00 +00:00
|
|
|
auto result = QtConcurrent::blockingMappedReduced(&pool, MoveOnlyVector<int>({ 1, 2, 3 }),
|
2020-09-29 08:30:59 +00:00
|
|
|
intCube, intSumReduce);
|
|
|
|
QCOMPARE(result, sumOfCubes);
|
|
|
|
}
|
2020-04-28 12:19:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 16:21:17 +00:00
|
|
|
void tst_QtConcurrentMap::mappedReducedWithMoveOnlyCallable()
|
|
|
|
{
|
|
|
|
const QList<int> intList { 1, 2, 3 };
|
|
|
|
const auto sum = 12;
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::mappedReduced<int>(
|
|
|
|
intList, MultiplyBy2(), IntSumReduceMoveOnly()).result();
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result =
|
|
|
|
QtConcurrent::mappedReduced<int>(intList.begin(), intList.end(),
|
|
|
|
MultiplyBy2(), IntSumReduceMoveOnly()).result();
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::blockingMappedReduced<int>(intList, MultiplyBy2(),
|
|
|
|
IntSumReduceMoveOnly());
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::blockingMappedReduced<int>(
|
|
|
|
intList.begin(), intList.end(), MultiplyBy2(), IntSumReduceMoveOnly());
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
|
|
|
|
QThreadPool pool;
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::mappedReduced<int>(&pool, intList, MultiplyBy2(),
|
|
|
|
IntSumReduceMoveOnly()).result();
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result =
|
|
|
|
QtConcurrent::mappedReduced<int>(&pool, intList.begin(), intList.end(),
|
|
|
|
MultiplyBy2(), IntSumReduceMoveOnly()).result();
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::blockingMappedReduced<int>(&pool, intList, MultiplyBy2(),
|
|
|
|
IntSumReduceMoveOnly());
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::blockingMappedReduced<int>(
|
|
|
|
&pool, intList.begin(), intList.end(), MultiplyBy2(), IntSumReduceMoveOnly());
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 09:54:40 +00:00
|
|
|
void tst_QtConcurrentMap::mappedReducedDifferentType()
|
|
|
|
{
|
|
|
|
const QList<int> intList {1, 2, 3};
|
|
|
|
const QList<Number> numberList {1, 2, 3};
|
|
|
|
const int sumOfSquares = 14;
|
|
|
|
|
|
|
|
auto lambdaSquare = [](Number x) {
|
|
|
|
return Number(x.toInt() * x.toInt());
|
|
|
|
};
|
|
|
|
auto lambdaSumReduce = [](int &sum, Number x) {
|
|
|
|
sum += x.toInt();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Test the case where reduce function of the form:
|
|
|
|
// V function(T &result, const U &intermediate)
|
|
|
|
// has T and U types different.
|
|
|
|
|
|
|
|
// FUNCTOR-other
|
|
|
|
testMappedReduced(intList, sumOfSquares, NumberSquare(), NumberSumReduce());
|
|
|
|
CHECK_FAIL("functor-functor");
|
|
|
|
testMappedReduced(intList, sumOfSquares, NumberSquare(), numberSumReduce);
|
|
|
|
CHECK_FAIL("functor-function");
|
|
|
|
testMappedReduced(intList, sumOfSquares, NumberSquare(), lambdaSumReduce);
|
|
|
|
CHECK_FAIL("functor-lambda");
|
|
|
|
|
|
|
|
// FUNCTION-other
|
|
|
|
testMappedReduced(intList, sumOfSquares, numberSquare, NumberSumReduce());
|
|
|
|
CHECK_FAIL("function-functor");
|
|
|
|
testMappedReduced(intList, sumOfSquares, numberSquare, numberSumReduce);
|
|
|
|
CHECK_FAIL("function-function");
|
|
|
|
testMappedReduced(intList, sumOfSquares, numberSquare, lambdaSumReduce);
|
|
|
|
CHECK_FAIL("function-lambda");
|
|
|
|
|
|
|
|
// MEMBER-other
|
|
|
|
testMappedReduced(numberList, sumOfSquares, &Number::squared, NumberSumReduce());
|
|
|
|
CHECK_FAIL("member-functor");
|
|
|
|
testMappedReduced(numberList, sumOfSquares, &Number::squared, numberSumReduce);
|
|
|
|
CHECK_FAIL("member-function");
|
|
|
|
testMappedReduced(numberList, sumOfSquares, &Number::squared, lambdaSumReduce);
|
|
|
|
CHECK_FAIL("member-lambda");
|
|
|
|
|
|
|
|
// LAMBDA-other
|
|
|
|
testMappedReduced(intList, sumOfSquares, lambdaSquare, NumberSumReduce());
|
|
|
|
CHECK_FAIL("lambda-functor");
|
|
|
|
testMappedReduced(intList, sumOfSquares, lambdaSquare, numberSumReduce);
|
|
|
|
CHECK_FAIL("lambda-function");
|
|
|
|
testMappedReduced(intList, sumOfSquares, lambdaSquare, lambdaSumReduce);
|
|
|
|
CHECK_FAIL("lambda-lambda");
|
|
|
|
}
|
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
template <typename SourceObject, typename ResultObject, typename InitialObject, typename MapObject, typename ReduceObject>
|
|
|
|
void testMappedReducedInitialValue(const QList<SourceObject> &sourceObjectList,
|
|
|
|
const ResultObject &expectedResult,
|
|
|
|
MapObject mapObject,
|
|
|
|
ReduceObject reduceObject,
|
|
|
|
InitialObject &&initialObject)
|
|
|
|
{
|
2020-10-30 10:21:01 +00:00
|
|
|
const ResultObject result1 =
|
|
|
|
QtConcurrent::mappedReduced<ResultObject>(sourceObjectList, mapObject, reduceObject,
|
|
|
|
initialObject).result();
|
2020-04-14 06:14:08 +00:00
|
|
|
QCOMPARE(result1, expectedResult);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
const ResultObject result2 = QtConcurrent::mappedReduced<ResultObject>(
|
2020-10-30 10:21:01 +00:00
|
|
|
sourceObjectList.constBegin(), sourceObjectList.constEnd(),
|
|
|
|
mapObject, reduceObject, initialObject).result();
|
2020-04-14 06:14:08 +00:00
|
|
|
QCOMPARE(result2, expectedResult);
|
2020-04-17 10:49:47 +00:00
|
|
|
|
|
|
|
const ResultObject result3 = QtConcurrent::blockingMappedReduced<ResultObject>(
|
|
|
|
sourceObjectList, mapObject, reduceObject, initialObject);
|
|
|
|
QCOMPARE(result3, expectedResult);
|
|
|
|
|
|
|
|
const ResultObject result4 = QtConcurrent::blockingMappedReduced<ResultObject>(
|
|
|
|
sourceObjectList.constBegin(), sourceObjectList.constEnd(), mapObject, reduceObject, initialObject);
|
|
|
|
QCOMPARE(result4, expectedResult);
|
2020-04-14 06:14:08 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
template <typename SourceObject, typename ResultObject, typename InitialObject, typename MapObject, typename ReduceObject>
|
|
|
|
void testMappedReducedInitialValue(const QList<SourceObject> &sourceObjectList,
|
|
|
|
const ResultObject &expectedResult,
|
|
|
|
MapObject mapObject,
|
|
|
|
ReduceObject reduceObject,
|
|
|
|
InitialObject &&initialObject,
|
|
|
|
QtConcurrent::ReduceOptions options)
|
|
|
|
{
|
|
|
|
const ResultObject result1 = QtConcurrent::mappedReduced(
|
2020-10-30 10:21:01 +00:00
|
|
|
sourceObjectList, mapObject, reduceObject, initialObject, options).result();
|
2020-04-14 06:14:08 +00:00
|
|
|
QCOMPARE(result1, expectedResult);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-10-30 10:21:01 +00:00
|
|
|
const ResultObject result2 =
|
|
|
|
QtConcurrent::mappedReduced(sourceObjectList.constBegin(), sourceObjectList.constEnd(),
|
|
|
|
mapObject, reduceObject, initialObject, options).result();
|
2020-04-14 06:14:08 +00:00
|
|
|
QCOMPARE(result2, expectedResult);
|
2020-04-17 10:49:47 +00:00
|
|
|
|
|
|
|
const ResultObject result3 = QtConcurrent::blockingMappedReduced(
|
|
|
|
sourceObjectList, mapObject, reduceObject, initialObject, options);
|
|
|
|
QCOMPARE(result3, expectedResult);
|
|
|
|
|
|
|
|
const ResultObject result4 = QtConcurrent::blockingMappedReduced(
|
|
|
|
sourceObjectList.constBegin(), sourceObjectList.constEnd(), mapObject, reduceObject, initialObject, options);
|
|
|
|
QCOMPARE(result4, expectedResult);
|
2020-04-14 06:14:08 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
void tst_QtConcurrentMap::mappedReducedInitialValue()
|
|
|
|
{
|
|
|
|
// This is a copy of tst_QtConcurrentMap::mappedReduced with the initial value parameter added
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
const QList<int> intList {1, 2, 3};
|
|
|
|
const QList<int> intListInitial {10};
|
|
|
|
const QList<int> intListAppended {10, 1, 2, 3};
|
|
|
|
const QList<int> intListSquaresAppended {10, 1, 4, 9};
|
|
|
|
const QList<Number> numberList {1, 2, 3};
|
|
|
|
const int sum = 16;
|
|
|
|
const int sumOfSquares = 24;
|
|
|
|
const int intInitial = 10;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-10-12 14:29:27 +00:00
|
|
|
void (QList<int>::*push_back)(QList<int>::parameter_type) = &QList<int>::push_back;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2020-04-14 06:14:08 +00:00
|
|
|
auto lambdaSquare = [](int x) {
|
|
|
|
return x * x;
|
|
|
|
};
|
|
|
|
auto lambdaSumReduce = [](int &sum, int x) {
|
|
|
|
sum += x;
|
|
|
|
};
|
|
|
|
|
|
|
|
// FUNCTOR-other
|
|
|
|
testMappedReducedInitialValue(intList, sumOfSquares, IntSquare(), IntSumReduce(), intInitial);
|
|
|
|
CHECK_FAIL("functor-functor");
|
|
|
|
testMappedReducedInitialValue(intList, sumOfSquares, IntSquare(), intSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("functor-function");
|
|
|
|
testMappedReducedInitialValue(intList, intListSquaresAppended, IntSquare(), push_back, intListInitial, OrderedReduce);
|
|
|
|
CHECK_FAIL("functor-member");
|
|
|
|
testMappedReducedInitialValue(intList, sumOfSquares, IntSquare(), lambdaSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("functor-lambda");
|
|
|
|
|
|
|
|
// FUNCTION-other
|
|
|
|
testMappedReducedInitialValue(intList, sumOfSquares, intSquare, IntSumReduce(), intInitial);
|
|
|
|
CHECK_FAIL("function-functor");
|
|
|
|
testMappedReducedInitialValue(intList, sumOfSquares, intSquare, intSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("function-function");
|
|
|
|
testMappedReducedInitialValue(intList, intListSquaresAppended, intSquare, push_back, intListInitial, OrderedReduce);
|
|
|
|
CHECK_FAIL("function-member");
|
|
|
|
testMappedReducedInitialValue(intList, sumOfSquares, intSquare, lambdaSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("function-lambda");
|
|
|
|
|
|
|
|
// MEMBER-other
|
|
|
|
testMappedReducedInitialValue(numberList, sum, &Number::toInt, IntSumReduce(), intInitial);
|
|
|
|
CHECK_FAIL("member-functor");
|
|
|
|
testMappedReducedInitialValue(numberList, sum, &Number::toInt, intSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("member-function");
|
|
|
|
testMappedReducedInitialValue(numberList, intListAppended, &Number::toInt, push_back, intListInitial, OrderedReduce);
|
|
|
|
CHECK_FAIL("member-member");
|
|
|
|
testMappedReducedInitialValue(numberList, sum, &Number::toInt, lambdaSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("member-lambda");
|
|
|
|
|
|
|
|
// LAMBDA-other
|
|
|
|
testMappedReducedInitialValue(intList, sumOfSquares, lambdaSquare, IntSumReduce(), intInitial);
|
|
|
|
CHECK_FAIL("lambda-functor");
|
|
|
|
testMappedReducedInitialValue(intList, sumOfSquares, lambdaSquare, intSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("lambda-function");
|
|
|
|
testMappedReducedInitialValue(intList, intListSquaresAppended, lambdaSquare, push_back, intListInitial, OrderedReduce);
|
|
|
|
CHECK_FAIL("lambda-member");
|
|
|
|
testMappedReducedInitialValue(intList, sumOfSquares, lambdaSquare, lambdaSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("lambda-lambda");
|
2020-09-24 08:48:33 +00:00
|
|
|
|
2020-09-29 08:30:59 +00:00
|
|
|
{
|
|
|
|
// rvalue sequences
|
|
|
|
auto future = QtConcurrent::mappedReduced(std::vector { 1, 2, 3 }, intSquare, intSumReduce,
|
|
|
|
intInitial);
|
2020-10-29 12:33:22 +00:00
|
|
|
QCOMPARE(future.result(), sumOfSquares);
|
2020-09-29 08:30:59 +00:00
|
|
|
|
|
|
|
auto result = QtConcurrent::blockingMappedReduced(std::vector { 1, 2, 3 }, intSquare,
|
|
|
|
intSumReduce, intInitial);
|
|
|
|
QCOMPARE(result, sumOfSquares);
|
|
|
|
}
|
2020-09-24 08:48:33 +00:00
|
|
|
|
2020-09-29 08:30:59 +00:00
|
|
|
{
|
|
|
|
// move only sequences
|
2020-10-08 22:02:00 +00:00
|
|
|
auto future = QtConcurrent::mappedReduced(MoveOnlyVector<int>({ 1, 2, 3 }), intSquare,
|
2020-09-29 08:30:59 +00:00
|
|
|
intSumReduce, intInitial);
|
2020-10-29 12:33:22 +00:00
|
|
|
QCOMPARE(future.result(), sumOfSquares);
|
2020-09-29 08:30:59 +00:00
|
|
|
|
2020-10-08 22:02:00 +00:00
|
|
|
auto result = QtConcurrent::blockingMappedReduced(MoveOnlyVector<int>({ 1, 2, 3 }),
|
|
|
|
intSquare, intSumReduce, intInitial);
|
2020-09-29 08:30:59 +00:00
|
|
|
QCOMPARE(result, sumOfSquares);
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 12:19:42 +00:00
|
|
|
template <typename SourceObject, typename ResultObject, typename InitialObject, typename MapObject, typename ReduceObject>
|
|
|
|
void testMappedReducedInitialValueThreadPool(QThreadPool *pool,
|
|
|
|
const QList<SourceObject> &sourceObjectList,
|
|
|
|
const ResultObject &expectedResult,
|
|
|
|
MapObject mapObject,
|
|
|
|
ReduceObject reduceObject,
|
|
|
|
InitialObject &&initialObject)
|
|
|
|
{
|
|
|
|
const ResultObject result1 = QtConcurrent::mappedReduced<ResultObject>(
|
2020-10-30 10:21:01 +00:00
|
|
|
pool, sourceObjectList, mapObject, reduceObject, initialObject).result();
|
2020-04-28 12:19:42 +00:00
|
|
|
QCOMPARE(result1, expectedResult);
|
|
|
|
QCOMPARE(threadCount(), 1); // ensure the only one thread was working
|
|
|
|
|
2020-10-30 10:21:01 +00:00
|
|
|
const ResultObject result2 =
|
|
|
|
QtConcurrent::mappedReduced<ResultObject>(pool, sourceObjectList.constBegin(),
|
|
|
|
sourceObjectList.constEnd(), mapObject,
|
|
|
|
reduceObject, initialObject).result();
|
2020-04-28 12:19:42 +00:00
|
|
|
QCOMPARE(result2, expectedResult);
|
|
|
|
QCOMPARE(threadCount(), 1); // ensure the only one thread was working
|
|
|
|
|
2020-05-14 16:46:15 +00:00
|
|
|
const ResultObject result3 = QtConcurrent::blockingMappedReduced<ResultObject>(
|
|
|
|
pool, sourceObjectList, mapObject, reduceObject, initialObject);
|
|
|
|
QCOMPARE(result3, expectedResult);
|
|
|
|
QCOMPARE(threadCount(), 1); // ensure the only one thread was working
|
2020-04-28 12:19:42 +00:00
|
|
|
|
2020-05-14 16:46:15 +00:00
|
|
|
const ResultObject result4 = QtConcurrent::blockingMappedReduced<ResultObject>(
|
|
|
|
pool, sourceObjectList.constBegin(), sourceObjectList.constEnd(),
|
|
|
|
mapObject, reduceObject, initialObject);
|
|
|
|
QCOMPARE(result4, expectedResult);
|
|
|
|
QCOMPARE(threadCount(), 1); // ensure the only one thread was working
|
2020-04-28 12:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QtConcurrentMap::mappedReducedInitialValueThreadPool()
|
|
|
|
{
|
|
|
|
// This is a copy of tst_QtConcurrentMap::mappedReduced with the initial value parameter added
|
|
|
|
|
|
|
|
const QList<int> intList {1, 2, 3};
|
|
|
|
const int sumOfCubes = 46;
|
|
|
|
const int intInitial = 10;
|
|
|
|
|
|
|
|
auto lambdaCube = [](int x) {
|
|
|
|
return x * x * x;
|
|
|
|
};
|
|
|
|
auto lambdaSumReduce = [](int &sum, int x) {
|
|
|
|
sum += x;
|
|
|
|
};
|
|
|
|
|
|
|
|
QThreadPool pool;
|
|
|
|
pool.setMaxThreadCount(1);
|
|
|
|
QCOMPARE(semaphore.available(), 1);
|
|
|
|
workingThreads.clear();
|
|
|
|
|
|
|
|
// FUNCTOR-other
|
|
|
|
testMappedReducedInitialValueThreadPool(&pool, intList, sumOfCubes, IntCube(),
|
|
|
|
IntSumReduce(), intInitial);
|
|
|
|
CHECK_FAIL("functor-functor");
|
|
|
|
testMappedReducedInitialValueThreadPool(&pool, intList, sumOfCubes, IntCube(),
|
|
|
|
intSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("functor-function");
|
|
|
|
testMappedReducedInitialValueThreadPool(&pool, intList, sumOfCubes, IntCube(),
|
|
|
|
lambdaSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("functor-lambda");
|
|
|
|
|
|
|
|
// FUNCTION-other
|
|
|
|
testMappedReducedInitialValueThreadPool(&pool, intList, sumOfCubes, intCube,
|
|
|
|
IntSumReduce(), intInitial);
|
|
|
|
CHECK_FAIL("function-functor");
|
|
|
|
testMappedReducedInitialValueThreadPool(&pool, intList, sumOfCubes, intCube,
|
|
|
|
intSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("function-function");
|
|
|
|
testMappedReducedInitialValueThreadPool(&pool, intList, sumOfCubes, intCube,
|
|
|
|
lambdaSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("function-lambda");
|
|
|
|
|
|
|
|
// LAMBDA-other
|
|
|
|
testMappedReducedInitialValueThreadPool(&pool, intList, sumOfCubes, lambdaCube,
|
|
|
|
IntSumReduce(), intInitial);
|
|
|
|
CHECK_FAIL("lambda-functor");
|
|
|
|
testMappedReducedInitialValueThreadPool(&pool, intList, sumOfCubes, lambdaCube,
|
|
|
|
intSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("lambda-function");
|
|
|
|
testMappedReducedInitialValueThreadPool(&pool, intList, sumOfCubes, lambdaCube,
|
|
|
|
lambdaSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("lambda-lambda");
|
2020-09-24 08:48:33 +00:00
|
|
|
|
2020-09-29 08:30:59 +00:00
|
|
|
{
|
|
|
|
// rvalue sequences
|
|
|
|
auto future = QtConcurrent::mappedReduced(&pool, std::vector { 1, 2, 3 }, intCube,
|
|
|
|
intSumReduce, intInitial);
|
2020-10-29 12:33:22 +00:00
|
|
|
QCOMPARE(future.result(), sumOfCubes);
|
2020-09-29 08:30:59 +00:00
|
|
|
|
|
|
|
auto result = QtConcurrent::blockingMappedReduced(&pool, std::vector { 1, 2, 3 }, intCube,
|
|
|
|
intSumReduce, intInitial);
|
|
|
|
QCOMPARE(result, sumOfCubes);
|
|
|
|
}
|
2020-09-24 08:48:33 +00:00
|
|
|
|
2020-09-29 08:30:59 +00:00
|
|
|
{
|
|
|
|
// move only sequences
|
2020-10-08 22:02:00 +00:00
|
|
|
auto future = QtConcurrent::mappedReduced(&pool, MoveOnlyVector<int>({ 1, 2, 3 }), intCube,
|
2020-09-29 08:30:59 +00:00
|
|
|
intSumReduce, intInitial);
|
2020-10-29 12:33:22 +00:00
|
|
|
QCOMPARE(future.result(), sumOfCubes);
|
2020-09-29 08:30:59 +00:00
|
|
|
|
2020-10-08 22:02:00 +00:00
|
|
|
auto result = QtConcurrent::blockingMappedReduced(&pool, MoveOnlyVector<int>({ 1, 2, 3 }),
|
2020-09-29 08:30:59 +00:00
|
|
|
intCube, intSumReduce, intInitial);
|
|
|
|
QCOMPARE(result, sumOfCubes);
|
|
|
|
}
|
2020-04-28 12:19:42 +00:00
|
|
|
}
|
|
|
|
|
2020-10-09 16:21:17 +00:00
|
|
|
void tst_QtConcurrentMap::mappedReducedInitialValueWithMoveOnlyCallable()
|
|
|
|
{
|
|
|
|
const QList<int> intList { 1, 2, 3 };
|
|
|
|
const auto initialValue = 10;
|
|
|
|
const auto sum = 22;
|
|
|
|
{
|
|
|
|
const auto result =
|
|
|
|
QtConcurrent::mappedReduced<int>(intList, MultiplyBy2(),
|
|
|
|
IntSumReduceMoveOnly(), initialValue).result();
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result =
|
|
|
|
QtConcurrent::mappedReduced<int>(intList.begin(), intList.end(), MultiplyBy2(),
|
|
|
|
IntSumReduceMoveOnly(), initialValue).result();
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::blockingMappedReduced<int>(
|
|
|
|
intList, MultiplyBy2(), IntSumReduceMoveOnly(), initialValue);
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::blockingMappedReduced<int>(
|
|
|
|
intList.begin(), intList.end(), MultiplyBy2(), IntSumReduceMoveOnly(),
|
|
|
|
initialValue);
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
|
|
|
|
QThreadPool pool;
|
|
|
|
{
|
|
|
|
const auto result =
|
|
|
|
QtConcurrent::mappedReduced<int>(&pool, intList, MultiplyBy2(),
|
|
|
|
IntSumReduceMoveOnly(), initialValue).result();
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::mappedReduced<int>(&pool, intList.begin(), intList.end(),
|
|
|
|
MultiplyBy2(), IntSumReduceMoveOnly(),
|
|
|
|
initialValue).result();
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::blockingMappedReduced<int>(
|
|
|
|
&pool, intList, MultiplyBy2(), IntSumReduceMoveOnly(), initialValue);
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const auto result = QtConcurrent::blockingMappedReduced<int>(
|
|
|
|
&pool, intList.begin(), intList.end(), MultiplyBy2(), IntSumReduceMoveOnly(),
|
|
|
|
initialValue);
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 09:54:40 +00:00
|
|
|
void tst_QtConcurrentMap::mappedReducedDifferentTypeInitialValue()
|
|
|
|
{
|
|
|
|
// This is a copy of tst_QtConcurrentMap::mappedReducedDifferentType
|
|
|
|
// with the initial value parameter added
|
|
|
|
|
|
|
|
const QList<Number> numberList {1, 2, 3};
|
|
|
|
const int sumOfSquares = 24;
|
|
|
|
const int intInitial = 10;
|
|
|
|
|
|
|
|
auto lambdaSquare = [](Number x) {
|
|
|
|
return Number(x.toInt() * x.toInt());
|
|
|
|
};
|
|
|
|
auto lambdaSumReduce = [](int &sum, Number x) {
|
|
|
|
sum += x.toInt();
|
|
|
|
};
|
|
|
|
|
|
|
|
// FUNCTOR-other
|
|
|
|
testMappedReducedInitialValue(numberList, sumOfSquares, NumberSquare(), NumberSumReduce(), intInitial);
|
|
|
|
CHECK_FAIL("functor-functor");
|
|
|
|
testMappedReducedInitialValue(numberList, sumOfSquares, NumberSquare(), numberSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("functor-function");
|
|
|
|
testMappedReducedInitialValue(numberList, sumOfSquares, NumberSquare(), lambdaSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("functor-lambda");
|
|
|
|
|
|
|
|
// FUNCTION-other
|
|
|
|
testMappedReducedInitialValue(numberList, sumOfSquares, numberSquare, NumberSumReduce(), intInitial);
|
|
|
|
CHECK_FAIL("function-functor");
|
|
|
|
testMappedReducedInitialValue(numberList, sumOfSquares, numberSquare, numberSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("function-function");
|
|
|
|
testMappedReducedInitialValue(numberList, sumOfSquares, numberSquare, lambdaSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("function-lambda");
|
|
|
|
|
|
|
|
// MEMBER-other
|
|
|
|
testMappedReducedInitialValue(numberList, sumOfSquares, &Number::squared, NumberSumReduce(), intInitial);
|
|
|
|
CHECK_FAIL("member-functor");
|
|
|
|
testMappedReducedInitialValue(numberList, sumOfSquares, &Number::squared, numberSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("member-function");
|
|
|
|
testMappedReducedInitialValue(numberList, sumOfSquares, &Number::squared, lambdaSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("member-lambda");
|
|
|
|
|
|
|
|
// LAMBDA-other
|
|
|
|
testMappedReducedInitialValue(numberList, sumOfSquares, lambdaSquare, NumberSumReduce(), intInitial);
|
|
|
|
CHECK_FAIL("lambda-functor");
|
|
|
|
testMappedReducedInitialValue(numberList, sumOfSquares, lambdaSquare, numberSumReduce, intInitial);
|
|
|
|
CHECK_FAIL("lambda-function");
|
|
|
|
testMappedReducedInitialValue(numberList, sumOfSquares, lambdaSquare, lambdaSumReduce, intInitial);
|
2020-04-14 06:14:08 +00:00
|
|
|
CHECK_FAIL("lambda-lambda");
|
2020-03-26 14:47:04 +00:00
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
int sleeper(int val)
|
|
|
|
{
|
|
|
|
QTest::qSleep(100);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QtConcurrentMap::assignResult()
|
|
|
|
{
|
|
|
|
const QList<int> startList = QList<int>() << 0 << 1 << 2;
|
|
|
|
QList<int> list = QtConcurrent::blockingMapped(startList, sleeper);
|
|
|
|
QCOMPARE(list.at(0), 0);
|
|
|
|
QCOMPARE(list.at(1), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fnConst(const int &i)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fn(int &i)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:33:15 +00:00
|
|
|
int fnConstNoExcept(const int &i) noexcept
|
2017-01-16 11:22:43 +00:00
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:33:15 +00:00
|
|
|
int fnNoExcept(int &i) noexcept
|
2017-01-16 11:22:43 +00:00
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QString changeTypeConst(const int &)
|
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString changeType(int &)
|
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:33:15 +00:00
|
|
|
QString changeTypeConstNoExcept(const int &) noexcept
|
2017-01-16 11:22:43 +00:00
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:33:15 +00:00
|
|
|
QString changeTypeNoExcept(int &) noexcept
|
2017-01-16 11:22:43 +00:00
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
int changeTypeQStringListConst(const QStringList &)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int changeTypeQStringList(QStringList &)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:33:15 +00:00
|
|
|
int changeTypeQStringListConstNoExcept(const QStringList &) noexcept
|
2017-01-16 11:22:43 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:33:15 +00:00
|
|
|
int changeTypeQStringListNoExcept(QStringList &) noexcept
|
2017-01-16 11:22:43 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
class MemFnTester
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MemFnTester() {}
|
|
|
|
|
|
|
|
MemFnTester fn()
|
|
|
|
{
|
|
|
|
return MemFnTester();
|
|
|
|
}
|
|
|
|
|
|
|
|
MemFnTester fnConst() const
|
|
|
|
{
|
|
|
|
return MemFnTester();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString changeType()
|
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString changeTypeConst() const
|
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
2017-01-16 11:22:43 +00:00
|
|
|
|
2019-04-09 11:33:15 +00:00
|
|
|
MemFnTester fnNoExcept() noexcept
|
2017-01-16 11:22:43 +00:00
|
|
|
{
|
|
|
|
return MemFnTester();
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:33:15 +00:00
|
|
|
MemFnTester fnConstNoExcept() const noexcept
|
2017-01-16 11:22:43 +00:00
|
|
|
{
|
|
|
|
return MemFnTester();
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:33:15 +00:00
|
|
|
QString changeTypeNoExcept() noexcept
|
2017-01-16 11:22:43 +00:00
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2019-04-09 11:33:15 +00:00
|
|
|
QString changeTypeConstNoExcept() const noexcept
|
2017-01-16 11:22:43 +00:00
|
|
|
{
|
|
|
|
return QString();
|
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
2020-06-22 13:07:48 +00:00
|
|
|
Q_DECLARE_METATYPE(QList<MemFnTester>);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
void tst_QtConcurrentMap::functionOverloads()
|
|
|
|
{
|
|
|
|
QList<int> intList;
|
|
|
|
const QList<int> constIntList;
|
|
|
|
QList<MemFnTester> classList;
|
|
|
|
const QList<MemFnTester> constMemFnTesterList;
|
|
|
|
|
|
|
|
QtConcurrent::mapped(intList, fnConst);
|
|
|
|
QtConcurrent::mapped(constIntList, fnConst);
|
|
|
|
QtConcurrent::mapped(classList, &MemFnTester::fnConst);
|
|
|
|
QtConcurrent::mapped(constMemFnTesterList, &MemFnTester::fnConst);
|
|
|
|
|
2020-06-22 13:07:48 +00:00
|
|
|
QtConcurrent::blockingMapped<QList<int>>(intList, fnConst);
|
|
|
|
QtConcurrent::blockingMapped<QList<int>>(constIntList, fnConst);
|
|
|
|
QtConcurrent::blockingMapped<QList<MemFnTester>>(classList, &MemFnTester::fnConst);
|
|
|
|
QtConcurrent::blockingMapped<QList<MemFnTester>>(constMemFnTesterList, &MemFnTester::fnConst);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QtConcurrent::blockingMapped<QList<QString> >(intList, changeTypeConst);
|
|
|
|
QtConcurrent::blockingMapped<QList<QString> >(constIntList, changeTypeConst);
|
|
|
|
QtConcurrent::blockingMapped<QList<QString> >(classList, &MemFnTester::changeTypeConst);
|
|
|
|
QtConcurrent::blockingMapped<QList<QString> >(constMemFnTesterList, &MemFnTester::changeTypeConst);
|
|
|
|
}
|
|
|
|
|
2017-01-16 11:22:43 +00:00
|
|
|
void tst_QtConcurrentMap::noExceptFunctionOverloads()
|
|
|
|
{
|
|
|
|
QList<int> intList;
|
|
|
|
const QList<int> constIntList;
|
|
|
|
QList<MemFnTester> classList;
|
|
|
|
const QList<MemFnTester> constMemFnTesterList;
|
|
|
|
|
|
|
|
QtConcurrent::mapped(intList, fnConstNoExcept);
|
|
|
|
QtConcurrent::mapped(constIntList, fnConstNoExcept);
|
|
|
|
QtConcurrent::mapped(classList, &MemFnTester::fnConstNoExcept);
|
|
|
|
QtConcurrent::mapped(constMemFnTesterList, &MemFnTester::fnConstNoExcept);
|
|
|
|
|
2020-06-22 13:07:48 +00:00
|
|
|
QtConcurrent::blockingMapped<QList<int>>(intList, fnConstNoExcept);
|
|
|
|
QtConcurrent::blockingMapped<QList<int>>(constIntList, fnConstNoExcept);
|
|
|
|
QtConcurrent::blockingMapped<QList<MemFnTester>>(classList, &MemFnTester::fnConstNoExcept);
|
|
|
|
QtConcurrent::blockingMapped<QList<MemFnTester>>(constMemFnTesterList,
|
|
|
|
&MemFnTester::fnConstNoExcept);
|
2017-01-16 11:22:43 +00:00
|
|
|
|
|
|
|
QtConcurrent::blockingMapped<QList<QString> >(intList, changeTypeConstNoExcept);
|
|
|
|
QtConcurrent::blockingMapped<QList<QString> >(constIntList, changeTypeConstNoExcept);
|
|
|
|
QtConcurrent::blockingMapped<QList<QString> >(classList, &MemFnTester::changeTypeConstNoExcept);
|
|
|
|
QtConcurrent::blockingMapped<QList<QString> >(constMemFnTesterList, &MemFnTester::changeTypeConstNoExcept);
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QAtomicInt currentInstanceCount;
|
|
|
|
QAtomicInt peakInstanceCount;
|
|
|
|
class InstanceCounter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
inline InstanceCounter()
|
|
|
|
{ currentInstanceCount.fetchAndAddRelaxed(1); updatePeak(); }
|
|
|
|
inline ~InstanceCounter()
|
|
|
|
{ currentInstanceCount.fetchAndAddRelaxed(-1);}
|
|
|
|
inline InstanceCounter(const InstanceCounter &)
|
|
|
|
{ currentInstanceCount.fetchAndAddRelaxed(1); updatePeak(); }
|
2021-04-11 09:08:13 +00:00
|
|
|
constexpr InstanceCounter &operator=(const InstanceCounter &) noexcept
|
|
|
|
{ return *this; }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
void updatePeak()
|
|
|
|
{
|
|
|
|
forever {
|
2019-06-10 09:08:29 +00:00
|
|
|
const int localPeak = peakInstanceCount.loadRelaxed();
|
|
|
|
const int localCurrent = currentInstanceCount.loadRelaxed();
|
2011-04-27 10:05:43 +00:00
|
|
|
if (localCurrent <= localPeak)
|
|
|
|
break;
|
|
|
|
if (peakInstanceCount.testAndSetOrdered(localPeak, localCurrent))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
InstanceCounter slowMap(const InstanceCounter &in)
|
|
|
|
{
|
|
|
|
QTest::qSleep(2);
|
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
InstanceCounter fastMap(const InstanceCounter &in)
|
|
|
|
{
|
2017-04-14 04:13:52 +00:00
|
|
|
QTest::qSleep(QRandomGenerator::global()->bounded(2) + 1);
|
2011-04-27 10:05:43 +00:00
|
|
|
return in;
|
|
|
|
}
|
|
|
|
|
|
|
|
void slowReduce(int &result, const InstanceCounter&)
|
|
|
|
{
|
2017-04-14 04:13:52 +00:00
|
|
|
QTest::qSleep(QRandomGenerator::global()->bounded(4) + 1);
|
2011-04-27 10:05:43 +00:00
|
|
|
++result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void fastReduce(int &result, const InstanceCounter&)
|
|
|
|
{
|
|
|
|
++result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QtConcurrentMap::throttling()
|
|
|
|
{
|
|
|
|
const int itemcount = 100;
|
|
|
|
const int allowedTemporaries = QThread::idealThreadCount() * 40;
|
|
|
|
|
|
|
|
{
|
2019-06-10 09:08:29 +00:00
|
|
|
currentInstanceCount.storeRelaxed(0);
|
|
|
|
peakInstanceCount.storeRelaxed(0);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QList<InstanceCounter> instances;
|
|
|
|
for (int i = 0; i < itemcount; ++i)
|
|
|
|
instances.append(InstanceCounter());
|
|
|
|
|
2019-06-10 09:08:29 +00:00
|
|
|
QCOMPARE(currentInstanceCount.loadRelaxed(), itemcount);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
int results = QtConcurrent::blockingMappedReduced(instances, slowMap, fastReduce);
|
|
|
|
QCOMPARE(results, itemcount);
|
2019-06-10 09:08:29 +00:00
|
|
|
QCOMPARE(currentInstanceCount.loadRelaxed(), itemcount);
|
|
|
|
QVERIFY(peakInstanceCount.loadRelaxed() < itemcount + allowedTemporaries);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2019-06-10 09:08:29 +00:00
|
|
|
QCOMPARE(currentInstanceCount.loadRelaxed(), 0);
|
|
|
|
peakInstanceCount.storeRelaxed(0);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QList<InstanceCounter> instances;
|
|
|
|
for (int i = 0; i < itemcount; ++i)
|
|
|
|
instances.append(InstanceCounter());
|
|
|
|
|
2019-06-10 09:08:29 +00:00
|
|
|
QCOMPARE(currentInstanceCount.loadRelaxed(), itemcount);
|
2011-04-27 10:05:43 +00:00
|
|
|
int results = QtConcurrent::blockingMappedReduced(instances, fastMap, slowReduce);
|
|
|
|
|
|
|
|
QCOMPARE(results, itemcount);
|
2019-06-10 09:08:29 +00:00
|
|
|
QCOMPARE(currentInstanceCount.loadRelaxed(), itemcount);
|
|
|
|
QVERIFY(peakInstanceCount.loadRelaxed() < itemcount + allowedTemporaries);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef QT_NO_EXCEPTIONS
|
|
|
|
void throwMapper(int &e)
|
|
|
|
{
|
|
|
|
Q_UNUSED(e);
|
2012-08-27 10:24:48 +00:00
|
|
|
throw QException();
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QtConcurrentMap::exceptions()
|
|
|
|
{
|
|
|
|
bool caught = false;
|
|
|
|
try {
|
|
|
|
QList<int> list = QList<int>() << 1 << 2 << 3;
|
|
|
|
QtConcurrent::map(list, throwMapper).waitForFinished();
|
2012-08-27 10:24:48 +00:00
|
|
|
} catch (const QException &) {
|
2011-04-27 10:05:43 +00:00
|
|
|
caught = true;
|
|
|
|
}
|
|
|
|
if (!caught)
|
|
|
|
QFAIL("did not get exception");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int mapper(const int &i)
|
|
|
|
{
|
|
|
|
QTest::qWait(1);
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QtConcurrentMap::incrementalResults()
|
|
|
|
{
|
|
|
|
const int count = 200;
|
2013-03-14 23:42:15 +00:00
|
|
|
QList<int> ints;
|
2011-04-27 10:05:43 +00:00
|
|
|
for (int i=0; i < count; ++i)
|
|
|
|
ints << i;
|
|
|
|
|
|
|
|
QFuture<int> future = QtConcurrent::mapped(ints, mapper);
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QList<int> results;
|
|
|
|
|
|
|
|
while (future.isFinished() == false) {
|
|
|
|
for (int i = 0; i < future.resultCount(); ++i) {
|
|
|
|
results += future.resultAt(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
QTest::qWait(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
QCOMPARE(future.isFinished(), true);
|
|
|
|
QCOMPARE(future.resultCount(), count);
|
|
|
|
QCOMPARE(future.results().count(), count);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Test that mapped does not cause deep copies when holding
|
|
|
|
references to Qt containers.
|
|
|
|
*/
|
2011-12-29 01:38:45 +00:00
|
|
|
void tst_QtConcurrentMap::noDetach()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
{
|
|
|
|
QList<int> l = QList<int>() << 1;
|
|
|
|
QVERIFY(l.isDetached());
|
|
|
|
|
|
|
|
QList<int> ll = l;
|
2015-07-23 11:26:28 +00:00
|
|
|
QVERIFY(!l.isDetached());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QtConcurrent::mapped(l, mapper).waitForFinished();
|
|
|
|
|
2015-07-23 11:26:28 +00:00
|
|
|
QVERIFY(!l.isDetached());
|
|
|
|
QVERIFY(!ll.isDetached());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QtConcurrent::mappedReduced(l, mapper, intSumReduce).waitForFinished();
|
|
|
|
|
2015-07-23 11:26:28 +00:00
|
|
|
QVERIFY(!l.isDetached());
|
|
|
|
QVERIFY(!ll.isDetached());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QtConcurrent::map(l, multiplyBy2Immutable).waitForFinished();
|
2015-07-23 11:26:28 +00:00
|
|
|
QVERIFY(l.isDetached());
|
|
|
|
QVERIFY(ll.isDetached());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const QList<int> l = QList<int>() << 1;
|
|
|
|
QVERIFY(l.isDetached());
|
|
|
|
|
|
|
|
const QList<int> ll = l;
|
2015-07-23 11:26:28 +00:00
|
|
|
QVERIFY(!l.isDetached());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QtConcurrent::mapped(l, mapper).waitForFinished();
|
|
|
|
|
2015-07-23 11:26:28 +00:00
|
|
|
QVERIFY(!l.isDetached());
|
|
|
|
QVERIFY(!ll.isDetached());
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QtConcurrent::mappedReduced(l, mapper, intSumReduce).waitForFinished();
|
|
|
|
|
2015-07-23 11:26:28 +00:00
|
|
|
QVERIFY(!l.isDetached());
|
|
|
|
QVERIFY(!ll.isDetached());
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QtConcurrentMap::stlContainers()
|
|
|
|
{
|
|
|
|
std::vector<int> vector;
|
|
|
|
vector.push_back(1);
|
|
|
|
vector.push_back(2);
|
|
|
|
|
2020-10-27 16:06:26 +00:00
|
|
|
std::vector<int> vector2 = QtConcurrent::blockingMapped(vector, mapper);
|
2020-11-26 20:25:06 +00:00
|
|
|
QCOMPARE(vector2.size(), 2u);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
std::list<int> list;
|
|
|
|
list.push_back(1);
|
|
|
|
list.push_back(2);
|
|
|
|
|
2020-10-27 16:06:26 +00:00
|
|
|
std::list<int> list2 = QtConcurrent::blockingMapped(list, mapper);
|
2020-11-30 11:00:46 +00:00
|
|
|
QCOMPARE(list2.size(), 2u);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QtConcurrent::mapped(list, mapper).waitForFinished();
|
2013-03-14 23:42:15 +00:00
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
QtConcurrent::blockingMap(list, multiplyBy2Immutable);
|
|
|
|
}
|
|
|
|
|
2020-03-26 14:47:04 +00:00
|
|
|
void tst_QtConcurrentMap::stlContainersLambda()
|
|
|
|
{
|
|
|
|
std::vector<int> vector;
|
|
|
|
vector.push_back(1);
|
|
|
|
vector.push_back(2);
|
|
|
|
|
2020-10-27 16:06:26 +00:00
|
|
|
std::vector<int> vector2 =
|
|
|
|
QtConcurrent::blockingMapped(vector, [](const int &i) { return mapper(i); });
|
2020-11-30 11:00:46 +00:00
|
|
|
QCOMPARE(vector2.size(), 2u);
|
2020-03-26 14:47:04 +00:00
|
|
|
|
|
|
|
std::list<int> list;
|
|
|
|
list.push_back(1);
|
|
|
|
list.push_back(2);
|
|
|
|
|
2020-10-27 16:06:26 +00:00
|
|
|
std::list<int> list2 =
|
|
|
|
QtConcurrent::blockingMapped(list, [](const int &i) { return mapper(i); });
|
2020-11-30 11:00:46 +00:00
|
|
|
QCOMPARE(list2.size(), 2u);
|
2020-03-26 14:47:04 +00:00
|
|
|
|
|
|
|
QtConcurrent::mapped(list, [](const int &i) { return mapper(i); }).waitForFinished();
|
|
|
|
|
|
|
|
QtConcurrent::blockingMap(list, [](int x) { x *= 2; });
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
InstanceCounter ic_fn(const InstanceCounter & ic)
|
|
|
|
{
|
|
|
|
return InstanceCounter(ic);
|
2018-02-06 13:58:06 +00:00
|
|
|
}
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
// Verify that held results are deleted when a future is
|
|
|
|
// assigned over with operator ==
|
|
|
|
void tst_QtConcurrentMap::qFutureAssignmentLeak()
|
|
|
|
{
|
2019-06-10 09:08:29 +00:00
|
|
|
currentInstanceCount.storeRelaxed(0);
|
|
|
|
peakInstanceCount.storeRelaxed(0);
|
2011-04-27 10:05:43 +00:00
|
|
|
QFuture<InstanceCounter> future;
|
|
|
|
{
|
|
|
|
QList<InstanceCounter> list;
|
|
|
|
for (int i=0;i<1000;++i)
|
|
|
|
list += InstanceCounter();
|
|
|
|
future = QtConcurrent::mapped(list, ic_fn);
|
|
|
|
future.waitForFinished();
|
|
|
|
|
|
|
|
future = QtConcurrent::mapped(list, ic_fn);
|
|
|
|
future.waitForFinished();
|
|
|
|
|
|
|
|
future = QtConcurrent::mapped(list, ic_fn);
|
|
|
|
future.waitForFinished();
|
|
|
|
}
|
|
|
|
|
2018-02-07 17:37:22 +00:00
|
|
|
// Use QTRY_COMPARE because QtConcurrent::ThreadEngine::asynchronousFinish()
|
|
|
|
// deletes its internals after signaling finished, so it might still be holding
|
|
|
|
// on to copies of InstanceCounter for a short while.
|
2019-06-10 09:08:29 +00:00
|
|
|
QTRY_COMPARE(currentInstanceCount.loadRelaxed(), 1000);
|
2011-04-27 10:05:43 +00:00
|
|
|
future = QFuture<InstanceCounter>();
|
2019-06-10 09:08:29 +00:00
|
|
|
QTRY_COMPARE(currentInstanceCount.loadRelaxed(), 0);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void increment(int &num)
|
|
|
|
{
|
|
|
|
++num;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline int echo(const int &num)
|
|
|
|
{
|
|
|
|
return num;
|
|
|
|
}
|
|
|
|
|
|
|
|
void add(int &result, const int &sum)
|
|
|
|
{
|
|
|
|
result += sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
void tst_QtConcurrentMap::stressTest()
|
|
|
|
{
|
|
|
|
const int listSize = 1000;
|
|
|
|
const int sum = (listSize - 1) * (listSize / 2);
|
|
|
|
QList<int> list;
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < listSize; ++i) {
|
|
|
|
list.append(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i =0 ; i < 100; ++i) {
|
|
|
|
QList<int> result = QtConcurrent::blockingMapped(list, echo);
|
|
|
|
for (int j = 0; j < listSize; ++j)
|
|
|
|
QCOMPARE(result.at(j), j);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0 ; i < 100; ++i) {
|
|
|
|
int result = QtConcurrent::blockingMappedReduced(list, echo, add);
|
|
|
|
QCOMPARE(result, sum);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0 ; i < 100; ++i) {
|
|
|
|
QtConcurrent::map(list, increment).waitForFinished();
|
|
|
|
for (int j = 0; j < listSize; ++j)
|
|
|
|
QCOMPARE(list.at(j), i + j + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-25 10:32:52 +00:00
|
|
|
struct LockedCounter
|
|
|
|
{
|
|
|
|
LockedCounter(QMutex *mutex, QAtomicInt *ai)
|
|
|
|
: mtx(mutex),
|
|
|
|
ref(ai) {}
|
|
|
|
|
|
|
|
int operator()(int x)
|
|
|
|
{
|
|
|
|
QMutexLocker locker(mtx);
|
|
|
|
ref->ref();
|
|
|
|
return ++x;
|
|
|
|
}
|
|
|
|
|
|
|
|
QMutex *mtx;
|
|
|
|
QAtomicInt *ref;
|
|
|
|
};
|
|
|
|
|
|
|
|
// The Thread engine holds the last reference
|
|
|
|
// to the QFuture, so this should not leak
|
|
|
|
// or fail.
|
|
|
|
void tst_QtConcurrentMap::persistentResultTest()
|
|
|
|
{
|
|
|
|
QFuture<void> voidFuture;
|
|
|
|
QMutex mtx;
|
|
|
|
QAtomicInt ref;
|
|
|
|
LockedCounter lc(&mtx, &ref);
|
|
|
|
QList<int> list;
|
|
|
|
{
|
|
|
|
list << 1 << 2 << 3;
|
|
|
|
mtx.lock();
|
|
|
|
QFuture<int> future = QtConcurrent::mapped(list
|
|
|
|
,lc);
|
|
|
|
voidFuture = future;
|
|
|
|
}
|
|
|
|
QCOMPARE(ref.loadAcquire(), 0);
|
|
|
|
mtx.unlock(); // Unblock
|
|
|
|
voidFuture.waitForFinished();
|
|
|
|
QCOMPARE(ref.loadAcquire(), 3);
|
|
|
|
}
|
|
|
|
|
2011-10-17 04:50:39 +00:00
|
|
|
QTEST_MAIN(tst_QtConcurrentMap)
|
2011-04-27 10:05:43 +00:00
|
|
|
#include "tst_qtconcurrentmap.moc"
|