| 1 |
// { dg-do run } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } } |
| 3 |
|
| 4 |
// Test co_yield in a loop with no local state. |
| 5 |
|
| 6 |
#include "../coro.h" |
| 7 |
|
| 8 |
// boiler-plate for tests of codegen |
| 9 |
#include "../coro1-ret-int-yield-int.h" |
| 10 |
|
| 11 |
int gX = 1; |
| 12 |
|
| 13 |
struct coro1 |
| 14 |
f () noexcept |
| 15 |
{ |
| 16 |
for (gX = 5; gX < 10 ; gX++) |
| 17 |
{ |
| 18 |
PRINTF ("f: about to yield %d\n", gX); |
| 19 |
co_yield gX; |
| 20 |
} |
| 21 |
|
| 22 |
PRINT ("f: about to return 6174"); |
| 23 |
co_return 6174; |
| 24 |
} |
| 25 |
|
| 26 |
int main () |
| 27 |
{ |
| 28 |
PRINT ("main: create coro1"); |
| 29 |
struct coro1 f_coro = f (); |
| 30 |
PRINT ("main: got coro1 - resuming (1)"); |
| 31 |
if (gX != 1) |
| 32 |
{ |
| 33 |
PRINTF ("main: gX is wrong : %d, should be 1\n", gX); |
| 34 |
abort (); |
| 35 |
} |
| 36 |
if (f_coro.handle.done()) |
| 37 |
abort(); |
| 38 |
f_coro.handle.resume(); |
| 39 |
PRINT ("main: after resume (1)"); |
| 40 |
int y = f_coro.handle.promise().get_value(); |
| 41 |
if (y != 5) |
| 42 |
{ |
| 43 |
PRINTF ("main: got %d not 5.\n",y); |
| 44 |
abort (); |
| 45 |
} |
| 46 |
PRINT ("main: gX OK -- looping"); |
| 47 |
do { |
| 48 |
y = f_coro.handle.promise().get_value(); |
| 49 |
if (y != gX) |
| 50 |
{ |
| 51 |
PRINTF ("main: got %d not %d.\n",y, gX); |
| 52 |
abort (); |
| 53 |
} |
| 54 |
PRINTF ("main: gX : %d \n", gX); |
| 55 |
f_coro.handle.resume(); |
| 56 |
} while (!f_coro.handle.done()); |
| 57 |
|
| 58 |
y = f_coro.handle.promise().get_value(); |
| 59 |
if ( y != 6174 ) |
| 60 |
abort (); |
| 61 |
PRINT ("main: apparently got 6174"); |
| 62 |
if (!f_coro.handle.done()) |
| 63 |
{ |
| 64 |
PRINT ("main: apparently not done..."); |
| 65 |
abort (); |
| 66 |
} |
| 67 |
PRINT ("main: returning"); |
| 68 |
return 0; |
| 69 |
} |