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