Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/pr95599.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } }
3
4 // The simplest co_await we can do.
5
6 #include "coro.h"
7
8 // boiler-plate for tests of codegen
9 #include "coro1-ret-int-yield-int.h"
10
11 int counter = 0;
12 int a, b, c, d;
13 int e, f, g;
14
15 struct suspend_always_order {
16 suspend_always_order (int *v) {
17 PRINT ("susp-always-order-ctor-value"); *v = counter++;
18 }
19 suspend_always_order () { PRINT ("susp-always-order-ctor"); }
20 constexpr bool await_ready() const noexcept { return false; }
21 void await_suspend(coro1::handle_type) const noexcept { PRINT ("susp-always-order-susp");}
22 void await_resume() const noexcept { PRINT ("susp-always-order-resume");}
23 ~suspend_always_order() { PRINT ("susp-always-order-dtor"); }
24 };
25
26 coro1
27 my_coro ()
28 {
29 (a = counter++, b = counter++, co_await suspend_always_order(&c), d = counter++);
30 co_await (e = counter++, suspend_always_order (&f));
31 co_return (g = counter++, 10);
32 }
33
34 int main ()
35 {
36 auto f_coro = my_coro ();
37
38 if (f_coro.handle.done())
39 {
40 PRINT ("main: we should not be 'done' [1]");
41 abort ();
42 }
43 PRINT ("main: resuming [1] initial suspend");
44 f_coro.handle.resume();
45 PRINT ("main: resuming [2] co_await");
46 f_coro.handle.resume();
47 PRINT ("main: resuming [3] co_await");
48 f_coro.handle.resume();
49
50 /* we should now have returned with the co_return */
51 if (!f_coro.handle.done())
52 {
53 PRINT ("main: we should be 'done' ");
54 abort ();
55 }
56 int y = f_coro.handle.promise().get_value();
57 if (y != 10)
58 {
59 PRINTF ("main: y is wrong : %d, should be 10\n", y);
60 abort ();
61 }
62 if (a != 0 || b != 1 || c != 2 || d != 3
63 || e != 4 || f != 5 || g != 6 )
64 {
65 PRINTF ("something happened in the wrong order %d, %d, %d, %d, %d, %d, %d\n", a, b, c, d, e, f, g);
66 abort ();
67 }
68 PRINT ("main: done");
69 return 0;
70 }