Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/pr95519-04-yield-value.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } }
3
4 #include "../coro.h"
5
6 struct pt_b
7 {
8 std::suspend_never initial_suspend() const noexcept { return {}; }
9 std::suspend_never final_suspend() const noexcept { return {}; }
10 void unhandled_exception() const noexcept {}
11 constexpr static void return_void () noexcept {}
12 };
13
14 int called_yv_op = 0;
15 int v;
16
17 struct pt_c : pt_b
18 {
19 struct yv
20 {
21 auto operator ()(int x){
22 PRINTF("call to operator yield val with %d\n", x);
23 called_yv_op++;
24 v = x;
25 return std::suspend_never{};
26 }
27 };
28 using handle_t = std::coroutine_handle<pt_c>;
29 auto get_return_object() noexcept { return handle_t::from_promise(*this); }
30 yv yield_value;
31 };
32
33 int called_lambda = 0;
34 struct pt_d : pt_b
35 {
36 using handle_t = std::coroutine_handle<pt_d>;
37 auto get_return_object() noexcept { return handle_t::from_promise(*this); }
38 static constexpr auto yield_value = [] (int x) -> std::suspend_never
39 {
40 PRINTF("call to lambda yield val %d\n", x);
41 called_lambda++;
42 v = x;
43 return {};
44 };
45 };
46
47 template <> struct std::coroutine_traits<pt_c::handle_t>
48 { using promise_type = pt_c; };
49
50 static pt_c::handle_t foo ()
51 {
52 co_yield 5;
53 }
54
55 template <> struct std::coroutine_traits<pt_d::handle_t>
56 { using promise_type = pt_d; };
57
58 static pt_d::handle_t bar ()
59 {
60 co_yield 3;
61 }
62
63 int main ()
64 {
65 /* These 'coroutines' run to completion imediately, like a regular fn. */
66 foo ();
67 if (v != 5)
68 {
69 PRINT ("foo failed to yield v");
70 abort ();
71 }
72
73 bar ();
74 if (v != 3)
75 {
76 PRINT ("bar failed to yield v");
77 abort ();
78 }
79
80 if (called_yv_op != 1 || called_lambda != 1)
81 {
82 PRINT ("Failed to call one of the yield_value cases");
83 abort ();
84 }
85 }