with Integer_Unbounded_Double_Lists;  use Integer_Unbounded_Double_Lists;

with Ada.Numerics.Discrete_Random;
with Ada.Streams.Stream_IO;   use Ada.Streams;
with Ada.Text_IO;             use Ada.Text_IO;
with Ada.Integer_Text_IO;     use Ada.Integer_Text_IO;

procedure Test_List_Stream_IO is

   procedure Print (L : List) is

      procedure Process (C : Cursor) is
      begin
         Put (Element (C), Width => 0);
         Put (' ');
      end;

      procedure Iterate is
         new Generic_Iteration;
   begin
      Iterate (L);
      New_Line;
   end;


   package Random_Integers is
      new Ada.Numerics.Discrete_Random (Integer);

   GI : Random_Integers.Generator;
   use Random_Integers;

   L, L2 : List;

   F : Stream_IO.File_Type;
   use Stream_IO;

   C, C2 : Cursor;
   E : Integer;

begin

   Reset (GI);

   pragma Assert (L2 = L);
   pragma Assert (Last (L) = Null_Cursor);
   pragma Assert (First (L) = Null_Cursor);

   Create (F);

   List'Write (Stream (F), L);

   Reset (F, In_File);

   List'Read (Stream (F), L2);
   pragma Assert (L2 = L);

   for I in 1 .. 100 loop

      Put (I, Width => 0);
      New_Line;

      E := Random (GI) mod 100;

      Insert (L, C, E, Position => C2);
      Put ("L1: ");
      Print (L);

      Reset (F, Out_File);

      List'Write (Stream (F), L);

      Reset (F, In_File);

      List'Read (Stream (F), L2);

      Put ("L2: ");
      Print (L2);
      New_Line;

      pragma Assert (L2 = L);
      pragma Assert (Next (Last (L)) = Null_Cursor);
      pragma Assert (Previous (First (L)) = Null_Cursor);

   end loop;

end Test_List_Stream_IO;


