From b5fc1e4e2643e73d3b44c483d159529f8deb8af1 Mon Sep 17 00:00:00 2001 From: Andreas Buhr Date: Tue, 29 Sep 2020 10:30:59 +0200 Subject: Add unit tests to assure QtConcurrent works on move-only sequences Unit tests are added to make sure QtConcurrent works on move-only sequences. Change-Id: I1d066f75ceab9cef98832e96c5827103cbfd72a8 Reviewed-by: Andrei Golubev Reviewed-by: Sona Kurazyan --- .../qtconcurrentfilter/tst_qtconcurrentfilter.cpp | 163 ++++++++++++++----- tests/auto/concurrent/qtconcurrentmap/functions.h | 32 ++++ .../qtconcurrentmap/tst_qtconcurrentmap.cpp | 174 ++++++++++++++++----- 3 files changed, 291 insertions(+), 78 deletions(-) (limited to 'tests/auto') diff --git a/tests/auto/concurrent/qtconcurrentfilter/tst_qtconcurrentfilter.cpp b/tests/auto/concurrent/qtconcurrentfilter/tst_qtconcurrentfilter.cpp index a2c5c20740..c7ab64c14c 100644 --- a/tests/auto/concurrent/qtconcurrentfilter/tst_qtconcurrentfilter.cpp +++ b/tests/auto/concurrent/qtconcurrentfilter/tst_qtconcurrentfilter.cpp @@ -222,12 +222,28 @@ void tst_QtConcurrentFilter::filtered() testFiltered(intList, intListEven, lambdaIsEven); CHECK_FAIL("lambda"); - // rvalue sequences - auto future = QtConcurrent::filtered(std::vector { 1, 2, 3, 4 }, keepEvenIntegers); - QCOMPARE(future.results(), QList({ 2, 4 })); + { + // rvalue sequences + auto future = QtConcurrent::filtered(std::vector { 1, 2, 3, 4 }, keepEvenIntegers); + QCOMPARE(future.results(), QList({ 2, 4 })); + + auto result = QtConcurrent::blockingFiltered(std::vector { 1, 2, 3, 4 }, keepEvenIntegers); + QCOMPARE(result, std::vector({ 2, 4 })); + } - auto result = QtConcurrent::blockingFiltered(std::vector { 1, 2, 3, 4 }, keepEvenIntegers); - QCOMPARE(result, std::vector({ 2, 4 })); +#if 0 + // does not work yet + { + // move only types sequences + auto future = QtConcurrent::filtered( + MoveOnlyVector({ 1, 2, 3, 4 }), keepEvenIntegers); + QCOMPARE(future.results(), QList({ 2, 4 })); + + auto result = QtConcurrent::blockingFiltered( + MoveOnlyVector({ 1, 2, 3, 4 }), keepEvenIntegers); + QCOMPARE(result, std::vector({ 2, 4 })); + } +#endif } template ({ 2, 4 })); + { + // rvalue sequences + auto future = QtConcurrent::filtered(&pool, std::vector { 1, 2, 3, 4 }, keepEvenIntegers); + QCOMPARE(future.results(), QList({ 2, 4 })); - auto result = - QtConcurrent::blockingFiltered(&pool, std::vector { 1, 2, 3, 4 }, keepEvenIntegers); - QCOMPARE(result, std::vector({ 2, 4 })); + auto result = + QtConcurrent::blockingFiltered(&pool, std::vector { 1, 2, 3, 4 }, keepEvenIntegers); + QCOMPARE(result, std::vector({ 2, 4 })); + } + +#if 0 + // does not work yet + { + // move-only sequences + auto future = QtConcurrent::filtered( + &pool, MoveOnlyVector({ 1, 2, 3, 4 }), keepEvenIntegers); + QCOMPARE(future.results(), QList({ 2, 4 })); + + auto result = + QtConcurrent::blockingFiltered( + &pool, MoveOnlyVector({ 1, 2, 3, 4 }), keepEvenIntegers); + QCOMPARE(result, std::vector({ 2, 4 })); + } +#endif } template + bool keepEvenIntegers(const int &x) { return (x & 1) == 0; @@ -133,4 +135,34 @@ public: } }; +class MoveOnlyVector +{ +public: + // rule of six + MoveOnlyVector() = default; + ~MoveOnlyVector() = default; + MoveOnlyVector(MoveOnlyVector &&other) = default; + MoveOnlyVector &operator=(MoveOnlyVector &&other) = default; + + MoveOnlyVector(const MoveOnlyVector &) = delete; + MoveOnlyVector &operator=(const MoveOnlyVector &) = delete; + + // convenience for creation + explicit MoveOnlyVector(const std::vector &v) : data(v) { } + + // minimal interface to be usable as a Sequence in QtConcurrent + typedef std::vector::const_iterator const_iterator; + typedef std::vector::iterator iterator; + const_iterator cbegin() const { return data.cbegin(); } + const_iterator cend() const { return data.cend(); } + iterator begin() { return data.begin(); } + iterator end() { return data.end(); } + const_iterator begin() const { return data.cbegin(); } + const_iterator end() const { return data.cend(); } + bool operator==(const MoveOnlyVector &other) const { return data == other.data; } + +private: + std::vector data; +}; + #endif diff --git a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp index 6b844fa73d..021dac86f0 100644 --- a/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp +++ b/tests/auto/concurrent/qtconcurrentmap/tst_qtconcurrentmap.cpp @@ -170,6 +170,15 @@ void tst_QtConcurrentMap::map() QCOMPARE(list, QList() << 1 << 2 << 3); } + // functors take arguments by reference, modifying the move-only sequence in place + { + MoveOnlyVector moveOnlyVector({ 1, 2, 3 }); + + // functor + QtConcurrent::map(moveOnlyVector, MultiplyBy2InPlace()).waitForFinished(); + QCOMPARE(moveOnlyVector, MoveOnlyVector({ 2, 4, 6 })); + } + #if 0 // not allowed: map() with immutable sequences makes no sense { @@ -252,6 +261,18 @@ void tst_QtConcurrentMap::blockingMap() QCOMPARE(list, QList() << 256 << 512 << 768); } + // functors take arguments by reference, modifying the move-only sequence in place + { + MoveOnlyVector moveOnlyVector({ 1, 2, 3 }); + + // functor + QtConcurrent::blockingMap(moveOnlyVector, MultiplyBy2InPlace()); + QCOMPARE(moveOnlyVector, MoveOnlyVector({ 2, 4, 6 })); + QtConcurrent::blockingMap(moveOnlyVector.begin(), moveOnlyVector.end(), + MultiplyBy2InPlace()); + QCOMPARE(moveOnlyVector, MoveOnlyVector({ 4, 8, 12 })); + } + // functors don't take arguments by reference, making these no-ops { QList list; @@ -497,13 +518,25 @@ void tst_QtConcurrentMap::mapped() CHECK_FAIL("member"); #endif - // rvalue sequences - auto future = QtConcurrent::mapped(std::vector { 1, 2, 3 }, multiplyBy2); - QCOMPARE(future.results(), QList({ 2, 4, 6 })); + { + // rvalue sequences + auto future = QtConcurrent::mapped(std::vector { 1, 2, 3 }, multiplyBy2); + QCOMPARE(future.results(), QList({ 2, 4, 6 })); - auto result = - QtConcurrent::blockingMapped>(std::vector { 1, 2, 3 }, multiplyBy2); - QCOMPARE(result, std::vector({ 2, 4, 6 })); + auto result = QtConcurrent::blockingMapped>(std::vector { 1, 2, 3 }, + multiplyBy2); + QCOMPARE(result, std::vector({ 2, 4, 6 })); + } + + { + // move only sequences + auto future = QtConcurrent::mapped(MoveOnlyVector({ 1, 2, 3 }), multiplyBy2); + QCOMPARE(future.results(), QList({ 2, 4, 6 })); + + auto result = QtConcurrent::blockingMapped>(MoveOnlyVector({ 1, 2, 3 }), + multiplyBy2); + QCOMPARE(result, std::vector({ 2, 4, 6 })); + } } static QSemaphore semaphore(1); @@ -589,13 +622,24 @@ void tst_QtConcurrentMap::mappedThreadPool() 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({ 2, 4, 6 })); + { + // rvalue sequences + auto future = QtConcurrent::mapped(&pool, std::vector { 1, 2, 3 }, multiplyBy2); + QCOMPARE(future.results(), QList({ 2, 4, 6 })); - auto result = QtConcurrent::blockingMapped>(&pool, std::vector { 1, 2, 3 }, - multiplyBy2); - QCOMPARE(result, std::vector({ 2, 4, 6 })); + auto result = QtConcurrent::blockingMapped>(&pool, std::vector { 1, 2, 3 }, + multiplyBy2); + QCOMPARE(result, std::vector({ 2, 4, 6 })); + } + { + // move only sequences + auto future = QtConcurrent::mapped(&pool, MoveOnlyVector({ 1, 2, 3 }), multiplyBy2); + QCOMPARE(future.results(), QList({ 2, 4, 6 })); + + auto result = QtConcurrent::blockingMapped>( + &pool, MoveOnlyVector({ 1, 2, 3 }), multiplyBy2); + QCOMPARE(result, std::vector({ 2, 4, 6 })); + } } int intSquare(int x) @@ -723,13 +767,26 @@ void tst_QtConcurrentMap::mappedReduced() 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); + { + // 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); + } + + { + // move only sequences + auto future = + QtConcurrent::mappedReduced(MoveOnlyVector({ 1, 2, 3 }), intSquare, intSumReduce); + QCOMPARE(future, sumOfSquares); - auto result = - QtConcurrent::blockingMappedReduced(std::vector { 1, 2, 3 }, intSquare, intSumReduce); - QCOMPARE(result, sumOfSquares); + auto result = QtConcurrent::blockingMappedReduced(MoveOnlyVector({ 1, 2, 3 }), intSquare, + intSumReduce); + QCOMPARE(result, sumOfSquares); + } } template @@ -817,14 +874,27 @@ void tst_QtConcurrentMap::mappedReducedThreadPool() 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); + { + // 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); + } - auto result = QtConcurrent::blockingMappedReduced(&pool, std::vector { 1, 2, 3 }, intCube, - intSumReduce); - QCOMPARE(result, sumOfCubes); + { + // move only sequences + auto future = QtConcurrent::mappedReduced(&pool, MoveOnlyVector({ 1, 2, 3 }), intCube, + intSumReduce); + QCOMPARE(future, sumOfCubes); + + auto result = QtConcurrent::blockingMappedReduced(&pool, MoveOnlyVector({ 1, 2, 3 }), + intCube, intSumReduce); + QCOMPARE(result, sumOfCubes); + } } void tst_QtConcurrentMap::mappedReducedDifferentType() @@ -988,14 +1058,27 @@ void tst_QtConcurrentMap::mappedReducedInitialValue() 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); + { + // 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); + } - auto result = QtConcurrent::blockingMappedReduced(std::vector { 1, 2, 3 }, intSquare, - intSumReduce, intInitial); - QCOMPARE(result, sumOfSquares); + { + // move only sequences + auto future = QtConcurrent::mappedReduced(MoveOnlyVector({ 1, 2, 3 }), intSquare, + intSumReduce, intInitial); + QCOMPARE(future, sumOfSquares); + + auto result = QtConcurrent::blockingMappedReduced(MoveOnlyVector({ 1, 2, 3 }), intSquare, + intSumReduce, intInitial); + QCOMPARE(result, sumOfSquares); + } } template @@ -1082,14 +1165,27 @@ void tst_QtConcurrentMap::mappedReducedInitialValueThreadPool() lambdaSumReduce, intInitial); CHECK_FAIL("lambda-lambda"); - // rvalue sequences - auto future = QtConcurrent::mappedReduced(&pool, std::vector { 1, 2, 3 }, intCube, intSumReduce, - intInitial); - QCOMPARE(future, sumOfCubes); + { + // 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); + } - auto result = QtConcurrent::blockingMappedReduced(&pool, std::vector { 1, 2, 3 }, intCube, - intSumReduce, intInitial); - QCOMPARE(result, sumOfCubes); + { + // move only sequences + auto future = QtConcurrent::mappedReduced(&pool, MoveOnlyVector({ 1, 2, 3 }), intCube, + intSumReduce, intInitial); + QCOMPARE(future, sumOfCubes); + + auto result = QtConcurrent::blockingMappedReduced(&pool, MoveOnlyVector({ 1, 2, 3 }), + intCube, intSumReduce, intInitial); + QCOMPARE(result, sumOfCubes); + } } void tst_QtConcurrentMap::mappedReducedDifferentTypeInitialValue() -- cgit v1.2.3