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
--  Continuous test for ZLib multithreading. If the test would fail
kusano fc6ab3
--  we should provide thread safe allocation routines for the Z_Stream.
kusano fc6ab3
--
kusano fc6ab3
--  $Id: mtest.adb,v 1.4 2004/07/23 07:49:54 vagul Exp $
kusano fc6ab3
kusano fc6ab3
with ZLib;
kusano fc6ab3
with Ada.Streams;
kusano fc6ab3
with Ada.Numerics.Discrete_Random;
kusano fc6ab3
with Ada.Text_IO;
kusano fc6ab3
with Ada.Exceptions;
kusano fc6ab3
with Ada.Task_Identification;
kusano fc6ab3
kusano fc6ab3
procedure MTest is
kusano fc6ab3
   use Ada.Streams;
kusano fc6ab3
   use ZLib;
kusano fc6ab3
kusano fc6ab3
   Stop : Boolean := False;
kusano fc6ab3
kusano fc6ab3
   pragma Atomic (Stop);
kusano fc6ab3
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
   task type Test_Task;
kusano fc6ab3
kusano fc6ab3
   task body Test_Task is
kusano fc6ab3
      Buffer : Stream_Element_Array (1 .. 100_000);
kusano fc6ab3
      Gen : Random_Elements.Generator;
kusano fc6ab3
kusano fc6ab3
      Buffer_First  : Stream_Element_Offset;
kusano fc6ab3
      Compare_First : Stream_Element_Offset;
kusano fc6ab3
kusano fc6ab3
      Deflate : Filter_Type;
kusano fc6ab3
      Inflate : Filter_Type;
kusano fc6ab3
kusano fc6ab3
      procedure Further (Item : in Stream_Element_Array);
kusano fc6ab3
kusano fc6ab3
      procedure Read_Buffer
kusano fc6ab3
        (Item : out Ada.Streams.Stream_Element_Array;
kusano fc6ab3
         Last : out Ada.Streams.Stream_Element_Offset);
kusano fc6ab3
kusano fc6ab3
      -------------
kusano fc6ab3
      -- Further --
kusano fc6ab3
      -------------
kusano fc6ab3
kusano fc6ab3
      procedure Further (Item : in Stream_Element_Array) is
kusano fc6ab3
kusano fc6ab3
         procedure Compare (Item : in Stream_Element_Array);
kusano fc6ab3
kusano fc6ab3
         -------------
kusano fc6ab3
         -- Compare --
kusano fc6ab3
         -------------
kusano fc6ab3
kusano fc6ab3
         procedure Compare (Item : in Stream_Element_Array) is
kusano fc6ab3
            Next_First : Stream_Element_Offset := Compare_First + Item'Length;
kusano fc6ab3
         begin
kusano fc6ab3
            if Buffer (Compare_First .. Next_First - 1) /= Item then
kusano fc6ab3
               raise Program_Error;
kusano fc6ab3
            end if;
kusano fc6ab3
kusano fc6ab3
            Compare_First := Next_First;
kusano fc6ab3
         end Compare;
kusano fc6ab3
kusano fc6ab3
         procedure Compare_Write is new ZLib.Write (Write => Compare);
kusano fc6ab3
      begin
kusano fc6ab3
         Compare_Write (Inflate, Item, No_Flush);
kusano fc6ab3
      end Further;
kusano fc6ab3
kusano fc6ab3
      -----------------
kusano fc6ab3
      -- Read_Buffer --
kusano fc6ab3
      -----------------
kusano fc6ab3
kusano fc6ab3
      procedure Read_Buffer
kusano fc6ab3
        (Item : out Ada.Streams.Stream_Element_Array;
kusano fc6ab3
         Last : out Ada.Streams.Stream_Element_Offset)
kusano fc6ab3
      is
kusano fc6ab3
         Buff_Diff   : Stream_Element_Offset := Buffer'Last - Buffer_First;
kusano fc6ab3
         Next_First : Stream_Element_Offset;
kusano fc6ab3
      begin
kusano fc6ab3
         if Item'Length <= Buff_Diff then
kusano fc6ab3
            Last := Item'Last;
kusano fc6ab3
kusano fc6ab3
            Next_First := Buffer_First + Item'Length;
kusano fc6ab3
kusano fc6ab3
            Item := Buffer (Buffer_First .. Next_First - 1);
kusano fc6ab3
kusano fc6ab3
            Buffer_First := Next_First;
kusano fc6ab3
         else
kusano fc6ab3
            Last := Item'First + Buff_Diff;
kusano fc6ab3
            Item (Item'First .. Last) := Buffer (Buffer_First .. Buffer'Last);
kusano fc6ab3
            Buffer_First := Buffer'Last + 1;
kusano fc6ab3
         end if;
kusano fc6ab3
      end Read_Buffer;
kusano fc6ab3
kusano fc6ab3
      procedure Translate is new Generic_Translate
kusano fc6ab3
                                   (Data_In  => Read_Buffer,
kusano fc6ab3
                                    Data_Out => Further);
kusano fc6ab3
kusano fc6ab3
   begin
kusano fc6ab3
      Random_Elements.Reset (Gen);
kusano fc6ab3
kusano fc6ab3
      Buffer := (others => 20);
kusano fc6ab3
kusano fc6ab3
      Main : loop
kusano fc6ab3
         for J in Buffer'Range loop
kusano fc6ab3
            Buffer (J) := Random_Elements.Random (Gen);
kusano fc6ab3
kusano fc6ab3
            Deflate_Init (Deflate);
kusano fc6ab3
            Inflate_Init (Inflate);
kusano fc6ab3
kusano fc6ab3
            Buffer_First  := Buffer'First;
kusano fc6ab3
            Compare_First := Buffer'First;
kusano fc6ab3
kusano fc6ab3
            Translate (Deflate);
kusano fc6ab3
kusano fc6ab3
            if Compare_First /= Buffer'Last + 1 then
kusano fc6ab3
               raise Program_Error;
kusano fc6ab3
            end if;
kusano fc6ab3
kusano fc6ab3
            Ada.Text_IO.Put_Line
kusano fc6ab3
              (Ada.Task_Identification.Image
kusano fc6ab3
                 (Ada.Task_Identification.Current_Task)
kusano fc6ab3
               & Stream_Element_Offset'Image (J)
kusano fc6ab3
               & ZLib.Count'Image (Total_Out (Deflate)));
kusano fc6ab3
kusano fc6ab3
            Close (Deflate);
kusano fc6ab3
            Close (Inflate);
kusano fc6ab3
kusano fc6ab3
            exit Main when Stop;
kusano fc6ab3
         end loop;
kusano fc6ab3
      end loop Main;
kusano fc6ab3
   exception
kusano fc6ab3
      when E : others =>
kusano fc6ab3
         Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Information (E));
kusano fc6ab3
         Stop := True;
kusano fc6ab3
   end Test_Task;
kusano fc6ab3
kusano fc6ab3
   Test : array (1 .. 4) of Test_Task;
kusano fc6ab3
kusano fc6ab3
   pragma Unreferenced (Test);
kusano fc6ab3
kusano fc6ab3
   Dummy : Character;
kusano fc6ab3
kusano fc6ab3
begin
kusano fc6ab3
   Ada.Text_IO.Get_Immediate (Dummy);
kusano fc6ab3
   Stop := True;
kusano fc6ab3
end MTest;