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