Debian Codegraph @

libstdc++-v3/testsuite/30_threads/promise/members/set_exception2.cc

raw
1 // { dg-do run }
2 // { dg-additional-options "-pthread" { target pthread } }
3 // { dg-require-effective-target c++11 }
4 // { dg-require-gthreads "" }
5
6 // Copyright (C) 2009-2025 Free Software Foundation, Inc.
7 //
8 // This file is part of the GNU ISO C++ Library. This library is free
9 // software; you can redistribute it and/or modify it under the
10 // terms of the GNU General Public License as published by the
11 // Free Software Foundation; either version 3, or (at your option)
12 // any later version.
13
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18
19 // You should have received a copy of the GNU General Public License along
20 // with this library; see the file COPYING3. If not see
21 // <http://www.gnu.org/licenses/>.
22
23 // Test that promise::set_exception throws when required.
24
25 #include <future>
26 #include <testsuite_hooks.h>
27
28 // Check for promise_already_satisfied error conditions.
29
30 void test01()
31 {
32 bool test = false;
33
34 std::promise<int> p1;
35 std::future<int> f1 = p1.get_future();
36
37 p1.set_exception(std::make_exception_ptr(0));
38
39 try
40 {
41 p1.set_exception(std::make_exception_ptr(1));
42 VERIFY( false );
43 }
44 catch (std::future_error& e)
45 {
46 VERIFY(e.code() ==
47 std::make_error_code(std::future_errc::promise_already_satisfied));
48 test = true;
49 }
50
51 try
52 {
53 f1.get();
54 test = false;
55 }
56 catch(int i)
57 {
58 VERIFY( i == 0 );
59 }
60
61 VERIFY( test );
62 }
63
64 void test02()
65 {
66 bool test = false;
67
68 std::promise<int> p1;
69 std::future<int> f1 = p1.get_future();
70
71 p1.set_value(2);
72
73 try
74 {
75 p1.set_exception(std::make_exception_ptr(0));
76 VERIFY( false );
77 }
78 catch (std::future_error& e)
79 {
80 VERIFY(e.code() ==
81 std::make_error_code(std::future_errc::promise_already_satisfied));
82 test = true;
83 }
84
85 VERIFY( test );
86 }
87
88 void test03()
89 {
90 bool test = false;
91
92 std::promise<int&> p1;
93 std::future<int&> f1 = p1.get_future();
94
95 p1.set_exception(std::make_exception_ptr(0));
96
97 try
98 {
99 p1.set_exception(std::make_exception_ptr(1));
100 VERIFY( false );
101 }
102 catch (std::future_error& e)
103 {
104 VERIFY(e.code() ==
105 std::make_error_code(std::future_errc::promise_already_satisfied));
106 test = true;
107 }
108
109 try
110 {
111 f1.get();
112 test = false;
113 }
114 catch(int i)
115 {
116 VERIFY( i == 0 );
117 }
118
119 VERIFY( test );
120 }
121
122 void test04()
123 {
124 bool test = false;
125
126 std::promise<int&> p1;
127 std::future<int&> f1 = p1.get_future();
128
129 int i = 2;
130 p1.set_value(i);
131
132 try
133 {
134 p1.set_exception(std::make_exception_ptr(0));
135 VERIFY( false );
136 }
137 catch (std::future_error& e)
138 {
139 VERIFY(e.code() ==
140 std::make_error_code(std::future_errc::promise_already_satisfied));
141 test = true;
142 }
143
144 VERIFY( test );
145 }
146
147 void test05()
148 {
149 bool test = false;
150
151 std::promise<void> p1;
152 std::future<void> f1 = p1.get_future();
153
154 p1.set_exception(std::make_exception_ptr(0));
155
156 try
157 {
158 p1.set_exception(std::make_exception_ptr(1));
159 VERIFY( false );
160 }
161 catch (std::future_error& e)
162 {
163 VERIFY(e.code() ==
164 std::make_error_code(std::future_errc::promise_already_satisfied));
165 test = true;
166 }
167
168 try
169 {
170 f1.get();
171 test = false;
172 }
173 catch(int i)
174 {
175 VERIFY( i == 0 );
176 }
177
178 VERIFY( test );
179 }
180
181 void test06()
182 {
183 bool test = false;
184
185 std::promise<void> p1;
186 std::future<void> f1 = p1.get_future();
187
188 p1.set_value();
189
190 try
191 {
192 p1.set_exception(std::make_exception_ptr(0));
193 VERIFY( false );
194 }
195 catch (std::future_error& e)
196 {
197 VERIFY(e.code() ==
198 std::make_error_code(std::future_errc::promise_already_satisfied));
199 test = true;
200 }
201
202 VERIFY( test );
203 }
204
205 // Check for no_state error condition (PR libstdc++/80316)
206
207 void test07()
208 {
209 using namespace std;
210
211 promise<int> p1;
212 promise<int> p2(std::move(p1));
213 try
214 {
215 p1.set_exception(std::make_exception_ptr(1));
216 VERIFY( false );
217 }
218 catch (std::future_error& e)
219 {
220 VERIFY(e.code() == make_error_code(future_errc::no_state));
221 }
222 }
223
224 void test08()
225 {
226 using namespace std;
227
228 promise<int&> p1;
229 promise<int&> p2(std::move(p1));
230 try
231 {
232 int i = 0;
233 p1.set_exception(std::make_exception_ptr(1));
234 VERIFY( false );
235 }
236 catch (std::future_error& e)
237 {
238 VERIFY(e.code() == make_error_code(future_errc::no_state));
239 }
240 }
241
242 void test09()
243 {
244 using namespace std;
245
246 promise<void> p1;
247 promise<void> p2(std::move(p1));
248 try
249 {
250 p1.set_exception(std::make_exception_ptr(1));
251 VERIFY( false );
252 }
253 catch (std::future_error& e)
254 {
255 VERIFY(e.code() == make_error_code(future_errc::no_state));
256 }
257 }
258
259 int main()
260 {
261 test01();
262 test02();
263 test03();
264 test04();
265 test05();
266 test06();
267 test07();
268 test08();
269 test09();
270 return 0;
271 }