variadic_pass.cpp 14 KB

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