with String_Integer_Sorted_Maps;  use String_Integer_Sorted_Maps;
with Ada.Text_IO;  use Ada.Text_IO;
with Ada.Numerics.Discrete_Random;
with Ada.Strings.Unbounded;  use Ada.Strings.Unbounded;

procedure Test_Sorted_String_Maps is

   procedure Check_Sorted (C : Container_Type) is
      procedure Process (I : Iterator_Type) is
      begin
         pragma Assert (I = First (C) or else 
                        (Key (Pred (I)) < Key (I)));
         null;
      end;

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

   subtype Integer_Subtype is Integer range 1 .. 1000;

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

   GE : Random_Elements.Generator;
   use Random_Elements;

   subtype Character_Subtype is 
     Character range Character'Succ (' ') .. '~';

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

   GK : Random_Characters.Generator;
   use Random_Characters;
   
   function Random_Key return String is
      
      N : constant Positive := 1 + Random (GE) mod 8;
      K : String (1 .. N);
   begin
      for I in K'Range loop
         K (I) := Random (GK);
      end loop;
      
      return K;
   end;
   
   M, M2 : Container_Type;

   K : Unbounded_String;
   E : Integer;
   
   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);

   K := To_Unbounded_String (Random_Key);

   Insert (M, To_String (K), E, I, B);
   pragma Assert (Length (M) = 1);
   pragma Assert (B);
   pragma Assert (Key (I) = To_String (K));
   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 => To_String (K));
   pragma Assert (J /= Back (M));
   pragma Assert (Key (J) = To_String (K));
   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 := To_Unbounded_String (Random_Key);
      E := Random (GE);

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

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

      J := Find (M, Key => To_String (K));
      pragma Assert (J /= Back (M));
      pragma Assert (Key (J) = To_String (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 (GE) mod Length (M));
      K := To_Unbounded_String (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 (To_String (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_String_Maps;


