with Ada.Synchronous_Task_Control; use Ada.Synchronous_Task_Control;
with Ada.Text_IO; use Ada.Text_IO;

--  The run time's tables and stacks grow as a program needs them (D185): 600 locks, more than 64 open files, a String of 10 MB on the secondary stack, a field wider
--  than 255
procedure Main is
   package Int_IO is new Integer_IO (Integer);

   Set : Natural := 0;
   Files : array (1 .. 70) of File_Type;

   function Long_Line (N : Natural) return String is
     (String'(1 .. N => 'a') & "b")
     with Pre => N < 100_000_000;
begin
   --  (each object elaborated has a lock of its own)
   for I in 1 .. 300 loop
      pragma Loop_Invariant (Set < I);
      declare
         Flag, Other : Suspension_Object;
      begin
         Set_True (Flag);
         declare
            Now : constant Boolean := Current_State (Flag);
            Not_Yet : constant Boolean := Current_State (Other);
         begin
            if Now and not Not_Yet then
               Set := Set + 1;
            end if;
         end;
      end;
   end loop;
   Put_Line (Natural'Image (Set));
   for I in Files'Range loop
      pragma Loop_Invariant (for all J in I .. Files'Last => not Is_Open (Files (J)));
      pragma Loop_Invariant (for all J in Files'First .. I - 1 => Is_Open (Files (J)));
      Create (Files (I), Out_File, "");
   end loop;
   Put_Line (Boolean'Image (Is_Open (Files (70))));
   for I in Files'Range loop
      pragma Loop_Invariant (for all J in I .. Files'Last => Is_Open (Files (J)));
      Close (Files (I));
   end loop;
   declare
      S : constant String := Long_Line (10_000_000);
   begin
      Put_Line (Natural'Image (S'Length) & " " & S (S'Last));
   end;
   Int_IO.Put (42, Width => 300);
   New_Line;
exception
   when Use_Error | Name_Error =>
      Put_Line ("could not use a file");
end Main;
