one_pass.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. // future<tuple<T>> when_any(T&&);
  15. #include <boost/config.hpp>
  16. #if ! defined BOOST_NO_CXX11_DECLTYPE
  17. #define BOOST_RESULT_OF_USE_DECLTYPE
  18. #endif
  19. #define BOOST_THREAD_VERSION 4
  20. #include <boost/thread/future.hpp>
  21. #include <boost/detail/lightweight_test.hpp>
  22. #ifdef BOOST_MSVC
  23. #pragma warning(disable: 4127) // conditional expression is constant
  24. #endif
  25. int p1()
  26. {
  27. return 123;
  28. }
  29. int main()
  30. {
  31. #if defined BOOST_THREAD_PROVIDES_FUTURE_WHEN_ALL_WHEN_ANY
  32. if (0) // todo not yet implemented
  33. { // invalid future copy-constructible
  34. boost::future<int> f1;
  35. BOOST_TEST(! f1.valid());
  36. boost::future<boost::csbl::tuple<boost::future<int> > > all = boost::when_any(boost::move(f1));
  37. BOOST_TEST(! f1.valid());
  38. BOOST_TEST(all.valid());
  39. boost::csbl::tuple<boost::future<int> > res = all.get();
  40. BOOST_TEST(boost::csbl::get<0>(res).valid());
  41. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  42. // has exception
  43. //BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  44. }
  45. { // is_ready future copy-constructible
  46. boost::future<int> f1 = boost::make_ready_future(123);
  47. BOOST_TEST(f1.valid());
  48. BOOST_TEST(f1.is_ready());
  49. boost::future<boost::csbl::tuple<boost::future<int> > > all = boost::when_any(boost::move(f1));
  50. BOOST_TEST(! f1.valid());
  51. BOOST_TEST(all.valid());
  52. if (0) // todo FAILS not yet implemented
  53. BOOST_TEST(all.is_ready());
  54. boost::csbl::tuple<boost::future<int> > res = all.get();
  55. BOOST_TEST(boost::csbl::get<0>(res).valid());
  56. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  57. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  58. }
  59. { // is_ready shared_future copy-constructible
  60. boost::shared_future<int> f1 = boost::make_ready_future(123).share();
  61. BOOST_TEST(f1.valid());
  62. BOOST_TEST(f1.is_ready());
  63. boost::future<boost::csbl::tuple<boost::shared_future<int> > > all = boost::when_any(f1);
  64. BOOST_TEST(f1.valid());
  65. BOOST_TEST(all.valid());
  66. if (0) // todo FAILS not yet implemented
  67. BOOST_TEST(all.is_ready());
  68. boost::csbl::tuple<boost::shared_future<int> > res = all.get();
  69. BOOST_TEST(boost::csbl::get<0>(res).valid());
  70. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  71. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  72. }
  73. { // packaged_task future copy-constructible
  74. boost::packaged_task<int()> pt1(&p1);
  75. boost::future<int> f1 = pt1.get_future();
  76. BOOST_TEST(f1.valid());
  77. boost::future<boost::csbl::tuple<boost::future<int> > > all = boost::when_any(boost::move(f1));
  78. BOOST_TEST(! f1.valid());
  79. BOOST_TEST(all.valid());
  80. pt1();
  81. boost::csbl::tuple<boost::future<int> > res = all.get();
  82. BOOST_TEST(boost::csbl::get<0>(res).valid());
  83. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  84. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  85. }
  86. { // packaged_task shared_future copy-constructible
  87. boost::packaged_task<int()> pt1(&p1);
  88. boost::shared_future<int> f1 = pt1.get_future().share();
  89. BOOST_TEST(f1.valid());
  90. boost::future<boost::csbl::tuple<boost::shared_future<int> > > all = boost::when_any(f1);
  91. BOOST_TEST(f1.valid());
  92. BOOST_TEST(all.valid());
  93. pt1();
  94. boost::csbl::tuple<boost::shared_future<int> > res = all.get();
  95. BOOST_TEST(boost::csbl::get<0>(res).valid());
  96. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  97. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  98. }
  99. { // async future copy-constructible
  100. boost::future<int> f1 = boost::async(boost::launch::async, &p1);
  101. BOOST_TEST(f1.valid());
  102. boost::future<boost::csbl::tuple<boost::future<int> > > all = boost::when_any(boost::move(f1));
  103. BOOST_TEST(! f1.valid());
  104. BOOST_TEST(all.valid());
  105. boost::csbl::tuple<boost::future<int> > res = all.get();
  106. BOOST_TEST(boost::csbl::get<0>(res).valid());
  107. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  108. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  109. }
  110. { // async shared_future copy-constructible
  111. boost::shared_future<int> f1 = boost::async(boost::launch::async, &p1).share();
  112. BOOST_TEST(f1.valid());
  113. boost::future<boost::csbl::tuple<boost::shared_future<int> > > all = boost::when_any(f1);
  114. BOOST_TEST(f1.valid());
  115. BOOST_TEST(all.valid());
  116. boost::csbl::tuple<boost::shared_future<int> > res = all.get();
  117. BOOST_TEST(boost::csbl::get<0>(res).valid());
  118. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  119. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  120. }
  121. #if defined BOOST_THREAD_PROVIDES_VARIADIC_THREAD
  122. // fixme darwin-4.8.0_11 terminate called without an active exception
  123. { // deferred future copy-constructible
  124. boost::future<int> f1 = boost::async(boost::launch::deferred, &p1);
  125. boost::future<boost::csbl::tuple<boost::future<int> > > all = boost::when_any(boost::move(f1));
  126. BOOST_TEST(! f1.valid());
  127. BOOST_TEST(all.valid());
  128. boost::csbl::tuple<boost::future<int> > res = all.get();
  129. BOOST_TEST(boost::csbl::get<0>(res).valid());
  130. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  131. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  132. }
  133. // fixme darwin-4.8.0_11 terminate called without an active exception
  134. { // deferred shared_future copy-constructible
  135. boost::shared_future<int> f1 = boost::async(boost::launch::deferred, &p1).share();
  136. boost::future<boost::csbl::tuple<boost::shared_future<int> > > all = boost::when_any(f1);
  137. BOOST_TEST(f1.valid());
  138. BOOST_TEST(all.valid());
  139. boost::csbl::tuple<boost::shared_future<int> > res = all.get();
  140. BOOST_TEST(boost::csbl::get<0>(res).valid());
  141. BOOST_TEST(boost::csbl::get<0>(res).is_ready());
  142. BOOST_TEST(boost::csbl::get<0>(res).get() == 123);
  143. }
  144. #endif
  145. #endif
  146. return boost::report_errors();
  147. }