with Integer_Character_Sorted_Maps;  use Integer_Character_Sorted_Maps;
with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;

procedure Test_Sorted_Maps is

   procedure Check_Sorted (C : Container_Type) is
      K : Integer := 0;

      procedure Process (I : Iterator_Type) is
         K2 : constant Integer := Key (I);
      begin
         pragma Assert (K2 > K);
         K := K2;
      end;

      procedure Iterate is
         new Integer_Character_Sorted_Maps.Generic_Iteration;
   begin
      Iterate (C);
   end;

   subtype Integer_Subtype is Integer range 1 .. 1000;

   package Random_Keys is
      new Ada.Numerics.Discrete_Random (Integer_Subtype);

   GK : Random_Keys.Generator;
   
   subtype Character_Subtype is 
     Character range Character'Succ (' ') .. '~';

   package Random_Elements is
       new Ada.Numerics.Discrete_Random (Character_Subtype);

   GE : Random_Elements.Generator;

   use Random_Keys, Random_Elements;

   M, M2 : Container_Type;

   K : Integer;
   E : Character;
   
   I, J : Iterator_Type;
   B : Boolean;

   N : Natural;

begin

   Reset (GK);
   Reset (GE);

   pragma Assert (First (M) = Back (M));
   pragma Assert (Succ (First (M)) = First (M));
   pragma Assert (Pred (First (M)) = First (M));
   pragma Assert (Succ (Back (M)) = Back (M));
   pragma Assert (Pred (Back (M)) = Back (M));
   pragma Assert (Length (M) = 0);

   pragma Assert (M = M2);

   M2 := M;
   pragma Assert (M = M2);

   Assign (Target => M2, Source => M);
   pragma Assert (M = M2);

   E := Random (GE);

   Insert (M, 42, E, I, B);
   pragma Assert (Length (M) = 1);
   pragma Assert (B);
   pragma Assert (Key (I) = 42);
   pragma Assert (Element (I) = E);
   pragma Assert (I = First (M));
   pragma Assert (I = Last (M));
   pragma Assert (Pred (I) = Back (M));
   pragma Assert (Succ (I) = Back (M));

   M2 := M;
   pragma Assert (M = M2);

   Assign (Target => M2, Source => M);
   pragma Assert (M = M2);

   J := Find (M, Key => 42);
   pragma Assert (J /= Back (M));
   pragma Assert (Key (J) = 42);
   pragma Assert (Element (J) = E);

   M2 := M;
   pragma Assert (M = M2);

   Assign (Target => M2, Source => M);
   pragma Assert (M = M2);

   Put_Line ("inserting");

   loop

      Check_Sorted (M);

      N := Length (M);

      exit when N > 900;

      K := Random (GK);
      E := Random (GE);

      Insert (M, K, E, I, B);

--      Put (E, Width => Integer_Subtype'Width);
--      Put (' ');
--      Put (Boolean'Image (B));
--      New_Line;

      pragma Assert (Pred (First (M)) = Back (M));
      pragma Assert (Succ (Back (M)) = First (M));
      pragma Assert (Key (I) = K);

      J := Find (M, K);
      pragma Assert (J /= Back (M));
      pragma Assert (Key (J) = K);

      if I = First (M) then
         pragma Assert (Key (I) < Key (Succ (I)));
         null;
      elsif I = Last (M) then
         pragma Assert (Key (I) > Key (Pred (I)));
         null;
      else
         pragma Assert (Key (I) > Key (Pred (I)));
         pragma Assert (Key (I) < Key (Succ (I)));
         null;
      end if;

      if B then
         pragma Assert (Length (M) = N + 1);
         pragma Assert (Element (J) = E);
         null;
      else
         pragma Assert (Length (M) = N);
         null;
      end if;

      M2 := M;
      pragma Assert (M = M2);

      Assign (Target => M2, Source => M);
      pragma Assert (M = M2);

   end loop;

   Put_Line ("deleting");

   while not Is_Empty (M) loop

      I := Succ (First (M), Offset => Random (GK) mod Length (M));
      K := Key (I);

--        Put (Length (S), Width => 0);
--        Put (' ');
--        Put (E, Width => 0);
--
--      if I = First (S) then
--         Put (" first");
--
--         if I = Last (S) then
--            Put (" last");
--         end if;
--
--      elsif I = Last (S) then
--         Put (" last");
--
--      end if;
--
--      New_Line;

      Delete (M, I);

      pragma Assert (Pred (First (M)) = Back (M));
      pragma Assert (Succ (Back (M)) = First (M));
      pragma Assert (not Is_In (K, M));

      Check_Sorted (M);

      M2 := M;
      pragma Assert (M = M2);

      Assign (Target => M2, Source => M);
      pragma Assert (M = M2);

   end loop;

end Test_Sorted_Maps;

