1 // { dg-do run } 2 3 // Test co_yield in a loop with no local state. 4 5 #include "../coro.h" 6 7 // boiler-plate for tests of codegen 8 #include "../coro1-ret-int-yield-int.h" 9 10 int gX = 1; 11 12 struct coro1 f()13f () noexcept 14 { 15 for (gX = 5; gX < 10 ; gX++) 16 { 17 PRINTF ("f: about to yield %d\n", gX); 18 co_yield gX; 19 } 20 21 PRINT ("f: about to return 6174"); 22 co_return 6174; 23 } 24 main()25int main () 26 { 27 PRINT ("main: create coro1"); 28 struct coro1 f_coro = f (); 29 PRINT ("main: got coro1 - resuming (1)"); 30 if (gX != 1) 31 { 32 PRINTF ("main: gX is wrong : %d, should be 1\n", gX); 33 abort (); 34 } 35 if (f_coro.handle.done()) 36 abort(); 37 f_coro.handle.resume(); 38 PRINT ("main: after resume (1)"); 39 int y = f_coro.handle.promise().get_value(); 40 if (y != 5) 41 { 42 PRINTF ("main: got %d not 5.\n",y); 43 abort (); 44 } 45 PRINT ("main: gX OK -- looping"); 46 do { 47 y = f_coro.handle.promise().get_value(); 48 if (y != gX) 49 { 50 PRINTF ("main: got %d not %d.\n",y, gX); 51 abort (); 52 } 53 PRINTF ("main: gX : %d \n", gX); 54 f_coro.handle.resume(); 55 } while (!f_coro.handle.done()); 56 57 y = f_coro.handle.promise().get_value(); 58 if ( y != 6174 ) 59 abort (); 60 PRINT ("main: apparently got 6174"); 61 if (!f_coro.handle.done()) 62 { 63 PRINT ("main: apparently not done..."); 64 abort (); 65 } 66 PRINT ("main: returning"); 67 return 0; 68 } 69