set_lvalue_pass.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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) 2011 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. // class promise<R>
  15. // void promise<R&>::set_value(R& r);
  16. #define BOOST_THREAD_VERSION 3
  17. #include <boost/thread/future.hpp>
  18. #include <boost/detail/lightweight_test.hpp>
  19. #include <boost/static_assert.hpp>
  20. int main()
  21. {
  22. {
  23. typedef int& T;
  24. int i = 3;
  25. boost::promise<T> p;
  26. boost::future<T> f = p.get_future();
  27. p.set_value(i);
  28. int& j = f.get();
  29. BOOST_TEST(j == 3);
  30. ++i;
  31. BOOST_TEST(j == 4);
  32. try
  33. {
  34. p.set_value(i);
  35. BOOST_TEST(false);
  36. }
  37. catch (const boost::future_error& e)
  38. {
  39. BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied));
  40. }
  41. catch (...)
  42. {
  43. BOOST_TEST(false);
  44. }
  45. }
  46. {
  47. typedef int& T;
  48. int i = 3;
  49. boost::promise<T> p;
  50. boost::future<T> f = p.get_future();
  51. p.set_value(i);
  52. int& j = f.get();
  53. BOOST_TEST(j == 3);
  54. ++i;
  55. BOOST_TEST(j == 4);
  56. try
  57. {
  58. p.set_value_deferred(i);
  59. BOOST_TEST(false);
  60. }
  61. catch (const boost::future_error& e)
  62. {
  63. BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied));
  64. }
  65. catch (...)
  66. {
  67. BOOST_TEST(false);
  68. }
  69. }
  70. {
  71. typedef int& T;
  72. int i = 3;
  73. boost::promise<T> p;
  74. boost::future<T> f = p.get_future();
  75. p.set_value_deferred(i);
  76. BOOST_TEST(!f.is_ready());
  77. p.notify_deferred();
  78. int& j = f.get();
  79. BOOST_TEST(j == 3);
  80. ++i;
  81. BOOST_TEST(j == 4);
  82. try
  83. {
  84. p.set_value_deferred(i);
  85. BOOST_TEST(false);
  86. }
  87. catch (const boost::future_error& e)
  88. {
  89. BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied));
  90. }
  91. catch (...)
  92. {
  93. BOOST_TEST(false);
  94. }
  95. }
  96. {
  97. typedef int& T;
  98. int i = 3;
  99. boost::promise<T> p;
  100. boost::future<T> f = p.get_future();
  101. p.set_value_deferred(i);
  102. BOOST_TEST(!f.is_ready());
  103. p.notify_deferred();
  104. int& j = f.get();
  105. BOOST_TEST(j == 3);
  106. ++i;
  107. BOOST_TEST(j == 4);
  108. try
  109. {
  110. p.set_value(i);
  111. BOOST_TEST(false);
  112. }
  113. catch (const boost::future_error& e)
  114. {
  115. BOOST_TEST(e.code() == boost::system::make_error_code(boost::future_errc::promise_already_satisfied));
  116. }
  117. catch (...)
  118. {
  119. BOOST_TEST(false);
  120. }
  121. }
  122. return boost::report_errors();
  123. }