testsuite: don't declare printf in coro.h
[official-gcc.git] / gcc / testsuite / g++.dg / coroutines / torture / co-await-22-truth-and-of-if.C
blob54659741cbedda84f8ce6cf6db495826d1dc5ca6
1 // { dg-do run }
3 // Test co-await in while condition.
5 #include "../coro.h"
7 // boiler-plate for tests of codegen
8 #include "../coro1-ret-int-yield-int.h"
10 /* An awaiter that suspends always and returns an int as the
11    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; }
21 /* An awaiter that suspends always and returns a boolean as the
22    await_resume output.  The boolean toggles on each call.  */
24 struct BoolAwaiter {
25   bool v;
26   BoolAwaiter (bool _v) : v(_v) {}
27   bool await_ready () { return false; }
28   void await_suspend (coro::coroutine_handle<>) {}
29   bool await_resume () { v = !v; return v; }
32 /* We will be able to establish that the second part of the conditional
33    expression is not evaluated (if it was, then we'd need an additional
34    resume to complete the coroutine).  */
36 struct coro1
37 my_coro (int t)
40   bool x = co_await IntAwaiter (t) == 5 || co_await BoolAwaiter (false);
42   if (x)
43     co_return 6174;
44   co_return 42;
47 int main ()
49   PRINT ("main: create coro");
50   struct coro1 x = my_coro (5);
52   if (x.handle.done())
53     {
54       PRINT ("main: apparently done when we should not be...");
55       abort ();
56     }
58   PRINT ("main: resume initial suspend");
59   x.handle.resume();
61   PRINT ("main: resume IntAwaiter");
62   x.handle.resume();
64   // The evaluation of 'co_await IntAwaiter (t) == 5' should be true, thus
65   // the second co_await in the expression will be unexecuted. 
66   
67   int y = x.handle.promise().get_value();
68   if ( y != 6174 )
69     {
70       PRINTF ("main: apparently wrong value : %d\n", y);
71       abort ();
72     }
74   if (!x.handle.done())
75     {
76       PRINT ("main: apparently not done...");
77       abort ();
78     }
79   PRINT ("main: returning");
80   return 0;