| 1 |
// { dg-do run } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } } |
| 3 |
|
| 4 |
// Test co-await in do-while conditional |
| 5 |
|
| 6 |
#include "../coro.h" |
| 7 |
|
| 8 |
// boiler-plate for tests of codegen |
| 9 |
#include "../coro1-ret-int-yield-int.h" |
| 10 |
|
| 11 |
/* An awaiter that suspends always and returns a boolean as the |
| 12 |
await_resume output. The boolean toggles on each call. */ |
| 13 |
struct BoolAwaiter { |
| 14 |
bool v; |
| 15 |
BoolAwaiter (bool _v) : v(_v) {} |
| 16 |
bool await_ready () { return false; } |
| 17 |
void await_suspend (coro::coroutine_handle<>) {} |
| 18 |
bool await_resume () { v = !v; return v; } |
| 19 |
}; |
| 20 |
|
| 21 |
int v = 32; |
| 22 |
|
| 23 |
struct coro1 |
| 24 |
my_coro (bool t) |
| 25 |
{ |
| 26 |
auto aw = BoolAwaiter (t); |
| 27 |
do { |
| 28 |
int five = 5; |
| 29 |
v += five; |
| 30 |
} while (co_await aw && !t); |
| 31 |
|
| 32 |
co_return v; |
| 33 |
} |
| 34 |
|
| 35 |
int main () |
| 36 |
{ |
| 37 |
PRINT ("main: create coro"); |
| 38 |
struct coro1 x = my_coro (false); |
| 39 |
|
| 40 |
if (x.handle.done()) |
| 41 |
{ |
| 42 |
PRINT ("main: apparently done when we should not be..."); |
| 43 |
abort (); |
| 44 |
} |
| 45 |
|
| 46 |
PRINT ("main: resume initial suspend"); |
| 47 |
x.handle.resume(); |
| 48 |
|
| 49 |
PRINT ("main: resume while test, succeed first time"); |
| 50 |
x.handle.resume(); |
| 51 |
|
| 52 |
PRINT ("main: resume while test, fail second"); |
| 53 |
x.handle.resume(); |
| 54 |
|
| 55 |
int y = x.handle.promise().get_value(); |
| 56 |
if ( y != 42 ) |
| 57 |
{ |
| 58 |
PRINTF ("main: apparently wrong value : %d\n", y); |
| 59 |
abort (); |
| 60 |
} |
| 61 |
|
| 62 |
if (!x.handle.done()) |
| 63 |
{ |
| 64 |
PRINT ("main: apparently not done..."); |
| 65 |
abort (); |
| 66 |
} |
| 67 |
PRINT ("main: returning"); |
| 68 |
return 0; |
| 69 |
} |