| 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 |
coro1 |
| 22 |
coro_a (bool t) |
| 23 |
{ |
| 24 |
int accum = 0; |
| 25 |
for (int x = 3; x < 10; x += co_await IntAwaiter (1)) |
| 26 |
accum += x; |
| 27 |
|
| 28 |
co_return accum; |
| 29 |
} |
| 30 |
|
| 31 |
coro1 |
| 32 |
coro_b (bool t) |
| 33 |
{ |
| 34 |
int accum = 0; |
| 35 |
for (int x = 3; x < 10; x += co_await IntAwaiter (1)) |
| 36 |
{ |
| 37 |
if (x & 1) |
| 38 |
continue; |
| 39 |
accum += x; |
| 40 |
} |
| 41 |
|
| 42 |
co_return accum; |
| 43 |
} |
| 44 |
|
| 45 |
void check_a_coro (coro1& x, int expected_answer) |
| 46 |
{ |
| 47 |
if (x.handle.done()) |
| 48 |
{ |
| 49 |
PRINT ("check_a_coro: apparently done when we shouldn't be..."); |
| 50 |
abort (); |
| 51 |
} |
| 52 |
|
| 53 |
PRINT ("check_a_coro: resume initial suspend"); |
| 54 |
x.handle.resume(); |
| 55 |
|
| 56 |
// will be false - so no yield expected. |
| 57 |
PRINT ("check_a_coro: resume for init"); |
| 58 |
while (!x.handle.done()) |
| 59 |
x.handle.resume(); |
| 60 |
|
| 61 |
int y = x.handle.promise().get_value(); |
| 62 |
if ( y != expected_answer ) |
| 63 |
{ |
| 64 |
PRINTF ("check_a_coro: apparently wrong value : %d\n", y); |
| 65 |
abort (); |
| 66 |
} |
| 67 |
|
| 68 |
if (!x.handle.done()) |
| 69 |
{ |
| 70 |
PRINT ("check_a_coro: apparently not done..."); |
| 71 |
abort (); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
int main () |
| 76 |
{ |
| 77 |
{ |
| 78 |
coro1 x = coro_a (false); |
| 79 |
check_a_coro (x, 42); |
| 80 |
} |
| 81 |
{ |
| 82 |
coro1 x = coro_b (false); |
| 83 |
check_a_coro (x, 18); |
| 84 |
} |
| 85 |
|
| 86 |
PRINT ("main: done"); |
| 87 |
return 0; |
| 88 |
} |