| 1 |
// { dg-do run } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } } |
| 3 |
|
| 4 |
// Test modifying a local var and yielding several instances of it. |
| 5 |
|
| 6 |
#include "../coro.h" |
| 7 |
|
| 8 |
// boiler-plate for tests of codegen |
| 9 |
#include "../coro1-ret-int-yield-int.h" |
| 10 |
|
| 11 |
struct coro1 |
| 12 |
f (int start) noexcept |
| 13 |
{ |
| 14 |
int value = start; |
| 15 |
PRINT ("f: about to yield start"); |
| 16 |
co_yield start; |
| 17 |
|
| 18 |
value -= 31; |
| 19 |
PRINT ("f: about to yield (value-31)"); |
| 20 |
co_yield value; |
| 21 |
|
| 22 |
value += 6163; |
| 23 |
PRINT ("f: about to return (value+6163)"); |
| 24 |
co_return value; |
| 25 |
} |
| 26 |
|
| 27 |
int main () |
| 28 |
{ |
| 29 |
PRINT ("main: create coro1"); |
| 30 |
struct coro1 x = f (42); |
| 31 |
PRINT ("main: got coro1 - resuming (1)"); |
| 32 |
if (x.handle.done()) |
| 33 |
abort(); |
| 34 |
x.handle.resume(); |
| 35 |
PRINT ("main: after resume (1)"); |
| 36 |
int y = x.handle.promise().get_value(); |
| 37 |
if ( y != 42 ) |
| 38 |
abort (); |
| 39 |
PRINT ("main: apparently got 42 - resuming (2)"); |
| 40 |
if (x.handle.done()) |
| 41 |
abort(); |
| 42 |
x.handle.resume(); |
| 43 |
PRINT ("main: after resume (2)"); |
| 44 |
y = x.handle.promise().get_value(); |
| 45 |
if ( y != 11 ) |
| 46 |
abort (); |
| 47 |
PRINT ("main: apparently got 11 - resuming (3)"); |
| 48 |
if (x.handle.done()) |
| 49 |
{ |
| 50 |
PRINT ("main: done?"); |
| 51 |
abort(); |
| 52 |
} |
| 53 |
x.handle.resume(); |
| 54 |
PRINT ("main: after resume (2) checking return"); |
| 55 |
y = x.handle.promise().get_value(); |
| 56 |
if ( y != 6174 ) |
| 57 |
abort (); |
| 58 |
PRINT ("main: apparently got 6174"); |
| 59 |
if (!x.handle.done()) |
| 60 |
{ |
| 61 |
PRINT ("main: apparently not done..."); |
| 62 |
abort (); |
| 63 |
} |
| 64 |
PRINT ("main: returning"); |
| 65 |
return 0; |
| 66 |
} |