From e0ba51543037a026e2a8d483dfdc0e196e9dc59e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Vr=C3=A1til?= Date: Thu, 11 Dec 2014 16:09:28 +0100 Subject: Async: add Reduce task --- async/autotests/asynctest.cpp | 50 ++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 12 deletions(-) (limited to 'async/autotests') diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp index 403fb83..7aedfc4 100644 --- a/async/autotests/asynctest.cpp +++ b/async/autotests/asynctest.cpp @@ -41,6 +41,7 @@ private Q_SLOTS: void testSyncPromises(); void testAsyncPromises(); void testSyncEach(); + void testSyncReduce(); }; void AsyncTest::testSyncPromises() @@ -65,27 +66,29 @@ void AsyncTest::testSyncPromises() job.exec(); Async::Future future = job.result(); + QVERIFY(future.isFinished()); QCOMPARE(future.value(), QString::fromLatin1("RESULT IS 42")); } void AsyncTest::testAsyncPromises() { auto job = Async::start( - [](Async::Future &future) { - QTimer *timer = new QTimer(); - QObject::connect(timer, &QTimer::timeout, - [&]() { - future.setValue(42); - future.setFinished(); - }); - QObject::connect(timer, &QTimer::timeout, - timer, &QObject::deleteLater); - timer->setSingleShot(true); - timer->start(200); - }); + [](Async::Future &future) { + QTimer *timer = new QTimer(); + QObject::connect(timer, &QTimer::timeout, + [&]() { + future.setValue(42); + future.setFinished(); + }); + QObject::connect(timer, &QTimer::timeout, + timer, &QObject::deleteLater); + timer->setSingleShot(true); + timer->start(200); + }); job.exec(); Async::Future future = job.result(); + QVERIFY(future.isFinished()); QCOMPARE(future.value(), 42); } @@ -105,9 +108,32 @@ void AsyncTest::testSyncEach() job.exec(); Async::Future> future = job.result(); const QList expected({ 2, 3, 4, 5 }); + QVERIFY(future.isFinished()); QCOMPARE(future.value(), expected); } +void AsyncTest::testSyncReduce() +{ + auto job = Async::start>( + [](Async::Future> &future) { + future.setValue(QList{ 1, 2, 3, 4 }); + future.setFinished(); + }) + .reduce>( + [](const QList &list, Async::Future &future) { + int sum = 0; + for (int i : list) sum += i; + future.setValue(sum); + future.setFinished(); + }); + + job.exec(); + Async::Future future = job.result(); + QVERIFY(future.isFinished()); + QCOMPARE(future.value(), 10); +} + + QTEST_MAIN(AsyncTest); -- cgit v1.2.3