summaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats/tests/cxa/cxac002.a
blob: e4b303c4bc9d4970b55ecff4004b74f0fab7e061 (plain)
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
-- CXAC002.A
--
--                             Grant of Unlimited Rights
--
--     Under contracts F33600-87-D-0337, F33600-84-D-0280, MDA903-79-C-0687,
--     F08630-91-C-0015, and DCA100-97-D-0025, the U.S. Government obtained
--     unlimited rights in the software and documentation contained herein.
--     Unlimited rights are defined in DFAR 252.227-7013(a)(19).  By making
--     this public release, the Government intends to confer upon all
--     recipients unlimited rights  equal to those held by the Government.
--     These rights include rights to use, duplicate, release or disclose the
--     released technical data and computer software in whole or in part, in
--     any manner and for any purpose whatsoever, and to have or permit others
--     to do so.
--
--                                    DISCLAIMER
--
--     ALL MATERIALS OR INFORMATION HEREIN RELEASED, MADE AVAILABLE OR
--     DISCLOSED ARE AS IS.  THE GOVERNMENT MAKES NO EXPRESS OR IMPLIED
--     WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING THE CONDITIONS OF THE
--     SOFTWARE, DOCUMENTATION OR OTHER INFORMATION RELEASED, MADE AVAILABLE
--     OR DISCLOSED, OR THE OWNERSHIP, MERCHANTABILITY, OR FITNESS FOR A
--     PARTICULAR PURPOSE OF SAID MATERIAL.
--*
--
-- OBJECTIVE:
--      Check that the subprograms defined in package Ada.Streams.Stream_IO
--      are accessible, and that they provide the appropriate functionality.
--
-- TEST DESCRIPTION:
--      This test simulates a user filter designed to capitalize the
--      characters of a string.  It utilizes a variety of the subprograms
--      contained in the package Ada.Streams.Stream_IO.
--      Its purpose is to demonstrate the use of a variety of the capabilities
--      found in the Ada.Streams.Stream_IO package.
--
-- APPLICABILITY CRITERIA:
--      This test is applicable to all implementations capable of supporting
--      external Stream_IO files.
--
--
-- CHANGE HISTORY:
--      06 Dec 94   SAIC    ACVC 2.0
--      14 Nov 95   SAIC    Corrected visibility problems; corrected
--                          subtest validating result from function Name
--                          for ACVC 2.0.1.
--      05 Oct 96   SAIC    Removed calls to Close/Open in test and replaced
--                          them with a single call to Reset (per AI95-0001)
--      26 Feb 97   PWB.CTA Allowed for non-support of some IO operations.
--      09 Feb 01   RLB     Corrected non-support check to avoid unintended
--                          failures.
--!

package CXAC002_0 is

   -- This function searches for the first instance of a specified substring
   -- within a specified string, returning boolean result. (Case insensitive
   -- analysis)

   function Find (Str : in String; Sub : in String) return Boolean;

end CXAC002_0;

package body CXAC002_0 is

   function Find (Str : in String; Sub : in String) return Boolean is

      New_Str : String(Str'First..Str'Last);
      New_Sub : String(Sub'First..Sub'Last);
      Pos     : Integer := Str'First;             -- Character index.

      function Upper_Case (Str : in String) return String is
         subtype Upper is Character range 'A'..'Z';
         subtype Lower is Character range 'a'..'z';
         Ret : String(Str'First..Str'Last);
         Pos : Integer;
      begin
         for I in Str'Range loop
            if (Str(I) in Lower) then
               Pos := Upper'Pos(Upper'First) +
                      (Lower'Pos(Str(I)) - Lower'Pos(Lower'First));
               Ret(I) := Upper'Val(Pos);
            else
               Ret(I) := Str (I);
            end if;
         end loop;
         return Ret;
      end Upper_Case;

   begin

      New_Str := Upper_Case(Str);             -- Convert Str and Sub to upper
      New_Sub := Upper_Case(Sub);             -- case for comparison.

      while (Pos <= New_Str'Last-New_Sub'Length+1)     -- Search until no more
        and then                                       -- sub-string-length
        (New_Str(Pos..Pos+New_Sub'Length-1) /= New_Sub) -- slices remain.
      loop
         Pos := Pos + 1;
      end loop;

      if (Pos > New_Str'Last-New_Sub'Length+1) then  -- Substring not found.
         return False;
      else
         return True;
      end if;

   end Find;

end CXAC002_0;


with Ada.Streams.Stream_IO, CXAC002_0, Report;
procedure CXAC002 is
   Filter_File     : Ada.Streams.Stream_IO.File_Type;
   Filter_Stream   : Ada.Streams.Stream_IO.Stream_Access;
   Filter_Filename : constant String :=
                              Report.Legal_File_Name ( Nam => "CXAC002" );
   Incomplete : Exception;

begin

   Report.Test ("CXAC002", "Check that the subprograms defined in "         &
                           "package Ada.Streams.Stream_IO are accessible, " &
                           "and that they provide the appropriate "         &
                           "functionality");

   Test_for_Stream_IO_Support:

   begin

      -- If an implementation does not support Stream_IO in a particular
      -- environment, the exception Use_Error or Name_Error will be raised on
      -- calls to various Stream_IO operations.  This block statement
      -- encloses a call to Create, which should produce an exception in a
      -- non-supportive environment.  These exceptions will be handled to
      -- produce a Not_Applicable result.

      Ada.Streams.Stream_IO.Create (Filter_File,                    -- Create.
                                    Ada.Streams.Stream_IO.Out_File,
                                    Filter_Filename);
   exception

       when Ada.Streams.Stream_IO.Use_Error | Ada.Streams.Stream_IO.Name_Error =>
          Report.Not_Applicable
             ( "Files not supported - Create as Out_File for Stream_IO" );
          raise Incomplete;

   end Test_for_Stream_IO_Support;

   Operational_Test_Block:
   declare

      use CXAC002_0;
      use type Ada.Streams.Stream_IO.File_Mode;
      use type Ada.Streams.Stream_IO.Count;

      File_Size  : Ada.Streams.Stream_IO.Count :=            -- Count.
        Ada.Streams.Stream_IO.Count'First;                   -- (0)
      File_Index : Ada.Streams.Stream_IO.Positive_Count :=   -- Pos. Count.
        Ada.Streams.Stream_IO.Positive_Count'First;          -- (1)

      First_String  : constant String := "this is going to be ";
      Second_String : constant String := "the best year of your life";
      Total_Length  : constant Natural := First_String'Length +
                                          Second_String'Length;
      Current_Char  : Character := ' ';

      Cap_String    : String (1..Total_Length) := (others => ' ');

      TC_Capital_String : constant String :=
                "THIS IS GOING TO BE THE BEST YEAR OF YOUR LIFE";

   begin

      if not Ada.Streams.Stream_IO.Is_Open (Filter_File) then   -- Is_Open
         Report.Failed ("File not open following Create");
      end if;

      -- Call function Find to determine if the filename (Sub) is contained
      -- in the result of Function Name.

      if not Find(Str => Ada.Streams.Stream_IO.Name(Filter_File), -- Name.
                  Sub => Filter_Filename)
      then
         Report.Failed ("Function Name provided incorrect filename");
      end if;
                                                                 -- Stream.
      Filter_Stream := Ada.Streams.Stream_IO.Stream (Filter_File);

      ---

      Enter_Data_In_Stream:
      declare
         Pos                 : Natural := 1;
         Bad_Character_Found : Boolean := False;
      begin

         -- Enter data from the first string into the stream.
         while Pos <= Natural(First_String'Length) loop
            -- Write all characters of the First_String to the stream.
            Character'Write (Filter_Stream, First_String (Pos));
            Pos := Pos + 1;
            -- Ensure data put in file on a regular basis.
            if Pos mod 5 = 0 then
               Ada.Streams.Stream_IO.Flush (Filter_File);         -- Flush.
            end if;
         end loop;

         Ada.Streams.Stream_IO.Flush (Filter_File);               -- Flush.
         -- Reset to In_File mode and read stream contents.
         Reset1:
         begin
            Ada.Streams.Stream_IO.Reset (Filter_File,                -- Reset.
                                         Ada.Streams.Stream_IO.In_File);
         exception
            when Ada.Streams.Stream_IO.Use_Error =>
               Report.Not_Applicable
                  ( "Reset to In_File not supported for Stream_IO" );
               raise Incomplete;
         end Reset1;

         Pos := 1;
         while Pos <= First_String'Length loop
            -- Read one character from the stream.
            Character'Read (Filter_Stream, Current_Char);         -- 'Read
            -- Verify character against the original string.
            if Current_Char /= First_String(Pos) then
               Bad_Character_Found := True;
            end if;
            Pos := Pos + 1;
         end loop;

         if Bad_Character_Found then
            Report.Failed ("Incorrect character read from stream");
         end if;

         -- Following user stream/string processing, the stream file is
         -- appended to as follows:

         Reset2:
         begin
            Ada.Streams.Stream_IO.Reset (Filter_File,                -- Reset.
                                         Ada.Streams.Stream_IO.Append_File);
         exception
            when Ada.Streams.Stream_IO.Use_Error =>
               Report.Not_Applicable
                  ( "Reset to Append_File not supported for Stream_IO" );
               raise Incomplete;
         end Reset2;

         if Ada.Streams.Stream_IO.Mode (Filter_File) /=           -- Mode.
            Ada.Streams.Stream_IO.Append_File
         then
            Report.Failed ("Incorrect mode following Reset to Append");
         end if;

         Pos := 1;
         while Pos <= Natural(Second_String'Length) loop
            -- Write all characters of the Second_String to the stream.
            Character'Write (Filter_Stream, Second_String (Pos)); -- 'Write
            Pos := Pos + 1;
         end loop;

         Ada.Streams.Stream_IO.Flush (Filter_File);               -- Flush.

         -- Record file statistics.
         File_Size  := Ada.Streams.Stream_IO.Size  (Filter_File); -- Size.

         Index_Might_Not_Be_Supported:
         begin
            File_Index := Ada.Streams.Stream_IO.Index (Filter_File); -- Index.
         exception
            when Ada.Streams.Stream_IO.Use_Error =>
               Report.Not_Applicable ( "Index not supported for Stream_IO" );
               raise Incomplete;
         end Index_Might_Not_Be_Supported;

      exception
         when Incomplete =>
            raise;
         when others =>
            Report.Failed ("Exception in Enter_Data_In_Stream block");
            raise;
      end Enter_Data_In_Stream;

      ---

      Filter_Block:
      declare
         Pos          : Positive := 1;
         Full_String  : constant String := First_String & Second_String;

         function Capitalize (Char : Character) return Character is
         begin
            if Char /= ' ' then
               return Character'Val( Character'Pos(Char) -
                      (Character'Pos('a') - Character'Pos('A')));
            else
               return Char;
            end if;
         end Capitalize;

      begin

         Reset3:
         begin
           Ada.Streams.Stream_IO.Reset (Filter_File,                -- Reset.
                                         Ada.Streams.Stream_IO.In_File);
         exception
            when Ada.Streams.Stream_IO.Use_Error =>
               Report.Not_Applicable
                  ( "Reset to In_File not supported for Stream_IO" );
               raise Incomplete;
         end Reset3;

         if Ada.Streams.Stream_IO.Mode (Filter_File) /=           -- Mode.
            Ada.Streams.Stream_IO.In_File
         then
            Report.Failed ("Incorrect mode following Reset to In_File");
         end if;

         if not Ada.Streams.Stream_IO.Is_Open (Filter_File) then -- Is_Open
            Report.Failed ( "Reset command did not leave file open" );
         end if;

         if Ada.Streams.Stream_IO.Size (Filter_File) /=            -- Size.
            File_Size
         then
            Report.Failed ("Reset file is not correct size");
         end if;

         if Ada.Streams.Stream_IO.Index (Filter_File) /= 1 then    -- Index.
            -- File position should have been reset to start of file.
            Report.Failed ("Index of file not set to 1 following Reset");
         end if;

         while Pos <= Full_String'Length loop
            -- Read one character from the stream.
            Character'Read (Filter_Stream, Current_Char);          -- 'Read
            -- Verify character against the original string.
            if Current_Char /= Full_String(Pos) then
               Report.Failed ("Incorrect character read from stream");
            else
               -- Capitalize the characters read from the stream, and
               -- place them in a string variable.
               Cap_String(Pos) := Capitalize (Current_Char);
            end if;
            Pos := Pos + 1;
         end loop;

         -- File index should now be set to the position following the final
         -- character in the file (the same as the index value stored at
         -- the completion of the Enter_Data_In_Stream block).
         if Ada.Streams.Stream_IO.Index (Filter_File) /=           -- Index.
            File_Index
         then
            Report.Failed ("Incorrect file index position");
         end if;

         -- The stream file should now be at EOF.                  -- EOF.
         if not Ada.Streams.Stream_IO.End_Of_File (Filter_File) then
            Report.Failed ("File not empty following filtering");
         end if;

      exception

         when Incomplete =>
            raise;
         when others =>
            Report.Failed ("Exception in Filter_Block");
            raise;
      end Filter_Block;

         ---

      Verification_Block:
      begin

         -- Verify that the entire string was examined, and that the
         -- process of capitalizing the character data was successful.
         if Cap_String /= TC_Capital_String then
            Report.Failed ("Incorrect Capitalization");
         end if;

      exception
         when others =>
            Report.Failed ("Exception in Verification_Block");
      end Verification_Block;


   exception

         when Incomplete =>
            raise;
         when others =>
            Report.Failed ("Exception raised in Operational Test Block");

   end Operational_Test_Block;

   Deletion:
   begin
      if Ada.Streams.Stream_IO.Is_Open (Filter_File) then          -- Is_Open.
         Ada.Streams.Stream_IO.Delete (Filter_File);               -- Delete.
      else
         Ada.Streams.Stream_IO.Open (Filter_File,                  -- Open.
                                     Ada.Streams.Stream_IO.Out_File,
                                     Filter_Filename);
         Ada.Streams.Stream_IO.Delete (Filter_File);               -- Delete.
      end if;
   exception
      when others =>
         Report.Failed
            ( "Delete not properly implemented for Stream_IO" );
   end Deletion;

   Report.Result;

exception
   when Incomplete =>
      Report.Result;
   when others     =>
      Report.Failed ( "Unexpected exception" );
      Report.Result;

end CXAC002;