diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-09-24 10:48:33 +0200 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-09-28 15:48:17 +0200 |
commit | a6e1f67937c280961f7ec23b2a002ca7d057761d (patch) | |
tree | 8ca782fdd8960d035077d112d0caa34ba16e7fa2 /tests/auto/concurrent | |
parent | b12d6c6a8ab5f7b01bdd2cb862a66a409700faa1 (diff) |
Fix QtConcurrent algorithms to work with temporary sequences
QtConcurrent algorithms are making an internal copy of the passed
sequence, to make sure it won't be destroyed before the execution
is finished. However, they were using iterators of the originally
passed sequence. So, if the original sequence is deleted, QtConcurrent
algorithms would use invalid iterators to a deleted sequence. This
might work with Qt containers thanks to implicit-sharing, but with
other containers will lead to unexpected results. Fixed them to work
on the internal copy of the original sequence.
Change-Id: I1d68692ed9746223c85f51bb05977bc1443b681d
Reviewed-by: Andreas Buhr <andreas.buhr@qt.io>
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Karsten Heimrich <karsten.heimrich@qt.io>
Diffstat (limited to 'tests/auto/concurrent')
-rw-r--r-- | tests/auto/concurrent/qtconcurrentfilter/tst_qtconcurrentfilter.cpp | 51 | ||||
-rw-r--r-- | tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp | 51 |
2 files changed, 102 insertions, 0 deletions
diff --git a/tests/auto/concurrent/qtconcurrentfilter/tst_qtconcurrentfilter.cpp b/tests/auto/concurrent/qtconcurrentfilter/tst_qtconcurrentfilter.cpp index 945f286c9d..a2c5c20740 100644 --- a/tests/auto/concurrent/qtconcurrentfilter/tst_qtconcurrentfilter.cpp +++ b/tests/auto/concurrent/qtconcurrentfilter/tst_qtconcurrentfilter.cpp @@ -221,6 +221,13 @@ void tst_QtConcurrentFilter::filtered() CHECK_FAIL("member"); testFiltered(intList, intListEven, lambdaIsEven); CHECK_FAIL("lambda"); + + // rvalue sequences + auto future = QtConcurrent::filtered(std::vector { 1, 2, 3, 4 }, keepEvenIntegers); + QCOMPARE(future.results(), QList<int>({ 2, 4 })); + + auto result = QtConcurrent::blockingFiltered(std::vector { 1, 2, 3, 4 }, keepEvenIntegers); + QCOMPARE(result, std::vector<int>({ 2, 4 })); } template <typename SourceObject, @@ -274,6 +281,14 @@ void tst_QtConcurrentFilter::filteredThreadPool() CHECK_FAIL("function"); testFilteredThreadPool(&pool, intList, intListEven, lambdaIsOdd); CHECK_FAIL("lambda"); + + // rvalue sequences + auto future = QtConcurrent::filtered(&pool, std::vector { 1, 2, 3, 4 }, keepEvenIntegers); + QCOMPARE(future.results(), QList<int>({ 2, 4 })); + + auto result = + QtConcurrent::blockingFiltered(&pool, std::vector { 1, 2, 3, 4 }, keepEvenIntegers); + QCOMPARE(result, std::vector<int>({ 2, 4 })); } template <typename SourceObject, @@ -409,6 +424,15 @@ void tst_QtConcurrentFilter::filteredReduced() CHECK_FAIL("lambda-member"); testFilteredReduced(intList, intSum, lambdaIsEven, lambdaIntSumReduce); CHECK_FAIL("lambda-lambda"); + + // rvalue sequences + auto future = QtConcurrent::filteredReduced(std::vector { 1, 2, 3, 4 }, keepEvenIntegers, + intSumReduce); + QCOMPARE(future, intSum); + + auto result = QtConcurrent::blockingFilteredReduced(std::vector { 1, 2, 3, 4 }, + keepEvenIntegers, intSumReduce); + QCOMPARE(result, intSum); } template <typename SourceObject, @@ -485,6 +509,15 @@ void tst_QtConcurrentFilter::filteredReducedThreadPool() CHECK_FAIL("lambda-function"); testFilteredReducedThreadPool(&pool, intList, intSum, lambdaIsOdd, lambdaSumReduce); CHECK_FAIL("lambda-lambda"); + + // rvalue sequences + auto future = QtConcurrent::filteredReduced(&pool, std::vector { 1, 2, 3, 4 }, keepOddIntegers, + intSumReduce); + QCOMPARE(future, intSum); + + auto result = QtConcurrent::blockingFilteredReduced(&pool, std::vector { 1, 2, 3, 4 }, + keepOddIntegers, intSumReduce); + QCOMPARE(result, intSum); } void tst_QtConcurrentFilter::filteredReducedDifferentType() @@ -680,6 +713,15 @@ void tst_QtConcurrentFilter::filteredReducedInitialValue() testFilteredReducedInitialValue(intList, intSum, lambdaIsEven, lambdaIntSumReduce, intInitial); CHECK_FAIL("lambda-lambda"); + + // rvalue sequences + auto future = QtConcurrent::filteredReduced(std::vector { 1, 2, 3, 4 }, keepEvenIntegers, + intSumReduce, intInitial); + QCOMPARE(future, intSum); + + auto result = QtConcurrent::blockingFilteredReduced(std::vector { 1, 2, 3, 4 }, + keepEvenIntegers, intSumReduce, intInitial); + QCOMPARE(result, intSum); } template <typename SourceObject, @@ -768,6 +810,15 @@ void tst_QtConcurrentFilter::filteredReducedInitialValueThreadPool() testFilteredReducedInitialValueThreadPool(&pool, intList, intSum, lambdaIsOdd, lambdaSumReduce, intInitial); CHECK_FAIL("lambda-lambda"); + + // rvalue sequences + auto future = QtConcurrent::filteredReduced(&pool, std::vector { 1, 2, 3, 4 }, keepOddIntegers, + intSumReduce, intInitial); + QCOMPARE(future, intSum); + + auto result = QtConcurrent::blockingFilteredReduced(&pool, std::vector { 1, 2, 3, 4 }, + keepOddIntegers, intSumReduce, intInitial); + QCOMPARE(result, intSum); } void tst_QtConcurrentFilter::filteredReducedDifferentTypeInitialValue() diff --git a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp index 39af0acd06..33ec251452 100644 --- a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp +++ b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp @@ -447,6 +447,14 @@ void tst_QtConcurrentMap::mapped() testMapped(stringList, intList, &QString::toInt); CHECK_FAIL("member"); #endif + + // rvalue sequences + auto future = QtConcurrent::mapped(std::vector { 1, 2, 3 }, multiplyBy2); + QCOMPARE(future.results(), QList<int>({ 2, 4, 6 })); + + auto result = + QtConcurrent::blockingMapped<std::vector<int>>(std::vector { 1, 2, 3 }, multiplyBy2); + QCOMPARE(result, std::vector<int>({ 2, 4, 6 })); } static QSemaphore semaphore(1); @@ -531,6 +539,14 @@ void tst_QtConcurrentMap::mappedThreadPool() CHECK_FAIL("function"); testMappedThreadPool(&pool, intList, intListMultipiedBy3, lambdaMultiplyBy3); CHECK_FAIL("lambda"); + + // rvalue sequences + auto future = QtConcurrent::mapped(&pool, std::vector { 1, 2, 3 }, multiplyBy2); + QCOMPARE(future.results(), QList<int>({ 2, 4, 6 })); + + auto result = QtConcurrent::blockingMapped<std::vector<int>>(&pool, std::vector { 1, 2, 3 }, + multiplyBy2); + QCOMPARE(result, std::vector<int>({ 2, 4, 6 })); } int intSquare(int x) @@ -657,6 +673,14 @@ void tst_QtConcurrentMap::mappedReduced() CHECK_FAIL("lambda-member"); testMappedReduced(intList, sumOfSquares, lambdaSquare, lambdaSumReduce); CHECK_FAIL("lambda-lambda"); + + // rvalue sequences + auto future = QtConcurrent::mappedReduced(std::vector { 1, 2, 3 }, intSquare, intSumReduce); + QCOMPARE(future, sumOfSquares); + + auto result = + QtConcurrent::blockingMappedReduced(std::vector { 1, 2, 3 }, intSquare, intSumReduce); + QCOMPARE(result, sumOfSquares); } template <typename SourceObject, typename ResultObject, typename MapObject, typename ReduceObject> @@ -743,6 +767,15 @@ void tst_QtConcurrentMap::mappedReducedThreadPool() CHECK_FAIL("lambda-function"); testMappedReducedThreadPool(&pool, intList, sumOfCubes, lambdaCube, lambdaSumReduce); CHECK_FAIL("lambda-lambda"); + + // rvalue sequences + auto future = + QtConcurrent::mappedReduced(&pool, std::vector { 1, 2, 3 }, intCube, intSumReduce); + QCOMPARE(future, sumOfCubes); + + auto result = QtConcurrent::blockingMappedReduced(&pool, std::vector { 1, 2, 3 }, intCube, + intSumReduce); + QCOMPARE(result, sumOfCubes); } void tst_QtConcurrentMap::mappedReducedDifferentType() @@ -905,6 +938,15 @@ void tst_QtConcurrentMap::mappedReducedInitialValue() CHECK_FAIL("lambda-member"); testMappedReducedInitialValue(intList, sumOfSquares, lambdaSquare, lambdaSumReduce, intInitial); CHECK_FAIL("lambda-lambda"); + + // rvalue sequences + auto future = QtConcurrent::mappedReduced(std::vector { 1, 2, 3 }, intSquare, intSumReduce, + intInitial); + QCOMPARE(future, sumOfSquares); + + auto result = QtConcurrent::blockingMappedReduced(std::vector { 1, 2, 3 }, intSquare, + intSumReduce, intInitial); + QCOMPARE(result, sumOfSquares); } template <typename SourceObject, typename ResultObject, typename InitialObject, typename MapObject, typename ReduceObject> @@ -990,6 +1032,15 @@ void tst_QtConcurrentMap::mappedReducedInitialValueThreadPool() testMappedReducedInitialValueThreadPool(&pool, intList, sumOfCubes, lambdaCube, lambdaSumReduce, intInitial); CHECK_FAIL("lambda-lambda"); + + // rvalue sequences + auto future = QtConcurrent::mappedReduced(&pool, std::vector { 1, 2, 3 }, intCube, intSumReduce, + intInitial); + QCOMPARE(future, sumOfCubes); + + auto result = QtConcurrent::blockingMappedReduced(&pool, std::vector { 1, 2, 3 }, intCube, + intSumReduce, intInitial); + QCOMPARE(result, sumOfCubes); } void tst_QtConcurrentMap::mappedReducedDifferentTypeInitialValue() |