with Integer_Sets;  use Integer_Sets;
with Ada.Numerics.Discrete_Random;
with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Integer_Text_IO;  use Ada.Integer_Text_IO;
with Ada.Containers;  use Ada.Containers;
with Ada.Command_Line;  use Ada.Command_Line;

procedure Test_Scores2 is

   subtype Score_Subtype is Integer range 0 .. 100;

   procedure Print (S : in Set) is

      I : Score_Subtype := 0;
      J : Score_Subtype := 10;

      CI : Cursor := First (S);
      pragma Assert (Has_Element (CI));
      pragma Assert (Element (CI) = -1);

      CJ : Cursor;

   begin

      loop

         if J /= 100 then
            Put (' ');
         end if;

         Put (I, Width => 2);
         Put ('-');
         Put (J, Width => 0);
         Put (':');

         CJ := Floor (S, J);

         while CI /= CJ loop
            Next (CI);
            Put (' ');
            Put (Element (CI), Width => 0);
         end loop;

         New_Line;

         exit when J = 100;

         I := J + 1;
         J := J + 10;

      end loop;

      New_Line;

   end Print;

   package Random_Scores is
      new Ada.Numerics.Discrete_Random (Score_Subtype);

   use Random_Scores;

   G : Generator;
   E : Score_Subtype;

   S : Set;
   C : Cursor;
   B : Boolean;

   Line : String (1 .. 4);
   Last : Natural;

begin

   if Argument_Count = 0 then
      Reset (G);
   else
      declare
         Seed : constant Integer := Integer'Value (Argument (1));
      begin
         Reset (G, Seed);

         Put ("Seed=");
         Put (Seed, Width => 0);
         New_Line;
      end;
   end if;

   New_Line;
   
   Insert (S, -1, C, B);

   loop

      Print (S);

      Put ("Ready: ");

      Get_Line (Line, Last);

      exit when Last = 1
        and then (Line (1) = 'x' or else Line (1) = 'q');

      E := Random (G);

      Put ("Inserting ");
      Put (E, Width => 0);

      Insert (S, E, C, B);

      if B then
         Put_Line (" (succeeded)");
      else
         Put_Line (" (failed)");
      end if;

      New_Line;

   end loop;

end Test_Scores2;

