LeechCraft 0.6.70-19424-g027e980fd1
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
futurestest.cpp
Go to the documentation of this file.
1/**********************************************************************
2 * LeechCraft - modular cross-platform feature rich internet client.
3 * Copyright (C) 2006-2014 Georg Rudoy
4 *
5 * Distributed under the Boost Software License, Version 1.0.
6 * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7 **********************************************************************/
8
9#include "futurestest.h"
10#include <QEventLoop>
11#include <QTest>
12#include <QTimer>
13#include <futures.h>
14#include "common.h"
15
16QTEST_GUILESS_MAIN (LC::Util::FuturesTest)
17
18namespace LC::Util
19{
20 void FuturesTest::testSequencer ()
21 {
22 QEventLoop loop;
23 int res = 0;
24 Sequence (nullptr, MkWaiter () (25))
25 .Then (MkWaiter ())
26 .Then (MkWaiter ())
27 .Then (MkWaiter ())
28 .Then ([&loop, &res] (int cnt)
29 {
30 res = cnt;
31 loop.quit ();
32 });
33
34 loop.exec ();
35
36 QCoreApplication::processEvents ();
37
38 QCOMPARE (res, 400);
39 }
40
41 void FuturesTest::testHeterogeneousTypes ()
42 {
43 struct Bar {};
44 struct Baz {};
45
46 QEventLoop loop;
47 bool executed = false;
48 Sequence (nullptr, MkWaiter () (50)) >>
49 [] (int) { return MakeReadyFuture<Bar> ({}); } >>
50 [] (Bar) { return MakeReadyFuture<Baz> ({}); } >>
51 [&executed, &loop] (Baz)
52 {
53 executed = true;
54 loop.quit ();
55 };
56
57 loop.exec ();
58
59 QCoreApplication::processEvents ();
60
61 QCOMPARE (executed, true);
62 }
63
64 void FuturesTest::testDestruction ()
65 {
66 QEventLoop loop;
67 bool executed = false;
68
69 {
70 QObject obj;
71 Sequence (&obj, MakeReadyFuture (0)) >>
72 [&executed, &loop] (int)
73 {
74 executed = true;
75 loop.quit ();
76 };
77 }
78
79 QTimer::singleShot (100, &loop, SLOT (quit ()));
80
81 loop.exec ();
82
83 QCoreApplication::processEvents ();
84
85 QCOMPARE (executed, false);
86 }
87
88 void FuturesTest::testDestructionHandler ()
89 {
90 const auto finished = 1;
91 const auto destructed = 2;
92
93 QEventLoop loop;
94 bool executed = false;
95 int value = 0;
96
97 QFuture<int> future;
98 {
99 QObject obj;
100 future = Sequence (&obj, MkWaiter () (100))
101 .DestructionValue ([] { return destructed; }) >>
102 [=] (int) { return MakeReadyFuture (finished); };
103 }
104 Sequence (nullptr, future) >>
105 [&executed, &value, &loop] (int val)
106 {
107 value = val;
108 executed = true;
109 loop.quit ();
110 };
111
112 QTimer::singleShot (10, &loop, SLOT (quit ()));
113
114 loop.exec ();
115
116 QCoreApplication::processEvents ();
117
118 QCOMPARE (executed, true);
119 QCOMPARE (value, destructed);
120 }
121
122 void FuturesTest::testNoDestrHandler ()
123 {
124 struct Bar {};
125 struct Baz {};
126
127 QEventLoop loop;
128 bool executed = false;
129 Sequence (nullptr, MkWaiter () (50))
130 .DestructionValue ([&executed] { executed = true; }) >>
131 [] (int) { return MakeReadyFuture<Bar> ({}); } >>
132 [] (Bar) { return MakeReadyFuture<Baz> ({}); } >>
133 [&loop] (Baz) { loop.quit (); };
134
135 loop.exec ();
136
137 QCoreApplication::processEvents ();
138
139 QCOMPARE (executed, false);
140 }
141
142 void FuturesTest::testNoDestrHandlerSetBuildable ()
143 {
144 const auto finished = 1;
145
146 QEventLoop loop;
147 bool executed = false;
148 int value = 0;
149
150 QFuture<int> future = Sequence (nullptr, MkWaiter () (10)) >>
151 [=] (int) { return MakeReadyFuture (finished); };
152 Sequence (nullptr, future) >>
153 [&executed, &value, &loop] (int val)
154 {
155 value = val;
156 executed = true;
157 loop.quit ();
158 };
159
160 loop.exec ();
161
162 QCoreApplication::processEvents ();
163
164 QCOMPARE (executed, true);
165 QCOMPARE (value, finished);
166 }
167
168 void FuturesTest::testMulti ()
169 {
170 QEventLoop loop;
171
172 QFutureInterface<int> iface;
173
174 int count = 0;
175 int sum = 0;
176 Sequence (nullptr, iface.future ())
177 .MultipleResults ([&] (int sub)
178 {
179 sum += sub;
180 ++count;
181 },
182 [&] { loop.quit (); });
183
184 iface.reportStarted ();
185 iface.setExpectedResultCount (3);
186 while (iface.resultCount () < iface.expectedResultCount ())
187 iface.reportResult (iface.resultCount () + 1, iface.resultCount ());
188 iface.reportFinished ();
189
190 loop.exec ();
191
192 QCoreApplication::processEvents ();
193
194 QCOMPARE (count, 3);
195 QCOMPARE (sum, 6);
196 }
197
198 void FuturesTest::testMultiRange ()
199 {
200 QEventLoop loop;
201
202 QFutureInterface<int> iface;
203
204 int count = 0;
205 int sum = 0;
206 Sequence (nullptr, iface.future ())
207 .MultipleResults ([&] (int sub)
208 {
209 sum += sub;
210 ++count;
211 },
212 [&] { loop.quit (); });
213
214 iface.reportStarted ();
215 iface.setProgressRange (0, 2);
216 iface.reportResult (1, 0);
217 iface.reportResult (2, 1);
218 iface.reportResult (3, 2);
219 iface.reportFinished ();
220
221 loop.exec ();
222
223 QCoreApplication::processEvents ();
224
225 QCOMPARE (count, 3);
226 QCOMPARE (sum, 6);
227 }
228}
constexpr detail::AggregateType< detail::AggregateFunction::Count, Ptr > count
Definition oral.h:1100
auto MkWaiter()
Definition common.h:19