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

procedure Test_Hashed_String_Multimaps is

   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;

   N : Natural;

begin

   Reset (GK);
   Reset (GE);

   pragma Assert (First (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);
   pragma Assert (Length (M) = 1);
   pragma Assert (Key (I) = To_String (K));
   pragma Assert (Element (I) = E);
   pragma Assert (I = First (M));
   pragma Assert (Succ (M, 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

      N := Length (M);

      exit when N > 900;

      K := To_Unbounded_String (Random_Key);
      E := Random (GE);

      Insert (M, To_String (K), E, I);
      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));
      pragma Assert (Length (M) = N + 1);
      pragma Assert (Element (J) = E);

      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 := First (M);

      for Index in 1 .. Random (GE) mod Length (M) loop
         Increment (M, I);
      end loop;

      K := To_Unbounded_String (Key (I));

      Delete (M, I);

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

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

   end loop;

end Test_Hashed_String_Multimaps;



