aicwl_3.24.1_73939c9e/sources/test_strings_edit/test_string_streams.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
--                                                                    --
--  procedure Test_String_Streams   Copyright (c)  Dmitry A. Kazakov  --
--  Test                                           Luebeck            --
--                                                 Spring, 2000       --
--                                                                    --
--  This  library  is  free software; you can redistribute it and/or  --
--  modify it under the terms of the GNU General Public  License  as  --
--  published by the Free Software Foundation; either version  2  of  --
--  the License, or (at your option) any later version. This library  --
--  is distributed in the hope that it will be useful,  but  WITHOUT  --
--  ANY   WARRANTY;   without   even   the   implied   warranty   of  --
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  --
--  General  Public  License  for  more  details.  You  should  have  --
--  received  a  copy  of  the GNU General Public License along with  --
--  this library; if not, write to  the  Free  Software  Foundation,  --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    --
--                                                                    --
--  As a special exception, if other files instantiate generics from  --
--  this unit, or you link this unit with other files to produce  an  --
--  executable, this unit does not by  itself  cause  the  resulting  --
--  executable to be covered by the GNU General Public License. This  --
--  exception  does not however invalidate any other reasons why the  --
--  executable file might be covered by the GNU Public License.       --
--____________________________________________________________________--

with Ada.Exceptions;        use Ada.Exceptions;
with Ada.Streams;           use type Ada.Streams.Stream_Element_Offset;
with Ada.Text_IO;           use Ada.Text_IO;
with Strings_Edit.Quoted;   use Strings_Edit.Quoted;
with Strings_Edit.Streams;  use Strings_Edit.Streams;

with Ada.Characters.Latin_1;
with Interfaces;
with Strings_Edit.Integers;
with Strings_Edit.Streams.Integers;
with Strings_Edit.Streams.Naturals;
with Strings_Edit.Streams.Unsigneds_32;
with Strings_Edit.UTF8.Handling;
with Strings_Edit.UTF8.Recoding_Streams;

procedure Test_String_Streams is
begin
------------------------------------------------------------------------
   declare
      use Ada.Streams;
      use Interfaces;
      use Strings_Edit.Streams.Unsigneds_32;
      Value   : Unsigned_32;
      Buffer  : Stream_Element_Array (1..80);
      Pointer : Stream_Element_Offset;
      Index   : Stream_Element_Offset;
   begin
      for Number in Unsigned_32 range 0..100_000 loop
         Pointer := 1;
         Put (Buffer, Pointer, Number);
         Index := 1;
         Get (Buffer, Index, Value);
         if Index /= Pointer then
            Raise_Exception
            (  Constraint_Error'Identity,
               (  "Input buffer length"
               &  Stream_Element_Offset'Image (Index)
               &  " /="
               &  Stream_Element_Offset'Image (Pointer)
               &  " output"
            )  );
         elsif Value /= Number then
            Raise_Exception
            (  Constraint_Error'Identity,
               (  "Input"
               &  Unsigned_32'Image (Value)
               &  " /="
               &  Unsigned_32'Image (Number)
               &  " output"
            )  );
         end if;
      end loop;
   end;
   declare
      use Ada.Streams;
      use Interfaces;
      use Strings_Edit.Streams.Unsigneds_32;
      S : aliased String_Stream (30);
      Value : Unsigned_32;
   begin
      for Number in Unsigned_32 range 0..100_000 loop
         Rewind (S);
         Output (S'Access, Number);
         Set (S, Get (S));
         Value := Input (S'Access);
         if Value /= Number then
            Raise_Exception
            (  Constraint_Error'Identity,
               (  "Stream input"
               &  Unsigned_32'Image (Value)
               &  " /="
               &  Unsigned_32'Image (Number)
               &  " output"
            )  );
         end if;
      end loop;
   end;
------------------------------------------------------------------------
   declare
      use Ada.Streams;
      use Interfaces;
      use Strings_Edit.Streams.Integers;
      Value   : Integer;
      Buffer  : Stream_Element_Array (1..80);
      Pointer : Stream_Element_Offset;
      Index   : Stream_Element_Offset;
   begin
      for Number in Integer range -50_000..50_000 loop
         Pointer := 1;
         Put (Buffer, Pointer, Number);
         Index := 1;
         Get (Buffer, Index, Value);
         if Index /= Pointer then
            Raise_Exception
            (  Constraint_Error'Identity,
               (  "Input buffer length"
               &  Stream_Element_Offset'Image (Index)
               &  " /="
               &  Stream_Element_Offset'Image (Pointer)
               &  " output"
            )  );
         elsif Value /= Number then
            Raise_Exception
            (  Constraint_Error'Identity,
               (  "Input"
               &  Integer'Image (Value)
               &  " /="
               &  Integer'Image (Number)
               &  " output"
            )  );
         end if;
      end loop;
   end;
   declare
      use Ada.Streams;
      use Strings_Edit.Streams.Integers;
      S : aliased String_Stream (30);
      Value : Integer;
   begin
      for Number in Integer range -50_000..50_000 loop
         Rewind (S);
         Output (S'Access, Number);
         Set (S, Get (S));
         Value := Input (S'Access);
         if Value /= Number then
            Raise_Exception
            (  Constraint_Error'Identity,
               (  "Stream input"
               &  Integer'Image (Value)
               &  " /="
               &  Integer'Image (Number)
               &  " output"
            )  );
         end if;
      end loop;
   end;
------------------------------------------------------------------------
   declare
      use Ada.Streams;
      use Strings_Edit.Streams.Naturals;
      Value   : Natural;
      Buffer  : Stream_Element_Array (1..80);
      Pointer : Stream_Element_Offset;
      Index   : Stream_Element_Offset;
   begin
      for Number in Natural range 0..100_000 loop
         Pointer := 1;
         Put (Buffer, Pointer, Number);
         Index := 1;
         Get (Buffer, Index, Value);
         if Index /= Pointer then
            Raise_Exception
            (  Constraint_Error'Identity,
               (  "Input buffer length"
               &  Stream_Element_Offset'Image (Index)
               &  " /="
               &  Stream_Element_Offset'Image (Pointer)
               &  " output"
            )  );
         elsif Value /= Number then
            Raise_Exception
            (  Constraint_Error'Identity,
               (  "Input"
               &  Integer'Image (Value)
               &  " /="
               &  Integer'Image (Number)
               &  " output"
            )  );
         end if;
      end loop;
   end;
   declare
      use Ada.Streams;
      use Strings_Edit.Streams.Naturals;
      S : aliased String_Stream (30);
      Value : Natural;
   begin
      for Number in Natural range 0..100_000 loop
         Rewind (S);
         Output (S'Access, Number);
         Set (S, Get (S));
         Value := Input (S'Access);
         if Value /= Number then
            Raise_Exception
            (  Constraint_Error'Identity,
               (  "Stream input"
               &  Integer'Image (Value)
               &  " /="
               &  Integer'Image (Number)
               &  " output"
            )  );
         end if;
      end loop;
   end;
------------------------------------------------------------------------
   declare
      S : aliased String_Stream (6);
      C : Character;
   begin
      Character'Write (S'Access, 'a');
      Rewind (S);
      Character'Read (S'Access, C);
      if C /= 'a' then
         Raise_Exception (Constraint_Error'Identity, "Read error");
      end if;
   end;
   declare
      S : aliased String_Stream (6);
      C : Character;
      T : String (1..5);
   begin
      Set (S, "abcdef");
      Character'Read (S'Access, C);
      if C /= 'a' or else Get_Size (S) /= 5 then
         Raise_Exception (Constraint_Error'Identity, "Read error");
      end if;
      String'Read (S'Access, T);
      if T /= "bcdef" or else Get_Size (S) /= 0 then
         Raise_Exception (Constraint_Error'Identity, "Read error");
      end if;
      Rewind (S); -- Rewind
      Character'Write (S'Access, '1');
      String'Write (S'Access, "23456");
      if S.Data /= "123456" or else Get (S) /= "123456" then
         Raise_Exception (Constraint_Error'Identity, "Write error");
      end if;
   end;
------------------------------------------------------------------------
   declare
      use Ada.Characters.Latin_1;
      use Strings_Edit.UTF8.Handling;
      use Strings_Edit.UTF8.Recoding_Streams;
      Encoded : aliased String_Stream (1024);
      Decoded : aliased Recoding_Stream
                        (  Encoded'Access,
                           Windows_1252,
                           Character'Pos ('?'),
                           '?'
                        );
      Text_1 : constant String := "L" & LC_U_Diaeresis & "beck";
      Text_2 : String (1..7);
   begin
      String'Write (Decoded'Access, To_UTF8 (Text_1));
      if Get (Encoded) /= Text_1 then
         Raise_Exception
         (  Constraint_Error'Identity,
            (  "Recoding write error: "
            &  Quote (Get (Encoded))
            &  ", written "
            &  Quote (Text_1)
         )  );
      end if;
      Rewind (Encoded);
      String'Read (Decoded'Access, Text_2);
      if To_UTF8 (Text_1) /= Text_2 then
         Raise_Exception
         (  Constraint_Error'Identity,
            (  "Recoding read error: "
            &  Quote (Text_1)
            &  ", read "
            &  Quote (Text_2)
         )  );
      end if;
   end;
   Put_Line ("... Done");
exception
   when Error : others =>
      Put ("Error: ");
      Put_Line (Exception_Information (Error));
end Test_String_Streams;