test.adb 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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. -- $Id: test.adb,v 1.17 2003/08/12 12:13:30 vagul Exp $
  9. -- The program has a few aims.
  10. -- 1. Test ZLib.Ada95 thick binding functionality.
  11. -- 2. Show the example of use main functionality of the ZLib.Ada95 binding.
  12. -- 3. Build this program automatically compile all ZLib.Ada95 packages under
  13. -- GNAT Ada95 compiler.
  14. with ZLib.Streams;
  15. with Ada.Streams.Stream_IO;
  16. with Ada.Numerics.Discrete_Random;
  17. with Ada.Text_IO;
  18. with Ada.Calendar;
  19. procedure Test is
  20. use Ada.Streams;
  21. use Stream_IO;
  22. ------------------------------------
  23. -- Test configuration parameters --
  24. ------------------------------------
  25. File_Size : Count := 100_000;
  26. Continuous : constant Boolean := False;
  27. Header : constant ZLib.Header_Type := ZLib.Default;
  28. -- ZLib.None;
  29. -- ZLib.Auto;
  30. -- ZLib.GZip;
  31. -- Do not use Header other then Default in ZLib versions 1.1.4
  32. -- and older.
  33. Strategy : constant ZLib.Strategy_Type := ZLib.Default_Strategy;
  34. Init_Random : constant := 10;
  35. -- End --
  36. In_File_Name : constant String := "testzlib.in";
  37. -- Name of the input file
  38. Z_File_Name : constant String := "testzlib.zlb";
  39. -- Name of the compressed file.
  40. Out_File_Name : constant String := "testzlib.out";
  41. -- Name of the decompressed file.
  42. File_In : File_Type;
  43. File_Out : File_Type;
  44. File_Back : File_Type;
  45. File_Z : ZLib.Streams.Stream_Type;
  46. Filter : ZLib.Filter_Type;
  47. Time_Stamp : Ada.Calendar.Time;
  48. procedure Generate_File;
  49. -- Generate file of spetsified size with some random data.
  50. -- The random data is repeatable, for the good compression.
  51. procedure Compare_Streams
  52. (Left, Right : in out Root_Stream_Type'Class);
  53. -- The procedure compearing data in 2 streams.
  54. -- It is for compare data before and after compression/decompression.
  55. procedure Compare_Files (Left, Right : String);
  56. -- Compare files. Based on the Compare_Streams.
  57. procedure Copy_Streams
  58. (Source, Target : in out Root_Stream_Type'Class;
  59. Buffer_Size : in Stream_Element_Offset := 1024);
  60. -- Copying data from one stream to another. It is for test stream
  61. -- interface of the library.
  62. procedure Data_In
  63. (Item : out Stream_Element_Array;
  64. Last : out Stream_Element_Offset);
  65. -- this procedure is for generic instantiation of
  66. -- ZLib.Generic_Translate.
  67. -- reading data from the File_In.
  68. procedure Data_Out (Item : in Stream_Element_Array);
  69. -- this procedure is for generic instantiation of
  70. -- ZLib.Generic_Translate.
  71. -- writing data to the File_Out.
  72. procedure Stamp;
  73. -- Store the timestamp to the local variable.
  74. procedure Print_Statistic (Msg : String; Data_Size : ZLib.Count);
  75. -- Print the time statistic with the message.
  76. procedure Translate is new ZLib.Generic_Translate
  77. (Data_In => Data_In,
  78. Data_Out => Data_Out);
  79. -- This procedure is moving data from File_In to File_Out
  80. -- with compression or decompression, depend on initialization of
  81. -- Filter parameter.
  82. -------------------
  83. -- Compare_Files --
  84. -------------------
  85. procedure Compare_Files (Left, Right : String) is
  86. Left_File, Right_File : File_Type;
  87. begin
  88. Open (Left_File, In_File, Left);
  89. Open (Right_File, In_File, Right);
  90. Compare_Streams (Stream (Left_File).all, Stream (Right_File).all);
  91. Close (Left_File);
  92. Close (Right_File);
  93. end Compare_Files;
  94. ---------------------
  95. -- Compare_Streams --
  96. ---------------------
  97. procedure Compare_Streams
  98. (Left, Right : in out Ada.Streams.Root_Stream_Type'Class)
  99. is
  100. Left_Buffer, Right_Buffer : Stream_Element_Array (0 .. 16#FFF#);
  101. Left_Last, Right_Last : Stream_Element_Offset;
  102. begin
  103. loop
  104. Read (Left, Left_Buffer, Left_Last);
  105. Read (Right, Right_Buffer, Right_Last);
  106. if Left_Last /= Right_Last then
  107. Ada.Text_IO.Put_Line ("Compare error :"
  108. & Stream_Element_Offset'Image (Left_Last)
  109. & " /= "
  110. & Stream_Element_Offset'Image (Right_Last));
  111. raise Constraint_Error;
  112. elsif Left_Buffer (0 .. Left_Last)
  113. /= Right_Buffer (0 .. Right_Last)
  114. then
  115. Ada.Text_IO.Put_Line ("ERROR: IN and OUT files is not equal.");
  116. raise Constraint_Error;
  117. end if;
  118. exit when Left_Last < Left_Buffer'Last;
  119. end loop;
  120. end Compare_Streams;
  121. ------------------
  122. -- Copy_Streams --
  123. ------------------
  124. procedure Copy_Streams
  125. (Source, Target : in out Ada.Streams.Root_Stream_Type'Class;
  126. Buffer_Size : in Stream_Element_Offset := 1024)
  127. is
  128. Buffer : Stream_Element_Array (1 .. Buffer_Size);
  129. Last : Stream_Element_Offset;
  130. begin
  131. loop
  132. Read (Source, Buffer, Last);
  133. Write (Target, Buffer (1 .. Last));
  134. exit when Last < Buffer'Last;
  135. end loop;
  136. end Copy_Streams;
  137. -------------
  138. -- Data_In --
  139. -------------
  140. procedure Data_In
  141. (Item : out Stream_Element_Array;
  142. Last : out Stream_Element_Offset) is
  143. begin
  144. Read (File_In, Item, Last);
  145. end Data_In;
  146. --------------
  147. -- Data_Out --
  148. --------------
  149. procedure Data_Out (Item : in Stream_Element_Array) is
  150. begin
  151. Write (File_Out, Item);
  152. end Data_Out;
  153. -------------------
  154. -- Generate_File --
  155. -------------------
  156. procedure Generate_File is
  157. subtype Visible_Symbols is Stream_Element range 16#20# .. 16#7E#;
  158. package Random_Elements is
  159. new Ada.Numerics.Discrete_Random (Visible_Symbols);
  160. Gen : Random_Elements.Generator;
  161. Buffer : Stream_Element_Array := (1 .. 77 => 16#20#) & 10;
  162. Buffer_Count : constant Count := File_Size / Buffer'Length;
  163. -- Number of same buffers in the packet.
  164. Density : constant Count := 30; -- from 0 to Buffer'Length - 2;
  165. procedure Fill_Buffer (J, D : in Count);
  166. -- Change the part of the buffer.
  167. -----------------
  168. -- Fill_Buffer --
  169. -----------------
  170. procedure Fill_Buffer (J, D : in Count) is
  171. begin
  172. for K in 0 .. D loop
  173. Buffer
  174. (Stream_Element_Offset ((J + K) mod (Buffer'Length - 1) + 1))
  175. := Random_Elements.Random (Gen);
  176. end loop;
  177. end Fill_Buffer;
  178. begin
  179. Random_Elements.Reset (Gen, Init_Random);
  180. Create (File_In, Out_File, In_File_Name);
  181. Fill_Buffer (1, Buffer'Length - 2);
  182. for J in 1 .. Buffer_Count loop
  183. Write (File_In, Buffer);
  184. Fill_Buffer (J, Density);
  185. end loop;
  186. -- fill remain size.
  187. Write
  188. (File_In,
  189. Buffer
  190. (1 .. Stream_Element_Offset
  191. (File_Size - Buffer'Length * Buffer_Count)));
  192. Flush (File_In);
  193. Close (File_In);
  194. end Generate_File;
  195. ---------------------
  196. -- Print_Statistic --
  197. ---------------------
  198. procedure Print_Statistic (Msg : String; Data_Size : ZLib.Count) is
  199. use Ada.Calendar;
  200. use Ada.Text_IO;
  201. package Count_IO is new Integer_IO (ZLib.Count);
  202. Curr_Dur : Duration := Clock - Time_Stamp;
  203. begin
  204. Put (Msg);
  205. Set_Col (20);
  206. Ada.Text_IO.Put ("size =");
  207. Count_IO.Put
  208. (Data_Size,
  209. Width => Stream_IO.Count'Image (File_Size)'Length);
  210. Put_Line (" duration =" & Duration'Image (Curr_Dur));
  211. end Print_Statistic;
  212. -----------
  213. -- Stamp --
  214. -----------
  215. procedure Stamp is
  216. begin
  217. Time_Stamp := Ada.Calendar.Clock;
  218. end Stamp;
  219. begin
  220. Ada.Text_IO.Put_Line ("ZLib " & ZLib.Version);
  221. loop
  222. Generate_File;
  223. for Level in ZLib.Compression_Level'Range loop
  224. Ada.Text_IO.Put_Line ("Level ="
  225. & ZLib.Compression_Level'Image (Level));
  226. -- Test generic interface.
  227. Open (File_In, In_File, In_File_Name);
  228. Create (File_Out, Out_File, Z_File_Name);
  229. Stamp;
  230. -- Deflate using generic instantiation.
  231. ZLib.Deflate_Init
  232. (Filter => Filter,
  233. Level => Level,
  234. Strategy => Strategy,
  235. Header => Header);
  236. Translate (Filter);
  237. Print_Statistic ("Generic compress", ZLib.Total_Out (Filter));
  238. ZLib.Close (Filter);
  239. Close (File_In);
  240. Close (File_Out);
  241. Open (File_In, In_File, Z_File_Name);
  242. Create (File_Out, Out_File, Out_File_Name);
  243. Stamp;
  244. -- Inflate using generic instantiation.
  245. ZLib.Inflate_Init (Filter, Header => Header);
  246. Translate (Filter);
  247. Print_Statistic ("Generic decompress", ZLib.Total_Out (Filter));
  248. ZLib.Close (Filter);
  249. Close (File_In);
  250. Close (File_Out);
  251. Compare_Files (In_File_Name, Out_File_Name);
  252. -- Test stream interface.
  253. -- Compress to the back stream.
  254. Open (File_In, In_File, In_File_Name);
  255. Create (File_Back, Out_File, Z_File_Name);
  256. Stamp;
  257. ZLib.Streams.Create
  258. (Stream => File_Z,
  259. Mode => ZLib.Streams.Out_Stream,
  260. Back => ZLib.Streams.Stream_Access
  261. (Stream (File_Back)),
  262. Back_Compressed => True,
  263. Level => Level,
  264. Strategy => Strategy,
  265. Header => Header);
  266. Copy_Streams
  267. (Source => Stream (File_In).all,
  268. Target => File_Z);
  269. -- Flushing internal buffers to the back stream.
  270. ZLib.Streams.Flush (File_Z, ZLib.Finish);
  271. Print_Statistic ("Write compress",
  272. ZLib.Streams.Write_Total_Out (File_Z));
  273. ZLib.Streams.Close (File_Z);
  274. Close (File_In);
  275. Close (File_Back);
  276. -- Compare reading from original file and from
  277. -- decompression stream.
  278. Open (File_In, In_File, In_File_Name);
  279. Open (File_Back, In_File, Z_File_Name);
  280. ZLib.Streams.Create
  281. (Stream => File_Z,
  282. Mode => ZLib.Streams.In_Stream,
  283. Back => ZLib.Streams.Stream_Access
  284. (Stream (File_Back)),
  285. Back_Compressed => True,
  286. Header => Header);
  287. Stamp;
  288. Compare_Streams (Stream (File_In).all, File_Z);
  289. Print_Statistic ("Read decompress",
  290. ZLib.Streams.Read_Total_Out (File_Z));
  291. ZLib.Streams.Close (File_Z);
  292. Close (File_In);
  293. Close (File_Back);
  294. -- Compress by reading from compression stream.
  295. Open (File_Back, In_File, In_File_Name);
  296. Create (File_Out, Out_File, Z_File_Name);
  297. ZLib.Streams.Create
  298. (Stream => File_Z,
  299. Mode => ZLib.Streams.In_Stream,
  300. Back => ZLib.Streams.Stream_Access
  301. (Stream (File_Back)),
  302. Back_Compressed => False,
  303. Level => Level,
  304. Strategy => Strategy,
  305. Header => Header);
  306. Stamp;
  307. Copy_Streams
  308. (Source => File_Z,
  309. Target => Stream (File_Out).all);
  310. Print_Statistic ("Read compress",
  311. ZLib.Streams.Read_Total_Out (File_Z));
  312. ZLib.Streams.Close (File_Z);
  313. Close (File_Out);
  314. Close (File_Back);
  315. -- Decompress to decompression stream.
  316. Open (File_In, In_File, Z_File_Name);
  317. Create (File_Back, Out_File, Out_File_Name);
  318. ZLib.Streams.Create
  319. (Stream => File_Z,
  320. Mode => ZLib.Streams.Out_Stream,
  321. Back => ZLib.Streams.Stream_Access
  322. (Stream (File_Back)),
  323. Back_Compressed => False,
  324. Header => Header);
  325. Stamp;
  326. Copy_Streams
  327. (Source => Stream (File_In).all,
  328. Target => File_Z);
  329. Print_Statistic ("Write decompress",
  330. ZLib.Streams.Write_Total_Out (File_Z));
  331. ZLib.Streams.Close (File_Z);
  332. Close (File_In);
  333. Close (File_Back);
  334. Compare_Files (In_File_Name, Out_File_Name);
  335. end loop;
  336. Ada.Text_IO.Put_Line (Count'Image (File_Size) & " Ok.");
  337. exit when not Continuous;
  338. File_Size := File_Size + 1;
  339. end loop;
  340. end Test;