Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/co-await-25-for-condition.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 = 3; x < co_await IntAwaiter (10); x++)
26 accum += x;
27
28 co_return accum;
29 }
30
31 /* An awaiter that suspends always and returns an int as the
32 await_resume output. */
33 struct TenAwaiter {
34 int v;
35 TenAwaiter (int _v) : v(_v) {}
36 bool await_ready () { return false; }
37 void await_suspend (coro::coroutine_handle<>) {}
38 bool await_resume () { return v < 10; }
39 };
40
41 struct coro1
42 coro_b (bool t)
43 {
44 int accum = 0;
45 for (int x = 3; co_await TenAwaiter (x); x++)
46 accum += x;
47
48 co_return accum;
49 }
50
51 void
52 check_a_coro (struct coro1& x)
53 {
54 if (x.handle.done())
55 {
56 PRINT ("check_a_coro: apparently done when we shouldn't be...");
57 abort ();
58 }
59
60 PRINT ("check_a_coro: resume initial suspend");
61 x.handle.resume();
62
63 // will be false - so no yield expected.
64 PRINT ("check_a_coro: resume loops");
65 while (!x.handle.done())
66 x.handle.resume();
67
68 int y = x.handle.promise().get_value();
69 if ( y != 42 )
70 {
71 PRINTF ("check_a_coro: apparently wrong value : %d\n", y);
72 abort ();
73 }
74
75 if (!x.handle.done())
76 {
77 PRINT ("check_a_coro: apparently not done...");
78 abort ();
79 }
80 }
81
82 int main ()
83 {
84 {
85 struct coro1 x = coro_a (false);
86 check_a_coro (x);
87 }
88 {
89 struct coro1 x = coro_b (false);
90 check_a_coro (x);
91 }
92
93 PRINT ("main: returning");
94 return 0;
95 }