Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/func-params-09-awaitable-parms.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for vector" { ! hostedlib } }
3
4 // Check that we correctly handle params with non-trivial DTORs and
5 // use the correct copy/move CTORs.
6
7 #include "../coro.h"
8 #include <vector>
9
10 // boiler-plate for tests of codegen
11 #include "../coro1-ret-int-yield-int.h"
12
13 int regular = 0;
14 int copy = 0;
15 int move = 0;
16
17 /* This is a more sophisticated awaitable... */
18
19 struct FooAwaitable {
20 FooAwaitable(int _v) : value(_v), x(1, _v)
21 {
22 regular++;
23 PRINTF ("FooAwaitable(%d)\n",_v);
24 }
25
26 FooAwaitable(const FooAwaitable& t)
27 {
28 value = t.value;
29 x = t.x;
30 copy++;
31 PRINTF ("FooAwaitable(&), %d\n",value);
32 }
33
34 FooAwaitable(FooAwaitable&& s)
35 {
36 value = s.value;
37 s.value = -1;
38 x = std::move(s.x);
39 s.x = std::vector<int> ();
40 move++;
41 PRINTF ("FooAwaitable(&&), %d\n", value);
42 }
43
44 ~FooAwaitable() {PRINTF ("~FooAwaitable(), %d\n", value);}
45
46 bool await_ready() { return false; }
47 void await_suspend(coro::coroutine_handle<>) {}
48 int await_resume() { return value + x[0];}
49
50 void return_value(int _v) { value = _v;}
51
52 int value;
53 std::vector<int> x;
54 };
55
56 coro1
57 my_coro (FooAwaitable t_lv, FooAwaitable& t_ref, FooAwaitable&& t_rv_ref)
58 {
59 PRINT ("my_coro");
60 // We are created suspended, so correct operation depends on
61 // the parms being copied.
62 int sum = co_await t_lv;
63 PRINT ("my_coro 1");
64 sum += co_await t_ref;
65 PRINT ("my_coro 2");
66 // This can't work for the rvalue ref, it's always dangling.
67 //sum += co_await t_rv_ref;
68 //PRINT ("my_coro 3");
69 co_return sum;
70 }
71
72 int main ()
73 {
74
75 PRINT ("main: create coro1");
76 FooAwaitable thing (4);
77 coro1 x = my_coro (FooAwaitable (1), thing, FooAwaitable (2));
78 PRINT ("main: done ramp");
79
80 if (x.handle.done())
81 abort();
82 x.handle.resume();
83 PRINT ("main: after resume (initial suspend)");
84
85 // now do the three co_awaits.
86 while(!x.handle.done())
87 x.handle.resume();
88 PRINT ("main: after resuming 2 co_awaits");
89
90 /* Now we should have the co_returned value. */
91 int y = x.handle.promise().get_value();
92 if (y != 10)
93 {
94 PRINTF ("main: wrong result (%d).", y);
95 abort ();
96 }
97
98 if (regular != 3 || copy != 0 || move != 1)
99 {
100 PRINTF ("main: unexpected ctor use (R:%d, C:%d, M:%d)\n",
101 regular, copy, move);
102 abort ();
103 }
104
105 PRINT ("main: returning");
106 return 0;
107 }