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