Debian Codegraph @

gcc/testsuite/g++.dg/coroutines/torture/co-await-12-operator-2.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() & noexcept {
15 return coro1::suspend_always_intprt{};
16 }
17 auto operator co_await() && noexcept {
18 return coro1::suspend_always_longprtsq(3L);
19 }
20 };
21
22 empty e{};
23
24 coro1
25 f ()
26 {
27 int a = co_await e; /* operator ovl lv. */
28 int b = co_await empty{}; /* operator ovl rv. */
29 co_return b + a;
30 }
31
32 int main ()
33 {
34 PRINT ("main: create coro1");
35 struct coro1 f_coro = f ();
36
37 if (f_coro.handle.done())
38 {
39 PRINT ("main: we should not be 'done'");
40 abort ();
41 }
42
43 PRINT ("main: resuming [1] initial suspend");
44 f_coro.handle.resume();
45
46 PRINT ("main: resuming [2] co_await a");
47 f_coro.handle.resume();
48
49 PRINT ("main: resuming [3] co_await b");
50 f_coro.handle.resume();
51
52 /* we should now have returned with the co_return (14) */
53 if (!f_coro.handle.done())
54 {
55 PRINT ("main: we should be 'done' ");
56 abort ();
57 }
58
59 int y = f_coro.handle.promise().get_value();
60 if (y != 14)
61 {
62 PRINTF ("main: y is wrong : %d, should be 14\n", y);
63 abort ();
64 }
65 PRINT ("main: done");
66 return 0;
67 }