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
9275793eaSopenharmony_ci--  $Id: read.adb,v 1.8 2004/05/31 10:53:40 vagul Exp $
10275793eaSopenharmony_ci
11275793eaSopenharmony_ci--  Test/demo program for the generic read interface.
12275793eaSopenharmony_ci
13275793eaSopenharmony_ciwith Ada.Numerics.Discrete_Random;
14275793eaSopenharmony_ciwith Ada.Streams;
15275793eaSopenharmony_ciwith Ada.Text_IO;
16275793eaSopenharmony_ci
17275793eaSopenharmony_ciwith ZLib;
18275793eaSopenharmony_ci
19275793eaSopenharmony_ciprocedure Read is
20275793eaSopenharmony_ci
21275793eaSopenharmony_ci   use Ada.Streams;
22275793eaSopenharmony_ci
23275793eaSopenharmony_ci   ------------------------------------
24275793eaSopenharmony_ci   --  Test configuration parameters --
25275793eaSopenharmony_ci   ------------------------------------
26275793eaSopenharmony_ci
27275793eaSopenharmony_ci   File_Size   : Stream_Element_Offset := 100_000;
28275793eaSopenharmony_ci
29275793eaSopenharmony_ci   Continuous  : constant Boolean          := False;
30275793eaSopenharmony_ci   --  If this constant is True, the test would be repeated again and again,
31275793eaSopenharmony_ci   --  with increment File_Size for every iteration.
32275793eaSopenharmony_ci
33275793eaSopenharmony_ci   Header      : constant ZLib.Header_Type := ZLib.Default;
34275793eaSopenharmony_ci   --  Do not use Header other than Default in ZLib versions 1.1.4 and older.
35275793eaSopenharmony_ci
36275793eaSopenharmony_ci   Init_Random : constant := 8;
37275793eaSopenharmony_ci   --  We are using the same random sequence, in case of we catch bug,
38275793eaSopenharmony_ci   --  so we would be able to reproduce it.
39275793eaSopenharmony_ci
40275793eaSopenharmony_ci   -- End --
41275793eaSopenharmony_ci
42275793eaSopenharmony_ci   Pack_Size : Stream_Element_Offset;
43275793eaSopenharmony_ci   Offset    : Stream_Element_Offset;
44275793eaSopenharmony_ci
45275793eaSopenharmony_ci   Filter     : ZLib.Filter_Type;
46275793eaSopenharmony_ci
47275793eaSopenharmony_ci   subtype Visible_Symbols
48275793eaSopenharmony_ci      is Stream_Element range 16#20# .. 16#7E#;
49275793eaSopenharmony_ci
50275793eaSopenharmony_ci   package Random_Elements is new
51275793eaSopenharmony_ci      Ada.Numerics.Discrete_Random (Visible_Symbols);
52275793eaSopenharmony_ci
53275793eaSopenharmony_ci   Gen : Random_Elements.Generator;
54275793eaSopenharmony_ci   Period  : constant Stream_Element_Offset := 200;
55275793eaSopenharmony_ci   --  Period constant variable for random generator not to be very random.
56275793eaSopenharmony_ci   --  Bigger period, harder random.
57275793eaSopenharmony_ci
58275793eaSopenharmony_ci   Read_Buffer : Stream_Element_Array (1 .. 2048);
59275793eaSopenharmony_ci   Read_First  : Stream_Element_Offset;
60275793eaSopenharmony_ci   Read_Last   : Stream_Element_Offset;
61275793eaSopenharmony_ci
62275793eaSopenharmony_ci   procedure Reset;
63275793eaSopenharmony_ci
64275793eaSopenharmony_ci   procedure Read
65275793eaSopenharmony_ci     (Item : out Stream_Element_Array;
66275793eaSopenharmony_ci      Last : out Stream_Element_Offset);
67275793eaSopenharmony_ci   --  this procedure is for generic instantiation of
68275793eaSopenharmony_ci   --  ZLib.Read
69275793eaSopenharmony_ci   --  reading data from the File_In.
70275793eaSopenharmony_ci
71275793eaSopenharmony_ci   procedure Read is new ZLib.Read
72275793eaSopenharmony_ci                           (Read,
73275793eaSopenharmony_ci                            Read_Buffer,
74275793eaSopenharmony_ci                            Rest_First => Read_First,
75275793eaSopenharmony_ci                            Rest_Last  => Read_Last);
76275793eaSopenharmony_ci
77275793eaSopenharmony_ci   ----------
78275793eaSopenharmony_ci   -- Read --
79275793eaSopenharmony_ci   ----------
80275793eaSopenharmony_ci
81275793eaSopenharmony_ci   procedure Read
82275793eaSopenharmony_ci     (Item : out Stream_Element_Array;
83275793eaSopenharmony_ci      Last : out Stream_Element_Offset) is
84275793eaSopenharmony_ci   begin
85275793eaSopenharmony_ci      Last := Stream_Element_Offset'Min
86275793eaSopenharmony_ci               (Item'Last,
87275793eaSopenharmony_ci                Item'First + File_Size - Offset);
88275793eaSopenharmony_ci
89275793eaSopenharmony_ci      for J in Item'First .. Last loop
90275793eaSopenharmony_ci         if J < Item'First + Period then
91275793eaSopenharmony_ci            Item (J) := Random_Elements.Random (Gen);
92275793eaSopenharmony_ci         else
93275793eaSopenharmony_ci            Item (J) := Item (J - Period);
94275793eaSopenharmony_ci         end if;
95275793eaSopenharmony_ci
96275793eaSopenharmony_ci         Offset   := Offset + 1;
97275793eaSopenharmony_ci      end loop;
98275793eaSopenharmony_ci   end Read;
99275793eaSopenharmony_ci
100275793eaSopenharmony_ci   -----------
101275793eaSopenharmony_ci   -- Reset --
102275793eaSopenharmony_ci   -----------
103275793eaSopenharmony_ci
104275793eaSopenharmony_ci   procedure Reset is
105275793eaSopenharmony_ci   begin
106275793eaSopenharmony_ci      Random_Elements.Reset (Gen, Init_Random);
107275793eaSopenharmony_ci      Pack_Size := 0;
108275793eaSopenharmony_ci      Offset := 1;
109275793eaSopenharmony_ci      Read_First := Read_Buffer'Last + 1;
110275793eaSopenharmony_ci      Read_Last  := Read_Buffer'Last;
111275793eaSopenharmony_ci   end Reset;
112275793eaSopenharmony_ci
113275793eaSopenharmony_cibegin
114275793eaSopenharmony_ci   Ada.Text_IO.Put_Line ("ZLib " & ZLib.Version);
115275793eaSopenharmony_ci
116275793eaSopenharmony_ci   loop
117275793eaSopenharmony_ci      for Level in ZLib.Compression_Level'Range loop
118275793eaSopenharmony_ci
119275793eaSopenharmony_ci         Ada.Text_IO.Put ("Level ="
120275793eaSopenharmony_ci            & ZLib.Compression_Level'Image (Level));
121275793eaSopenharmony_ci
122275793eaSopenharmony_ci         --  Deflate using generic instantiation.
123275793eaSopenharmony_ci
124275793eaSopenharmony_ci         ZLib.Deflate_Init
125275793eaSopenharmony_ci               (Filter,
126275793eaSopenharmony_ci                Level,
127275793eaSopenharmony_ci                Header => Header);
128275793eaSopenharmony_ci
129275793eaSopenharmony_ci         Reset;
130275793eaSopenharmony_ci
131275793eaSopenharmony_ci         Ada.Text_IO.Put
132275793eaSopenharmony_ci           (Stream_Element_Offset'Image (File_Size) & " ->");
133275793eaSopenharmony_ci
134275793eaSopenharmony_ci         loop
135275793eaSopenharmony_ci            declare
136275793eaSopenharmony_ci               Buffer : Stream_Element_Array (1 .. 1024);
137275793eaSopenharmony_ci               Last   : Stream_Element_Offset;
138275793eaSopenharmony_ci            begin
139275793eaSopenharmony_ci               Read (Filter, Buffer, Last);
140275793eaSopenharmony_ci
141275793eaSopenharmony_ci               Pack_Size := Pack_Size + Last - Buffer'First + 1;
142275793eaSopenharmony_ci
143275793eaSopenharmony_ci               exit when Last < Buffer'Last;
144275793eaSopenharmony_ci            end;
145275793eaSopenharmony_ci         end loop;
146275793eaSopenharmony_ci
147275793eaSopenharmony_ci         Ada.Text_IO.Put_Line (Stream_Element_Offset'Image (Pack_Size));
148275793eaSopenharmony_ci
149275793eaSopenharmony_ci         ZLib.Close (Filter);
150275793eaSopenharmony_ci      end loop;
151275793eaSopenharmony_ci
152275793eaSopenharmony_ci      exit when not Continuous;
153275793eaSopenharmony_ci
154275793eaSopenharmony_ci      File_Size := File_Size + 1;
155275793eaSopenharmony_ci   end loop;
156275793eaSopenharmony_ciend Read;
157