| 1 |
// { dg-do run } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } } |
| 3 |
|
| 4 |
// Test co-await in while condition. |
| 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 an int as the |
| 12 |
await_resume output. */ |
| 13 |
struct IntAwaiter { |
| 14 |
int v; |
| 15 |
IntAwaiter (int _v) : v(_v) {} |
| 16 |
bool await_ready () { return false; } |
| 17 |
void await_suspend (coro::coroutine_handle<>) {} |
| 18 |
int await_resume () { return v; } |
| 19 |
}; |
| 20 |
|
| 21 |
struct coro1 |
| 22 |
my_coro (int t) |
| 23 |
{ |
| 24 |
int v = 6174; |
| 25 |
switch (co_await IntAwaiter (t) + 5) |
| 26 |
{ |
| 27 |
default: break; |
| 28 |
case 22: co_return v; |
| 29 |
} |
| 30 |
co_return 42; |
| 31 |
} |
| 32 |
|
| 33 |
int main () |
| 34 |
{ |
| 35 |
PRINT ("main: create coro"); |
| 36 |
struct coro1 x = my_coro (17); |
| 37 |
|
| 38 |
if (x.handle.done()) |
| 39 |
{ |
| 40 |
PRINT ("main: apparently done when we should not be..."); |
| 41 |
abort (); |
| 42 |
} |
| 43 |
|
| 44 |
PRINT ("main: resume initial suspend"); |
| 45 |
x.handle.resume(); |
| 46 |
|
| 47 |
PRINT ("main: resume switch condition"); |
| 48 |
x.handle.resume(); |
| 49 |
|
| 50 |
int y = x.handle.promise().get_value(); |
| 51 |
if ( y != 6174 ) |
| 52 |
{ |
| 53 |
PRINTF ("main: apparently wrong value : %d\n", y); |
| 54 |
abort (); |
| 55 |
} |
| 56 |
|
| 57 |
if (!x.handle.done()) |
| 58 |
{ |
| 59 |
PRINT ("main: apparently not done..."); |
| 60 |
abort (); |
| 61 |
} |
| 62 |
PRINT ("main: returning"); |
| 63 |
return 0; |
| 64 |
} |