1 // { dg-do run } 2 3 // Test co-await in while condition. 4 5 #include "../coro.h" 6 7 // boiler-plate for tests of codegen 8 #include "../coro1-ret-int-yield-int.h" 9 10 /* An awaiter that suspends always and returns an int as the 11 await_resume output. */ 12 struct IntAwaiter { 13 int v; IntAwaiterIntAwaiter14 IntAwaiter (int _v) : v(_v) {} await_readyIntAwaiter15 bool await_ready () { return false; } await_suspendIntAwaiter16 void await_suspend (coro::coroutine_handle<>) {} await_resumeIntAwaiter17 int await_resume () { return v; } 18 }; 19 20 coro1 coro_a(bool t)21coro_a (bool t) 22 { 23 int accum = 0; 24 for (int x = 3; x < 10; x += co_await IntAwaiter (1)) 25 accum += x; 26 27 co_return accum; 28 } 29 30 coro1 coro_b(bool t)31coro_b (bool t) 32 { 33 int accum = 0; 34 for (int x = 3; x < 10; x += co_await IntAwaiter (1)) 35 { 36 if (x & 1) 37 continue; 38 accum += x; 39 } 40 41 co_return accum; 42 } 43 check_a_coro(coro1 & x,int expected_answer)44void check_a_coro (coro1& x, int expected_answer) 45 { 46 if (x.handle.done()) 47 { 48 PRINT ("check_a_coro: apparently done when we shouldn't be..."); 49 abort (); 50 } 51 52 PRINT ("check_a_coro: resume initial suspend"); 53 x.handle.resume(); 54 55 // will be false - so no yield expected. 56 PRINT ("check_a_coro: resume for init"); 57 while (!x.handle.done()) 58 x.handle.resume(); 59 60 int y = x.handle.promise().get_value(); 61 if ( y != expected_answer ) 62 { 63 PRINTF ("check_a_coro: apparently wrong value : %d\n", y); 64 abort (); 65 } 66 67 if (!x.handle.done()) 68 { 69 PRINT ("check_a_coro: apparently not done..."); 70 abort (); 71 } 72 } 73 main()74int main () 75 { 76 { 77 coro1 x = coro_a (false); 78 check_a_coro (x, 42); 79 } 80 { 81 coro1 x = coro_b (false); 82 check_a_coro (x, 18); 83 } 84 85 PRINT ("main: done"); 86 return 0; 87 } 88