diff options
author | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-04-15 11:44:46 +0200 |
---|---|---|
committer | Sona Kurazyan <sona.kurazyan@qt.io> | 2020-04-18 12:17:41 +0200 |
commit | a7264d9b8c0a50e5ffbf0ea5e7b908637687d9f4 (patch) | |
tree | adbb57746d656e9b4878739c6eaa0339b76d502f /tests/auto/corelib/thread/qfuture/tst_qfuture.cpp | |
parent | 3eed6d76b7ce280674caab448682a2e4837cb4d7 (diff) |
Make continuations work with move-only types
Use the move-only versions of result reporting and getting operations,
if the type of QFuture is not copyable.
Task-number: QTBUG-81941
Change-Id: Ic9fa978380e2c24e190e68d974051a650b0e5571
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Vitaly Fanaskov <vitaly.fanaskov@qt.io>
Diffstat (limited to 'tests/auto/corelib/thread/qfuture/tst_qfuture.cpp')
-rw-r--r-- | tests/auto/corelib/thread/qfuture/tst_qfuture.cpp | 59 |
1 files changed, 58 insertions, 1 deletions
diff --git a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp index 1caa386638..4784cf16db 100644 --- a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp +++ b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp @@ -71,6 +71,8 @@ private: std::function<void ()> m_fn; }; +using UniquePtr = std::unique_ptr<int>; + class tst_QFuture: public QObject { Q_OBJECT @@ -100,12 +102,14 @@ private slots: void nonGlobalThreadPool(); void then(); + void thenForMoveOnlyTypes(); void thenOnCanceledFuture(); #ifndef QT_NO_EXCEPTIONS void thenOnExceptionFuture(); void thenThrows(); void onFailed(); void onFailedTestCallables(); + void onFailedForMoveOnlyTypes(); #endif void takeResults(); void takeResult(); @@ -114,7 +118,6 @@ private slots: void resultsReadyAt(); private: using size_type = std::vector<int>::size_type; - using UniquePtr = std::unique_ptr<int>; static void testSingleResult(const UniquePtr &p); static void testSingleResult(const std::vector<int> &v); @@ -1874,6 +1877,38 @@ void tst_QFuture::then() } } +template<class Type, class Callable> +bool runThenForMoveOnly(Callable &&callable) +{ + QFutureInterface<Type> promise; + auto future = promise.future(); + + auto then = future.then(std::forward<Callable>(callable)); + + promise.reportStarted(); + if constexpr (!std::is_same_v<Type, void>) + promise.reportAndMoveResult(std::make_unique<int>(42)); + promise.reportFinished(); + then.waitForFinished(); + + bool success = true; + if constexpr (!std::is_same_v<decltype(then), QFuture<void>>) + success &= *then.takeResult() == 42; + + if constexpr (!std::is_same_v<Type, void>) + success &= !future.isValid(); + + return success; +} + +void tst_QFuture::thenForMoveOnlyTypes() +{ + QVERIFY(runThenForMoveOnly<UniquePtr>([](UniquePtr res) { return res; })); + QVERIFY(runThenForMoveOnly<UniquePtr>([](UniquePtr res) { Q_UNUSED(res); })); + QVERIFY(runThenForMoveOnly<UniquePtr>([](QFuture<UniquePtr> res) { return res.takeResult(); })); + QVERIFY(runThenForMoveOnly<void>([] { return std::make_unique<int>(42); })); +} + void tst_QFuture::thenOnCanceledFuture() { // Continuations on a canceled future @@ -2482,6 +2517,28 @@ void tst_QFuture::onFailedTestCallables() QVERIFY(runForCallable(Functor3())); } +template<class Callable> +bool runOnFailedForMoveOnly(Callable &&callable) +{ + QFutureInterface<UniquePtr> promise; + auto future = promise.future(); + + auto failedFuture = future.onFailed(std::forward<Callable>(callable)); + + promise.reportStarted(); + QException e; + promise.reportException(e); + promise.reportFinished(); + + return *failedFuture.takeResult() == -1; +} + +void tst_QFuture::onFailedForMoveOnlyTypes() +{ + QVERIFY(runOnFailedForMoveOnly([](const QException &) { return std::make_unique<int>(-1); })); + QVERIFY(runOnFailedForMoveOnly([] { return std::make_unique<int>(-1); })); +} + #endif // QT_NO_EXCEPTIONS void tst_QFuture::testSingleResult(const UniquePtr &p) |