Mercurial > hg > CbC > CbC_gcc
comparison gcc/testsuite/g++.dg/coroutines/torture/func-params-08.C @ 152:2b5abeee2509
update gcc11
author | anatofuz |
---|---|
date | Mon, 25 May 2020 07:50:57 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
145:1830386684a0 | 152:2b5abeee2509 |
---|---|
1 // { dg-do run } | |
2 | |
3 // Check that we correctly handle params with non-trivial DTORs and | |
4 // use the correct copy/move CTORs. | |
5 | |
6 #include "../coro.h" | |
7 #include <vector> | |
8 | |
9 // boiler-plate for tests of codegen | |
10 #include "../coro1-ret-int-yield-int.h" | |
11 | |
12 int regular = 0; | |
13 int copy = 0; | |
14 int move = 0; | |
15 | |
16 struct Foo { | |
17 Foo(int _v) : value(_v), x(1, _v) | |
18 { | |
19 regular++; | |
20 PRINTF ("FOO(%d)\n",_v); | |
21 } | |
22 | |
23 Foo(const Foo& t) | |
24 { | |
25 value = t.value; | |
26 x = t.x; | |
27 copy++; | |
28 PRINTF ("FOO(&), %d\n",value); | |
29 } | |
30 | |
31 Foo(Foo&& s) | |
32 { | |
33 value = s.value; | |
34 s.value = -1; | |
35 x = std::move(s.x); | |
36 s.x = std::vector<int> (); | |
37 move++; | |
38 PRINTF ("FOO(&&), %d\n", value); | |
39 } | |
40 | |
41 ~Foo() {PRINTF ("~FOO(), %d\n", value);} | |
42 | |
43 auto operator co_await() | |
44 { | |
45 struct awaitable | |
46 { | |
47 int v; | |
48 awaitable (int _v) : v(_v) {} | |
49 bool await_ready() { return false; } | |
50 void await_suspend(coro::coroutine_handle<>) {} | |
51 int await_resume() { return v;} | |
52 }; | |
53 return awaitable{value + x[0]}; | |
54 }; | |
55 | |
56 void return_value(int _v) { value = _v;} | |
57 | |
58 int value; | |
59 std::vector<int> x; | |
60 }; | |
61 | |
62 coro1 | |
63 my_coro (Foo t_lv, Foo& t_ref, Foo&& t_rv_ref) | |
64 { | |
65 PRINT ("my_coro"); | |
66 // We are created suspended, so correct operation depends on | |
67 // the parms being copied. | |
68 int sum = co_await t_lv; | |
69 PRINT ("my_coro 1"); | |
70 sum += co_await t_ref; | |
71 PRINT ("my_coro 2"); | |
72 sum += co_await t_rv_ref; | |
73 PRINT ("my_coro 3"); | |
74 co_return sum; | |
75 } | |
76 | |
77 int main () | |
78 { | |
79 | |
80 PRINT ("main: create coro1"); | |
81 Foo thing (4); | |
82 coro1 x = my_coro (Foo (1), thing, Foo (2)); | |
83 PRINT ("main: done ramp"); | |
84 | |
85 if (x.handle.done()) | |
86 abort(); | |
87 x.handle.resume(); | |
88 PRINT ("main: after resume (initial suspend)"); | |
89 | |
90 // now do the three co_awaits. | |
91 while(!x.handle.done()) | |
92 x.handle.resume(); | |
93 PRINT ("main: after resuming 3 co_awaits"); | |
94 | |
95 /* Now we should have the co_returned value. */ | |
96 int y = x.handle.promise().get_value(); | |
97 if (y != 14) | |
98 { | |
99 PRINTF ("main: wrong result (%d).", y); | |
100 abort (); | |
101 } | |
102 | |
103 if (regular != 3 || copy != 1 || move != 1) | |
104 { | |
105 PRINTF ("main: unexpected ctor use (R:%d, C:%d, M:%d)\n", | |
106 regular, copy, move); | |
107 abort (); | |
108 } | |
109 | |
110 PRINT ("main: returning"); | |
111 return 0; | |
112 } |