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 // XFAIL: libcpp-no-exceptions
11 // UNSUPPORTED: libcpp-has-no-threads
12 // UNSUPPORTED: c++98, c++03
13 
14 // <future>
15 
16 // class promise<R>
17 
18 // ~promise();
19 
20 #include <future>
21 #include <cassert>
22 
23 int main()
24 {
25     {
26         typedef int T;
27         std::future<T> f;
28         {
29             std::promise<T> p;
30             f = p.get_future();
31             p.set_value(3);
32         }
33         assert(f.get() == 3);
34     }
35     {
36         typedef int T;
37         std::future<T> f;
38         {
39             std::promise<T> p;
40             f = p.get_future();
41         }
42         try
43         {
44             T i = f.get();
45             assert(false);
46         }
47         catch (const std::future_error& e)
48         {
49             assert(e.code() == make_error_code(std::future_errc::broken_promise));
50         }
51     }
52 
53     {
54         typedef int& T;
55         int i = 4;
56         std::future<T> f;
57         {
58             std::promise<T> p;
59             f = p.get_future();
60             p.set_value(i);
61         }
62         assert(&f.get() == &i);
63     }
64     {
65         typedef int& T;
66         std::future<T> f;
67         {
68             std::promise<T> p;
69             f = p.get_future();
70         }
71         try
72         {
73             T i = f.get();
74             assert(false);
75         }
76         catch (const std::future_error& e)
77         {
78             assert(e.code() == make_error_code(std::future_errc::broken_promise));
79         }
80     }
81 
82     {
83         typedef void T;
84         std::future<T> f;
85         {
86             std::promise<T> p;
87             f = p.get_future();
88             p.set_value();
89         }
90         f.get();
91         assert(true);
92     }
93     {
94         typedef void T;
95         std::future<T> f;
96         {
97             std::promise<T> p;
98             f = p.get_future();
99         }
100         try
101         {
102             f.get();
103             assert(false);
104         }
105         catch (const std::future_error& e)
106         {
107             // LWG 2056 changed the values of future_errc, so if we're using new
108             // headers with an old library the error codes won't line up.
109             //
110             // Note that this particular check only applies to promise<void>
111             // since the other specializations happen to be implemented in the
112             // header rather than the library.
113             assert(
114                 e.code() == make_error_code(std::future_errc::broken_promise) ||
115                 e.code() == std::error_code(0, std::future_category()));
116         }
117     }
118 }
119