Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/func-params-08.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 struct Foo {
18 Foo(int _v) : value(_v), x(1, _v)
19 {
20 regular++;
21 PRINTF ("FOO(%d)\n",_v);
22 }
23
24 Foo(const Foo& t)
25 {
26 value = t.value;
27 x = t.x;
28 copy++;
29 PRINTF ("FOO(&), %d\n",value);
30 }
31
32 Foo(Foo&& s)
33 {
34 value = s.value;
35 s.value = -1;
36 x = std::move(s.x);
37 s.x = std::vector<int> ();
38 move++;
39 PRINTF ("FOO(&&), %d\n", value);
40 }
41
42 ~Foo() {PRINTF ("~FOO(), %d\n", value);}
43
44 auto operator co_await()
45 {
46 struct awaitable
47 {
48 int v;
49 awaitable (int _v) : v(_v) {}
50 bool await_ready() { return false; }
51 void await_suspend(coro::coroutine_handle<>) {}
52 int await_resume() { return v;}
53 };
54 return awaitable{value + x[0]};
55 };
56
57 void return_value(int _v) { value = _v;}
58
59 int value;
60 std::vector<int> x;
61 };
62
63 coro1
64 my_coro (Foo t_lv, Foo& t_ref, Foo&& t_rv_ref)
65 {
66 PRINT ("my_coro");
67 // We are created suspended, so correct operation depends on
68 // the parms being copied.
69 int sum = co_await t_lv;
70 PRINT ("my_coro 1");
71 sum += co_await t_ref;
72 PRINT ("my_coro 2");
73 // This can't work for the rvalue ref, it's always dangling.
74 //sum += co_await t_rv_ref;
75 //PRINT ("my_coro 3");
76 co_return sum;
77 }
78
79 int main ()
80 {
81
82 PRINT ("main: create coro1");
83 Foo thing (4);
84 coro1 x = my_coro (Foo (1), thing, Foo (2));
85 PRINT ("main: done ramp");
86
87 if (x.handle.done())
88 abort();
89 x.handle.resume();
90 PRINT ("main: after resume (initial suspend)");
91
92 // now do the three co_awaits.
93 while(!x.handle.done())
94 x.handle.resume();
95 PRINT ("main: after resuming 2 co_awaits");
96
97 /* Now we should have the co_returned value. */
98 int y = x.handle.promise().get_value();
99 if (y != 10)
100 {
101 PRINTF ("main: wrong result (%d).", y);
102 abort ();
103 }
104
105 if (regular != 3 || copy != 0 || move != 1)
106 {
107 PRINTF ("main: unexpected ctor use (R:%d, C:%d, M:%d)\n",
108 regular, copy, move);
109 abort ();
110 }
111
112 PRINT ("main: returning");
113 return 0;
114 }