1 // { dg-do run }
2 
3 // Test co-await in while condition.
4 
5 #include "../coro.h"
6 
7 // boiler-plate for tests of codegen
8 #include "../coro1-ret-int-yield-int.h"
9 
10 /* An awaiter that suspends always and returns an int as the
11    await_resume output.  */
12 struct IntAwaiter {
13   int v;
IntAwaiterIntAwaiter14   IntAwaiter (int _v) : v(_v) {}
await_readyIntAwaiter15   bool await_ready () { return false; }
await_suspendIntAwaiter16   void await_suspend (coro::coroutine_handle<>) {}
await_resumeIntAwaiter17   int await_resume () { return v; }
18 };
19 
20 struct coro1
coro_a(bool t)21 coro_a (bool t)
22 {
23   int accum = 0;
24   for (int x = co_await IntAwaiter (3); x < 10; x++)
25     accum += x;
26 
27   co_return accum;
28 }
29 
30 struct coro1
coro_b(bool t)31 coro_b (bool t)
32 {
33   int accum = 0;
34   int x;
35   for (x = co_await IntAwaiter (3); x < 10; x++)
36     accum += x;
37 
38   co_return accum;
39 }
40 
41 struct coro1
coro_c(bool t)42 coro_c (bool t)
43 {
44   int accum = 0;
45   int x = 3;
46   for (co_await IntAwaiter (3); x < 10; x++)
47     accum += x;
48 
49   co_return accum;
50 }
51 
52 void
check_a_coro(struct coro1 & x)53 check_a_coro (struct coro1& x)
54 {
55   if (x.handle.done())
56     {
57       PRINT ("check_a_coro: apparently done when we shouldn't be...");
58       abort ();
59     }
60 
61   PRINT ("check_a_coro: resume initial suspend");
62   x.handle.resume();
63 
64   // will be false - so no yield expected.
65   PRINT ("check_a_coro: resume for init");
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 
main()82 int main ()
83 {
84   {
85     struct coro1 x = coro_a (false);
86     check_a_coro (x);
87   }
88 
89   {
90     struct coro1 x = coro_b (false);
91     check_a_coro (x);
92   }
93 
94   {
95     struct coro1 x = coro_c (false);
96     check_a_coro (x);
97   }
98 
99   PRINT ("main: done");
100   return 0;
101 }
102