read.adb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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: read.adb,v 1.8 2004/05/31 10:53:40 vagul Exp $
  9. -- Test/demo program for the generic read interface.
  10. with Ada.Numerics.Discrete_Random;
  11. with Ada.Streams;
  12. with Ada.Text_IO;
  13. with ZLib;
  14. procedure Read is
  15. use Ada.Streams;
  16. ------------------------------------
  17. -- Test configuration parameters --
  18. ------------------------------------
  19. File_Size : Stream_Element_Offset := 100_000;
  20. Continuous : constant Boolean := False;
  21. -- If this constant is True, the test would be repeated again and again,
  22. -- with increment File_Size for every iteration.
  23. Header : constant ZLib.Header_Type := ZLib.Default;
  24. -- Do not use Header other than Default in ZLib versions 1.1.4 and older.
  25. Init_Random : constant := 8;
  26. -- We are using the same random sequence, in case of we catch bug,
  27. -- so we would be able to reproduce it.
  28. -- End --
  29. Pack_Size : Stream_Element_Offset;
  30. Offset : Stream_Element_Offset;
  31. Filter : ZLib.Filter_Type;
  32. subtype Visible_Symbols
  33. is Stream_Element range 16#20# .. 16#7E#;
  34. package Random_Elements is new
  35. Ada.Numerics.Discrete_Random (Visible_Symbols);
  36. Gen : Random_Elements.Generator;
  37. Period : constant Stream_Element_Offset := 200;
  38. -- Period constant variable for random generator not to be very random.
  39. -- Bigger period, harder random.
  40. Read_Buffer : Stream_Element_Array (1 .. 2048);
  41. Read_First : Stream_Element_Offset;
  42. Read_Last : Stream_Element_Offset;
  43. procedure Reset;
  44. procedure Read
  45. (Item : out Stream_Element_Array;
  46. Last : out Stream_Element_Offset);
  47. -- this procedure is for generic instantiation of
  48. -- ZLib.Read
  49. -- reading data from the File_In.
  50. procedure Read is new ZLib.Read
  51. (Read,
  52. Read_Buffer,
  53. Rest_First => Read_First,
  54. Rest_Last => Read_Last);
  55. ----------
  56. -- Read --
  57. ----------
  58. procedure Read
  59. (Item : out Stream_Element_Array;
  60. Last : out Stream_Element_Offset) is
  61. begin
  62. Last := Stream_Element_Offset'Min
  63. (Item'Last,
  64. Item'First + File_Size - Offset);
  65. for J in Item'First .. Last loop
  66. if J < Item'First + Period then
  67. Item (J) := Random_Elements.Random (Gen);
  68. else
  69. Item (J) := Item (J - Period);
  70. end if;
  71. Offset := Offset + 1;
  72. end loop;
  73. end Read;
  74. -----------
  75. -- Reset --
  76. -----------
  77. procedure Reset is
  78. begin
  79. Random_Elements.Reset (Gen, Init_Random);
  80. Pack_Size := 0;
  81. Offset := 1;
  82. Read_First := Read_Buffer'Last + 1;
  83. Read_Last := Read_Buffer'Last;
  84. end Reset;
  85. begin
  86. Ada.Text_IO.Put_Line ("ZLib " & ZLib.Version);
  87. loop
  88. for Level in ZLib.Compression_Level'Range loop
  89. Ada.Text_IO.Put ("Level ="
  90. & ZLib.Compression_Level'Image (Level));
  91. -- Deflate using generic instantiation.
  92. ZLib.Deflate_Init
  93. (Filter,
  94. Level,
  95. Header => Header);
  96. Reset;
  97. Ada.Text_IO.Put
  98. (Stream_Element_Offset'Image (File_Size) & " ->");
  99. loop
  100. declare
  101. Buffer : Stream_Element_Array (1 .. 1024);
  102. Last : Stream_Element_Offset;
  103. begin
  104. Read (Filter, Buffer, Last);
  105. Pack_Size := Pack_Size + Last - Buffer'First + 1;
  106. exit when Last < Buffer'Last;
  107. end;
  108. end loop;
  109. Ada.Text_IO.Put_Line (Stream_Element_Offset'Image (Pack_Size));
  110. ZLib.Close (Filter);
  111. end loop;
  112. exit when not Continuous;
  113. File_Size := File_Size + 1;
  114. end loop;
  115. end Read;