diff options
Diffstat (limited to 'tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp')
-rw-r--r-- | tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp | 118 |
1 files changed, 81 insertions, 37 deletions
diff --git a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp index 973a9b4fa5..3e3165013f 100644 --- a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp +++ b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp @@ -1,30 +1,5 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** 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 -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** 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. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <qtconcurrentmap.h> #include <qexception.h> #include <qdebug.h> @@ -32,6 +7,7 @@ #include <QThread> #include <QMutex> #include <QTest> +#include <QSet> #include <QRandomGenerator> #include "../testhelper_functions.h" @@ -54,6 +30,7 @@ private slots: void mappedReducedInitialValueThreadPool(); void mappedReducedInitialValueWithMoveOnlyCallable(); void mappedReducedDifferentTypeInitialValue(); + void mappedReduceOptionConvertableToResultType(); void assignResult(); void functionOverloads(); void noExceptFunctionOverloads(); @@ -73,7 +50,7 @@ public slots: using namespace QtConcurrent; -void multiplyBy2Immutable(int x) +void multiplyBy2Immutable([[maybe_unused]] int x) { x *= 2; } @@ -81,7 +58,7 @@ void multiplyBy2Immutable(int x) class MultiplyBy2Immutable { public: - void operator()(int x) + void operator()([[maybe_unused]] int x) { x *= 2; } @@ -186,9 +163,9 @@ void tst_QtConcurrentMap::map() QCOMPARE(list, QList<int>() << 1 << 2 << 3); // lambda - QtConcurrent::map(list, [](int x){x *= 2;}).waitForFinished(); + QtConcurrent::map(list, []([[maybe_unused]] int x){x *= 2;}).waitForFinished(); QCOMPARE(list, QList<int>() << 1 << 2 << 3); - QtConcurrent::map(list.begin(), list.end(), [](int x){x *= 2;}).waitForFinished(); + QtConcurrent::map(list.begin(), list.end(), []([[maybe_unused]] int x){x *= 2;}).waitForFinished(); QCOMPARE(list, QList<int>() << 1 << 2 << 3); } @@ -208,6 +185,17 @@ void tst_QtConcurrentMap::map() QCOMPARE(list, NonTemplateSequence({ 2, 4, 6 })); } + // custom pool with invalid number of threads + { + QList<int> list; + list << 1 << 2 << 3; + QThreadPool pool; + pool.setMaxThreadCount(0); // explicitly set incorrect value + // This should not crash + QtConcurrent::map(&pool, list, MultiplyBy2InPlace()).waitForFinished(); + QCOMPARE(list, QList<int>() << 2 << 4 << 6); + } + #if 0 // not allowed: map() with immutable sequences makes no sense { @@ -238,7 +226,7 @@ void tst_QtConcurrentMap::map() #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;; + const QList<int> list = QList<int>() << 1 << 2 << 3; QtConcurrent::map(list, MultiplyBy2InPlace()); QtConcurrent::map(list, multiplyBy2InPlace); @@ -332,9 +320,9 @@ void tst_QtConcurrentMap::blockingMap() QCOMPARE(list, QList<int>() << 1 << 2 << 3); // lambda - QtConcurrent::blockingMap(list, [](int x) { x *= 2; }); + QtConcurrent::blockingMap(list, []([[maybe_unused]] int x) { x *= 2; }); QCOMPARE(list, QList<int>() << 1 << 2 << 3); - QtConcurrent::blockingMap(list.begin(), list.end(), [](int x) { x *= 2; }); + QtConcurrent::blockingMap(list.begin(), list.end(), []([[maybe_unused]] int x) { x *= 2; }); QCOMPARE(list, QList<int>() << 1 << 2 << 3); } @@ -375,7 +363,7 @@ void tst_QtConcurrentMap::blockingMap() #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;; + const QList<int> list = QList<int>() << 1 << 2 << 3; QtConcurrent::blockingMap(list, MultiplyBy2InPlace()); QtConcurrent::blockingMap(list, multiplyBy2InPlace); @@ -1098,6 +1086,17 @@ void tst_QtConcurrentMap::mappedReducedThreadPool() intCube, intSumReduce); QCOMPARE(result, sumOfCubes); } + + { + // pool with invalid number of threads + QThreadPool pool; + pool.setMaxThreadCount(0); // explicitly set incorrect value + + // This should not crash + NonTemplateSequence list { 1, 2, 3 }; + auto future = QtConcurrent::mappedReduced(&pool, list, multiplyBy2, intSumReduce); + QCOMPARE(future.result(), 12); + } } void tst_QtConcurrentMap::mappedReducedWithMoveOnlyCallable() @@ -1629,6 +1628,51 @@ void tst_QtConcurrentMap::mappedReducedDifferentTypeInitialValue() CHECK_FAIL("lambda-lambda"); } +void tst_QtConcurrentMap::mappedReduceOptionConvertableToResultType() +{ + const QList<int> intList { 1, 2, 3 }; + const int sum = 12; + QThreadPool p; + ReduceOption ro = OrderedReduce; + + // With container + QCOMPARE(QtConcurrent::mappedReduced(intList, multiplyBy2, intSumReduce, ro).result(), sum); + QCOMPARE(QtConcurrent::blockingMappedReduced(intList, multiplyBy2, intSumReduce, ro), sum); + + // With iterators + QCOMPARE(QtConcurrent::mappedReduced(intList.begin(), intList.end(), multiplyBy2, intSumReduce, + ro).result(), sum); + QCOMPARE(QtConcurrent::blockingMappedReduced(intList.begin(), intList.end(), multiplyBy2, + intSumReduce, ro), sum); + + // With custom QThreadPool; + QCOMPARE(QtConcurrent::mappedReduced(&p, intList, multiplyBy2, intSumReduce, ro).result(), sum); + QCOMPARE(QtConcurrent::blockingMappedReduced(&p, intList, multiplyBy2, intSumReduce, ro), sum); + QCOMPARE(QtConcurrent::mappedReduced(&p, intList.begin(), intList.end(), multiplyBy2, + intSumReduce, ro).result(), sum); + QCOMPARE(QtConcurrent::blockingMappedReduced(&p, intList.begin(), intList.end(), multiplyBy2, + intSumReduce, ro), sum); + + // The same as above, but specify the result type explicitly (this invokes different overloads) + QCOMPARE(QtConcurrent::mappedReduced<int>(intList, multiplyBy2, intSumReduce, ro).result(), + sum); + QCOMPARE(QtConcurrent::blockingMappedReduced<int>(intList, multiplyBy2, intSumReduce, ro), sum); + + QCOMPARE(QtConcurrent::mappedReduced<int>(intList.begin(), intList.end(), multiplyBy2, + intSumReduce, ro).result(), sum); + QCOMPARE(QtConcurrent::blockingMappedReduced<int>(intList.begin(), intList.end(), multiplyBy2, + intSumReduce, ro), sum); + + QCOMPARE(QtConcurrent::mappedReduced<int>(&p, intList, multiplyBy2, intSumReduce, ro).result(), + sum); + QCOMPARE(QtConcurrent::blockingMappedReduced<int>(&p, intList, multiplyBy2, intSumReduce, ro), + sum); + QCOMPARE(QtConcurrent::mappedReduced<int>(&p, intList.begin(), intList.end(), multiplyBy2, + intSumReduce, ro).result(), sum); + QCOMPARE(QtConcurrent::blockingMappedReduced<int>(&p, intList.begin(), intList.end(), + multiplyBy2, intSumReduce, ro), sum); +} + int sleeper(int val) { QTest::qSleep(100); @@ -1934,7 +1978,7 @@ void tst_QtConcurrentMap::incrementalResults() QCOMPARE(future.isFinished(), true); QCOMPARE(future.resultCount(), count); - QCOMPARE(future.results().count(), count); + QCOMPARE(future.results().size(), count); } /* @@ -2025,7 +2069,7 @@ void tst_QtConcurrentMap::stlContainersLambda() QtConcurrent::mapped(list, [](const int &i) { return mapper(i); }).waitForFinished(); - QtConcurrent::blockingMap(list, [](int x) { x *= 2; }); + QtConcurrent::blockingMap(list, []([[maybe_unused]] int x) { x *= 2; }); } InstanceCounter ic_fn(const InstanceCounter & ic) |