diff options
author | Ivan Solovev <ivan.solovev@qt.io> | 2020-11-12 18:02:09 +0100 |
---|---|---|
committer | Ivan Solovev <ivan.solovev@qt.io> | 2020-11-23 09:50:21 +0100 |
commit | 54875be84de059374920e4c0deacd13a41caaa13 (patch) | |
tree | 3fd95411cda0b2d0e21ae749d7133c96323c9131 /tests/auto/corelib/thread | |
parent | f61f8bb966fdc04662fb4e6597698b573fcb8b94 (diff) |
Add convenience functions for QFuture creation
[ChangeLog][QtCore][QFuture] Add convenience functions to create
a ready QFuture and a QFuture with an exception
Task-number: QTBUG-86713
Change-Id: Ic7f9ca590a8ea8a9696b84f35bad074780794461
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Diffstat (limited to 'tests/auto/corelib/thread')
-rw-r--r-- | tests/auto/corelib/thread/qfuture/tst_qfuture.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp index 0d1097ce1c..c7d552cb30 100644 --- a/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp +++ b/tests/auto/corelib/thread/qfuture/tst_qfuture.cpp @@ -156,6 +156,8 @@ private slots: void rejectPendingResultOverwrite_data() { rejectResultOverwrite_data(); } void rejectPendingResultOverwrite(); + void createReadyFutures(); + private: using size_type = std::vector<int>::size_type; @@ -3338,5 +3340,72 @@ void tst_QFuture::rejectPendingResultOverwrite() QCOMPARE(f.results(), initResults); } +void tst_QFuture::createReadyFutures() +{ + // using const T & + { + const int val = 42; + QFuture<int> f = QtFuture::makeReadyFuture(val); + QCOMPARE(f.result(), val); + } + + // using T + { + int val = 42; + QFuture<int> f = QtFuture::makeReadyFuture(val); + QCOMPARE(f.result(), val); + } + + // using T && + { + auto f = QtFuture::makeReadyFuture(std::make_unique<int>(42)); + QCOMPARE(*f.takeResult(), 42); + } + + // using void + { + auto f = QtFuture::makeReadyFuture(); + QVERIFY(f.isStarted()); + QVERIFY(!f.isRunning()); + QVERIFY(f.isFinished()); + } + + // using const QList<T> & + { + const QList<int> values { 1, 2, 3 }; + auto f = QtFuture::makeReadyFuture(values); + QCOMPARE(f.resultCount(), 3); + QCOMPARE(f.results(), values); + } + +#ifndef QT_NO_EXCEPTIONS + // using QException + { + QException e; + auto f = QtFuture::makeExceptionalFuture<int>(e); + bool caught = false; + try { + f.result(); + } catch (QException &) { + caught = true; + } + QVERIFY(caught); + } + + // using std::exception_ptr and QFuture<void> + { + auto exception = std::make_exception_ptr(TestException()); + auto f = QtFuture::makeExceptionalFuture(exception); + bool caught = false; + try { + f.waitForFinished(); + } catch (TestException &) { + caught = true; + } + QVERIFY(caught); + } +#endif +} + QTEST_MAIN(tst_QFuture) #include "tst_qfuture.moc" |