1275793eaSopenharmony_ci----------------------------------------------------------------
2275793eaSopenharmony_ci--  ZLib for Ada thick binding.                               --
3275793eaSopenharmony_ci--                                                            --
4275793eaSopenharmony_ci--  Copyright (C) 2002-2003 Dmitriy Anisimkov                 --
5275793eaSopenharmony_ci--                                                            --
6275793eaSopenharmony_ci--  Open source license information is in the zlib.ads file.  --
7275793eaSopenharmony_ci----------------------------------------------------------------
8275793eaSopenharmony_ci--  Continuous test for ZLib multithreading. If the test would fail
9275793eaSopenharmony_ci--  we should provide thread safe allocation routines for the Z_Stream.
10275793eaSopenharmony_ci--
11275793eaSopenharmony_ci--  $Id: mtest.adb,v 1.4 2004/07/23 07:49:54 vagul Exp $
12275793eaSopenharmony_ci
13275793eaSopenharmony_ciwith ZLib;
14275793eaSopenharmony_ciwith Ada.Streams;
15275793eaSopenharmony_ciwith Ada.Numerics.Discrete_Random;
16275793eaSopenharmony_ciwith Ada.Text_IO;
17275793eaSopenharmony_ciwith Ada.Exceptions;
18275793eaSopenharmony_ciwith Ada.Task_Identification;
19275793eaSopenharmony_ci
20275793eaSopenharmony_ciprocedure MTest is
21275793eaSopenharmony_ci   use Ada.Streams;
22275793eaSopenharmony_ci   use ZLib;
23275793eaSopenharmony_ci
24275793eaSopenharmony_ci   Stop : Boolean := False;
25275793eaSopenharmony_ci
26275793eaSopenharmony_ci   pragma Atomic (Stop);
27275793eaSopenharmony_ci
28275793eaSopenharmony_ci   subtype Visible_Symbols is Stream_Element range 16#20# .. 16#7E#;
29275793eaSopenharmony_ci
30275793eaSopenharmony_ci   package Random_Elements is
31275793eaSopenharmony_ci      new Ada.Numerics.Discrete_Random (Visible_Symbols);
32275793eaSopenharmony_ci
33275793eaSopenharmony_ci   task type Test_Task;
34275793eaSopenharmony_ci
35275793eaSopenharmony_ci   task body Test_Task is
36275793eaSopenharmony_ci      Buffer : Stream_Element_Array (1 .. 100_000);
37275793eaSopenharmony_ci      Gen : Random_Elements.Generator;
38275793eaSopenharmony_ci
39275793eaSopenharmony_ci      Buffer_First  : Stream_Element_Offset;
40275793eaSopenharmony_ci      Compare_First : Stream_Element_Offset;
41275793eaSopenharmony_ci
42275793eaSopenharmony_ci      Deflate : Filter_Type;
43275793eaSopenharmony_ci      Inflate : Filter_Type;
44275793eaSopenharmony_ci
45275793eaSopenharmony_ci      procedure Further (Item : in Stream_Element_Array);
46275793eaSopenharmony_ci
47275793eaSopenharmony_ci      procedure Read_Buffer
48275793eaSopenharmony_ci        (Item : out Ada.Streams.Stream_Element_Array;
49275793eaSopenharmony_ci         Last : out Ada.Streams.Stream_Element_Offset);
50275793eaSopenharmony_ci
51275793eaSopenharmony_ci      -------------
52275793eaSopenharmony_ci      -- Further --
53275793eaSopenharmony_ci      -------------
54275793eaSopenharmony_ci
55275793eaSopenharmony_ci      procedure Further (Item : in Stream_Element_Array) is
56275793eaSopenharmony_ci
57275793eaSopenharmony_ci         procedure Compare (Item : in Stream_Element_Array);
58275793eaSopenharmony_ci
59275793eaSopenharmony_ci         -------------
60275793eaSopenharmony_ci         -- Compare --
61275793eaSopenharmony_ci         -------------
62275793eaSopenharmony_ci
63275793eaSopenharmony_ci         procedure Compare (Item : in Stream_Element_Array) is
64275793eaSopenharmony_ci            Next_First : Stream_Element_Offset := Compare_First + Item'Length;
65275793eaSopenharmony_ci         begin
66275793eaSopenharmony_ci            if Buffer (Compare_First .. Next_First - 1) /= Item then
67275793eaSopenharmony_ci               raise Program_Error;
68275793eaSopenharmony_ci            end if;
69275793eaSopenharmony_ci
70275793eaSopenharmony_ci            Compare_First := Next_First;
71275793eaSopenharmony_ci         end Compare;
72275793eaSopenharmony_ci
73275793eaSopenharmony_ci         procedure Compare_Write is new ZLib.Write (Write => Compare);
74275793eaSopenharmony_ci      begin
75275793eaSopenharmony_ci         Compare_Write (Inflate, Item, No_Flush);
76275793eaSopenharmony_ci      end Further;
77275793eaSopenharmony_ci
78275793eaSopenharmony_ci      -----------------
79275793eaSopenharmony_ci      -- Read_Buffer --
80275793eaSopenharmony_ci      -----------------
81275793eaSopenharmony_ci
82275793eaSopenharmony_ci      procedure Read_Buffer
83275793eaSopenharmony_ci        (Item : out Ada.Streams.Stream_Element_Array;
84275793eaSopenharmony_ci         Last : out Ada.Streams.Stream_Element_Offset)
85275793eaSopenharmony_ci      is
86275793eaSopenharmony_ci         Buff_Diff   : Stream_Element_Offset := Buffer'Last - Buffer_First;
87275793eaSopenharmony_ci         Next_First : Stream_Element_Offset;
88275793eaSopenharmony_ci      begin
89275793eaSopenharmony_ci         if Item'Length <= Buff_Diff then
90275793eaSopenharmony_ci            Last := Item'Last;
91275793eaSopenharmony_ci
92275793eaSopenharmony_ci            Next_First := Buffer_First + Item'Length;
93275793eaSopenharmony_ci
94275793eaSopenharmony_ci            Item := Buffer (Buffer_First .. Next_First - 1);
95275793eaSopenharmony_ci
96275793eaSopenharmony_ci            Buffer_First := Next_First;
97275793eaSopenharmony_ci         else
98275793eaSopenharmony_ci            Last := Item'First + Buff_Diff;
99275793eaSopenharmony_ci            Item (Item'First .. Last) := Buffer (Buffer_First .. Buffer'Last);
100275793eaSopenharmony_ci            Buffer_First := Buffer'Last + 1;
101275793eaSopenharmony_ci         end if;
102275793eaSopenharmony_ci      end Read_Buffer;
103275793eaSopenharmony_ci
104275793eaSopenharmony_ci      procedure Translate is new Generic_Translate
105275793eaSopenharmony_ci                                   (Data_In  => Read_Buffer,
106275793eaSopenharmony_ci                                    Data_Out => Further);
107275793eaSopenharmony_ci
108275793eaSopenharmony_ci   begin
109275793eaSopenharmony_ci      Random_Elements.Reset (Gen);
110275793eaSopenharmony_ci
111275793eaSopenharmony_ci      Buffer := (others => 20);
112275793eaSopenharmony_ci
113275793eaSopenharmony_ci      Main : loop
114275793eaSopenharmony_ci         for J in Buffer'Range loop
115275793eaSopenharmony_ci            Buffer (J) := Random_Elements.Random (Gen);
116275793eaSopenharmony_ci
117275793eaSopenharmony_ci            Deflate_Init (Deflate);
118275793eaSopenharmony_ci            Inflate_Init (Inflate);
119275793eaSopenharmony_ci
120275793eaSopenharmony_ci            Buffer_First  := Buffer'First;
121275793eaSopenharmony_ci            Compare_First := Buffer'First;
122275793eaSopenharmony_ci
123275793eaSopenharmony_ci            Translate (Deflate);
124275793eaSopenharmony_ci
125275793eaSopenharmony_ci            if Compare_First /= Buffer'Last + 1 then
126275793eaSopenharmony_ci               raise Program_Error;
127275793eaSopenharmony_ci            end if;
128275793eaSopenharmony_ci
129275793eaSopenharmony_ci            Ada.Text_IO.Put_Line
130275793eaSopenharmony_ci              (Ada.Task_Identification.Image
131275793eaSopenharmony_ci                 (Ada.Task_Identification.Current_Task)
132275793eaSopenharmony_ci               & Stream_Element_Offset'Image (J)
133275793eaSopenharmony_ci               & ZLib.Count'Image (Total_Out (Deflate)));
134275793eaSopenharmony_ci
135275793eaSopenharmony_ci            Close (Deflate);
136275793eaSopenharmony_ci            Close (Inflate);
137275793eaSopenharmony_ci
138275793eaSopenharmony_ci            exit Main when Stop;
139275793eaSopenharmony_ci         end loop;
140275793eaSopenharmony_ci      end loop Main;
141275793eaSopenharmony_ci   exception
142275793eaSopenharmony_ci      when E : others =>
143275793eaSopenharmony_ci         Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Information (E));
144275793eaSopenharmony_ci         Stop := True;
145275793eaSopenharmony_ci   end Test_Task;
146275793eaSopenharmony_ci
147275793eaSopenharmony_ci   Test : array (1 .. 4) of Test_Task;
148275793eaSopenharmony_ci
149275793eaSopenharmony_ci   pragma Unreferenced (Test);
150275793eaSopenharmony_ci
151275793eaSopenharmony_ci   Dummy : Character;
152275793eaSopenharmony_ci
153275793eaSopenharmony_cibegin
154275793eaSopenharmony_ci   Ada.Text_IO.Get_Immediate (Dummy);
155275793eaSopenharmony_ci   Stop := True;
156275793eaSopenharmony_ciend MTest;
157