]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/thread/test/sync/futures/when_any/variadic_pass.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / thread / test / sync / futures / when_any / variadic_pass.cpp
CommitLineData
7c673cae
FG
1//===----------------------------------------------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// Copyright (C) 2014 Vicente J. Botet Escriba
11//
12// Distributed under the Boost Software License, Version 1.0. (See accompanying
13// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
14
15// <boost/thread/future.hpp>
16
17// template <class T, class Ts>
18// future<tuple<T, Ts...>> when_any(T&&, Ts&& ...);
19
20#include <boost/config.hpp>
21
22#if ! defined BOOST_NO_CXX11_DECLTYPE
23#define BOOST_RESULT_OF_USE_DECLTYPE
24#endif
25
26#define BOOST_THREAD_VERSION 4
27
28#include <boost/thread/future.hpp>
29#include <boost/detail/lightweight_test.hpp>
30#include <stdexcept>
31
b32b8144
FG
32#ifdef BOOST_MSVC
33#pragma warning(disable: 4127) // conditional expression is constant
34#endif
35
7c673cae
FG
36int p1()
37{
38 return 123;
39}
40
41int thr()
42{
43 throw std::logic_error("123");
44}
45int p2()
46{
47 boost::this_thread::sleep_for(boost::chrono::milliseconds(200));
48 return 321;
49}
50
51int main()
52{
53#if defined BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
54 if (0) // todo not yet implemented
55 { // invalid future copy-constructible
56 boost::future<int> f1;
57 boost::future<int> f2 = boost::make_ready_future(321);
58 BOOST_TEST(! f1.valid());
59 BOOST_TEST(f2.valid());
60 boost::future<boost::csbl::tuple<boost::future<int>,boost::future<int> > > all = boost::when_any(boost::move(f1), boost::move(f2));
61 BOOST_TEST(! f1.valid());
62 BOOST_TEST(! f2.valid());
63 BOOST_TEST(all.valid());
64 boost::csbl::tuple<boost::future<int>,boost::future<int> > res = all.get();
65 BOOST_TEST(boost::csbl::get<0>(res).valid());
66 BOOST_TEST(boost::csbl::get<1>(res).valid());
67 BOOST_TEST(boost::csbl::get<0>(res).is_ready() || boost::csbl::get<1>(res).is_ready());
68 // has exception
69 //BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
70 BOOST_TEST(boost::csbl::get<1>(res).get() == 321);
71 }
72 { // is_ready future copy-constructible
73 boost::future<int> f1 = boost::make_ready_future(123);
74 boost::future<int> f2 = boost::make_ready_future(321);
75 BOOST_TEST(f1.valid());
76 BOOST_TEST(f1.is_ready());
77 BOOST_TEST(f2.valid());
78 BOOST_TEST(f2.is_ready());
79 boost::future<boost::csbl::tuple<boost::future<int>,boost::future<int> > > all = boost::when_any(boost::move(f1), boost::move(f2));
80 BOOST_TEST(! f1.valid());
81 BOOST_TEST(! f2.valid());
82 BOOST_TEST(all.valid());
83 if (0) // todo FAILS not yet implemented
84 BOOST_TEST(all.is_ready());
85 boost::csbl::tuple<boost::future<int>,boost::future<int> > res = all.get();
86 BOOST_TEST(boost::csbl::get<0>(res).valid());
87 BOOST_TEST(boost::csbl::get<1>(res).valid());
88 BOOST_TEST(boost::csbl::get<0>(res).is_ready() || boost::csbl::get<1>(res).is_ready());
89 BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
90 BOOST_TEST(boost::csbl::get<1>(res).get() == 321);
91 }
92 { // is_ready shared_future copy-constructible
93 boost::shared_future<int> f1 = boost::make_ready_future(123).share();
94 boost::shared_future<int> f2 = boost::make_ready_future(321).share();
95 BOOST_TEST(f1.valid());
96 BOOST_TEST(f1.is_ready());
97 BOOST_TEST(f2.valid());
98 BOOST_TEST(f2.is_ready());
99 boost::future<boost::csbl::tuple<boost::shared_future<int>,boost::shared_future<int> > > all = boost::when_any(f1, f2);
100 BOOST_TEST(f1.valid());
101 BOOST_TEST(f2.valid());
102 BOOST_TEST(all.valid());
103 if (0) // todo FAILS not yet implemented
104 BOOST_TEST(all.is_ready());
105 boost::csbl::tuple<boost::shared_future<int>,boost::shared_future<int> > res = all.get();
106 BOOST_TEST(boost::csbl::get<0>(res).valid());
107 BOOST_TEST(boost::csbl::get<1>(res).valid());
108 BOOST_TEST(boost::csbl::get<0>(res).is_ready() || boost::csbl::get<1>(res).is_ready());
109 BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
110 BOOST_TEST(boost::csbl::get<1>(res).get() == 321);
111 }
112 { // packaged_task future copy-constructible
113 boost::packaged_task<int()> pt1(&p1);
114 boost::future<int> f1 = pt1.get_future();
115 BOOST_TEST(f1.valid());
116 boost::packaged_task<int()> pt2(&p2);
117 boost::future<int> f2 = pt2.get_future();
118 BOOST_TEST(f2.valid());
119 boost::future<boost::csbl::tuple<boost::future<int>,boost::future<int> > > all = boost::when_any(boost::move(f1), boost::move(f2));
120 BOOST_TEST(! f1.valid());
121 BOOST_TEST(! f2.valid());
122 BOOST_TEST(all.valid());
123 pt1();
124 pt2();
125 boost::csbl::tuple<boost::future<int>,boost::future<int> > res = all.get();
126 BOOST_TEST(boost::csbl::get<0>(res).valid());
127 BOOST_TEST(boost::csbl::get<1>(res).valid());
128 BOOST_TEST(boost::csbl::get<0>(res).is_ready() || boost::csbl::get<1>(res).is_ready());
129 BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
130 BOOST_TEST(boost::csbl::get<1>(res).get() == 321);
131 }
132 { // packaged_task future copy-constructible
133 boost::packaged_task<int()> pt1(&thr);
134 boost::future<int> f1 = pt1.get_future();
135 BOOST_TEST(f1.valid());
136 boost::packaged_task<int()> pt2(&p2);
137 boost::future<int> f2 = pt2.get_future();
138 BOOST_TEST(f2.valid());
139 boost::future<boost::csbl::tuple<boost::future<int>,boost::future<int> > > all = boost::when_any(boost::move(f1), boost::move(f2));
140 BOOST_TEST(! f1.valid());
141 BOOST_TEST(! f2.valid());
142 BOOST_TEST(all.valid());
143 pt1();
144 pt2();
145 boost::csbl::tuple<boost::future<int>,boost::future<int> > res = all.get();
146 BOOST_TEST(boost::csbl::get<0>(res).valid());
147 BOOST_TEST(boost::csbl::get<1>(res).valid());
148 BOOST_TEST(boost::csbl::get<0>(res).is_ready() || boost::csbl::get<1>(res).is_ready());
149 try {
150 boost::csbl::get<0>(res).get();
151 BOOST_TEST(false);
152 } catch (std::logic_error& ex) {
153 BOOST_TEST(ex.what() == std::string("123"));
154 } catch (...) {
155 BOOST_TEST(false);
156 }
157 BOOST_TEST(boost::csbl::get<1>(res).get() == 321);
158 }
159 { // packaged_task shared_future copy-constructible
160 boost::packaged_task<int()> pt1(&p1);
161 boost::shared_future<int> f1 = pt1.get_future().share();
162 BOOST_TEST(f1.valid());
163 boost::packaged_task<int()> pt2(&p2);
164 boost::shared_future<int> f2 = pt2.get_future().share();
165 BOOST_TEST(f2.valid());
166 boost::future<boost::csbl::tuple<boost::shared_future<int>,boost::shared_future<int> > > all = boost::when_any(f1, f2);
167 BOOST_TEST(f1.valid());
168 BOOST_TEST(f2.valid());
169 BOOST_TEST(all.valid());
170 BOOST_TEST(! all.is_ready());
171 pt1();
172 BOOST_TEST(! all.is_ready());
173 pt2();
174 boost::this_thread::sleep_for(boost::chrono::milliseconds(300));
175 BOOST_TEST(all.is_ready());
176 boost::csbl::tuple<boost::shared_future<int>,boost::shared_future<int> > res = all.get();
177 BOOST_TEST(boost::csbl::get<0>(res).valid());
178 BOOST_TEST(boost::csbl::get<1>(res).valid());
179 BOOST_TEST(boost::csbl::get<0>(res).is_ready() || boost::csbl::get<1>(res).is_ready());
180 BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
181 BOOST_TEST(boost::csbl::get<1>(res).get() == 321);
182 }
183 { // async future copy-constructible
184 boost::future<int> f1 = boost::async(boost::launch::async, &p1);
185 BOOST_TEST(f1.valid());
186 boost::future<int> f2 = boost::async(boost::launch::async, &p2);
187 BOOST_TEST(f2.valid());
188 boost::future<boost::csbl::tuple<boost::future<int>,boost::future<int> > > all = boost::when_any(boost::move(f1), boost::move(f2));
189 BOOST_TEST(! f1.valid());
190 BOOST_TEST(! f2.valid());
191 BOOST_TEST(all.valid());
192 boost::csbl::tuple<boost::future<int>,boost::future<int> > res = all.get();
193 BOOST_TEST(boost::csbl::get<0>(res).valid());
194 BOOST_TEST(boost::csbl::get<0>(res).is_ready() || boost::csbl::get<1>(res).is_ready());
195 BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
196 BOOST_TEST(boost::csbl::get<1>(res).get() == 321);
197 }
198 { // async shared_future copy-constructible
199 boost::shared_future<int> f1 = boost::async(boost::launch::async, &p1).share();
200 BOOST_TEST(f1.valid());
201 boost::shared_future<int> f2 = boost::async(boost::launch::async, &p2).share();
202 BOOST_TEST(f2.valid());
203 boost::future<boost::csbl::tuple<boost::shared_future<int>,boost::shared_future<int> > > all = boost::when_any(f1, f2);
204 BOOST_TEST(f1.valid());
205 BOOST_TEST(f2.valid());
206 BOOST_TEST(all.valid());
207 boost::csbl::tuple<boost::shared_future<int>,boost::shared_future<int> > res = all.get();
208 BOOST_TEST(boost::csbl::get<0>(res).valid());
209 BOOST_TEST(boost::csbl::get<1>(res).valid());
210 BOOST_TEST(boost::csbl::get<0>(res).is_ready() || boost::csbl::get<1>(res).is_ready());
211 BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
212 BOOST_TEST(boost::csbl::get<1>(res).get() == 321);
213 }
214 { // async future copy-constructible
215 boost::future<int> f1 = boost::async(boost::launch::async, &p1);
216 BOOST_TEST(f1.valid());
217 boost::future<int> f2 = boost::make_ready_future(321);
218 BOOST_TEST(f2.valid());
219 BOOST_TEST(f2.is_ready());
220 boost::future<boost::csbl::tuple<boost::future<int>,boost::future<int> > > all = boost::when_any(boost::move(f1), boost::move(f2));
221 BOOST_TEST(! f1.valid());
222 BOOST_TEST(! f2.valid());
223 BOOST_TEST(all.valid());
224 boost::csbl::tuple<boost::future<int>,boost::future<int> > res = all.get();
225 BOOST_TEST(boost::csbl::get<0>(res).valid());
226 //BOOST_TEST(boost::csbl::get<0>(res).is_ready());
227 BOOST_TEST(boost::csbl::get<1>(res).valid());
228 BOOST_TEST(boost::csbl::get<1>(res).is_ready());
229 BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
230 BOOST_TEST(boost::csbl::get<1>(res).get() == 321);
231 }
232#if defined BOOST_THREAD_PROVIDES_VARIADIC_THREAD
233 // fixme darwin-4.8.0_11 terminate called without an active exception
234 { // deferred future copy-constructible
235 boost::future<int> f1 = boost::async(boost::launch::deferred, &p1);
236 boost::future<int> f2 = boost::async(boost::launch::deferred, &p2);
237 std::cout << __FILE__ << " " << __LINE__ << std::endl;
238
239 boost::future<boost::csbl::tuple<boost::future<int>,boost::future<int> > > all = boost::when_any(boost::move(f1), boost::move(f2));
240 std::cout << __FILE__ << " " << __LINE__ << std::endl;
241 BOOST_TEST(! f1.valid());
242 BOOST_TEST(! f2.valid());
243 BOOST_TEST(all.valid());
244 std::cout << __FILE__ << " " << __LINE__ << std::endl;
245 boost::csbl::tuple<boost::future<int>,boost::future<int> > res = all.get();
246 std::cout << __FILE__ << " " << __LINE__ << std::endl;
247 BOOST_TEST(boost::csbl::get<0>(res).valid());
248 BOOST_TEST(boost::csbl::get<1>(res).valid());
249 BOOST_TEST(boost::csbl::get<0>(res).is_ready() || boost::csbl::get<1>(res).is_ready());
250 BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
251 BOOST_TEST(boost::csbl::get<1>(res).get() == 321);
252 }
253 // fixme darwin-4.8.0_11 terminate called without an active exception
254 { // deferred future copy-constructible
255 boost::future<int> f1 = boost::async(boost::launch::deferred, &p1);
256 boost::future<int> f2 = boost::async(boost::launch::async, &p2);
257 boost::future<boost::csbl::tuple<boost::future<int>,boost::future<int> > > all = boost::when_any(boost::move(f1), boost::move(f2));
258 BOOST_TEST(! f1.valid());
259 BOOST_TEST(! f2.valid());
260 BOOST_TEST(all.valid());
261 boost::csbl::tuple<boost::future<int>,boost::future<int> > res = all.get();
262 BOOST_TEST(boost::csbl::get<0>(res).valid());
263 BOOST_TEST(boost::csbl::get<0>(res).is_ready());
264 BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
265 BOOST_TEST(boost::csbl::get<1>(res).valid());
266 //BOOST_TEST(! boost::csbl::get<1>(res).is_ready());
267 BOOST_TEST(boost::csbl::get<1>(res).get() == 321);
268 }
269 // fixme darwin-4.8.0_11 terminate called without an active exception
270 { // deferred future copy-constructible
271 boost::future<int> f1 = boost::async(boost::launch::async, &p1);
272 boost::future<int> f2 = boost::async(boost::launch::deferred, &p2);
273 boost::future<boost::csbl::tuple<boost::future<int>,boost::future<int> > > all = boost::when_any(boost::move(f1), boost::move(f2));
274 BOOST_TEST(! f1.valid());
275 BOOST_TEST(! f2.valid());
276 BOOST_TEST(all.valid());
277 boost::csbl::tuple<boost::future<int>,boost::future<int> > res = all.get();
278 BOOST_TEST(boost::csbl::get<0>(res).valid());
279 //BOOST_TEST(boost::csbl::get<0>(res).is_ready());
280 BOOST_TEST(boost::csbl::get<1>(res).valid());
281 BOOST_TEST(boost::csbl::get<1>(res).is_ready());
282 BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
283 BOOST_TEST(boost::csbl::get<1>(res).get() == 321);
284 }
285 // fixme darwin-4.8.0_11 terminate called without an active exception
286 { // deferred shared_future copy-constructible
287 boost::shared_future<int> f1 = boost::async(boost::launch::deferred, &p1).share();
288 boost::shared_future<int> f2 = boost::async(boost::launch::deferred, &p2).share();
289 boost::future<boost::csbl::tuple<boost::shared_future<int>,boost::shared_future<int> > > all = boost::when_any(f1, f2);
290 BOOST_TEST(f1.valid());
291 BOOST_TEST(f2.valid());
292 BOOST_TEST(all.valid());
293 boost::csbl::tuple<boost::shared_future<int>,boost::shared_future<int> > res = all.get();
294 BOOST_TEST(boost::csbl::get<0>(res).valid());
295 BOOST_TEST(boost::csbl::get<1>(res).valid());
296 BOOST_TEST(boost::csbl::get<0>(res).is_ready() || boost::csbl::get<1>(res).is_ready());
297 BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
298 BOOST_TEST(boost::csbl::get<1>(res).get() == 321);
299 }
300#endif
301#if ! defined BOOST_NO_CXX11_LAMBDAS
302 { // async futures copy-constructible then()
303 boost::future<int> f1 = boost::async(boost::launch::async, &p1);
304 BOOST_TEST(f1.valid());
305 boost::future<int> f2 = boost::async(boost::launch::async, &p2);
306 BOOST_TEST(f2.valid());
307 boost::future<boost::csbl::tuple<boost::future<int>,boost::future<int> > > all = boost::when_any(boost::move(f1), boost::move(f2));
308 BOOST_TEST(! f1.valid());
309 BOOST_TEST(! f2.valid());
310 BOOST_TEST(all.valid());
311 boost::future<int> sum = all.then([](boost::future<boost::csbl::tuple<boost::future<int>, boost::future<int> > > f)
312 {
313 boost::csbl::tuple<boost::future<int>,boost::future<int> > v = f.get();
314 return boost::csbl::get<0>(v).get()+boost::csbl::get<1>(v).get();
315 });
316 BOOST_TEST(sum.valid());
317 BOOST_TEST(sum.get() == 444);
318 }
319#endif
320#endif
321
322 return boost::report_errors();
323}
324