testsuite: don't declare printf in coro.h
[official-gcc.git] / gcc / testsuite / g++.dg / coroutines / torture / co-await-24-for-init.C
blob1bf2f6d912deb5c1ff75a5844f5c27b634c16b46
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.  */
12 struct IntAwaiter {
13   int v;
14   IntAwaiter (int _v) : v(_v) {}
15   bool await_ready () { return false; }
16   void await_suspend (coro::coroutine_handle<>) {}
17   int await_resume () { return v; }
20 struct coro1
21 coro_a (bool t)
23   int accum = 0;
24   for (int x = co_await IntAwaiter (3); x < 10; x++)
25     accum += x;
27   co_return accum;
30 struct coro1
31 coro_b (bool t)
33   int accum = 0;
34   int x;
35   for (x = co_await IntAwaiter (3); x < 10; x++)
36     accum += x;
38   co_return accum;
41 struct coro1
42 coro_c (bool t)
44   int accum = 0;
45   int x = 3;
46   for (co_await IntAwaiter (3); x < 10; x++)
47     accum += x;
49   co_return accum;
52 void
53 check_a_coro (struct coro1& x)
55   if (x.handle.done())
56     {
57       PRINT ("check_a_coro: apparently done when we shouldn't be...");
58       abort ();
59     }
61   PRINT ("check_a_coro: resume initial suspend");
62   x.handle.resume();
64   // will be false - so no yield expected.
65   PRINT ("check_a_coro: resume for init");
66   x.handle.resume();
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     }
75   if (!x.handle.done())
76     {
77       PRINT ("check_a_coro: apparently not done...");
78       abort ();
79     }
82 int main ()
84   {
85     struct coro1 x = coro_a (false);
86     check_a_coro (x);
87   }
89   {
90     struct coro1 x = coro_b (false);
91     check_a_coro (x);
92   }
94   {
95     struct coro1 x = coro_c (false);
96     check_a_coro (x);
97   }
99   PRINT ("main: done");
100   return 0;