LeechCraft  0.6.70-10870-g558588d6ec
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  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "futurestest.h"
31 #include <QEventLoop>
32 #include <QtTest>
33 #include <futures.h>
34 #include "common.h"
35 
37 
38 namespace LeechCraft
39 {
40 namespace Util
41 {
42  void FuturesTest::testSequencer ()
43  {
44  QEventLoop loop;
45  int res = 0;
46  Sequence (nullptr, MkWaiter () (25))
47  .Then (MkWaiter ())
48  .Then (MkWaiter ())
49  .Then (MkWaiter ())
50  .Then ([&loop, &res] (int cnt)
51  {
52  res = cnt;
53  loop.quit ();
54  });
55 
56  loop.exec ();
57 
58  QCoreApplication::processEvents ();
59 
60  QCOMPARE (res, 400);
61  }
62 
63  void FuturesTest::testHeterogeneousTypes ()
64  {
65  struct Bar {};
66  struct Baz {};
67 
68  QEventLoop loop;
69  bool executed = false;
70  Sequence (nullptr, MkWaiter () (50)) >>
71  [] (int) { return MakeReadyFuture<Bar> ({}); } >>
72  [] (Bar) { return MakeReadyFuture<Baz> ({}); } >>
73  [&executed, &loop] (Baz)
74  {
75  executed = true;
76  loop.quit ();
77  };
78 
79  loop.exec ();
80 
81  QCoreApplication::processEvents ();
82 
83  QCOMPARE (executed, true);
84  }
85 
86  void FuturesTest::testDestruction ()
87  {
88  QEventLoop loop;
89  bool executed = false;
90 
91  {
92  QObject obj;
93  Sequence (&obj, MakeReadyFuture (0)) >>
94  [&executed, &loop] (int)
95  {
96  executed = true;
97  loop.quit ();
98  };
99  }
100 
101  QTimer::singleShot (100, &loop, SLOT (quit ()));
102 
103  loop.exec ();
104 
105  QCoreApplication::processEvents ();
106 
107  QCOMPARE (executed, false);
108  }
109 
110  void FuturesTest::testDestructionHandler ()
111  {
112  const auto finished = 1;
113  const auto destructed = 2;
114 
115  QEventLoop loop;
116  bool executed = false;
117  int value = 0;
118 
119  QFuture<int> future;
120  {
121  QObject obj;
122  future = Sequence (&obj, MkWaiter () (100))
123  .DestructionValue ([] { return destructed; }) >>
124  [=] (int) { return MakeReadyFuture (finished); };
125  }
126  Sequence (nullptr, future) >>
127  [&executed, &value, &loop] (int val)
128  {
129  value = val;
130  executed = true;
131  loop.quit ();
132  };
133 
134  QTimer::singleShot (10, &loop, SLOT (quit ()));
135 
136  loop.exec ();
137 
138  QCoreApplication::processEvents ();
139 
140  QCOMPARE (executed, true);
141  QCOMPARE (value, destructed);
142  }
143 
144  void FuturesTest::testNoDestrHandler ()
145  {
146  struct Bar {};
147  struct Baz {};
148 
149  QEventLoop loop;
150  bool executed = false;
151  Sequence (nullptr, MkWaiter () (50))
152  .DestructionValue ([&executed] { executed = true; }) >>
153  [] (int) { return MakeReadyFuture<Bar> ({}); } >>
154  [] (Bar) { return MakeReadyFuture<Baz> ({}); } >>
155  [&loop] (Baz) { loop.quit (); };
156 
157  loop.exec ();
158 
159  QCoreApplication::processEvents ();
160 
161  QCOMPARE (executed, false);
162  }
163 
164  void FuturesTest::testNoDestrHandlerSetBuildable ()
165  {
166  const auto finished = 1;
167 
168  QEventLoop loop;
169  bool executed = false;
170  int value = 0;
171 
172  QFuture<int> future = Sequence (nullptr, MkWaiter () (10)) >>
173  [=] (int) { return MakeReadyFuture (finished); };
174  Sequence (nullptr, future) >>
175  [&executed, &value, &loop] (int val)
176  {
177  value = val;
178  executed = true;
179  loop.quit ();
180  };
181 
182  loop.exec ();
183 
184  QCoreApplication::processEvents ();
185 
186  QCOMPARE (executed, true);
187  QCOMPARE (value, finished);
188  }
189 
190  void FuturesTest::testMulti ()
191  {
192  QEventLoop loop;
193 
194  QFutureInterface<int> iface;
195 
196  int count = 0;
197  int sum = 0;
198  Sequence (nullptr, iface.future ())
199  .MultipleResults ([&] (int sub)
200  {
201  sum += sub;
202  ++count;
203  },
204  [&] { loop.quit (); });
205 
206  iface.reportStarted ();
207  iface.setProgressRange (0, 2);
208  iface.reportResult (1, 0);
209  iface.reportResult (2, 1);
210  iface.reportResult (3, 2);
211  iface.reportFinished ();
212 
213  loop.exec ();
214 
215  QCoreApplication::processEvents ();
216 
217  QCOMPARE (count, 3);
218  QCOMPARE (sum, 6);
219  }
220 }
221 }
constexpr detail::AggregateType< detail::AggregateFunction::Count > count
Definition: oral.h:936
auto MkWaiter()
Definition: common.h:40