| 1 |
-- CXAC005.A |
| 2 |
-- |
| 3 |
-- Grant of Unlimited Rights |
| 4 |
-- |
| 5 |
-- The Ada Conformity Assessment Authority (ACAA) holds unlimited |
| 6 |
-- rights in the software and documentation contained herein. Unlimited |
| 7 |
-- rights are the same as those granted by the U.S. Government for older |
| 8 |
-- parts of the Ada Conformity Assessment Test Suite, and are defined |
| 9 |
-- in DFAR 252.227-7013(a)(19). By making this public release, the ACAA |
| 10 |
-- intends to confer upon all recipients unlimited rights equal to those |
| 11 |
-- held by the ACAA. These rights include rights to use, duplicate, |
| 12 |
-- release or disclose the released technical data and computer software |
| 13 |
-- in whole or in part, in any manner and for any purpose whatsoever, and |
| 14 |
-- to have or permit others to do so. |
| 15 |
-- |
| 16 |
-- DISCLAIMER |
| 17 |
-- |
| 18 |
-- ALL MATERIALS OR INFORMATION HEREIN RELEASED, MADE AVAILABLE OR |
| 19 |
-- DISCLOSED ARE AS IS. THE GOVERNMENT MAKES NO EXPRESS OR IMPLIED |
| 20 |
-- WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING THE CONDITIONS OF THE |
| 21 |
-- SOFTWARE, DOCUMENTATION OR OTHER INFORMATION RELEASED, MADE AVAILABLE |
| 22 |
-- OR DISCLOSED, OR THE OWNERSHIP, MERCHANTABILITY, OR FITNESS FOR A |
| 23 |
-- PARTICULAR PURPOSE OF SAID MATERIAL. |
| 24 |
--* |
| 25 |
-- |
| 26 |
-- OBJECTIVE: |
| 27 |
-- Check that stream file positioning work as specified. (Defect Report |
| 28 |
-- 8652/0055). |
| 29 |
-- |
| 30 |
-- CHANGE HISTORY: |
| 31 |
-- 12 FEB 2001 PHL Initial version. |
| 32 |
-- 14 MAR 2001 RLB Readied for release; fixed Not_Applicable check |
| 33 |
-- to terminate test gracefully. |
| 34 |
-- 05 MAR 2007 RLB Updated to avoid problems with return-by-reference. |
| 35 |
-- |
| 36 |
--! |
| 37 |
with Ada.Streams.Stream_Io; |
| 38 |
use Ada.Streams; |
| 39 |
with Ada.Exceptions; |
| 40 |
use Ada.Exceptions; |
| 41 |
with Report; |
| 42 |
use Report; |
| 43 |
procedure CXAC005 is |
| 44 |
|
| 45 |
Incomplete : exception; |
| 46 |
|
| 47 |
procedure TC_Assert (Condition : Boolean; Message : String) is |
| 48 |
begin |
| 49 |
if not Condition then |
| 50 |
Failed (Message); |
| 51 |
end if; |
| 52 |
end TC_Assert; |
| 53 |
|
| 54 |
package Checked_Stream_Io is |
| 55 |
|
| 56 |
type File_Type (Max_Size : Stream_Element_Count) is limited private; |
| 57 |
|
| 58 |
procedure Create (File : in out File_Type; |
| 59 |
Mode : in Stream_Io.File_Mode := Stream_Io.Out_File; |
| 60 |
Name : in String := ""; |
| 61 |
Form : in String := ""); |
| 62 |
|
| 63 |
procedure Open (File : in out File_Type; |
| 64 |
Mode : in Stream_Io.File_Mode; |
| 65 |
Name : in String; |
| 66 |
Form : in String := ""); |
| 67 |
|
| 68 |
procedure Close (File : in out File_Type); |
| 69 |
procedure Delete (File : in out File_Type); |
| 70 |
|
| 71 |
procedure Reset (File : in out File_Type; |
| 72 |
Mode : in Stream_Io.File_Mode); |
| 73 |
procedure Reset (File : in out File_Type); |
| 74 |
|
| 75 |
procedure Read (File : in out File_Type; |
| 76 |
Item : out Stream_Element_Array; |
| 77 |
Last : out Stream_Element_Offset; |
| 78 |
From : in Stream_Io.Positive_Count); |
| 79 |
|
| 80 |
procedure Read (File : in out File_Type; |
| 81 |
Item : out Stream_Element_Array; |
| 82 |
Last : out Stream_Element_Offset); |
| 83 |
|
| 84 |
procedure Write (File : in out File_Type; |
| 85 |
Item : in Stream_Element_Array; |
| 86 |
To : in Stream_Io.Positive_Count); |
| 87 |
|
| 88 |
procedure Write (File : in out File_Type; |
| 89 |
Item : in Stream_Element_Array); |
| 90 |
|
| 91 |
procedure Set_Index (File : in out File_Type; |
| 92 |
To : in Stream_Io.Positive_Count); |
| 93 |
|
| 94 |
function Index (File : in File_Type) return Stream_Io.Positive_Count; |
| 95 |
|
| 96 |
function Size (File : in File_Type) return Stream_Io.Count; |
| 97 |
|
| 98 |
procedure Set_Mode (File : in out File_Type; |
| 99 |
Mode : in Stream_Io.File_Mode); |
| 100 |
|
| 101 |
private |
| 102 |
type File_Type (Max_Size : Stream_Element_Count) is |
| 103 |
record |
| 104 |
File : Stream_Io.File_Type; |
| 105 |
Index : Stream_Io.Positive_Count; |
| 106 |
Contents : |
| 107 |
Stream_Element_Array |
| 108 |
(Stream_Element_Offset (Ident_Int (1)) .. Max_Size); |
| 109 |
end record; |
| 110 |
end Checked_Stream_Io; |
| 111 |
|
| 112 |
package body Checked_Stream_Io is |
| 113 |
|
| 114 |
use Stream_Io; |
| 115 |
|
| 116 |
procedure Create (File : in out File_Type; |
| 117 |
Mode : in Stream_Io.File_Mode := Stream_Io.Out_File; |
| 118 |
Name : in String := ""; |
| 119 |
Form : in String := "") is |
| 120 |
begin |
| 121 |
Stream_Io.Create (File.File, Mode, Name, Form); |
| 122 |
File.Index := Stream_Io.Index (File.File); |
| 123 |
if Mode = Append_File then |
| 124 |
TC_Assert (File.Index = Stream_Io.Size (File.File) + 1, |
| 125 |
"Index /= Size + 1 -- Create - Append_File"); |
| 126 |
else |
| 127 |
TC_Assert (File.Index = 1, "Index /= 1 -- Create - " & |
| 128 |
File_Mode'Image (Mode)); |
| 129 |
end if; |
| 130 |
end Create; |
| 131 |
|
| 132 |
procedure Open (File : in out File_Type; |
| 133 |
Mode : in Stream_Io.File_Mode; |
| 134 |
Name : in String; |
| 135 |
Form : in String := "") is |
| 136 |
begin |
| 137 |
Stream_Io.Open (File.File, Mode, Name, Form); |
| 138 |
File.Index := Stream_Io.Index (File.File); |
| 139 |
if Mode = Append_File then |
| 140 |
TC_Assert (File.Index = Stream_Io.Size (File.File) + 1, |
| 141 |
"Index /= Size + 1 -- Open - Append_File"); |
| 142 |
else |
| 143 |
TC_Assert (File.Index = 1, "Index /= 1 -- Open - " & |
| 144 |
File_Mode'Image (Mode)); |
| 145 |
end if; |
| 146 |
end Open; |
| 147 |
|
| 148 |
procedure Close (File : in out File_Type) is |
| 149 |
begin |
| 150 |
Stream_Io.Close (File.File); |
| 151 |
end Close; |
| 152 |
|
| 153 |
procedure Delete (File : in out File_Type) is |
| 154 |
begin |
| 155 |
Stream_Io.Delete (File.File); |
| 156 |
end Delete; |
| 157 |
|
| 158 |
procedure Reset (File : in out File_Type; |
| 159 |
Mode : in Stream_Io.File_Mode) is |
| 160 |
begin |
| 161 |
Stream_Io.Reset (File.File, Mode); |
| 162 |
File.Index := Stream_Io.Index (File.File); |
| 163 |
if Mode = Append_File then |
| 164 |
TC_Assert (File.Index = Stream_Io.Size (File.File) + 1, |
| 165 |
"Index /= Size + 1 -- Reset - Append_File"); |
| 166 |
else |
| 167 |
TC_Assert (File.Index = 1, "Index /= 1 -- Reset - " & |
| 168 |
File_Mode'Image (Mode)); |
| 169 |
end if; |
| 170 |
end Reset; |
| 171 |
|
| 172 |
procedure Reset (File : in out File_Type) is |
| 173 |
begin |
| 174 |
Reset (File, Stream_Io.Mode (File.File)); |
| 175 |
end Reset; |
| 176 |
|
| 177 |
|
| 178 |
procedure Read (File : in out File_Type; |
| 179 |
Item : out Stream_Element_Array; |
| 180 |
Last : out Stream_Element_Offset; |
| 181 |
From : in Stream_Io.Positive_Count) is |
| 182 |
begin |
| 183 |
Set_Index (File, From); |
| 184 |
Read (File, Item, Last); |
| 185 |
end Read; |
| 186 |
|
| 187 |
procedure Read (File : in out File_Type; |
| 188 |
Item : out Stream_Element_Array; |
| 189 |
Last : out Stream_Element_Offset) is |
| 190 |
Index : constant Stream_Element_Offset := |
| 191 |
Stream_Element_Offset (File.Index); |
| 192 |
begin |
| 193 |
Stream_Io.Read (File.File, Item, Last); |
| 194 |
if Last < Item'Last then |
| 195 |
TC_Assert (Item (Item'First .. Last) = |
| 196 |
File.Contents (Index .. Index + Last - Item'First), |
| 197 |
"Incorrect data read from file - 1"); |
| 198 |
TC_Assert (Count (Index + Last - Item'First) = |
| 199 |
Stream_Io.Size (File.File), |
| 200 |
"Read stopped before end of file"); |
| 201 |
File.Index := Count (Index + Last - Item'First) + 1; |
| 202 |
else |
| 203 |
TC_Assert (Item = File.Contents (Index .. Index + Item'Length - 1), |
| 204 |
"Incorrect data read from file - 2"); |
| 205 |
File.Index := File.Index + Item'Length; |
| 206 |
end if; |
| 207 |
end Read; |
| 208 |
|
| 209 |
procedure Write (File : in out File_Type; |
| 210 |
Item : in Stream_Element_Array; |
| 211 |
To : in Stream_Io.Positive_Count) is |
| 212 |
begin |
| 213 |
Set_Index (File, To); |
| 214 |
Write (File, Item); |
| 215 |
end Write; |
| 216 |
|
| 217 |
procedure Write (File : in out File_Type; |
| 218 |
Item : in Stream_Element_Array) is |
| 219 |
Index : constant Stream_Element_Offset := |
| 220 |
Stream_Element_Offset (File.Index); |
| 221 |
begin |
| 222 |
Stream_Io.Write (File.File, Item); |
| 223 |
File.Contents (Index .. Index + Item'Length - 1) := Item; |
| 224 |
File.Index := File.Index + Item'Length; |
| 225 |
TC_Assert (File.Index = Stream_Io.Index (File.File), |
| 226 |
"Write failed to move the index"); |
| 227 |
end Write; |
| 228 |
|
| 229 |
procedure Set_Index (File : in out File_Type; |
| 230 |
To : in Stream_Io.Positive_Count) is |
| 231 |
begin |
| 232 |
Stream_Io.Set_Index (File.File, To); |
| 233 |
File.Index := Stream_Io.Index (File.File); |
| 234 |
TC_Assert (File.Index = To, "Set_Index failed"); |
| 235 |
end Set_Index; |
| 236 |
|
| 237 |
function Index (File : in File_Type) return Stream_Io.Positive_Count is |
| 238 |
New_Index : constant Count := Stream_Io.Index (File.File); |
| 239 |
begin |
| 240 |
TC_Assert (New_Index = File.Index, "Index changed unexpectedly"); |
| 241 |
return New_Index; |
| 242 |
end Index; |
| 243 |
|
| 244 |
function Size (File : in File_Type) return Stream_Io.Count is |
| 245 |
New_Size : constant Count := Stream_Io.Size (File.File); |
| 246 |
begin |
| 247 |
TC_Assert (New_Size <= Count(File.Max_Size), "File too large"); |
| 248 |
return New_Size; |
| 249 |
end Size; |
| 250 |
|
| 251 |
procedure Set_Mode (File : in out File_Type; |
| 252 |
Mode : in Stream_Io.File_Mode) is |
| 253 |
Old_Index : constant Count := File.Index; |
| 254 |
begin |
| 255 |
Stream_Io.Set_Mode (File.File, Mode); |
| 256 |
File.Index := Stream_Io.Index (File.File); |
| 257 |
if Mode = Append_File then |
| 258 |
TC_Assert (File.Index = Stream_Io.Size (File.File) + 1, |
| 259 |
"Index /= Size + 1 -- Set_Mode - Append_File"); |
| 260 |
else |
| 261 |
TC_Assert (File.Index = Old_Index, "Set_Mode changed the index"); |
| 262 |
end if; |
| 263 |
end Set_Mode; |
| 264 |
|
| 265 |
end Checked_Stream_Io; |
| 266 |
|
| 267 |
package Csio renames Checked_Stream_Io; |
| 268 |
|
| 269 |
F : Csio.File_Type (100); |
| 270 |
S : Stream_Element_Array (1 .. 10); |
| 271 |
Last : Stream_Element_Offset; |
| 272 |
|
| 273 |
begin |
| 274 |
|
| 275 |
Report.Test ("CXAC005", |
| 276 |
"Check that stream file positioning work as specified"); |
| 277 |
|
| 278 |
declare |
| 279 |
Name : constant String := Legal_File_Name; |
| 280 |
begin |
| 281 |
begin |
| 282 |
Csio.Create (F, Name => Name); |
| 283 |
exception |
| 284 |
when others => |
| 285 |
Not_Applicable ("Files not supported - Creation with Out_File for Stream_IO"); |
| 286 |
raise Incomplete; |
| 287 |
end; |
| 288 |
|
| 289 |
for I in Stream_Element range 1 .. 10 loop |
| 290 |
Csio.Write (F, ((1 => I + 2))); |
| 291 |
end loop; |
| 292 |
Csio.Write (F, (1 .. 15 => 11)); |
| 293 |
Csio.Write (F, (1 .. 15 => 12), To => 15); |
| 294 |
|
| 295 |
Csio.Reset (F); |
| 296 |
|
| 297 |
for I in Stream_Element range 1 .. 10 loop |
| 298 |
Csio.Write (F, (1 => I)); |
| 299 |
end loop; |
| 300 |
Csio.Write (F, (1 .. 15 => 13)); |
| 301 |
Csio.Write (F, (1 .. 15 => 14), To => 15); |
| 302 |
Csio.Write (F, (1 => 90)); |
| 303 |
|
| 304 |
Csio.Set_Mode (F, Stream_Io.In_File); |
| 305 |
|
| 306 |
Csio.Read (F, S, Last); |
| 307 |
Csio.Read (F, S, Last, From => 3); |
| 308 |
Csio.Read (F, S, Last, From => 28); |
| 309 |
|
| 310 |
Csio.Set_Mode (F, Stream_Io.Append_File); |
| 311 |
Csio.Write (F, (1 .. 5 => 88)); |
| 312 |
|
| 313 |
Csio.Close (F); |
| 314 |
|
| 315 |
Csio.Open (F, Name => Name, Mode => Stream_Io.Append_File); |
| 316 |
Csio.Write (F, (1 .. 3 => 33)); |
| 317 |
|
| 318 |
Csio.Set_Mode (F, Stream_Io.In_File); |
| 319 |
Csio.Read (F, S, Last, From => 20); |
| 320 |
Csio.Read (F, S, Last); |
| 321 |
Csio.Reset (F, Stream_Io.Out_File); |
| 322 |
|
| 323 |
Csio.Write (F, (1 .. 9 => 99)); |
| 324 |
|
| 325 |
-- Check the contents of the entire file. |
| 326 |
declare |
| 327 |
S : Stream_Element_Array |
| 328 |
(1 .. Stream_Element_Offset (Csio.Size (F))); |
| 329 |
begin |
| 330 |
Csio.Reset (F, Stream_Io.In_File); |
| 331 |
Csio.Read (F, S, Last); |
| 332 |
end; |
| 333 |
|
| 334 |
Csio.Delete (F); |
| 335 |
end; |
| 336 |
|
| 337 |
Report.Result; |
| 338 |
exception |
| 339 |
when Incomplete => |
| 340 |
Report.Result; |
| 341 |
when E:others => |
| 342 |
Report.Failed ("Unexpected exception raised - " & Exception_Name (E) & |
| 343 |
" - " & Exception_Message (E)); |
| 344 |
Report.Result; |
| 345 |
|
| 346 |
end CXAC005; |
| 347 |
|