task_coroutines_0.1.0_700f643c/tests/src/tests.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
pragma Ada_2022;

with Task_Coroutines.Coroutine;
with Task_Coroutines.Generator;

with Ada.Text_IO;

with GNAT.Source_Info;

procedure Tests is

   use Task_Coroutines;

   procedure Test_Coro is

      procedure My_Coroutine (Ctrl : in out Coroutine.Inner_Control'Class) is
      begin
         Ada.Text_IO.Put_Line ("Step 1");
         Ctrl.Yield;
         Ada.Text_IO.Put_Line ("Step 2");
         Ctrl.Yield;
         Ada.Text_IO.Put_Line ("Step 3");
      end My_Coroutine;

      C : aliased Coroutine.Instance;

   begin
      Ada.Text_IO.Put_Line ("Start the Coroutine");
      C.Start (My_Coroutine'Unrestricted_Access);
      loop
         Ada.Text_IO.Put_Line ("Poll");
         C.Poll;
         exit when C.Done;
      end loop;
   end Test_Coro;

   procedure Test_Coro_Time is

      procedure My_Coroutine (Ctrl : in out Coroutine.Inner_Control'Class) is
      begin
         loop
            Ctrl.Delay_Seconds (1.0);
            Ada.Text_IO.Put_Line (GNAT.Source_Info.Enclosing_Entity &
                                    ": Clock"
                                  & Ctrl.Clock'Img);

            exit when Ctrl.Clock > 5.0;
         end loop;
      end My_Coroutine;

      C : aliased Coroutine.Instance;

   begin

      C.Start (My_Coroutine'Unrestricted_Access);
      loop
         C.Poll (0.1);

         exit when C.Done;

         delay 0.1;
      end loop;
   end Test_Coro_Time;

   procedure Test_Gen is

      package Int_Generator is new Generator (Integer);

      procedure My_Gen_Proc
        (Ctrl : in out Int_Generator.Inner_Control'Class)
      is

         procedure Gen_Positive
           (Ctrl : in out Int_Generator.Inner_Control'Class)
         is
            Cnt : Positive := 1;
         begin
            loop
               Ctrl.Yield (Cnt);
               Cnt := Cnt + 1;
               exit when Cnt > 5;
            end loop;
         end Gen_Positive;

         Nested : aliased Int_Generator.Instance;
         --  This generator is nested in the first one
      begin
         Nested.Start (Gen_Positive'Unrestricted_Access);

         for Elt of Nested loop
            Ctrl.Yield (Elt * 2);
         end loop;
         Nested.Stop;
      end My_Gen_Proc;

      G : aliased Int_Generator.Instance;

   begin
      G.Start (My_Gen_Proc'Unrestricted_Access);

      for Elt of G loop
         Ada.Text_IO.Put_Line ("Gen returned: " & Elt'Img);
      end loop;
   end Test_Gen;

begin
   Test_Coro;
   Test_Gen;
   Test_Coro_Time;
end Tests;