one_pass.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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>> when_all(T&&);
  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 main()
  36. {
  37. #if defined BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
  38. if (0) // todo not yet implemented
  39. { // invalid future copy-constructible
  40. boost::future<int> f1;
  41. BOOST_TEST(! f1.valid());
  42. boost::future<boost::csbl::tuple<boost::future<int> > > all = boost::when_all(boost::move(f1));
  43. BOOST_TEST(! f1.valid());
  44. BOOST_TEST(all.valid());
  45. boost::csbl::tuple<boost::future<int> > res = all.get();
  46. BOOST_TEST(boost::csbl::get<0>(res).valid());
  47. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  48. // has exception
  49. //BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  50. }
  51. { // is_ready future copy-constructible
  52. boost::future<int> f1 = boost::make_ready_future(123);
  53. BOOST_TEST(f1.valid());
  54. BOOST_TEST(f1.is_ready());
  55. boost::future<boost::csbl::tuple<boost::future<int> > > all = boost::when_all(boost::move(f1));
  56. BOOST_TEST(! f1.valid());
  57. BOOST_TEST(all.valid());
  58. if (0) // todo FAILS not yet implemented
  59. BOOST_TEST(all.is_ready());
  60. boost::csbl::tuple<boost::future<int> > res = all.get();
  61. BOOST_TEST(boost::csbl::get<0>(res).valid());
  62. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  63. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  64. }
  65. { // is_ready shared_future copy-constructible
  66. boost::shared_future<int> f1 = boost::make_ready_future(123).share();
  67. BOOST_TEST(f1.valid());
  68. BOOST_TEST(f1.is_ready());
  69. boost::future<boost::csbl::tuple<boost::shared_future<int> > > all = boost::when_all(f1);
  70. BOOST_TEST(f1.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::shared_future<int> > res = all.get();
  75. BOOST_TEST(boost::csbl::get<0>(res).valid());
  76. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  77. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  78. }
  79. { // packaged_task future copy-constructible
  80. boost::packaged_task<int()> pt1(&p1);
  81. boost::future<int> f1 = pt1.get_future();
  82. BOOST_TEST(f1.valid());
  83. boost::future<boost::csbl::tuple<boost::future<int> > > all = boost::when_all(boost::move(f1));
  84. BOOST_TEST(! f1.valid());
  85. BOOST_TEST(all.valid());
  86. pt1();
  87. boost::csbl::tuple<boost::future<int> > res = all.get();
  88. BOOST_TEST(boost::csbl::get<0>(res).valid());
  89. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  90. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  91. }
  92. { // packaged_task shared_future copy-constructible
  93. boost::packaged_task<int()> pt1(&p1);
  94. boost::shared_future<int> f1 = pt1.get_future().share();
  95. BOOST_TEST(f1.valid());
  96. boost::future<boost::csbl::tuple<boost::shared_future<int> > > all = boost::when_all(f1);
  97. BOOST_TEST(f1.valid());
  98. BOOST_TEST(all.valid());
  99. pt1();
  100. boost::csbl::tuple<boost::shared_future<int> > res = all.get();
  101. BOOST_TEST(boost::csbl::get<0>(res).valid());
  102. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  103. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  104. }
  105. { // packaged_task future copy-constructible
  106. boost::packaged_task<int()> pt1(&thr);
  107. boost::future<int> f1 = pt1.get_future();
  108. BOOST_TEST(f1.valid());
  109. boost::future<boost::csbl::tuple<boost::future<int> > > all = boost::when_all(boost::move(f1));
  110. BOOST_TEST(! f1.valid());
  111. BOOST_TEST(all.valid());
  112. pt1();
  113. boost::csbl::tuple<boost::future<int> > res = all.get();
  114. BOOST_TEST(boost::csbl::get<0>(res).valid());
  115. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  116. try {
  117. boost::csbl::get<0>(res).get();
  118. BOOST_TEST(false);
  119. } catch (std::logic_error& ex) {
  120. BOOST_TEST(ex.what() == std::string("123"));
  121. } catch (...) {
  122. BOOST_TEST(false);
  123. }
  124. }
  125. { // async future copy-constructible
  126. boost::future<int> f1 = boost::async(boost::launch::async, &p1);
  127. BOOST_TEST(f1.valid());
  128. boost::future<boost::csbl::tuple<boost::future<int> > > all = boost::when_all(boost::move(f1));
  129. BOOST_TEST(! f1.valid());
  130. BOOST_TEST(all.valid());
  131. boost::csbl::tuple<boost::future<int> > res = all.get();
  132. BOOST_TEST(boost::csbl::get<0>(res).valid());
  133. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  134. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  135. }
  136. { // async shared_future copy-constructible
  137. boost::shared_future<int> f1 = boost::async(boost::launch::async, &p1).share();
  138. BOOST_TEST(f1.valid());
  139. boost::future<boost::csbl::tuple<boost::shared_future<int> > > all = boost::when_all(f1);
  140. BOOST_TEST(f1.valid());
  141. BOOST_TEST(all.valid());
  142. boost::csbl::tuple<boost::shared_future<int> > res = all.get();
  143. BOOST_TEST(boost::csbl::get<0>(res).valid());
  144. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  145. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  146. }
  147. #if defined BOOST_THREAD_PROVIDES_VARIADIC_THREAD
  148. // fixme darwin-4.8.0_11 terminate called without an active exception
  149. { // deferred future copy-constructible
  150. boost::future<int> f1 = boost::async(boost::launch::deferred, &p1);
  151. boost::future<boost::csbl::tuple<boost::future<int> > > all = boost::when_all(boost::move(f1));
  152. BOOST_TEST(! f1.valid());
  153. BOOST_TEST(all.valid());
  154. boost::csbl::tuple<boost::future<int> > res = all.get();
  155. BOOST_TEST(boost::csbl::get<0>(res).valid());
  156. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  157. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  158. }
  159. // fixme darwin-4.8.0_11 terminate called without an active exception
  160. { // deferred shared_future copy-constructible
  161. boost::shared_future<int> f1 = boost::async(boost::launch::deferred, &p1).share();
  162. boost::future<boost::csbl::tuple<boost::shared_future<int> > > all = boost::when_all(f1);
  163. BOOST_TEST(f1.valid());
  164. BOOST_TEST(all.valid());
  165. boost::csbl::tuple<boost::shared_future<int> > res = all.get();
  166. BOOST_TEST(boost::csbl::get<0>(res).valid());
  167. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  168. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  169. }
  170. #endif
  171. #endif
  172. return boost::report_errors();
  173. }