diff options
author | Dan Vrátil <dvratil@redhat.com> | 2014-12-11 16:09:28 +0100 |
---|---|---|
committer | Dan Vrátil <dvratil@redhat.com> | 2014-12-12 13:24:11 +0100 |
commit | e0ba51543037a026e2a8d483dfdc0e196e9dc59e (patch) | |
tree | 17bad6e8b23249ed4df7731258f2de9700e2fbf8 /async/autotests/asynctest.cpp | |
parent | b30fe2fa42a717d6e89710cde82ecf7f419b2fe9 (diff) | |
download | sink-e0ba51543037a026e2a8d483dfdc0e196e9dc59e.tar.gz sink-e0ba51543037a026e2a8d483dfdc0e196e9dc59e.zip |
Async: add Reduce task
Diffstat (limited to 'async/autotests/asynctest.cpp')
-rw-r--r-- | async/autotests/asynctest.cpp | 50 |
1 files changed, 38 insertions, 12 deletions
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: | |||
41 | void testSyncPromises(); | 41 | void testSyncPromises(); |
42 | void testAsyncPromises(); | 42 | void testAsyncPromises(); |
43 | void testSyncEach(); | 43 | void testSyncEach(); |
44 | void testSyncReduce(); | ||
44 | }; | 45 | }; |
45 | 46 | ||
46 | void AsyncTest::testSyncPromises() | 47 | void AsyncTest::testSyncPromises() |
@@ -65,27 +66,29 @@ void AsyncTest::testSyncPromises() | |||
65 | job.exec(); | 66 | job.exec(); |
66 | Async::Future<QString> future = job.result(); | 67 | Async::Future<QString> future = job.result(); |
67 | 68 | ||
69 | QVERIFY(future.isFinished()); | ||
68 | QCOMPARE(future.value(), QString::fromLatin1("RESULT IS 42")); | 70 | QCOMPARE(future.value(), QString::fromLatin1("RESULT IS 42")); |
69 | } | 71 | } |
70 | 72 | ||
71 | void AsyncTest::testAsyncPromises() | 73 | void AsyncTest::testAsyncPromises() |
72 | { | 74 | { |
73 | auto job = Async::start<int>( | 75 | auto job = Async::start<int>( |
74 | [](Async::Future<int> &future) { | 76 | [](Async::Future<int> &future) { |
75 | QTimer *timer = new QTimer(); | 77 | QTimer *timer = new QTimer(); |
76 | QObject::connect(timer, &QTimer::timeout, | 78 | QObject::connect(timer, &QTimer::timeout, |
77 | [&]() { | 79 | [&]() { |
78 | future.setValue(42); | 80 | future.setValue(42); |
79 | future.setFinished(); | 81 | future.setFinished(); |
80 | }); | 82 | }); |
81 | QObject::connect(timer, &QTimer::timeout, | 83 | QObject::connect(timer, &QTimer::timeout, |
82 | timer, &QObject::deleteLater); | 84 | timer, &QObject::deleteLater); |
83 | timer->setSingleShot(true); | 85 | timer->setSingleShot(true); |
84 | timer->start(200); | 86 | timer->start(200); |
85 | }); | 87 | }); |
86 | 88 | ||
87 | job.exec(); | 89 | job.exec(); |
88 | Async::Future<int> future = job.result(); | 90 | Async::Future<int> future = job.result(); |
91 | QVERIFY(future.isFinished()); | ||
89 | QCOMPARE(future.value(), 42); | 92 | QCOMPARE(future.value(), 42); |
90 | } | 93 | } |
91 | 94 | ||
@@ -105,9 +108,32 @@ void AsyncTest::testSyncEach() | |||
105 | job.exec(); | 108 | job.exec(); |
106 | Async::Future<QList<int>> future = job.result(); | 109 | Async::Future<QList<int>> future = job.result(); |
107 | const QList<int> expected({ 2, 3, 4, 5 }); | 110 | const QList<int> expected({ 2, 3, 4, 5 }); |
111 | QVERIFY(future.isFinished()); | ||
108 | QCOMPARE(future.value(), expected); | 112 | QCOMPARE(future.value(), expected); |
109 | } | 113 | } |
110 | 114 | ||
115 | void AsyncTest::testSyncReduce() | ||
116 | { | ||
117 | auto job = Async::start<QList<int>>( | ||
118 | [](Async::Future<QList<int>> &future) { | ||
119 | future.setValue(QList<int>{ 1, 2, 3, 4 }); | ||
120 | future.setFinished(); | ||
121 | }) | ||
122 | .reduce<int, QList<int>>( | ||
123 | [](const QList<int> &list, Async::Future<int> &future) { | ||
124 | int sum = 0; | ||
125 | for (int i : list) sum += i; | ||
126 | future.setValue(sum); | ||
127 | future.setFinished(); | ||
128 | }); | ||
129 | |||
130 | job.exec(); | ||
131 | Async::Future<int> future = job.result(); | ||
132 | QVERIFY(future.isFinished()); | ||
133 | QCOMPARE(future.value(), 10); | ||
134 | } | ||
135 | |||
136 | |||
111 | 137 | ||
112 | QTEST_MAIN(AsyncTest); | 138 | QTEST_MAIN(AsyncTest); |
113 | 139 | ||