summaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats/tests/cxa/cxac001.a
blob: a77d561f5d6686b3365b4003a206c930cd7acf10 (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
-- CXAC001.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 attribute T'Write will, for any specific non-limited
--      type T, write an item of the subtype to the stream.
--
--      Check that the attribute T'Read will, for a specific non-limited
--      type T, read a value of the subtype from the stream.
--
-- TEST DESCRIPTION:
--      The scenario depicted in this test is that of an environment where 
--      product data is stored in stream form, then reconstructed into the
--      appropriate data structures.  Several records of product information
--      are stored in an array; the array is passed as a parameter to a 
--      procedure for storage in the stream.  A header is created based on the
--      number of data records stored in the array.  The header is then written
--      to the stream, followed by each record maintained in the array.
--      In order to retrieve data from the stream, the header information is
--      read from the stream, and the data stored in the header is used to 
--      perform the appropriate number of read operations of record data from
--      the stream.  All data read from the stream is validated against the
---     values that were written to the stream.
--
-- APPLICABILITY CRITERIA: 
--      Applicable to all systems capable of supporting IO operations on 
--      external Stream_IO files.
--
--       
-- CHANGE HISTORY:
--      06 Dec 94   SAIC    ACVC 2.0
--      08 Nov 95   SAIC    Corrected call to Read in Procedure Retrieve_Data
--                          for ACVC 2.0.1.
--      27 Feb 08   PWB.CTA Allowed for non-support of certain IO operations.
--!

with Ada.Streams.Stream_IO;
with Report;

procedure CXAC001 is

   package Strm_Pack renames Ada.Streams.Stream_IO;
   The_File     : Strm_Pack.File_Type;
   The_Filename : constant String :=
                     Report.Legal_File_Name ( Nam => "CXAC001" );
   Incomplete : exception;


begin 

   Report.Test ("CXAC001", "Check that the 'Read and 'Write attributes " &
                           "will transfer an object of a specific, "     &
                           "non-limited type to/from a stream");

   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.

      Strm_Pack.Create (The_File, Strm_Pack.Out_File, The_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

      The_Stream : Strm_Pack.Stream_Access;
      Todays_Date : String (1 .. 6) := "271193";

      type ID_Type   is range 1 .. 100;
      type Size_Type is (Small, Medium, Large, XLarge);

      type Header_Type is record
         Number_of_Elements : Natural := 0;
         Origination_Date   : String (1 .. 6);
      end record;

      type Data_Type is record
         ID   : ID_Type;
         Size : Size_Type;
      end record;

      type Data_Array_Type is array (Positive range <>) of Data_Type;

      Product_Information_1 : Data_Array_Type (1 .. 3) := ((20, Large),
                                                           (55, Small),
                                                           (89, XLarge));

      Product_Information_2 : Data_Array_Type (1 .. 4) := (( 5, XLarge),
                                                           (27, Small),
                                                           (79, Medium),
                                                           (93, XLarge));

      procedure Store_Data ( The_Stream : in Strm_Pack.Stream_Access;
                             The_Array  : in Data_Array_Type ) is
         Header     : Header_Type;
      begin

         -- Fill in header info.
         Header.Number_of_Elements := The_Array'Length;
         Header.Origination_Date := Todays_Date;       

         -- Write header to stream.
         Header_Type'Write (The_Stream, Header);   
                                                      
         -- Write each record in the array to the stream.
         for I in 1 .. Header.Number_of_Elements loop
           Data_Type'Write (The_Stream, The_Array (I));
         end loop;                                     

      end Store_Data;

      procedure Retrieve_Data (The_Stream : in     Strm_Pack.Stream_Access;
                               The_Header :    out Header_Type;
                               The_Array  :    out Data_Array_Type ) is
      begin

         -- Read header from the stream.
         Header_Type'Read (The_Stream, The_Header);  
                                                   
         -- Read the records from the stream into the array.
         for I in 1 .. The_Header.Number_of_Elements loop    
            Data_Type'Read (The_Stream, The_Array (I));  
         end loop;                                       

      end Retrieve_Data;

   begin

      -- Assign access value.
      The_Stream := Strm_Pack.Stream (The_File);      

      -- Product information is to be stored in the stream file.  These
      -- data arrays are of different sizes (actually, the records
      -- are stored individually, not as a single array).  Prior to the
      -- record data being written, a header record is initialized with
      -- information about the data to be written, then itself is written
      -- to the stream.

      Store_Data (The_Stream, Product_Information_1);
      Store_Data (The_Stream, Product_Information_2);

      Test_Verification_Block:
      declare
         Product_Header_1 : Header_Type;
         Product_Header_2 : Header_Type;
         Product_Array_1  : Data_Array_Type (1 .. 3);
         Product_Array_2  : Data_Array_Type (1 .. 4);
      begin

         Reset1:
         begin
            Strm_Pack.Reset (The_File, Strm_Pack.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;

         -- Data is read from the stream, first the appropriate header, 
         -- then the associated data records, which are then reconstructed
         -- into a data array of product information.

         Retrieve_Data (The_Stream, Product_Header_1, Product_Array_1);

         -- Validate a field in the header.
         if (Product_Header_1.Origination_Date   /= Todays_Date) or
            (Product_Header_1.Number_of_Elements /= 3)    
         then
            Report.Failed ("Incorrect Header_1 info read from stream");
         end if;

         -- Validate the data records read from the file.
         for I in 1 .. Product_Header_1.Number_of_Elements loop
            if (Product_Array_1(I) /= Product_Information_1(I)) then
               Report.Failed ("Incorrect Product 1 info read from" &
                              " record: " & Integer'Image (I));
            end if;
         end loop;

         -- Repeat this read and verify operation for the next parcel of
         -- data.  Again, header and data record information are read from
         -- the same stream file.  
         Retrieve_Data (The_Stream, Product_Header_2, Product_Array_2);

         if (Product_Header_2.Origination_Date   /= Todays_Date) or
            (Product_Header_2.Number_of_Elements /= 4)    
         then 
            Report.Failed ("Incorrect Header_2 info read from stream");
         end if;

         for I in 1 .. Product_Header_2.Number_of_Elements loop
            if (Product_Array_2(I) /= Product_Information_2(I)) then
               Report.Failed ("Incorrect Product_2 info read from" &
                              " record: " & Integer'Image (I));
            end if;
         end loop;

      exception

         when Incomplete =>
            raise; 

         when Strm_Pack.End_Error =>           -- If correct number of 
                                               -- items not in file (data 
                                               -- overwritten), then fail.
            Report.Failed ("Incorrect number of record elements in file");
            if not Strm_Pack.Is_Open (The_File) then
               Strm_Pack.Open (The_File, Strm_Pack.Out_File, The_Filename);
            end if;

         when others => 
            Report.Failed ("Exception raised in Data Verification Block");
            if not Strm_Pack.Is_Open (The_File) then
               Strm_Pack.Open (The_File, Strm_Pack.Out_File, The_Filename);
            end if;

      end Test_Verification_Block;

   exception

      when Incomplete =>
         raise; 

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

   end Operational_Test_Block;

   Deletion:
   begin
      -- Delete the file.
      if Strm_Pack.Is_Open (The_File) then
         Strm_Pack.Delete (The_File);
      else
         Strm_Pack.Open (The_File, Strm_Pack.Out_File, The_Filename);
         Strm_Pack.Delete (The_File);
      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 CXAC001;