Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/co-await-18-if-cond.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } }
3
4 // Test co-await in if 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 /* An awaiter that suspends always and returns an int as the
22 await_resume output. */
23 struct IntAwaiter {
24 int v;
25 IntAwaiter (int _v) : v(_v) {}
26 bool await_ready () { return false; }
27 void await_suspend (coro::coroutine_handle<>) {}
28 int await_resume () { return v; }
29 };
30
31 //extern int tt ();
32 int three = 3;
33 int two = 2;
34
35 struct coro1
36 my_coro (bool t) noexcept
37 {
38 // if (int two = 2;tt () || co_await BoolAwaiter (t) && t && co_await IntAwaiter (5) == co_await IntAwaiter (7))
39 if (co_await BoolAwaiter (t) && co_await IntAwaiter (5) == 5)
40 {
41 int five = three + two;
42 co_return 6169 + five;
43 }
44 else
45 {
46 int five = three + two;
47 co_return 37 + five;
48 }
49 }
50
51 int main ()
52 {
53 PRINT ("main: create coro");
54 struct coro1 x = my_coro (true);
55
56 if (x.handle.done())
57 {
58 PRINT ("main: apparently done when we should not be...");
59 abort ();
60 }
61
62 PRINT ("main: resume initial suspend");
63 x.handle.resume();
64
65 PRINT ("main: if condition 1 - true");
66 x.handle.resume();
67
68 PRINT ("main: if condition 2 - true");
69 x.handle.resume();
70
71 PRINT ("main: after resume");
72 int y = x.handle.promise().get_value();
73 if ( y != 6174 )
74 {
75 PRINTF ("main: apparently wrong value : %d\n", y);
76 abort ();
77 }
78
79 if (!x.handle.done())
80 {
81 PRINT ("main: apparently not done...");
82 abort ();
83 }
84 PRINT ("main: returning");
85 return 0;
86 }