mtest.adb 4.4 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. -- Continuous test for ZLib multithreading. If the test would fail
  9. -- we should provide thread safe allocation routines for the Z_Stream.
  10. --
  11. -- $Id: mtest.adb,v 1.4 2004/07/23 07:49:54 vagul Exp $
  12. with ZLib;
  13. with Ada.Streams;
  14. with Ada.Numerics.Discrete_Random;
  15. with Ada.Text_IO;
  16. with Ada.Exceptions;
  17. with Ada.Task_Identification;
  18. procedure MTest is
  19. use Ada.Streams;
  20. use ZLib;
  21. Stop : Boolean := False;
  22. pragma Atomic (Stop);
  23. subtype Visible_Symbols is Stream_Element range 16#20# .. 16#7E#;
  24. package Random_Elements is
  25. new Ada.Numerics.Discrete_Random (Visible_Symbols);
  26. task type Test_Task;
  27. task body Test_Task is
  28. Buffer : Stream_Element_Array (1 .. 100_000);
  29. Gen : Random_Elements.Generator;
  30. Buffer_First : Stream_Element_Offset;
  31. Compare_First : Stream_Element_Offset;
  32. Deflate : Filter_Type;
  33. Inflate : Filter_Type;
  34. procedure Further (Item : in Stream_Element_Array);
  35. procedure Read_Buffer
  36. (Item : out Ada.Streams.Stream_Element_Array;
  37. Last : out Ada.Streams.Stream_Element_Offset);
  38. -------------
  39. -- Further --
  40. -------------
  41. procedure Further (Item : in Stream_Element_Array) is
  42. procedure Compare (Item : in Stream_Element_Array);
  43. -------------
  44. -- Compare --
  45. -------------
  46. procedure Compare (Item : in Stream_Element_Array) is
  47. Next_First : Stream_Element_Offset := Compare_First + Item'Length;
  48. begin
  49. if Buffer (Compare_First .. Next_First - 1) /= Item then
  50. raise Program_Error;
  51. end if;
  52. Compare_First := Next_First;
  53. end Compare;
  54. procedure Compare_Write is new ZLib.Write (Write => Compare);
  55. begin
  56. Compare_Write (Inflate, Item, No_Flush);
  57. end Further;
  58. -----------------
  59. -- Read_Buffer --
  60. -----------------
  61. procedure Read_Buffer
  62. (Item : out Ada.Streams.Stream_Element_Array;
  63. Last : out Ada.Streams.Stream_Element_Offset)
  64. is
  65. Buff_Diff : Stream_Element_Offset := Buffer'Last - Buffer_First;
  66. Next_First : Stream_Element_Offset;
  67. begin
  68. if Item'Length <= Buff_Diff then
  69. Last := Item'Last;
  70. Next_First := Buffer_First + Item'Length;
  71. Item := Buffer (Buffer_First .. Next_First - 1);
  72. Buffer_First := Next_First;
  73. else
  74. Last := Item'First + Buff_Diff;
  75. Item (Item'First .. Last) := Buffer (Buffer_First .. Buffer'Last);
  76. Buffer_First := Buffer'Last + 1;
  77. end if;
  78. end Read_Buffer;
  79. procedure Translate is new Generic_Translate
  80. (Data_In => Read_Buffer,
  81. Data_Out => Further);
  82. begin
  83. Random_Elements.Reset (Gen);
  84. Buffer := (others => 20);
  85. Main : loop
  86. for J in Buffer'Range loop
  87. Buffer (J) := Random_Elements.Random (Gen);
  88. Deflate_Init (Deflate);
  89. Inflate_Init (Inflate);
  90. Buffer_First := Buffer'First;
  91. Compare_First := Buffer'First;
  92. Translate (Deflate);
  93. if Compare_First /= Buffer'Last + 1 then
  94. raise Program_Error;
  95. end if;
  96. Ada.Text_IO.Put_Line
  97. (Ada.Task_Identification.Image
  98. (Ada.Task_Identification.Current_Task)
  99. & Stream_Element_Offset'Image (J)
  100. & ZLib.Count'Image (Total_Out (Deflate)));
  101. Close (Deflate);
  102. Close (Inflate);
  103. exit Main when Stop;
  104. end loop;
  105. end loop Main;
  106. exception
  107. when E : others =>
  108. Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Information (E));
  109. Stop := True;
  110. end Test_Task;
  111. Test : array (1 .. 4) of Test_Task;
  112. pragma Unreferenced (Test);
  113. Dummy : Character;
  114. begin
  115. Ada.Text_IO.Get_Immediate (Dummy);
  116. Stop := True;
  117. end MTest;