kusano fc6ab3
----------------------------------------------------------------
kusano fc6ab3
--  ZLib for Ada thick binding.                               --
kusano fc6ab3
--                                                            --
kusano fc6ab3
--  Copyright (C) 2002-2003 Dmitriy Anisimkov                 --
kusano fc6ab3
--                                                            --
kusano fc6ab3
--  Open source license information is in the zlib.ads file.  --
kusano fc6ab3
----------------------------------------------------------------
kusano fc6ab3
kusano fc6ab3
--  $Id: test.adb,v 1.17 2003/08/12 12:13:30 vagul Exp $
kusano fc6ab3
kusano fc6ab3
--  The program has a few aims.
kusano fc6ab3
--  1. Test ZLib.Ada95 thick binding functionality.
kusano fc6ab3
--  2. Show the example of use main functionality of the ZLib.Ada95 binding.
kusano fc6ab3
--  3. Build this program automatically compile all ZLib.Ada95 packages under
kusano fc6ab3
--     GNAT Ada95 compiler.
kusano fc6ab3
kusano fc6ab3
with ZLib.Streams;
kusano fc6ab3
with Ada.Streams.Stream_IO;
kusano fc6ab3
with Ada.Numerics.Discrete_Random;
kusano fc6ab3
kusano fc6ab3
with Ada.Text_IO;
kusano fc6ab3
kusano fc6ab3
with Ada.Calendar;
kusano fc6ab3
kusano fc6ab3
procedure Test is
kusano fc6ab3
kusano fc6ab3
   use Ada.Streams;
kusano fc6ab3
   use Stream_IO;
kusano fc6ab3
kusano fc6ab3
   ------------------------------------
kusano fc6ab3
   --  Test configuration parameters --
kusano fc6ab3
   ------------------------------------
kusano fc6ab3
kusano fc6ab3
   File_Size   : Count   := 100_000;
kusano fc6ab3
   Continuous  : constant Boolean := False;
kusano fc6ab3
kusano fc6ab3
   Header      : constant ZLib.Header_Type := ZLib.Default;
kusano fc6ab3
                                              --  ZLib.None;
kusano fc6ab3
                                              --  ZLib.Auto;
kusano fc6ab3
                                              --  ZLib.GZip;
kusano fc6ab3
   --  Do not use Header other then Default in ZLib versions 1.1.4
kusano fc6ab3
   --  and older.
kusano fc6ab3
kusano fc6ab3
   Strategy    : constant ZLib.Strategy_Type := ZLib.Default_Strategy;
kusano fc6ab3
   Init_Random : constant := 10;
kusano fc6ab3
kusano fc6ab3
   -- End --
kusano fc6ab3
kusano fc6ab3
   In_File_Name  : constant String := "testzlib.in";
kusano fc6ab3
   --  Name of the input file
kusano fc6ab3
kusano fc6ab3
   Z_File_Name   : constant String := "testzlib.zlb";
kusano fc6ab3
   --  Name of the compressed file.
kusano fc6ab3
kusano fc6ab3
   Out_File_Name : constant String := "testzlib.out";
kusano fc6ab3
   --  Name of the decompressed file.
kusano fc6ab3
kusano fc6ab3
   File_In   : File_Type;
kusano fc6ab3
   File_Out  : File_Type;
kusano fc6ab3
   File_Back : File_Type;
kusano fc6ab3
   File_Z    : ZLib.Streams.Stream_Type;
kusano fc6ab3
kusano fc6ab3
   Filter : ZLib.Filter_Type;
kusano fc6ab3
kusano fc6ab3
   Time_Stamp : Ada.Calendar.Time;
kusano fc6ab3
kusano fc6ab3
   procedure Generate_File;
kusano fc6ab3
   --  Generate file of spetsified size with some random data.
kusano fc6ab3
   --  The random data is repeatable, for the good compression.
kusano fc6ab3
kusano fc6ab3
   procedure Compare_Streams
kusano fc6ab3
     (Left, Right : in out Root_Stream_Type'Class);
kusano fc6ab3
   --  The procedure compearing data in 2 streams.
kusano fc6ab3
   --  It is for compare data before and after compression/decompression.
kusano fc6ab3
kusano fc6ab3
   procedure Compare_Files (Left, Right : String);
kusano fc6ab3
   --  Compare files. Based on the Compare_Streams.
kusano fc6ab3
kusano fc6ab3
   procedure Copy_Streams
kusano fc6ab3
     (Source, Target : in out Root_Stream_Type'Class;
kusano fc6ab3
      Buffer_Size    : in     Stream_Element_Offset := 1024);
kusano fc6ab3
   --  Copying data from one stream to another. It is for test stream
kusano fc6ab3
   --  interface of the library.
kusano fc6ab3
kusano fc6ab3
   procedure Data_In
kusano fc6ab3
     (Item : out Stream_Element_Array;
kusano fc6ab3
      Last : out Stream_Element_Offset);
kusano fc6ab3
   --  this procedure is for generic instantiation of
kusano fc6ab3
   --  ZLib.Generic_Translate.
kusano fc6ab3
   --  reading data from the File_In.
kusano fc6ab3
kusano fc6ab3
   procedure Data_Out (Item : in Stream_Element_Array);
kusano fc6ab3
   --  this procedure is for generic instantiation of
kusano fc6ab3
   --  ZLib.Generic_Translate.
kusano fc6ab3
   --  writing data to the File_Out.
kusano fc6ab3
kusano fc6ab3
   procedure Stamp;
kusano fc6ab3
   --  Store the timestamp to the local variable.
kusano fc6ab3
kusano fc6ab3
   procedure Print_Statistic (Msg : String; Data_Size : ZLib.Count);
kusano fc6ab3
   --  Print the time statistic with the message.
kusano fc6ab3
kusano fc6ab3
   procedure Translate is new ZLib.Generic_Translate
kusano fc6ab3
                                (Data_In  => Data_In,
kusano fc6ab3
                                 Data_Out => Data_Out);
kusano fc6ab3
   --  This procedure is moving data from File_In to File_Out
kusano fc6ab3
   --  with compression or decompression, depend on initialization of
kusano fc6ab3
   --  Filter parameter.
kusano fc6ab3
kusano fc6ab3
   -------------------
kusano fc6ab3
   -- Compare_Files --
kusano fc6ab3
   -------------------
kusano fc6ab3
kusano fc6ab3
   procedure Compare_Files (Left, Right : String) is
kusano fc6ab3
      Left_File, Right_File : File_Type;
kusano fc6ab3
   begin
kusano fc6ab3
      Open (Left_File, In_File, Left);
kusano fc6ab3
      Open (Right_File, In_File, Right);
kusano fc6ab3
      Compare_Streams (Stream (Left_File).all, Stream (Right_File).all);
kusano fc6ab3
      Close (Left_File);
kusano fc6ab3
      Close (Right_File);
kusano fc6ab3
   end Compare_Files;
kusano fc6ab3
kusano fc6ab3
   ---------------------
kusano fc6ab3
   -- Compare_Streams --
kusano fc6ab3
   ---------------------
kusano fc6ab3
kusano fc6ab3
   procedure Compare_Streams
kusano fc6ab3
     (Left, Right : in out Ada.Streams.Root_Stream_Type'Class)
kusano fc6ab3
   is
kusano fc6ab3
      Left_Buffer, Right_Buffer : Stream_Element_Array (0 .. 16#FFF#);
kusano fc6ab3
      Left_Last, Right_Last : Stream_Element_Offset;
kusano fc6ab3
   begin
kusano fc6ab3
      loop
kusano fc6ab3
         Read (Left, Left_Buffer, Left_Last);
kusano fc6ab3
         Read (Right, Right_Buffer, Right_Last);
kusano fc6ab3
kusano fc6ab3
         if Left_Last /= Right_Last then
kusano fc6ab3
            Ada.Text_IO.Put_Line ("Compare error :"
kusano fc6ab3
              & Stream_Element_Offset'Image (Left_Last)
kusano fc6ab3
              & " /= "
kusano fc6ab3
              & Stream_Element_Offset'Image (Right_Last));
kusano fc6ab3
kusano fc6ab3
            raise Constraint_Error;
kusano fc6ab3
kusano fc6ab3
         elsif Left_Buffer (0 .. Left_Last)
kusano fc6ab3
               /= Right_Buffer (0 .. Right_Last)
kusano fc6ab3
         then
kusano fc6ab3
            Ada.Text_IO.Put_Line ("ERROR: IN and OUT files is not equal.");
kusano fc6ab3
            raise Constraint_Error;
kusano fc6ab3
kusano fc6ab3
         end if;
kusano fc6ab3
kusano fc6ab3
         exit when Left_Last < Left_Buffer'Last;
kusano fc6ab3
      end loop;
kusano fc6ab3
   end Compare_Streams;
kusano fc6ab3
kusano fc6ab3
   ------------------
kusano fc6ab3
   -- Copy_Streams --
kusano fc6ab3
   ------------------
kusano fc6ab3
kusano fc6ab3
   procedure Copy_Streams
kusano fc6ab3
     (Source, Target : in out Ada.Streams.Root_Stream_Type'Class;
kusano fc6ab3
      Buffer_Size    : in     Stream_Element_Offset := 1024)
kusano fc6ab3
   is
kusano fc6ab3
      Buffer : Stream_Element_Array (1 .. Buffer_Size);
kusano fc6ab3
      Last   : Stream_Element_Offset;
kusano fc6ab3
   begin
kusano fc6ab3
      loop
kusano fc6ab3
         Read  (Source, Buffer, Last);
kusano fc6ab3
         Write (Target, Buffer (1 .. Last));
kusano fc6ab3
kusano fc6ab3
         exit when Last < Buffer'Last;
kusano fc6ab3
      end loop;
kusano fc6ab3
   end Copy_Streams;
kusano fc6ab3
kusano fc6ab3
   -------------
kusano fc6ab3
   -- Data_In --
kusano fc6ab3
   -------------
kusano fc6ab3
kusano fc6ab3
   procedure Data_In
kusano fc6ab3
     (Item : out Stream_Element_Array;
kusano fc6ab3
      Last : out Stream_Element_Offset) is
kusano fc6ab3
   begin
kusano fc6ab3
      Read (File_In, Item, Last);
kusano fc6ab3
   end Data_In;
kusano fc6ab3
kusano fc6ab3
   --------------
kusano fc6ab3
   -- Data_Out --
kusano fc6ab3
   --------------
kusano fc6ab3
kusano fc6ab3
   procedure Data_Out (Item : in Stream_Element_Array) is
kusano fc6ab3
   begin
kusano fc6ab3
      Write (File_Out, Item);
kusano fc6ab3
   end Data_Out;
kusano fc6ab3
kusano fc6ab3
   -------------------
kusano fc6ab3
   -- Generate_File --
kusano fc6ab3
   -------------------
kusano fc6ab3
kusano fc6ab3
   procedure Generate_File is
kusano fc6ab3
      subtype Visible_Symbols is Stream_Element range 16#20# .. 16#7E#;
kusano fc6ab3
kusano fc6ab3
      package Random_Elements is
kusano fc6ab3
         new Ada.Numerics.Discrete_Random (Visible_Symbols);
kusano fc6ab3
kusano fc6ab3
      Gen    : Random_Elements.Generator;
kusano fc6ab3
      Buffer : Stream_Element_Array := (1 .. 77 => 16#20#) & 10;
kusano fc6ab3
kusano fc6ab3
      Buffer_Count : constant Count := File_Size / Buffer'Length;
kusano fc6ab3
      --  Number of same buffers in the packet.
kusano fc6ab3
kusano fc6ab3
      Density : constant Count := 30; --  from 0 to Buffer'Length - 2;
kusano fc6ab3
kusano fc6ab3
      procedure Fill_Buffer (J, D : in Count);
kusano fc6ab3
      --  Change the part of the buffer.
kusano fc6ab3
kusano fc6ab3
      -----------------
kusano fc6ab3
      -- Fill_Buffer --
kusano fc6ab3
      -----------------
kusano fc6ab3
kusano fc6ab3
      procedure Fill_Buffer (J, D : in Count) is
kusano fc6ab3
      begin
kusano fc6ab3
         for K in 0 .. D loop
kusano fc6ab3
            Buffer
kusano fc6ab3
              (Stream_Element_Offset ((J + K) mod (Buffer'Length - 1) + 1))
kusano fc6ab3
             := Random_Elements.Random (Gen);
kusano fc6ab3
kusano fc6ab3
         end loop;
kusano fc6ab3
      end Fill_Buffer;
kusano fc6ab3
kusano fc6ab3
   begin
kusano fc6ab3
      Random_Elements.Reset (Gen, Init_Random);
kusano fc6ab3
kusano fc6ab3
      Create (File_In, Out_File, In_File_Name);
kusano fc6ab3
kusano fc6ab3
      Fill_Buffer (1, Buffer'Length - 2);
kusano fc6ab3
kusano fc6ab3
      for J in 1 .. Buffer_Count loop
kusano fc6ab3
         Write (File_In, Buffer);
kusano fc6ab3
kusano fc6ab3
         Fill_Buffer (J, Density);
kusano fc6ab3
      end loop;
kusano fc6ab3
kusano fc6ab3
      --  fill remain size.
kusano fc6ab3
kusano fc6ab3
      Write
kusano fc6ab3
        (File_In,
kusano fc6ab3
         Buffer
kusano fc6ab3
           (1 .. Stream_Element_Offset
kusano fc6ab3
                   (File_Size - Buffer'Length * Buffer_Count)));
kusano fc6ab3
kusano fc6ab3
      Flush (File_In);
kusano fc6ab3
      Close (File_In);
kusano fc6ab3
   end Generate_File;
kusano fc6ab3
kusano fc6ab3
   ---------------------
kusano fc6ab3
   -- Print_Statistic --
kusano fc6ab3
   ---------------------
kusano fc6ab3
kusano fc6ab3
   procedure Print_Statistic (Msg : String; Data_Size : ZLib.Count) is
kusano fc6ab3
      use Ada.Calendar;
kusano fc6ab3
      use Ada.Text_IO;
kusano fc6ab3
kusano fc6ab3
      package Count_IO is new Integer_IO (ZLib.Count);
kusano fc6ab3
kusano fc6ab3
      Curr_Dur : Duration := Clock - Time_Stamp;
kusano fc6ab3
   begin
kusano fc6ab3
      Put (Msg);
kusano fc6ab3
kusano fc6ab3
      Set_Col (20);
kusano fc6ab3
      Ada.Text_IO.Put ("size =");
kusano fc6ab3
kusano fc6ab3
      Count_IO.Put
kusano fc6ab3
        (Data_Size,
kusano fc6ab3
         Width => Stream_IO.Count'Image (File_Size)'Length);
kusano fc6ab3
kusano fc6ab3
      Put_Line (" duration =" & Duration'Image (Curr_Dur));
kusano fc6ab3
   end Print_Statistic;
kusano fc6ab3
kusano fc6ab3
   -----------
kusano fc6ab3
   -- Stamp --
kusano fc6ab3
   -----------
kusano fc6ab3
kusano fc6ab3
   procedure Stamp is
kusano fc6ab3
   begin
kusano fc6ab3
      Time_Stamp := Ada.Calendar.Clock;
kusano fc6ab3
   end Stamp;
kusano fc6ab3
kusano fc6ab3
begin
kusano fc6ab3
   Ada.Text_IO.Put_Line ("ZLib " & ZLib.Version);
kusano fc6ab3
kusano fc6ab3
   loop
kusano fc6ab3
      Generate_File;
kusano fc6ab3
kusano fc6ab3
      for Level in ZLib.Compression_Level'Range loop
kusano fc6ab3
kusano fc6ab3
         Ada.Text_IO.Put_Line ("Level ="
kusano fc6ab3
            & ZLib.Compression_Level'Image (Level));
kusano fc6ab3
kusano fc6ab3
         --  Test generic interface.
kusano fc6ab3
         Open   (File_In, In_File, In_File_Name);
kusano fc6ab3
         Create (File_Out, Out_File, Z_File_Name);
kusano fc6ab3
kusano fc6ab3
         Stamp;
kusano fc6ab3
kusano fc6ab3
         --  Deflate using generic instantiation.
kusano fc6ab3
kusano fc6ab3
         ZLib.Deflate_Init
kusano fc6ab3
               (Filter   => Filter,
kusano fc6ab3
                Level    => Level,
kusano fc6ab3
                Strategy => Strategy,
kusano fc6ab3
                Header   => Header);
kusano fc6ab3
kusano fc6ab3
         Translate (Filter);
kusano fc6ab3
         Print_Statistic ("Generic compress", ZLib.Total_Out (Filter));
kusano fc6ab3
         ZLib.Close (Filter);
kusano fc6ab3
kusano fc6ab3
         Close (File_In);
kusano fc6ab3
         Close (File_Out);
kusano fc6ab3
kusano fc6ab3
         Open   (File_In, In_File, Z_File_Name);
kusano fc6ab3
         Create (File_Out, Out_File, Out_File_Name);
kusano fc6ab3
kusano fc6ab3
         Stamp;
kusano fc6ab3
kusano fc6ab3
         --  Inflate using generic instantiation.
kusano fc6ab3
kusano fc6ab3
         ZLib.Inflate_Init (Filter, Header => Header);
kusano fc6ab3
kusano fc6ab3
         Translate (Filter);
kusano fc6ab3
         Print_Statistic ("Generic decompress", ZLib.Total_Out (Filter));
kusano fc6ab3
kusano fc6ab3
         ZLib.Close (Filter);
kusano fc6ab3
kusano fc6ab3
         Close (File_In);
kusano fc6ab3
         Close (File_Out);
kusano fc6ab3
kusano fc6ab3
         Compare_Files (In_File_Name, Out_File_Name);
kusano fc6ab3
kusano fc6ab3
         --  Test stream interface.
kusano fc6ab3
kusano fc6ab3
         --  Compress to the back stream.
kusano fc6ab3
kusano fc6ab3
         Open   (File_In, In_File, In_File_Name);
kusano fc6ab3
         Create (File_Back, Out_File, Z_File_Name);
kusano fc6ab3
kusano fc6ab3
         Stamp;
kusano fc6ab3
kusano fc6ab3
         ZLib.Streams.Create
kusano fc6ab3
           (Stream          => File_Z,
kusano fc6ab3
            Mode            => ZLib.Streams.Out_Stream,
kusano fc6ab3
            Back            => ZLib.Streams.Stream_Access
kusano fc6ab3
                                 (Stream (File_Back)),
kusano fc6ab3
            Back_Compressed => True,
kusano fc6ab3
            Level           => Level,
kusano fc6ab3
            Strategy        => Strategy,
kusano fc6ab3
            Header          => Header);
kusano fc6ab3
kusano fc6ab3
         Copy_Streams
kusano fc6ab3
           (Source => Stream (File_In).all,
kusano fc6ab3
            Target => File_Z);
kusano fc6ab3
kusano fc6ab3
         --  Flushing internal buffers to the back stream.
kusano fc6ab3
kusano fc6ab3
         ZLib.Streams.Flush (File_Z, ZLib.Finish);
kusano fc6ab3
kusano fc6ab3
         Print_Statistic ("Write compress",
kusano fc6ab3
                          ZLib.Streams.Write_Total_Out (File_Z));
kusano fc6ab3
kusano fc6ab3
         ZLib.Streams.Close (File_Z);
kusano fc6ab3
kusano fc6ab3
         Close (File_In);
kusano fc6ab3
         Close (File_Back);
kusano fc6ab3
kusano fc6ab3
         --  Compare reading from original file and from
kusano fc6ab3
         --  decompression stream.
kusano fc6ab3
kusano fc6ab3
         Open (File_In,   In_File, In_File_Name);
kusano fc6ab3
         Open (File_Back, In_File, Z_File_Name);
kusano fc6ab3
kusano fc6ab3
         ZLib.Streams.Create
kusano fc6ab3
           (Stream          => File_Z,
kusano fc6ab3
            Mode            => ZLib.Streams.In_Stream,
kusano fc6ab3
            Back            => ZLib.Streams.Stream_Access
kusano fc6ab3
                                 (Stream (File_Back)),
kusano fc6ab3
            Back_Compressed => True,
kusano fc6ab3
            Header          => Header);
kusano fc6ab3
kusano fc6ab3
         Stamp;
kusano fc6ab3
         Compare_Streams (Stream (File_In).all, File_Z);
kusano fc6ab3
kusano fc6ab3
         Print_Statistic ("Read decompress",
kusano fc6ab3
                          ZLib.Streams.Read_Total_Out (File_Z));
kusano fc6ab3
kusano fc6ab3
         ZLib.Streams.Close (File_Z);
kusano fc6ab3
         Close (File_In);
kusano fc6ab3
         Close (File_Back);
kusano fc6ab3
kusano fc6ab3
         --  Compress by reading from compression stream.
kusano fc6ab3
kusano fc6ab3
         Open (File_Back, In_File, In_File_Name);
kusano fc6ab3
         Create (File_Out, Out_File, Z_File_Name);
kusano fc6ab3
kusano fc6ab3
         ZLib.Streams.Create
kusano fc6ab3
           (Stream          => File_Z,
kusano fc6ab3
            Mode            => ZLib.Streams.In_Stream,
kusano fc6ab3
            Back            => ZLib.Streams.Stream_Access
kusano fc6ab3
                                 (Stream (File_Back)),
kusano fc6ab3
            Back_Compressed => False,
kusano fc6ab3
            Level           => Level,
kusano fc6ab3
            Strategy        => Strategy,
kusano fc6ab3
            Header          => Header);
kusano fc6ab3
kusano fc6ab3
         Stamp;
kusano fc6ab3
         Copy_Streams
kusano fc6ab3
           (Source => File_Z,
kusano fc6ab3
            Target => Stream (File_Out).all);
kusano fc6ab3
kusano fc6ab3
         Print_Statistic ("Read compress",
kusano fc6ab3
                          ZLib.Streams.Read_Total_Out (File_Z));
kusano fc6ab3
kusano fc6ab3
         ZLib.Streams.Close (File_Z);
kusano fc6ab3
kusano fc6ab3
         Close (File_Out);
kusano fc6ab3
         Close (File_Back);
kusano fc6ab3
kusano fc6ab3
         --  Decompress to decompression stream.
kusano fc6ab3
kusano fc6ab3
         Open   (File_In,   In_File, Z_File_Name);
kusano fc6ab3
         Create (File_Back, Out_File, Out_File_Name);
kusano fc6ab3
kusano fc6ab3
         ZLib.Streams.Create
kusano fc6ab3
           (Stream          => File_Z,
kusano fc6ab3
            Mode            => ZLib.Streams.Out_Stream,
kusano fc6ab3
            Back            => ZLib.Streams.Stream_Access
kusano fc6ab3
                                 (Stream (File_Back)),
kusano fc6ab3
            Back_Compressed => False,
kusano fc6ab3
            Header          => Header);
kusano fc6ab3
kusano fc6ab3
         Stamp;
kusano fc6ab3
kusano fc6ab3
         Copy_Streams
kusano fc6ab3
           (Source => Stream (File_In).all,
kusano fc6ab3
            Target => File_Z);
kusano fc6ab3
kusano fc6ab3
         Print_Statistic ("Write decompress",
kusano fc6ab3
                          ZLib.Streams.Write_Total_Out (File_Z));
kusano fc6ab3
kusano fc6ab3
         ZLib.Streams.Close (File_Z);
kusano fc6ab3
         Close (File_In);
kusano fc6ab3
         Close (File_Back);
kusano fc6ab3
kusano fc6ab3
         Compare_Files (In_File_Name, Out_File_Name);
kusano fc6ab3
      end loop;
kusano fc6ab3
kusano fc6ab3
      Ada.Text_IO.Put_Line (Count'Image (File_Size) & " Ok.");
kusano fc6ab3
kusano fc6ab3
      exit when not Continuous;
kusano fc6ab3
kusano fc6ab3
      File_Size := File_Size + 1;
kusano fc6ab3
   end loop;
kusano fc6ab3
end Test;