summaryrefslogtreecommitdiffstats
path: root/tests/auto/concurrent
diff options
context:
space:
mode:
authorJarek Kobus <jaroslaw.kobus@qt.io>2020-04-17 11:54:40 +0200
committerJarek Kobus <jaroslaw.kobus@qt.io>2020-04-21 10:22:26 +0200
commitd998a467ace766e3266dca798c8357c8e1ef5653 (patch)
tree0f21bb800ffbd87017742b7b27ede6139b9c3a8d /tests/auto/concurrent
parent82729ddb2a862fee117c42828948d47513428c28 (diff)
Add more tests for QtConcurrent::mappedReduced()
Test the case where reduce function of the form: V function(T &result, const U &intermediate) has T and U types different. Make use of numberSumReduce function and corresponding functor that reduce Number class object to the result of int type. Fixes: QTBUG-83258 Change-Id: I194d290988b48e7bca91228c0cd5d39efd1b4712 Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'tests/auto/concurrent')
-rw-r--r--tests/auto/concurrent/qtconcurrentmap/functions.h5
-rw-r--r--tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp216
2 files changed, 221 insertions, 0 deletions
diff --git a/tests/auto/concurrent/qtconcurrentmap/functions.h b/tests/auto/concurrent/qtconcurrentmap/functions.h
index 19b19f6057..838b7090d8 100644
--- a/tests/auto/concurrent/qtconcurrentmap/functions.h
+++ b/tests/auto/concurrent/qtconcurrentmap/functions.h
@@ -80,6 +80,11 @@ public:
return QString::number(n);
}
+ Number squared() const
+ {
+ return Number(n * n);
+ }
+
bool operator==(const Number &other) const
{
return n == other.n;
diff --git a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp
index 33ea90a844..322da5a688 100644
--- a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp
+++ b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp
@@ -46,8 +46,12 @@ private slots:
void blocking_mapped();
void mappedReduced();
void blocking_mappedReduced();
+ void mappedReducedDifferentType();
+ void blocking_mappedReducedDifferentType();
void mappedReducedInitialValue();
void blocking_mappedReducedInitialValue();
+ void mappedReducedDifferentTypeInitialValue();
+ void blocking_mappedReducedDifferentTypeInitialValue();
void assignResult();
void functionOverloads();
void noExceptFunctionOverloads();
@@ -521,6 +525,20 @@ public:
}
};
+Number numberSquare(Number x)
+{
+ return Number(x.toInt() * x.toInt());
+}
+
+class NumberSquare
+{
+public:
+ Number operator()(Number x)
+ {
+ return Number(x.toInt() * x.toInt());
+ }
+};
+
template <typename SourceObject, typename ResultObject, typename MapObject, typename ReduceObject>
void testMappedReduced(const QList<SourceObject> &sourceObjectList, const ResultObject &expectedResult, MapObject mapObject, ReduceObject reduceObject)
{
@@ -685,6 +703,106 @@ void tst_QtConcurrentMap::blocking_mappedReduced()
CHECK_FAIL("lambda-lambda");
}
+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");
+}
+
+void tst_QtConcurrentMap::blocking_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
+ testBlockingMappedReduced(intList, sumOfSquares, NumberSquare(), NumberSumReduce());
+ CHECK_FAIL("functor-functor");
+ testBlockingMappedReduced(intList, sumOfSquares, NumberSquare(), numberSumReduce);
+ CHECK_FAIL("functor-function");
+ testBlockingMappedReduced(intList, sumOfSquares, NumberSquare(), lambdaSumReduce);
+ CHECK_FAIL("functor-lambda");
+
+ // FUNCTION-other
+ testBlockingMappedReduced(intList, sumOfSquares, numberSquare, NumberSumReduce());
+ CHECK_FAIL("function-functor");
+ testBlockingMappedReduced(intList, sumOfSquares, numberSquare, numberSumReduce);
+ CHECK_FAIL("function-function");
+ testBlockingMappedReduced(intList, sumOfSquares, numberSquare, lambdaSumReduce);
+ CHECK_FAIL("function-lambda");
+
+ // MEMBER-other
+ testBlockingMappedReduced(numberList, sumOfSquares, &Number::squared, NumberSumReduce());
+ CHECK_FAIL("member-functor");
+ testBlockingMappedReduced(numberList, sumOfSquares, &Number::squared, numberSumReduce);
+ CHECK_FAIL("member-function");
+ testBlockingMappedReduced(numberList, sumOfSquares, &Number::squared, lambdaSumReduce);
+ CHECK_FAIL("member-lambda");
+
+ // LAMBDA-other
+ testBlockingMappedReduced(intList, sumOfSquares, lambdaSquare, NumberSumReduce());
+ CHECK_FAIL("lambda-functor");
+ testBlockingMappedReduced(intList, sumOfSquares, lambdaSquare, numberSumReduce);
+ CHECK_FAIL("lambda-function");
+ testBlockingMappedReduced(intList, sumOfSquares, lambdaSquare, lambdaSumReduce);
+ CHECK_FAIL("lambda-lambda");
+}
+
template <typename SourceObject, typename ResultObject, typename InitialObject, typename MapObject, typename ReduceObject>
void testMappedReducedInitialValue(const QList<SourceObject> &sourceObjectList,
const ResultObject &expectedResult,
@@ -878,6 +996,104 @@ void tst_QtConcurrentMap::blocking_mappedReducedInitialValue()
CHECK_FAIL("lambda-lambda");
}
+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);
+ CHECK_FAIL("lambda-lambda");
+}
+
+void tst_QtConcurrentMap::blocking_mappedReducedDifferentTypeInitialValue()
+{
+ // This is a copy of tst_QtConcurrentMap::blocking_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
+ testBlockingMappedReducedInitialValue(numberList, sumOfSquares, NumberSquare(), NumberSumReduce(), intInitial);
+ CHECK_FAIL("functor-functor");
+ testBlockingMappedReducedInitialValue(numberList, sumOfSquares, NumberSquare(), numberSumReduce, intInitial);
+ CHECK_FAIL("functor-function");
+ testBlockingMappedReducedInitialValue(numberList, sumOfSquares, NumberSquare(), lambdaSumReduce, intInitial);
+ CHECK_FAIL("functor-lambda");
+
+ // FUNCTION-other
+ testBlockingMappedReducedInitialValue(numberList, sumOfSquares, numberSquare, NumberSumReduce(), intInitial);
+ CHECK_FAIL("function-functor");
+ testBlockingMappedReducedInitialValue(numberList, sumOfSquares, numberSquare, numberSumReduce, intInitial);
+ CHECK_FAIL("function-function");
+ testBlockingMappedReducedInitialValue(numberList, sumOfSquares, numberSquare, lambdaSumReduce, intInitial);
+ CHECK_FAIL("function-lambda");
+
+ // MEMBER-other
+ testBlockingMappedReducedInitialValue(numberList, sumOfSquares, &Number::squared, NumberSumReduce(), intInitial);
+ CHECK_FAIL("member-functor");
+ testBlockingMappedReducedInitialValue(numberList, sumOfSquares, &Number::squared, numberSumReduce, intInitial);
+ CHECK_FAIL("member-function");
+ testBlockingMappedReducedInitialValue(numberList, sumOfSquares, &Number::squared, lambdaSumReduce, intInitial);
+ CHECK_FAIL("member-lambda");
+
+ // LAMBDA-other
+ testBlockingMappedReducedInitialValue(numberList, sumOfSquares, lambdaSquare, NumberSumReduce(), intInitial);
+ CHECK_FAIL("lambda-functor");
+ testBlockingMappedReducedInitialValue(numberList, sumOfSquares, lambdaSquare, numberSumReduce, intInitial);
+ CHECK_FAIL("lambda-function");
+ testBlockingMappedReducedInitialValue(numberList, sumOfSquares, lambdaSquare, lambdaSumReduce, intInitial);
+ CHECK_FAIL("lambda-lambda");
+}
+
int sleeper(int val)
{
QTest::qSleep(100);