Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/co-await-19-while-cond.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 a boolean as the
12 await_resume output. */
13 struct BoolAwaiter {
14 bool v;
15 BoolAwaiter (bool _v) : v(_v) {}
16 bool await_ready () { return false; }
17 void await_suspend (coro::coroutine_handle<>) {}
18 bool await_resume () { return v; }
19 };
20
21 //extern bool tt(void);
22 int three = 3;
23 struct coro1
24 my_coro (bool t)
25 {
26 //int three = 3;
27 while (co_await BoolAwaiter (t) && t)
28 {
29 int five = three + 2;
30 co_yield 6169 + five;
31 }
32
33 co_return 42;
34 }
35
36 int main ()
37 {
38 PRINT ("main: create coro");
39 struct coro1 x = my_coro (false);
40
41 if (x.handle.done())
42 {
43 PRINT ("main: apparently done when we shouldn't be...");
44 abort ();
45 }
46
47 PRINT ("main: resume initial suspend");
48 x.handle.resume();
49
50 // will be false - so no yield expected.
51 PRINT ("main: while condition");
52 x.handle.resume();
53
54 int y = x.handle.promise().get_value();
55 if ( y != 42 )
56 {
57 PRINTF ("main: apparently wrong value : %d\n", y);
58 abort ();
59 }
60
61 if (!x.handle.done())
62 {
63 PRINT ("main: apparently not done...");
64 abort ();
65 }
66
67 PRINT ("main: returning");
68 return 0;
69 }