Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/co-await-06-ovl.C

raw
1 // { dg-do run }
2 // { dg-skip-if "requires hosted libstdc++ for cstdlib abort" { ! hostedlib } }
3
4 // Basic check of the co_await operator overload.
5
6 #include "../coro.h"
7
8 // boiler-plate for tests of codegen
9 #include "../coro1-ret-int-yield-int.h"
10
11 /* A very simple overload. */
12 struct empty
13 {
14 auto operator co_await() const & noexcept {
15 return coro1::suspend_always_intprt{};
16 }
17 };
18
19 int gX = 1;
20 empty e{};
21
22 coro1
23 f ()
24 {
25 int a = co_await(e); /* operator ovl. */
26 co_return gX + 5 + a;
27 }
28
29 int main ()
30 {
31 PRINT ("main: create coro1");
32 struct coro1 f_coro = f ();
33 PRINT ("main: got coro1 - checking gX");
34 if (gX != 1)
35 {
36 PRINTF ("main: gX is wrong : %d, should be 1\n", gX);
37 abort ();
38 }
39 if (f_coro.handle.done())
40 {
41 PRINT ("main: we should not be 'done'");
42 abort ();
43 }
44
45 PRINT ("main: resuming [1] initial suspend");
46 f_coro.handle.resume();
47
48 PRINT ("main: resuming [2] co_await");
49 f_coro.handle.resume();
50
51 /* we should now have returned with the co_return (11) */
52 if (!f_coro.handle.done())
53 {
54 PRINT ("main: we should be 'done' ");
55 abort ();
56 }
57
58 int y = f_coro.handle.promise().get_value();
59 if (y != 11)
60 {
61 PRINTF ("main: y is wrong : %d, should be 11\n", y);
62 abort ();
63 }
64 PRINT ("main: done");
65 return 0;
66 }