| 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_value 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_value(1); |
| 38 |
|
| 39 |
try |
| 40 |
{ |
| 41 |
p1.set_value(2); |
| 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 |
std::chrono::milliseconds delay(1); |
| 52 |
VERIFY( f1.wait_for(delay) == std::future_status::ready ); |
| 53 |
VERIFY( f1.get() == 1 ); |
| 54 |
VERIFY( test ); |
| 55 |
} |
| 56 |
|
| 57 |
void test02() |
| 58 |
{ |
| 59 |
bool test = false; |
| 60 |
|
| 61 |
std::promise<int> p1; |
| 62 |
std::future<int> f1 = p1.get_future(); |
| 63 |
|
| 64 |
p1.set_value(3); |
| 65 |
|
| 66 |
try |
| 67 |
{ |
| 68 |
p1.set_exception(std::make_exception_ptr(4)); |
| 69 |
VERIFY( false ); |
| 70 |
} |
| 71 |
catch (std::future_error& e) |
| 72 |
{ |
| 73 |
VERIFY(e.code() == |
| 74 |
std::make_error_code(std::future_errc::promise_already_satisfied)); |
| 75 |
test = true; |
| 76 |
} |
| 77 |
|
| 78 |
std::chrono::milliseconds delay(1); |
| 79 |
VERIFY( f1.wait_for(delay) == std::future_status::ready ); |
| 80 |
VERIFY( f1.get() == 3 ); |
| 81 |
VERIFY( test ); |
| 82 |
} |
| 83 |
|
| 84 |
void test03() |
| 85 |
{ |
| 86 |
bool test = false; |
| 87 |
|
| 88 |
std::promise<int> p1; |
| 89 |
std::future<int> f1 = p1.get_future(); |
| 90 |
|
| 91 |
p1.set_exception(std::make_exception_ptr(4)); |
| 92 |
|
| 93 |
try |
| 94 |
{ |
| 95 |
p1.set_value(3); |
| 96 |
VERIFY( false ); |
| 97 |
} |
| 98 |
catch (std::future_error& e) |
| 99 |
{ |
| 100 |
VERIFY(e.code() == |
| 101 |
std::make_error_code(std::future_errc::promise_already_satisfied)); |
| 102 |
test = true; |
| 103 |
} |
| 104 |
|
| 105 |
std::chrono::milliseconds delay(1); |
| 106 |
VERIFY( f1.wait_for(delay) == std::future_status::ready ); |
| 107 |
test = false; |
| 108 |
try |
| 109 |
{ |
| 110 |
f1.get(); |
| 111 |
VERIFY( false ); |
| 112 |
} |
| 113 |
catch (int e) |
| 114 |
{ |
| 115 |
VERIFY(e == 4 ); |
| 116 |
test = true; |
| 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 = 1; |
| 130 |
p1.set_value(i); |
| 131 |
|
| 132 |
try |
| 133 |
{ |
| 134 |
p1.set_value(i); |
| 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 |
std::chrono::milliseconds delay(1); |
| 145 |
VERIFY( f1.wait_for(delay) == std::future_status::ready ); |
| 146 |
VERIFY( f1.get() == 1 ); |
| 147 |
VERIFY( test ); |
| 148 |
} |
| 149 |
|
| 150 |
void test05() |
| 151 |
{ |
| 152 |
bool test = false; |
| 153 |
|
| 154 |
std::promise<int&> p1; |
| 155 |
std::future<int&> f1 = p1.get_future(); |
| 156 |
|
| 157 |
int i = 3; |
| 158 |
p1.set_value(i); |
| 159 |
|
| 160 |
try |
| 161 |
{ |
| 162 |
p1.set_exception(std::make_exception_ptr(4)); |
| 163 |
VERIFY( false ); |
| 164 |
} |
| 165 |
catch (std::future_error& e) |
| 166 |
{ |
| 167 |
VERIFY(e.code() == |
| 168 |
std::make_error_code(std::future_errc::promise_already_satisfied)); |
| 169 |
test = true; |
| 170 |
} |
| 171 |
|
| 172 |
std::chrono::milliseconds delay(1); |
| 173 |
VERIFY( f1.wait_for(delay) == std::future_status::ready ); |
| 174 |
VERIFY( f1.get() == 3 ); |
| 175 |
VERIFY( test ); |
| 176 |
} |
| 177 |
|
| 178 |
void test06() |
| 179 |
{ |
| 180 |
bool test = false; |
| 181 |
|
| 182 |
std::promise<int&> p1; |
| 183 |
std::future<int&> f1 = p1.get_future(); |
| 184 |
|
| 185 |
p1.set_exception(std::make_exception_ptr(4)); |
| 186 |
|
| 187 |
try |
| 188 |
{ |
| 189 |
int i = 3; |
| 190 |
p1.set_value(i); |
| 191 |
VERIFY( false ); |
| 192 |
} |
| 193 |
catch (std::future_error& e) |
| 194 |
{ |
| 195 |
VERIFY(e.code() == |
| 196 |
std::make_error_code(std::future_errc::promise_already_satisfied)); |
| 197 |
test = true; |
| 198 |
} |
| 199 |
|
| 200 |
std::chrono::milliseconds delay(1); |
| 201 |
VERIFY( f1.wait_for(delay) == std::future_status::ready ); |
| 202 |
test = false; |
| 203 |
try |
| 204 |
{ |
| 205 |
f1.get(); |
| 206 |
VERIFY( false ); |
| 207 |
} |
| 208 |
catch (int e) |
| 209 |
{ |
| 210 |
VERIFY(e == 4 ); |
| 211 |
test = true; |
| 212 |
} |
| 213 |
|
| 214 |
VERIFY( test ); |
| 215 |
} |
| 216 |
|
| 217 |
void test07() |
| 218 |
{ |
| 219 |
bool test = false; |
| 220 |
|
| 221 |
std::promise<void> p1; |
| 222 |
std::future<void> f1 = p1.get_future(); |
| 223 |
|
| 224 |
p1.set_value(); |
| 225 |
|
| 226 |
try |
| 227 |
{ |
| 228 |
p1.set_value(); |
| 229 |
VERIFY( false ); |
| 230 |
} |
| 231 |
catch (std::future_error& e) |
| 232 |
{ |
| 233 |
VERIFY(e.code() == |
| 234 |
std::make_error_code(std::future_errc::promise_already_satisfied)); |
| 235 |
test = true; |
| 236 |
} |
| 237 |
|
| 238 |
std::chrono::milliseconds delay(1); |
| 239 |
VERIFY( f1.wait_for(delay) == std::future_status::ready ); |
| 240 |
f1.get(); |
| 241 |
VERIFY( test ); |
| 242 |
} |
| 243 |
|
| 244 |
void test08() |
| 245 |
{ |
| 246 |
bool test = false; |
| 247 |
|
| 248 |
std::promise<void> p1; |
| 249 |
std::future<void> f1 = p1.get_future(); |
| 250 |
|
| 251 |
p1.set_value(); |
| 252 |
|
| 253 |
try |
| 254 |
{ |
| 255 |
p1.set_exception(std::make_exception_ptr(4)); |
| 256 |
VERIFY( false ); |
| 257 |
} |
| 258 |
catch (std::future_error& e) |
| 259 |
{ |
| 260 |
VERIFY(e.code() == |
| 261 |
std::make_error_code(std::future_errc::promise_already_satisfied)); |
| 262 |
test = true; |
| 263 |
} |
| 264 |
|
| 265 |
std::chrono::milliseconds delay(1); |
| 266 |
VERIFY( f1.wait_for(delay) == std::future_status::ready ); |
| 267 |
f1.get(); |
| 268 |
VERIFY( test ); |
| 269 |
} |
| 270 |
|
| 271 |
void test09() |
| 272 |
{ |
| 273 |
bool test = false; |
| 274 |
|
| 275 |
std::promise<void> p1; |
| 276 |
std::future<void> f1 = p1.get_future(); |
| 277 |
|
| 278 |
p1.set_exception(std::make_exception_ptr(4)); |
| 279 |
|
| 280 |
try |
| 281 |
{ |
| 282 |
p1.set_value(); |
| 283 |
VERIFY( false ); |
| 284 |
} |
| 285 |
catch (std::future_error& e) |
| 286 |
{ |
| 287 |
VERIFY(e.code() == |
| 288 |
std::make_error_code(std::future_errc::promise_already_satisfied)); |
| 289 |
test = true; |
| 290 |
} |
| 291 |
|
| 292 |
std::chrono::milliseconds delay(1); |
| 293 |
VERIFY( f1.wait_for(delay) == std::future_status::ready ); |
| 294 |
test = false; |
| 295 |
try |
| 296 |
{ |
| 297 |
f1.get(); |
| 298 |
VERIFY( false ); |
| 299 |
} |
| 300 |
catch (int e) |
| 301 |
{ |
| 302 |
VERIFY(e == 4 ); |
| 303 |
test = true; |
| 304 |
} |
| 305 |
|
| 306 |
VERIFY( test ); |
| 307 |
} |
| 308 |
|
| 309 |
// Check for no_state error condition (PR libstdc++/80316) |
| 310 |
|
| 311 |
void test10() |
| 312 |
{ |
| 313 |
using namespace std; |
| 314 |
|
| 315 |
promise<int> p1; |
| 316 |
promise<int> p2(std::move(p1)); |
| 317 |
try |
| 318 |
{ |
| 319 |
p1.set_value(1); |
| 320 |
VERIFY( false ); |
| 321 |
} |
| 322 |
catch (std::future_error& e) |
| 323 |
{ |
| 324 |
VERIFY(e.code() == make_error_code(future_errc::no_state)); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
void test11() |
| 329 |
{ |
| 330 |
using namespace std; |
| 331 |
|
| 332 |
promise<int&> p1; |
| 333 |
promise<int&> p2(std::move(p1)); |
| 334 |
try |
| 335 |
{ |
| 336 |
int i = 0; |
| 337 |
p1.set_value(i); |
| 338 |
VERIFY( false ); |
| 339 |
} |
| 340 |
catch (std::future_error& e) |
| 341 |
{ |
| 342 |
VERIFY(e.code() == make_error_code(future_errc::no_state)); |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
void test12() |
| 347 |
{ |
| 348 |
using namespace std; |
| 349 |
|
| 350 |
promise<void> p1; |
| 351 |
promise<void> p2(std::move(p1)); |
| 352 |
try |
| 353 |
{ |
| 354 |
p1.set_value(); |
| 355 |
VERIFY( false ); |
| 356 |
} |
| 357 |
catch (std::future_error& e) |
| 358 |
{ |
| 359 |
VERIFY(e.code() == make_error_code(future_errc::no_state)); |
| 360 |
} |
| 361 |
} |
| 362 |
|
| 363 |
int main() |
| 364 |
{ |
| 365 |
test01(); |
| 366 |
test02(); |
| 367 |
test03(); |
| 368 |
test04(); |
| 369 |
test05(); |
| 370 |
test06(); |
| 371 |
test07(); |
| 372 |
test08(); |
| 373 |
test09(); |
| 374 |
test10(); |
| 375 |
test11(); |
| 376 |
test12(); |
| 377 |
return 0; |
| 378 |
} |