| 1 |
// { dg-do run } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } } |
| 3 |
|
| 4 |
// lambda with mutable closure object. |
| 5 |
|
| 6 |
#include "../coro.h" |
| 7 |
|
| 8 |
// boiler-plate for tests of codegen |
| 9 |
#include "../coro1-ret-int-yield-int.h" |
| 10 |
|
| 11 |
/* Creates a coro lambda with a mutable closure and |
| 12 |
suspend-always initial suspend. */ |
| 13 |
|
| 14 |
auto make_co_lambda () |
| 15 |
{ |
| 16 |
return [i = 1] () mutable -> coro1 { co_return i++; }; |
| 17 |
} |
| 18 |
|
| 19 |
/* We make this behave sequentially for the purposes of testing. */ |
| 20 |
int main() |
| 21 |
{ |
| 22 |
auto co_l = make_co_lambda (); |
| 23 |
auto v1 = co_l (); |
| 24 |
auto v2 = co_l (); |
| 25 |
auto v3 = co_l (); |
| 26 |
|
| 27 |
v3.handle.resume(); |
| 28 |
v2.handle.resume(); |
| 29 |
v1.handle.resume(); |
| 30 |
|
| 31 |
int res1 = v1.handle.promise().get_value (); |
| 32 |
int res2 = v2.handle.promise().get_value (); |
| 33 |
int res3 = v3.handle.promise().get_value (); |
| 34 |
PRINTF ("main: co-lambda %d, %d, %d\n",res1, res2, res3); |
| 35 |
if ( res1 != 3 || res2 != 2 || res3 != 1) |
| 36 |
{ |
| 37 |
PRINT ("main: bad return value."); |
| 38 |
abort (); |
| 39 |
} |
| 40 |
|
| 41 |
if (!v1.handle.done() || !v2.handle.done() || !v3.handle.done()) |
| 42 |
{ |
| 43 |
PRINT ("main: apparently something was not done..."); |
| 44 |
abort (); |
| 45 |
} |
| 46 |
|
| 47 |
PRINT ("main: done."); |
| 48 |
} |
| 49 |
|