| 1 |
-- CXACB02.A |
| 2 |
-- |
| 3 |
-- Grant of Unlimited Rights |
| 4 |
-- |
| 5 |
-- Under contracts F33600-87-D-0337, F33600-84-D-0280, MDA903-79-C-0687, |
| 6 |
-- F08630-91-C-0015, and DCA100-97-D-0025, the U.S. Government obtained |
| 7 |
-- unlimited rights in the software and documentation contained herein. |
| 8 |
-- Unlimited rights are defined in DFAR 252.227-7013(a)(19). By making |
| 9 |
-- this public release, the Government intends to confer upon all |
| 10 |
-- recipients unlimited rights equal to those held by the Government. |
| 11 |
-- These rights include rights to use, duplicate, release or disclose the |
| 12 |
-- released technical data and computer software in whole or in part, in |
| 13 |
-- any manner and for any purpose whatsoever, and to have or permit others |
| 14 |
-- 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 user defined subprograms can override the default |
| 28 |
-- attributes 'Input and 'Output using attribute definition clauses, |
| 29 |
-- when used with objects of discriminated record and multi-dimensional |
| 30 |
-- array types. |
| 31 |
-- |
| 32 |
-- TEST DESCRIPTION: |
| 33 |
-- This test demonstrates that the default implementations of the |
| 34 |
-- 'Input and 'Output attributes can be overridden by user specified |
| 35 |
-- subprograms in conjunction with attribute definition clauses. |
| 36 |
-- These attributes have been overridden below, and in the user defined |
| 37 |
-- substitutes, values are added or subtracted to global variables. |
| 38 |
-- Following the completion of the writing/reading test, the global |
| 39 |
-- variables are evaluated to ensure that the user defined subprograms |
| 40 |
-- were used in overriding the type-related default attributes. |
| 41 |
-- |
| 42 |
-- APPLICABILITY CRITERIA: |
| 43 |
-- Applicable to all implementations that support external |
| 44 |
-- Stream_IO files. |
| 45 |
-- |
| 46 |
-- |
| 47 |
-- CHANGE HISTORY: |
| 48 |
-- 06 Dec 94 SAIC ACVC 2.0 |
| 49 |
-- 14 Nov 95 SAIC Corrected test errors for ACVC 2.0.1. |
| 50 |
-- |
| 51 |
--! |
| 52 |
|
| 53 |
with Report; |
| 54 |
with Ada.Streams.Stream_IO; |
| 55 |
|
| 56 |
procedure CXACB02 is |
| 57 |
begin |
| 58 |
|
| 59 |
Report.Test ("CXACB02", "Check that user defined subprograms can " & |
| 60 |
"override the default attributes 'Input and " & |
| 61 |
"'Output using attribute definition clauses"); |
| 62 |
|
| 63 |
Test_for_Stream_IO_Support: |
| 64 |
declare |
| 65 |
|
| 66 |
Util_File : Ada.Streams.Stream_IO.File_Type; |
| 67 |
Util_Stream : Ada.Streams.Stream_IO.Stream_Access; |
| 68 |
Utility_Filename : constant String := Report.Legal_File_Name; |
| 69 |
|
| 70 |
begin |
| 71 |
|
| 72 |
-- If an implementation does not support Stream_IO in a particular |
| 73 |
-- environment, the exception Use_Error or Name_Error will be raised on |
| 74 |
-- calls to various Stream_IO operations. This block statement |
| 75 |
-- encloses a call to Create, which should produce an exception in a |
| 76 |
-- non-supportive environment. These exceptions will be handled to |
| 77 |
-- produce a Not_Applicable result. |
| 78 |
|
| 79 |
Ada.Streams.Stream_IO.Create (Util_File, |
| 80 |
Ada.Streams.Stream_IO.Out_File, |
| 81 |
Utility_Filename); |
| 82 |
|
| 83 |
Operational_Test_Block: |
| 84 |
declare |
| 85 |
|
| 86 |
type Customer_Type is (Residence, Apartment, Commercial); |
| 87 |
type Electric_Usage_Type is range 0..100000; |
| 88 |
type Months_In_Service_Type is range 1..12; |
| 89 |
type Quarterly_Period_Type is (Spring, Summer, Autumn, Winter); |
| 90 |
subtype Month_In_Quarter_Type is Positive range 1..3; |
| 91 |
type Service_History_Type is |
| 92 |
array (Quarterly_Period_Type range <>, |
| 93 |
Month_In_Quarter_Type range <>) of Electric_Usage_Type; |
| 94 |
|
| 95 |
type Service_Type (Customer : Customer_Type) is |
| 96 |
record |
| 97 |
Name : String (1..21); |
| 98 |
Account_ID : Natural range 0..100; |
| 99 |
case Customer is |
| 100 |
when Residence | Apartment => |
| 101 |
Low_Income_Credit : Boolean := False; |
| 102 |
when Commercial => |
| 103 |
Baseline_Allowance : Natural range 0..1000; |
| 104 |
Quantity_Discount : Boolean := False; |
| 105 |
end case; |
| 106 |
end record; |
| 107 |
|
| 108 |
|
| 109 |
-- Mode conformant, user defined subprograms that will override |
| 110 |
-- the type-related attributes. |
| 111 |
-- In this test, the user defines these subprograms to add/subtract |
| 112 |
-- specific values from global variables. |
| 113 |
|
| 114 |
function Service_Input |
| 115 |
(Stream : access Ada.Streams.Root_Stream_Type'Class) |
| 116 |
return Service_Type; |
| 117 |
|
| 118 |
procedure Service_Output |
| 119 |
(Stream : access Ada.Streams.Root_Stream_Type'Class; |
| 120 |
Item : Service_Type); |
| 121 |
|
| 122 |
function History_Input |
| 123 |
(Stream : access Ada.Streams.Root_Stream_Type'Class) |
| 124 |
return Service_History_Type; |
| 125 |
|
| 126 |
procedure History_Output |
| 127 |
(Stream : access Ada.Streams.Root_Stream_Type'Class; |
| 128 |
Item : Service_History_Type); |
| 129 |
|
| 130 |
|
| 131 |
-- Attribute definition clauses. |
| 132 |
|
| 133 |
for Service_Type'Input use Service_Input; |
| 134 |
for Service_Type'Output use Service_Output; |
| 135 |
|
| 136 |
for Service_History_Type'Input use History_Input; |
| 137 |
for Service_History_Type'Output use History_Output; |
| 138 |
|
| 139 |
|
| 140 |
-- Object Declarations |
| 141 |
|
| 142 |
Customer1 : Service_Type (Residence) := |
| 143 |
(Residence, "1221 Morningstar Lane", 44, False); |
| 144 |
Customer2 : Service_Type (Apartment) := |
| 145 |
(Customer => Apartment, |
| 146 |
Account_ID => 67, |
| 147 |
Name => "15 South Front St. #8", |
| 148 |
Low_Income_Credit => True); |
| 149 |
Customer3 : Service_Type (Commercial) := |
| 150 |
(Commercial, |
| 151 |
"12442 Central Avenue ", |
| 152 |
100, |
| 153 |
Baseline_Allowance => 938, |
| 154 |
Quantity_Discount => True); |
| 155 |
|
| 156 |
C1_Service_History : |
| 157 |
Service_History_Type (Quarterly_Period_Type, |
| 158 |
Month_In_Quarter_Type) := |
| 159 |
(Spring => (1 => 35, 2 => 39, 3 => 32), |
| 160 |
Summer => (1 => 34, 2 => 33, 3 => 39), |
| 161 |
Autumn => (1 => 45, 2 => 40, 3 => 38), |
| 162 |
Winter => (1 => 53, 2 => 0, 3 => 0)); |
| 163 |
|
| 164 |
C2_Service_History : |
| 165 |
Service_History_Type (Quarterly_Period_Type range Spring..Summer, |
| 166 |
Month_In_Quarter_Type) := |
| 167 |
(Spring => (23, 22, 0), Summer => (0, 0, 0)); |
| 168 |
|
| 169 |
C3_Service_History : |
| 170 |
Service_History_Type (Quarterly_Period_Type, |
| 171 |
Month_In_Quarter_Type) := |
| 172 |
(others => (others => 200)); |
| 173 |
|
| 174 |
|
| 175 |
TC_Input_Total : Integer := 0; |
| 176 |
TC_Output_Total : Integer := 0; |
| 177 |
|
| 178 |
|
| 179 |
-- Subprogram bodies. |
| 180 |
-- These subprograms are designed to override the default attributes |
| 181 |
-- 'Input and 'Output for the specified types. Each adds/subtracts |
| 182 |
-- a quantity to/from a program control variable, indicating its |
| 183 |
-- activity. Each user defined "Input" function uses the 'Read |
| 184 |
-- attribute for the type to accomplish the operation. Likewise, |
| 185 |
-- each user defined "Output" subprogram uses the 'Write attribute |
| 186 |
-- for the type. |
| 187 |
|
| 188 |
function Service_Input |
| 189 |
( Stream : access Ada.Streams.Root_Stream_Type'Class ) |
| 190 |
return Service_Type is |
| 191 |
Customer : Customer_Type; |
| 192 |
begin |
| 193 |
TC_Input_Total := TC_Input_Total + 1; |
| 194 |
|
| 195 |
-- Extract the discriminant value from the stream. |
| 196 |
-- This discriminant would not otherwise be extracted from the |
| 197 |
-- stream when the Service_Type'Read attribute is used below. |
| 198 |
Customer_Type'Read (Stream, Customer); |
| 199 |
|
| 200 |
declare |
| 201 |
-- Declare a constant of Service_Type, using the value just |
| 202 |
-- read from the stream as the discriminant value of the |
| 203 |
-- object. |
| 204 |
Service : Service_Type(Customer); |
| 205 |
begin |
| 206 |
Service_Type'Read (Stream, Service); |
| 207 |
return Service; |
| 208 |
end; |
| 209 |
end Service_Input; |
| 210 |
|
| 211 |
|
| 212 |
procedure Service_Output |
| 213 |
( Stream : access Ada.Streams.Root_Stream_Type'Class; |
| 214 |
Item : Service_Type ) is |
| 215 |
begin |
| 216 |
TC_Output_Total := TC_Output_Total + 2; |
| 217 |
-- Write the discriminant value to the stream. |
| 218 |
-- The attribute 'Write (for the record type) will not write the |
| 219 |
-- discriminant of the record object to the stream. Therefore, it |
| 220 |
-- must be explicitly written using the 'Write attribute of the |
| 221 |
-- discriminant type. |
| 222 |
Customer_Type'Write (Stream, Item.Customer); |
| 223 |
-- Write the record component values (but not the discriminant) to |
| 224 |
-- the stream. |
| 225 |
Service_Type'Write (Stream, Item); |
| 226 |
end Service_Output; |
| 227 |
|
| 228 |
|
| 229 |
function History_Input |
| 230 |
( Stream : access Ada.Streams.Root_Stream_Type'Class ) |
| 231 |
return Service_History_Type is |
| 232 |
Quarter_Bound_Low : Quarterly_Period_Type; |
| 233 |
Quarter_Bound_High : Quarterly_Period_Type; |
| 234 |
Month_Bound_Low : Month_In_Quarter_Type; |
| 235 |
Month_Bound_High : Month_In_Quarter_Type; |
| 236 |
begin |
| 237 |
TC_Input_Total := TC_Input_Total + 3; |
| 238 |
|
| 239 |
-- Read the value of the array bounds from the stream. |
| 240 |
-- Use these bounds in the creation of an array object that will |
| 241 |
-- be used to store data from the stream. |
| 242 |
-- The array bound values would not otherwise be read from the |
| 243 |
-- stream by use of the Service_History_Type'Read attribute. |
| 244 |
Quarterly_Period_Type'Read (Stream, Quarter_Bound_Low); |
| 245 |
Quarterly_Period_Type'Read (Stream, Quarter_Bound_High); |
| 246 |
Month_In_Quarter_Type'Read (Stream, Month_Bound_Low); |
| 247 |
Month_In_Quarter_Type'Read (Stream, Month_Bound_High); |
| 248 |
|
| 249 |
declare |
| 250 |
Service_History_Array : |
| 251 |
Service_History_Type |
| 252 |
(Quarterly_Period_Type range |
| 253 |
Quarter_Bound_Low..Quarter_Bound_High, |
| 254 |
Month_In_Quarter_Type range |
| 255 |
Month_Bound_Low .. Month_Bound_High); |
| 256 |
begin |
| 257 |
Service_History_Type'Read (Stream, Service_History_Array); |
| 258 |
return Service_History_Array; |
| 259 |
end; |
| 260 |
end History_Input; |
| 261 |
|
| 262 |
|
| 263 |
procedure History_Output |
| 264 |
( Stream : access Ada.Streams.Root_Stream_Type'Class; |
| 265 |
Item : Service_History_Type ) is |
| 266 |
begin |
| 267 |
TC_Output_Total := TC_Output_Total + 7; |
| 268 |
-- Write the upper/lower bounds of the array object dimensions to |
| 269 |
-- the stream. |
| 270 |
Quarterly_Period_Type'Write (Stream, Item'First(1)); |
| 271 |
Quarterly_Period_Type'Write (Stream, Item'Last(1)); |
| 272 |
Month_In_Quarter_Type'Write (Stream, Item'First(2)); |
| 273 |
Month_In_Quarter_Type'Write (Stream, Item'Last(2)); |
| 274 |
-- Write the array values to the stream in canonical order (last |
| 275 |
-- dimension varying fastest). |
| 276 |
Service_History_Type'Write (Stream, Item); |
| 277 |
end History_Output; |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
begin |
| 282 |
|
| 283 |
Util_Stream := Ada.Streams.Stream_IO.Stream (Util_File); |
| 284 |
|
| 285 |
-- Write data to the stream. A customer service record is followed |
| 286 |
-- by a service history array. |
| 287 |
|
| 288 |
Service_Type'Output (Util_Stream, Customer1); |
| 289 |
Service_History_Type'Output (Util_Stream, C1_Service_History); |
| 290 |
|
| 291 |
Service_Type'Output (Util_Stream, Customer2); |
| 292 |
Service_History_Type'Output (Util_Stream, C2_Service_History); |
| 293 |
|
| 294 |
Service_Type'Output (Util_Stream, Customer3); |
| 295 |
Service_History_Type'Output (Util_Stream, C3_Service_History); |
| 296 |
|
| 297 |
|
| 298 |
-- Read data from the stream, and verify the use of the user specified |
| 299 |
-- attributes. |
| 300 |
|
| 301 |
Verify_Data_Block: |
| 302 |
declare |
| 303 |
|
| 304 |
TC_Residence : Service_Type (Residence); |
| 305 |
TC_Apartment : Service_Type (Apartment); |
| 306 |
TC_Commercial : Service_Type (Commercial); |
| 307 |
|
| 308 |
TC_History1 : Service_History_Type (Quarterly_Period_Type, |
| 309 |
Month_In_Quarter_Type) := |
| 310 |
(others => (others => Electric_Usage_Type'First)); |
| 311 |
|
| 312 |
TC_History2 : Service_History_Type (Quarterly_Period_Type |
| 313 |
range Spring .. Summer, |
| 314 |
Month_In_Quarter_Type) := |
| 315 |
(others => (others => Electric_Usage_Type'First)); |
| 316 |
|
| 317 |
TC_History3 : Service_History_Type (Quarterly_Period_Type, |
| 318 |
Month_In_Quarter_Type) := |
| 319 |
(others => (others => Electric_Usage_Type'First)); |
| 320 |
|
| 321 |
begin |
| 322 |
|
| 323 |
-- Reset Stream file to mode In_File. |
| 324 |
|
| 325 |
Ada.Streams.Stream_IO.Reset (Util_File, |
| 326 |
Ada.Streams.Stream_IO.In_File); |
| 327 |
|
| 328 |
-- Read data from the stream. |
| 329 |
|
| 330 |
TC_Residence := Service_Type'Input (Util_Stream); |
| 331 |
TC_History1 := Service_History_Type'Input (Util_Stream); |
| 332 |
|
| 333 |
TC_Apartment := Service_Type'Input (Util_Stream); |
| 334 |
TC_History2 := Service_History_Type'Input (Util_Stream); |
| 335 |
|
| 336 |
TC_Commercial := Service_Type'Input (Util_Stream); |
| 337 |
TC_History3 := Service_History_Type'Input (Util_Stream); |
| 338 |
|
| 339 |
|
| 340 |
-- Verify product data was correctly written to/read from stream, |
| 341 |
-- including discriminants and array bounds. |
| 342 |
|
| 343 |
if (TC_Residence /= Customer1) or |
| 344 |
(TC_Residence.Customer /= Customer1.Customer) or |
| 345 |
(TC_History1'Last(1) /= C1_Service_History'Last(1)) or |
| 346 |
(TC_History1'First(1) /= C1_Service_History'First(1)) or |
| 347 |
(TC_History1'Last(2) /= C1_Service_History'Last(2)) or |
| 348 |
(TC_History1'First(2) /= C1_Service_History'First(2)) |
| 349 |
then |
| 350 |
Report.Failed ("Incorrect data from stream - 1"); |
| 351 |
end if; |
| 352 |
|
| 353 |
if (TC_Apartment /= Customer2) or |
| 354 |
(TC_Apartment.Customer /= Customer2.Customer) or |
| 355 |
(TC_History2 /= C2_Service_History) or |
| 356 |
(TC_History2'Last(1) /= C2_Service_History'Last(1)) or |
| 357 |
(TC_History2'First(1) /= C2_Service_History'First(1)) or |
| 358 |
(TC_History2'Last(2) /= C2_Service_History'Last(2)) or |
| 359 |
(TC_History2'First(2) /= C2_Service_History'First(2)) |
| 360 |
then |
| 361 |
Report.Failed ("Incorrect data from stream - 2"); |
| 362 |
end if; |
| 363 |
|
| 364 |
if (TC_Commercial /= Customer3) or |
| 365 |
(TC_Commercial.Customer /= Customer3.Customer) or |
| 366 |
(TC_History3 /= C3_Service_History) or |
| 367 |
(TC_History3'Last(1) /= C3_Service_History'Last(1)) or |
| 368 |
(TC_History3'First(1) /= C3_Service_History'First(1)) or |
| 369 |
(TC_History3'Last(2) /= C3_Service_History'Last(2)) or |
| 370 |
(TC_History3'First(2) /= C3_Service_History'First(2)) |
| 371 |
then |
| 372 |
Report.Failed ("Incorrect data from stream - 3"); |
| 373 |
end if; |
| 374 |
|
| 375 |
-- Verify that the user defined subprograms were used to override |
| 376 |
-- the default 'Input and 'Output attributes. |
| 377 |
-- There were three calls on each of the user defined attributes. |
| 378 |
|
| 379 |
if (TC_Input_Total /= 12 ) or (TC_Output_Total /= 27 ) then |
| 380 |
Report.Failed ("Incorrect use of user defined attributes"); |
| 381 |
end if; |
| 382 |
|
| 383 |
end Verify_Data_Block; |
| 384 |
|
| 385 |
exception |
| 386 |
|
| 387 |
when others => |
| 388 |
Report.Failed ("Exception raised in Operational Test Block"); |
| 389 |
|
| 390 |
end Operational_Test_Block; |
| 391 |
|
| 392 |
if Ada.Streams.Stream_IO.Is_Open (Util_File) then |
| 393 |
Ada.Streams.Stream_IO.Delete (Util_File); |
| 394 |
else |
| 395 |
Ada.Streams.Stream_IO.Open (Util_File, |
| 396 |
Ada.Streams.Stream_IO.Out_File, |
| 397 |
Utility_Filename); |
| 398 |
Ada.Streams.Stream_IO.Delete (Util_File); |
| 399 |
end if; |
| 400 |
|
| 401 |
|
| 402 |
exception |
| 403 |
|
| 404 |
-- Since Use_Error or Name_Error can be raised if, for the specified |
| 405 |
-- mode, the environment does not support Stream_IO operations, |
| 406 |
-- the following handlers are included: |
| 407 |
|
| 408 |
when Ada.Streams.Stream_IO.Name_Error => |
| 409 |
Report.Not_Applicable ("Name_Error raised on Stream IO Create"); |
| 410 |
|
| 411 |
when Ada.Streams.Stream_IO.Use_Error => |
| 412 |
Report.Not_Applicable ("Use_Error raised on Stream IO Create"); |
| 413 |
|
| 414 |
when others => |
| 415 |
Report.Failed ("Unexpected exception raised"); |
| 416 |
|
| 417 |
end Test_for_Stream_IO_Support; |
| 418 |
|
| 419 |
Report.Result; |
| 420 |
|
| 421 |
end CXACB02; |