| 1 |
// { dg-do run } |
| 2 |
// { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } } |
| 3 |
|
| 4 |
// Test yielding an int. |
| 5 |
|
| 6 |
// We will use the promise to contain this to avoid having to include |
| 7 |
// additional C++ headers. |
| 8 |
|
| 9 |
// Check that we resolve the correct overload for the yield_value method. |
| 10 |
|
| 11 |
#include "../coro.h" |
| 12 |
|
| 13 |
struct coro1 { |
| 14 |
struct promise_type; |
| 15 |
using handle_type = coro::coroutine_handle<coro1::promise_type>; |
| 16 |
handle_type handle; |
| 17 |
coro1 () : handle(0) {} |
| 18 |
coro1 (handle_type _handle) |
| 19 |
: handle(_handle) { |
| 20 |
PRINT("Created coro1 object from handle"); |
| 21 |
} |
| 22 |
coro1 (const coro1 &) = delete; // no copying |
| 23 |
coro1 (coro1 &&s) : handle(s.handle) { |
| 24 |
s.handle = nullptr; |
| 25 |
PRINT("coro1 mv ctor "); |
| 26 |
} |
| 27 |
coro1 &operator = (coro1 &&s) { |
| 28 |
handle = s.handle; |
| 29 |
s.handle = nullptr; |
| 30 |
PRINT("coro1 op= "); |
| 31 |
return *this; |
| 32 |
} |
| 33 |
~coro1() { |
| 34 |
PRINT("Destroyed coro1"); |
| 35 |
if ( handle ) |
| 36 |
handle.destroy(); |
| 37 |
} |
| 38 |
|
| 39 |
struct suspend_never_prt { |
| 40 |
bool await_ready() const noexcept { return true; } |
| 41 |
void await_suspend(handle_type) const noexcept { PRINT ("susp-never-susp"); } |
| 42 |
void await_resume() const noexcept { PRINT ("susp-never-resume");} |
| 43 |
}; |
| 44 |
|
| 45 |
/* NOTE: this has a DTOR to test that pathway. */ |
| 46 |
struct suspend_always_prt { |
| 47 |
bool await_ready() const noexcept { return false; } |
| 48 |
void await_suspend(handle_type) const noexcept { PRINT ("susp-always-susp"); } |
| 49 |
void await_resume() const noexcept { PRINT ("susp-always-resume"); } |
| 50 |
~suspend_always_prt() { PRINT ("susp-always-DTOR"); } |
| 51 |
}; |
| 52 |
|
| 53 |
struct promise_type { |
| 54 |
int value; |
| 55 |
promise_type() { PRINT ("Created Promise"); } |
| 56 |
~promise_type() { PRINT ("Destroyed Promise"); } |
| 57 |
|
| 58 |
auto get_return_object () { |
| 59 |
PRINT ("get_return_object: handle from promise"); |
| 60 |
return handle_type::from_promise (*this); |
| 61 |
} |
| 62 |
auto initial_suspend () { |
| 63 |
PRINT ("get initial_suspend (always)"); |
| 64 |
return suspend_always_prt{}; |
| 65 |
} |
| 66 |
auto final_suspend () noexcept { |
| 67 |
PRINT ("get final_suspend (always)"); |
| 68 |
return suspend_always_prt{}; |
| 69 |
} |
| 70 |
void return_value (int v) { |
| 71 |
PRINTF ("return_value () %d\n",v); |
| 72 |
value = v; |
| 73 |
} |
| 74 |
auto yield_value (int v) { |
| 75 |
PRINTF ("yield_value () %d and suspend always\n",v); |
| 76 |
value = v; |
| 77 |
return suspend_always_prt{}; |
| 78 |
} |
| 79 |
/* Some non-matching overloads. */ |
| 80 |
auto yield_value (suspend_always_prt s, int x) { |
| 81 |
return s; |
| 82 |
} |
| 83 |
auto yield_value (void) { |
| 84 |
return 42; |
| 85 |
} |
| 86 |
int get_value (void) { return value; } |
| 87 |
void unhandled_exception() { PRINT ("** unhandled exception"); } |
| 88 |
}; |
| 89 |
}; |
| 90 |
|
| 91 |
struct coro1 |
| 92 |
f () noexcept |
| 93 |
{ |
| 94 |
PRINT ("f: about to yield 42"); |
| 95 |
co_yield 42; |
| 96 |
|
| 97 |
PRINT ("f: about to return 6174"); |
| 98 |
co_return 6174; |
| 99 |
} |
| 100 |
|
| 101 |
int main () |
| 102 |
{ |
| 103 |
PRINT ("main: create coro1"); |
| 104 |
struct coro1 x = f (); |
| 105 |
PRINT ("main: got coro1 - resuming (1)"); |
| 106 |
if (x.handle.done()) |
| 107 |
abort(); |
| 108 |
x.handle.resume(); |
| 109 |
PRINT ("main: after resume (1)"); |
| 110 |
int y = x.handle.promise().get_value(); |
| 111 |
if ( y != 42 ) |
| 112 |
abort (); |
| 113 |
PRINT ("main: apparently got 42"); |
| 114 |
PRINT ("main: got coro1 - resuming (2)"); |
| 115 |
if (x.handle.done()) |
| 116 |
abort(); |
| 117 |
x.handle.resume(); |
| 118 |
PRINT ("main: after resume (2)"); |
| 119 |
y = x.handle.promise().get_value(); |
| 120 |
if ( y != 6174 ) |
| 121 |
abort (); |
| 122 |
PRINT ("main: apparently got 6174"); |
| 123 |
if (!x.handle.done()) |
| 124 |
{ |
| 125 |
PRINT ("main: apparently not done..."); |
| 126 |
abort (); |
| 127 |
} |
| 128 |
PRINT ("main: returning"); |
| 129 |
return 0; |
| 130 |
} |