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