| 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 |
|
| 14 |
struct IntAwaiter { |
| 15 |
int v; |
| 16 |
IntAwaiter (int _v) : v(_v) {} |
| 17 |
bool await_ready () { return false; } |
| 18 |
void await_suspend (coro::coroutine_handle<>) {} |
| 19 |
int await_resume () { return v; } |
| 20 |
}; |
| 21 |
|
| 22 |
/* An awaiter that suspends always and returns a boolean as the |
| 23 |
await_resume output. The boolean toggles on each call. */ |
| 24 |
|
| 25 |
struct BoolAwaiter { |
| 26 |
bool v; |
| 27 |
BoolAwaiter (bool _v) : v(_v) {} |
| 28 |
bool await_ready () { return false; } |
| 29 |
void await_suspend (coro::coroutine_handle<>) {} |
| 30 |
bool await_resume () { v = !v; return v; } |
| 31 |
}; |
| 32 |
|
| 33 |
/* We will be able to establish that the second part of the conditional |
| 34 |
expression is not evaluated (if it was, then we'd need an additional |
| 35 |
resume to complete the coroutine). */ |
| 36 |
|
| 37 |
struct coro1 |
| 38 |
my_coro (int t) |
| 39 |
{ |
| 40 |
|
| 41 |
bool x = co_await IntAwaiter (t) == 5 || co_await BoolAwaiter (false); |
| 42 |
|
| 43 |
if (x) |
| 44 |
co_return 6174; |
| 45 |
co_return 42; |
| 46 |
} |
| 47 |
|
| 48 |
int main () |
| 49 |
{ |
| 50 |
PRINT ("main: create coro"); |
| 51 |
struct coro1 x = my_coro (5); |
| 52 |
|
| 53 |
if (x.handle.done()) |
| 54 |
{ |
| 55 |
PRINT ("main: apparently done when we should not be..."); |
| 56 |
abort (); |
| 57 |
} |
| 58 |
|
| 59 |
PRINT ("main: resume initial suspend"); |
| 60 |
x.handle.resume(); |
| 61 |
|
| 62 |
PRINT ("main: resume IntAwaiter"); |
| 63 |
x.handle.resume(); |
| 64 |
|
| 65 |
// The evaluation of 'co_await IntAwaiter (t) == 5' should be true, thus |
| 66 |
// the second co_await in the expression will be unexecuted. |
| 67 |
|
| 68 |
int y = x.handle.promise().get_value(); |
| 69 |
if ( y != 6174 ) |
| 70 |
{ |
| 71 |
PRINTF ("main: apparently wrong value : %d\n", y); |
| 72 |
abort (); |
| 73 |
} |
| 74 |
|
| 75 |
if (!x.handle.done()) |
| 76 |
{ |
| 77 |
PRINT ("main: apparently not done..."); |
| 78 |
abort (); |
| 79 |
} |
| 80 |
PRINT ("main: returning"); |
| 81 |
return 0; |
| 82 |
} |