2 // { dg-additional-options "-pthread" { target pthread } }
3 // { dg-require-effective-target c++11 }
4 // { dg-require-gthreads "" }
6 // Copyright (C) 2009-2023 Free Software Foundation, Inc.
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)
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.
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/>.
23 // Test that promise::set_value throws when required.
26 #include <testsuite_hooks.h>
28 // Check for promise_already_satisfied error conditions.
35 std::future
<int> f1
= p1
.get_future();
44 catch (std::future_error
& e
)
47 std::make_error_code(std::future_errc::promise_already_satisfied
));
51 std::chrono::milliseconds
delay(1);
52 VERIFY( f1
.wait_for(delay
) == std::future_status::ready
);
53 VERIFY( f1
.get() == 1 );
62 std::future
<int> f1
= p1
.get_future();
68 p1
.set_exception(std::make_exception_ptr(4));
71 catch (std::future_error
& e
)
74 std::make_error_code(std::future_errc::promise_already_satisfied
));
78 std::chrono::milliseconds
delay(1);
79 VERIFY( f1
.wait_for(delay
) == std::future_status::ready
);
80 VERIFY( f1
.get() == 3 );
89 std::future
<int> f1
= p1
.get_future();
91 p1
.set_exception(std::make_exception_ptr(4));
98 catch (std::future_error
& e
)
101 std::make_error_code(std::future_errc::promise_already_satisfied
));
105 std::chrono::milliseconds
delay(1);
106 VERIFY( f1
.wait_for(delay
) == std::future_status::ready
);
126 std::promise
<int&> p1
;
127 std::future
<int&> f1
= p1
.get_future();
137 catch (std::future_error
& e
)
140 std::make_error_code(std::future_errc::promise_already_satisfied
));
144 std::chrono::milliseconds
delay(1);
145 VERIFY( f1
.wait_for(delay
) == std::future_status::ready
);
146 VERIFY( f1
.get() == 1 );
154 std::promise
<int&> p1
;
155 std::future
<int&> f1
= p1
.get_future();
162 p1
.set_exception(std::make_exception_ptr(4));
165 catch (std::future_error
& e
)
168 std::make_error_code(std::future_errc::promise_already_satisfied
));
172 std::chrono::milliseconds
delay(1);
173 VERIFY( f1
.wait_for(delay
) == std::future_status::ready
);
174 VERIFY( f1
.get() == 3 );
182 std::promise
<int&> p1
;
183 std::future
<int&> f1
= p1
.get_future();
185 p1
.set_exception(std::make_exception_ptr(4));
193 catch (std::future_error
& e
)
196 std::make_error_code(std::future_errc::promise_already_satisfied
));
200 std::chrono::milliseconds
delay(1);
201 VERIFY( f1
.wait_for(delay
) == std::future_status::ready
);
221 std::promise
<void> p1
;
222 std::future
<void> f1
= p1
.get_future();
231 catch (std::future_error
& e
)
234 std::make_error_code(std::future_errc::promise_already_satisfied
));
238 std::chrono::milliseconds
delay(1);
239 VERIFY( f1
.wait_for(delay
) == std::future_status::ready
);
248 std::promise
<void> p1
;
249 std::future
<void> f1
= p1
.get_future();
255 p1
.set_exception(std::make_exception_ptr(4));
258 catch (std::future_error
& e
)
261 std::make_error_code(std::future_errc::promise_already_satisfied
));
265 std::chrono::milliseconds
delay(1);
266 VERIFY( f1
.wait_for(delay
) == std::future_status::ready
);
275 std::promise
<void> p1
;
276 std::future
<void> f1
= p1
.get_future();
278 p1
.set_exception(std::make_exception_ptr(4));
285 catch (std::future_error
& e
)
288 std::make_error_code(std::future_errc::promise_already_satisfied
));
292 std::chrono::milliseconds
delay(1);
293 VERIFY( f1
.wait_for(delay
) == std::future_status::ready
);
309 // Check for no_state error condition (PR libstdc++/80316)
316 promise
<int> p2(std::move(p1
));
322 catch (std::future_error
& e
)
324 VERIFY(e
.code() == make_error_code(future_errc::no_state
));
333 promise
<int&> p2(std::move(p1
));
340 catch (std::future_error
& e
)
342 VERIFY(e
.code() == make_error_code(future_errc::no_state
));
351 promise
<void> p2(std::move(p1
));
357 catch (std::future_error
& e
)
359 VERIFY(e
.code() == make_error_code(future_errc::no_state
));