Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/co-await-24-for-init.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } }
3
4 // Test co-await in while condition.
5
6 #include "../coro.h"
7
8 // boiler-plate for tests of codegen
9 #include "../coro1-ret-int-yield-int.h"
10
11 /* An awaiter that suspends always and returns an int as the
12 await_resume output. */
13 struct IntAwaiter {
14 int v;
15 IntAwaiter (int _v) : v(_v) {}
16 bool await_ready () { return false; }
17 void await_suspend (coro::coroutine_handle<>) {}
18 int await_resume () { return v; }
19 };
20
21 struct coro1
22 coro_a (bool t)
23 {
24 int accum = 0;
25 for (int x = co_await IntAwaiter (3); x < 10; x++)
26 accum += x;
27
28 co_return accum;
29 }
30
31 struct coro1
32 coro_b (bool t)
33 {
34 int accum = 0;
35 int x;
36 for (x = co_await IntAwaiter (3); x < 10; x++)
37 accum += x;
38
39 co_return accum;
40 }
41
42 struct coro1
43 coro_c (bool t)
44 {
45 int accum = 0;
46 int x = 3;
47 for (co_await IntAwaiter (3); x < 10; x++)
48 accum += x;
49
50 co_return accum;
51 }
52
53 void
54 check_a_coro (struct coro1& x)
55 {
56 if (x.handle.done())
57 {
58 PRINT ("check_a_coro: apparently done when we shouldn't be...");
59 abort ();
60 }
61
62 PRINT ("check_a_coro: resume initial suspend");
63 x.handle.resume();
64
65 // will be false - so no yield expected.
66 PRINT ("check_a_coro: resume for init");
67 x.handle.resume();
68
69 int y = x.handle.promise().get_value();
70 if ( y != 42 )
71 {
72 PRINTF ("check_a_coro: apparently wrong value : %d\n", y);
73 abort ();
74 }
75
76 if (!x.handle.done())
77 {
78 PRINT ("check_a_coro: apparently not done...");
79 abort ();
80 }
81 }
82
83 int main ()
84 {
85 {
86 struct coro1 x = coro_a (false);
87 check_a_coro (x);
88 }
89
90 {
91 struct coro1 x = coro_b (false);
92 check_a_coro (x);
93 }
94
95 {
96 struct coro1 x = coro_c (false);
97 check_a_coro (x);
98 }
99
100 PRINT ("main: done");
101 return 0;
102 }