summaryrefslogtreecommitdiffstats
path: root/async/autotests/asynctest.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'async/autotests/asynctest.cpp')
-rw-r--r--async/autotests/asynctest.cpp140
1 files changed, 140 insertions, 0 deletions
diff --git a/async/autotests/asynctest.cpp b/async/autotests/asynctest.cpp
new file mode 100644
index 0000000..7aedfc4
--- /dev/null
+++ b/async/autotests/asynctest.cpp
@@ -0,0 +1,140 @@
1/*
2 * Copyright 2014 Daniel Vrátil <dvratil@redhat.com>
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of
7 * the License or (at your option) version 3 or any later version
8 * accepted by the membership of KDE e.V. (or its successor approved
9 * by the membership of KDE e.V.), which shall act as a proxy
10 * defined in Section 14 of version 3 of the license.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 */
21
22#include "../src/async.h"
23
24#include <QObject>
25#include <QString>
26#include <QTimer>
27#include <QtTest/QTest>
28
29class AsyncTest : public QObject
30{
31 Q_OBJECT
32
33public:
34 AsyncTest()
35 {}
36
37 ~AsyncTest()
38 {}
39
40private Q_SLOTS:
41 void testSyncPromises();
42 void testAsyncPromises();
43 void testSyncEach();
44 void testSyncReduce();
45};
46
47void AsyncTest::testSyncPromises()
48{
49 auto baseJob = Async::start<int>(
50 [](Async::Future<int> &f) {
51 f.setValue(42);
52 f.setFinished();
53 })
54 .then<QString, int>(
55 [](int v, Async::Future<QString> &f) {
56 f.setValue("Result is " + QString::number(v));
57 f.setFinished();
58 });
59
60 auto job = baseJob.then<QString, QString>(
61 [](const QString &v, Async::Future<QString> &f) {
62 f.setValue(v.toUpper());
63 f.setFinished();
64 });
65
66 job.exec();
67 Async::Future<QString> future = job.result();
68
69 QVERIFY(future.isFinished());
70 QCOMPARE(future.value(), QString::fromLatin1("RESULT IS 42"));
71}
72
73void AsyncTest::testAsyncPromises()
74{
75 auto job = Async::start<int>(
76 [](Async::Future<int> &future) {
77 QTimer *timer = new QTimer();
78 QObject::connect(timer, &QTimer::timeout,
79 [&]() {
80 future.setValue(42);
81 future.setFinished();
82 });
83 QObject::connect(timer, &QTimer::timeout,
84 timer, &QObject::deleteLater);
85 timer->setSingleShot(true);
86 timer->start(200);
87 });
88
89 job.exec();
90 Async::Future<int> future = job.result();
91 QVERIFY(future.isFinished());
92 QCOMPARE(future.value(), 42);
93}
94
95void AsyncTest::testSyncEach()
96{
97 auto job = Async::start<QList<int>>(
98 [](Async::Future<QList<int>> &future) {
99 future.setValue(QList<int>{ 1, 2, 3, 4 });
100 future.setFinished();
101 })
102 .each<QList<int>, int>(
103 [](const int &v, Async::Future<QList<int>> &future) {
104 future.setValue(QList<int>{ v + 1 });
105 future.setFinished();
106 });
107
108 job.exec();
109 Async::Future<QList<int>> future = job.result();
110 const QList<int> expected({ 2, 3, 4, 5 });
111 QVERIFY(future.isFinished());
112 QCOMPARE(future.value(), expected);
113}
114
115void 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
137
138QTEST_MAIN(AsyncTest);
139
140#include "asynctest.moc"