1 | ----------------------------------------------------------------
|
---|
2 | -- ZLib for Ada thick binding. --
|
---|
3 | -- --
|
---|
4 | -- Copyright (C) 2002-2003 Dmitriy Anisimkov --
|
---|
5 | -- --
|
---|
6 | -- Open source license information is in the zlib.ads file. --
|
---|
7 | ----------------------------------------------------------------
|
---|
8 |
|
---|
9 | -- $Id: read.adb,v 1.1 2004/11/15 16:42:25 bird Exp $
|
---|
10 |
|
---|
11 | -- Test/demo program for the generic read interface.
|
---|
12 |
|
---|
13 | with Ada.Numerics.Discrete_Random;
|
---|
14 | with Ada.Streams;
|
---|
15 | with Ada.Text_IO;
|
---|
16 |
|
---|
17 | with ZLib;
|
---|
18 |
|
---|
19 | procedure Read is
|
---|
20 |
|
---|
21 | use Ada.Streams;
|
---|
22 |
|
---|
23 | ------------------------------------
|
---|
24 | -- Test configuration parameters --
|
---|
25 | ------------------------------------
|
---|
26 |
|
---|
27 | File_Size : Stream_Element_Offset := 100_000;
|
---|
28 |
|
---|
29 | Continuous : constant Boolean := False;
|
---|
30 | -- If this constant is True, the test would be repeated again and again,
|
---|
31 | -- with increment File_Size for every iteration.
|
---|
32 |
|
---|
33 | Header : constant ZLib.Header_Type := ZLib.Default;
|
---|
34 | -- Do not use Header other than Default in ZLib versions 1.1.4 and older.
|
---|
35 |
|
---|
36 | Init_Random : constant := 8;
|
---|
37 | -- We are using the same random sequence, in case of we catch bug,
|
---|
38 | -- so we would be able to reproduce it.
|
---|
39 |
|
---|
40 | -- End --
|
---|
41 |
|
---|
42 | Pack_Size : Stream_Element_Offset;
|
---|
43 | Offset : Stream_Element_Offset;
|
---|
44 |
|
---|
45 | Filter : ZLib.Filter_Type;
|
---|
46 |
|
---|
47 | subtype Visible_Symbols
|
---|
48 | is Stream_Element range 16#20# .. 16#7E#;
|
---|
49 |
|
---|
50 | package Random_Elements is new
|
---|
51 | Ada.Numerics.Discrete_Random (Visible_Symbols);
|
---|
52 |
|
---|
53 | Gen : Random_Elements.Generator;
|
---|
54 | Period : constant Stream_Element_Offset := 200;
|
---|
55 | -- Period constant variable for random generator not to be very random.
|
---|
56 | -- Bigger period, harder random.
|
---|
57 |
|
---|
58 | Read_Buffer : Stream_Element_Array (1 .. 2048);
|
---|
59 | Read_First : Stream_Element_Offset;
|
---|
60 | Read_Last : Stream_Element_Offset;
|
---|
61 |
|
---|
62 | procedure Reset;
|
---|
63 |
|
---|
64 | procedure Read
|
---|
65 | (Item : out Stream_Element_Array;
|
---|
66 | Last : out Stream_Element_Offset);
|
---|
67 | -- this procedure is for generic instantiation of
|
---|
68 | -- ZLib.Read
|
---|
69 | -- reading data from the File_In.
|
---|
70 |
|
---|
71 | procedure Read is new ZLib.Read (Read, Read_Buffer, Read_First, Read_Last);
|
---|
72 |
|
---|
73 | ----------
|
---|
74 | -- Read --
|
---|
75 | ----------
|
---|
76 |
|
---|
77 | procedure Read
|
---|
78 | (Item : out Stream_Element_Array;
|
---|
79 | Last : out Stream_Element_Offset) is
|
---|
80 | begin
|
---|
81 | Last := Stream_Element_Offset'Min
|
---|
82 | (Item'Last,
|
---|
83 | Item'First + File_Size - Offset);
|
---|
84 |
|
---|
85 | for J in Item'First .. Last loop
|
---|
86 | if J < Item'First + Period then
|
---|
87 | Item (J) := Random_Elements.Random (Gen);
|
---|
88 | else
|
---|
89 | Item (J) := Item (J - Period);
|
---|
90 | end if;
|
---|
91 |
|
---|
92 | Offset := Offset + 1;
|
---|
93 | end loop;
|
---|
94 | end Read;
|
---|
95 |
|
---|
96 | -----------
|
---|
97 | -- Reset --
|
---|
98 | -----------
|
---|
99 |
|
---|
100 | procedure Reset is
|
---|
101 | begin
|
---|
102 | Random_Elements.Reset (Gen, Init_Random);
|
---|
103 | Pack_Size := 0;
|
---|
104 | Offset := 1;
|
---|
105 | Read_First := Read_Buffer'Last + 1;
|
---|
106 | end Reset;
|
---|
107 |
|
---|
108 | begin
|
---|
109 | Ada.Text_IO.Put_Line ("ZLib " & ZLib.Version);
|
---|
110 |
|
---|
111 | loop
|
---|
112 | for Level in ZLib.Compression_Level'Range loop
|
---|
113 |
|
---|
114 | Ada.Text_IO.Put ("Level ="
|
---|
115 | & ZLib.Compression_Level'Image (Level));
|
---|
116 |
|
---|
117 | -- Deflate using generic instantiation.
|
---|
118 |
|
---|
119 | ZLib.Deflate_Init
|
---|
120 | (Filter,
|
---|
121 | Level,
|
---|
122 | Header => Header);
|
---|
123 |
|
---|
124 | Reset;
|
---|
125 |
|
---|
126 | Ada.Text_IO.Put
|
---|
127 | (Stream_Element_Offset'Image (File_Size) & " ->");
|
---|
128 |
|
---|
129 | loop
|
---|
130 | declare
|
---|
131 | Buffer : Stream_Element_Array (1 .. 1024);
|
---|
132 | Last : Stream_Element_Offset;
|
---|
133 | begin
|
---|
134 | Read (Filter, Buffer, Last);
|
---|
135 |
|
---|
136 | Pack_Size := Pack_Size + Last - Buffer'First + 1;
|
---|
137 |
|
---|
138 | exit when Last < Buffer'Last;
|
---|
139 | end;
|
---|
140 | end loop;
|
---|
141 |
|
---|
142 | Ada.Text_IO.Put_Line (Stream_Element_Offset'Image (Pack_Size));
|
---|
143 |
|
---|
144 | ZLib.Close (Filter);
|
---|
145 | end loop;
|
---|
146 |
|
---|
147 | exit when not Continuous;
|
---|
148 |
|
---|
149 | File_Size := File_Size + 1;
|
---|
150 | end loop;
|
---|
151 | end Read;
|
---|