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