| 1 |
-- C761006.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 Program_Error is raised when: |
| 28 |
-- * an exception is raised if Finalize invoked as part of an |
| 29 |
-- assignment operation; or |
| 30 |
-- * an exception is raised if Adjust invoked as part of an assignment |
| 31 |
-- operation, after any other adjustment due to be performed are |
| 32 |
-- performed; or |
| 33 |
-- * an exception is raised if Finalize invoked as part of a call on |
| 34 |
-- Unchecked_Deallocation, after any other finalizations to be |
| 35 |
-- performed are performed. |
| 36 |
-- |
| 37 |
-- TEST DESCRIPTION: |
| 38 |
-- This test defines these four controlled types: |
| 39 |
-- Good |
| 40 |
-- Bad_Initialize |
| 41 |
-- Bad_Adjust |
| 42 |
-- Bad_Finalize |
| 43 |
-- The type name conveys the associated failure. The operations in type |
| 44 |
-- good will "touch" the boolean array indicating correct path |
| 45 |
-- utilization for the purposes of checking "other <operations> are |
| 46 |
-- performed", where <operations> ::= initialization, adjusting, and |
| 47 |
-- finalization |
| 48 |
-- |
| 49 |
-- |
| 50 |
-- |
| 51 |
-- CHANGE HISTORY: |
| 52 |
-- 12 APR 94 SAIC Initial version |
| 53 |
-- 02 MAY 96 SAIC Visibility fixed for 2.1 |
| 54 |
-- 13 FEB 97 PWB.CTA Corrected value of Events_Occurring at line 286 |
| 55 |
-- 01 DEC 97 EDS Made correction wrt RM 7.6(21) |
| 56 |
-- 16 MAR 01 RLB Corrected Adjust cases to avoid problems with |
| 57 |
-- RM 7.6.1(16/1) from Technical Corrigendum 1. |
| 58 |
-- 05 JUL 12 RLB Redid Unchecked_Deallocation case to handle |
| 59 |
-- the fact that the behavior is unspecified (see |
| 60 |
-- AI95-0179-1). Also fixed indentation. |
| 61 |
-- |
| 62 |
--! |
| 63 |
|
| 64 |
------------------------------------------------------------- C761006_Support |
| 65 |
|
| 66 |
package C761006_Support is |
| 67 |
|
| 68 |
type Events is ( Good_Initialize, Good_Adjust, Good_Finalize ); |
| 69 |
|
| 70 |
type Event_Array is array(Events) of Boolean; |
| 71 |
|
| 72 |
Events_Occurring : Event_Array := (others => False); |
| 73 |
|
| 74 |
Propagating_Exception : exception; |
| 75 |
|
| 76 |
procedure Raise_Propagating_Exception(Do_It: Boolean); |
| 77 |
|
| 78 |
function Unique_Value return Natural; |
| 79 |
|
| 80 |
end C761006_Support; |
| 81 |
|
| 82 |
------------------------------------------------------------- C761006_Support |
| 83 |
|
| 84 |
with Report; |
| 85 |
package body C761006_Support is |
| 86 |
|
| 87 |
procedure Raise_Propagating_Exception(Do_It: Boolean) is |
| 88 |
begin |
| 89 |
if Report.Ident_Bool(Do_It) then |
| 90 |
raise Propagating_Exception; |
| 91 |
end if; |
| 92 |
end Raise_Propagating_Exception; |
| 93 |
|
| 94 |
Seed : Natural := 0; |
| 95 |
|
| 96 |
function Unique_Value return Natural is |
| 97 |
begin |
| 98 |
Seed := Seed +1; |
| 99 |
return Seed; |
| 100 |
end Unique_Value; |
| 101 |
|
| 102 |
end C761006_Support; |
| 103 |
|
| 104 |
------------------------------------------------------------------- C761006_0 |
| 105 |
|
| 106 |
with Ada.Finalization; |
| 107 |
with C761006_Support; |
| 108 |
package C761006_0 is |
| 109 |
|
| 110 |
type Good is new Ada.Finalization.Controlled |
| 111 |
with record |
| 112 |
Initialized : Boolean := False; |
| 113 |
Adjusted : Boolean := False; |
| 114 |
Unique : Natural := C761006_Support.Unique_Value; |
| 115 |
end record; |
| 116 |
|
| 117 |
procedure Initialize( It: in out Good ); |
| 118 |
procedure Adjust ( It: in out Good ); |
| 119 |
procedure Finalize ( It: in out Good ); |
| 120 |
|
| 121 |
type Bad_Initialize is private; |
| 122 |
|
| 123 |
type Bad_Adjust is private; |
| 124 |
|
| 125 |
type Bad_Finalize is private; |
| 126 |
|
| 127 |
Inits_Order : String(1..255); |
| 128 |
Inits_Called : Natural := 0; |
| 129 |
private |
| 130 |
type Bad_Initialize is new Ada.Finalization.Controlled |
| 131 |
with null record; |
| 132 |
procedure Initialize( It: in out Bad_Initialize ); |
| 133 |
|
| 134 |
type Bad_Adjust is new Ada.Finalization.Controlled |
| 135 |
with null record; |
| 136 |
procedure Adjust ( It: in out Bad_Adjust ); |
| 137 |
|
| 138 |
type Bad_Finalize is |
| 139 |
new Ada.Finalization.Controlled with null record; |
| 140 |
procedure Finalize ( It: in out Bad_Finalize ); |
| 141 |
end C761006_0; |
| 142 |
|
| 143 |
------------------------------------------------------------------- C761006_1 |
| 144 |
|
| 145 |
with Ada.Finalization; |
| 146 |
with C761006_0; |
| 147 |
package C761006_1 is |
| 148 |
|
| 149 |
type Init_Check_Root is new Ada.Finalization.Controlled with record |
| 150 |
Good_Component : C761006_0.Good; |
| 151 |
Init_Fails : C761006_0.Bad_Initialize; |
| 152 |
end record; |
| 153 |
|
| 154 |
type Adj_Check_Root is new Ada.Finalization.Controlled with record |
| 155 |
Good_Component : C761006_0.Good; |
| 156 |
Adj_Fails : C761006_0.Bad_Adjust; |
| 157 |
end record; |
| 158 |
|
| 159 |
type Fin_Check_Root is new Ada.Finalization.Controlled with record |
| 160 |
Good_Component : C761006_0.Good; |
| 161 |
Fin_Fails : C761006_0.Bad_Finalize; |
| 162 |
end record; |
| 163 |
|
| 164 |
end C761006_1; |
| 165 |
|
| 166 |
------------------------------------------------------------------- C761006_2 |
| 167 |
|
| 168 |
with C761006_1; |
| 169 |
package C761006_2 is |
| 170 |
|
| 171 |
type Init_Check is new C761006_1.Init_Check_Root with null record; |
| 172 |
type Adj_Check is new C761006_1.Adj_Check_Root with null record; |
| 173 |
type Fin_Check is new C761006_1.Fin_Check_Root with null record; |
| 174 |
|
| 175 |
end C761006_2; |
| 176 |
|
| 177 |
------------------------------------------------------------------- C761006_0 |
| 178 |
|
| 179 |
with Report; |
| 180 |
with C761006_Support; |
| 181 |
package body C761006_0 is |
| 182 |
|
| 183 |
package Sup renames C761006_Support; |
| 184 |
|
| 185 |
procedure Initialize( It: in out Good ) is |
| 186 |
begin |
| 187 |
Sup.Events_Occurring( Sup.Good_Initialize ) := True; |
| 188 |
It.Initialized := True; |
| 189 |
end Initialize; |
| 190 |
|
| 191 |
procedure Adjust ( It: in out Good ) is |
| 192 |
begin |
| 193 |
Sup.Events_Occurring( Sup.Good_Adjust ) := True; |
| 194 |
It.Adjusted := True; |
| 195 |
It.Unique := C761006_Support.Unique_Value; |
| 196 |
end Adjust; |
| 197 |
|
| 198 |
procedure Finalize ( It: in out Good ) is |
| 199 |
begin |
| 200 |
Sup.Events_Occurring( Sup.Good_Finalize ) := True; |
| 201 |
end Finalize; |
| 202 |
|
| 203 |
procedure Initialize( It: in out Bad_Initialize ) is |
| 204 |
begin |
| 205 |
Sup.Raise_Propagating_Exception(Report.Ident_Bool(True)); |
| 206 |
end Initialize; |
| 207 |
|
| 208 |
procedure Adjust( It: in out Bad_Adjust ) is |
| 209 |
begin |
| 210 |
Sup.Raise_Propagating_Exception(Report.Ident_Bool(True)); |
| 211 |
end Adjust; |
| 212 |
|
| 213 |
procedure Finalize( It: in out Bad_Finalize ) is |
| 214 |
begin |
| 215 |
Sup.Raise_Propagating_Exception(Report.Ident_Bool(True)); |
| 216 |
end Finalize; |
| 217 |
|
| 218 |
end C761006_0; |
| 219 |
|
| 220 |
--------------------------------------------------------------------- C761006 |
| 221 |
|
| 222 |
with Report; |
| 223 |
with C761006_0; |
| 224 |
with C761006_2; |
| 225 |
with C761006_Support; |
| 226 |
with Ada.Exceptions; |
| 227 |
with Ada.Finalization; |
| 228 |
with Unchecked_Deallocation; |
| 229 |
procedure C761006 is |
| 230 |
|
| 231 |
package Sup renames C761006_Support; |
| 232 |
use type Sup.Event_Array; |
| 233 |
|
| 234 |
type Procedure_Handle is access procedure; |
| 235 |
|
| 236 |
type Test_ID is ( Simple, Initialize, Adjust, Finalize ); |
| 237 |
|
| 238 |
Sub_Tests : array(Test_ID) of Procedure_Handle; |
| 239 |
|
| 240 |
procedure Simple_Test is |
| 241 |
A_Good_Object : C761006_0.Good; -- should call Initialize |
| 242 |
begin |
| 243 |
if not A_Good_Object.Initialized then |
| 244 |
Report.Failed("Good object not initialized"); |
| 245 |
end if; |
| 246 |
|
| 247 |
-- should call Adjust |
| 248 |
A_Good_Object := ( Ada.Finalization.Controlled |
| 249 |
with Unique => 0, others => False ); |
| 250 |
if not A_Good_Object.Adjusted then |
| 251 |
Report.Failed("Good object not adjusted"); |
| 252 |
end if; |
| 253 |
|
| 254 |
-- should call Finalize before end of scope |
| 255 |
end Simple_Test; |
| 256 |
|
| 257 |
procedure Initialize_Test is |
| 258 |
begin |
| 259 |
declare |
| 260 |
This_Object_Fails_In_Initialize : C761006_2.Init_Check; |
| 261 |
begin |
| 262 |
Report.Failed("Exception in Initialize did not occur"); |
| 263 |
exception |
| 264 |
when others => |
| 265 |
Report.Failed("Initialize caused exception at wrong lex"); |
| 266 |
end; |
| 267 |
|
| 268 |
Report.Failed("Error in execution sequence"); |
| 269 |
|
| 270 |
exception |
| 271 |
when Sup.Propagating_Exception => -- this is correct |
| 272 |
if not Sup.Events_Occurring(Sup.Good_Initialize) then |
| 273 |
Report.Failed("Initialization of Good Component did not occur"); |
| 274 |
end if; |
| 275 |
end Initialize_Test; |
| 276 |
|
| 277 |
procedure Adjust_Test is |
| 278 |
This_Object_OK : C761006_2.Adj_Check; |
| 279 |
This_Object_Target : C761006_2.Adj_Check; |
| 280 |
begin |
| 281 |
|
| 282 |
Check_Adjust_Due_To_Assignment: begin |
| 283 |
This_Object_Target := This_Object_OK; |
| 284 |
Report.Failed("Adjust did not propagate any exception"); |
| 285 |
exception |
| 286 |
when Program_Error => -- expected case |
| 287 |
if not This_Object_Target.Good_Component.Adjusted then |
| 288 |
Report.Failed("other adjustment not performed"); |
| 289 |
end if; |
| 290 |
when others => |
| 291 |
Report.Failed("Adjust propagated wrong exception"); |
| 292 |
end Check_Adjust_Due_To_Assignment; |
| 293 |
|
| 294 |
C761006_Support.Events_Occurring := (True, False, False); |
| 295 |
|
| 296 |
Check_Adjust_Due_To_Initial_Assignment: declare |
| 297 |
Another_Target : C761006_2.Adj_Check := This_Object_OK; |
| 298 |
begin |
| 299 |
Report.Failed("Adjust did not propagate any exception"); |
| 300 |
exception |
| 301 |
when others => Report.Failed("Adjust caused exception at wrong lex"); |
| 302 |
end Check_Adjust_Due_To_Initial_Assignment; |
| 303 |
|
| 304 |
exception |
| 305 |
when Program_Error => -- expected case |
| 306 |
if Sup.Events_Occurring(Sup.Good_Finalize) /= |
| 307 |
Sup.Events_Occurring(Sup.Good_Adjust) then |
| 308 |
-- RM 7.6.1(16/1) says that the good Adjust may or may not |
| 309 |
-- be performed; but if it is, then the Finalize must be |
| 310 |
-- performed; and if it is not, then the Finalize must not |
| 311 |
-- performed. |
| 312 |
if Sup.Events_Occurring(Sup.Good_Finalize) then |
| 313 |
Report.Failed("Good adjust not performed with bad adjust, " & |
| 314 |
"but good finalize was"); |
| 315 |
else |
| 316 |
Report.Failed("Good adjust performed with bad adjust, " & |
| 317 |
"but good finalize was not"); |
| 318 |
end if; |
| 319 |
end if; |
| 320 |
when others => |
| 321 |
Report.Failed("Adjust propagated wrong exception"); |
| 322 |
end Adjust_Test; |
| 323 |
|
| 324 |
procedure Finalize_Test is |
| 325 |
|
| 326 |
Fin_Not_Perf : constant String := "other finalizations not performed"; |
| 327 |
|
| 328 |
procedure Finalize_15 is |
| 329 |
Item : C761006_2.Fin_Check; |
| 330 |
Target : C761006_2.Fin_Check; |
| 331 |
begin |
| 332 |
|
| 333 |
Item := Target; |
| 334 |
-- finalization of Item should cause PE |
| 335 |
-- ARM7.6:21 allows the implementation to omit the assignment of the |
| 336 |
-- value into an anonymous object, which is the point at which Adjust |
| 337 |
-- is normally called. However, this would result in Program_Error's |
| 338 |
-- being raised before the call to Adjust, with the consequence that |
| 339 |
-- Adjust is never called. |
| 340 |
|
| 341 |
exception |
| 342 |
when Program_Error => -- expected case |
| 343 |
if not Sup.Events_Occurring(Sup.Good_Finalize) then |
| 344 |
Report.Failed("Assignment: " & Fin_Not_Perf); |
| 345 |
end if; |
| 346 |
when others => |
| 347 |
Report.Failed("Other exception in Finalize_15"); |
| 348 |
|
| 349 |
-- finalization of Item/Target should cause PE |
| 350 |
end Finalize_15; |
| 351 |
|
| 352 |
-- check failure in finalize due to Unchecked_Deallocation |
| 353 |
|
| 354 |
procedure Finalize_17_Outer is |
| 355 |
-- This procedure exists to make Shark local, so everything allocated |
| 356 |
-- on it will be finalized when this routine exits. |
| 357 |
|
| 358 |
type Shark is access C761006_2.Fin_Check; |
| 359 |
|
| 360 |
procedure Catch is |
| 361 |
new Unchecked_Deallocation( C761006_2.Fin_Check, Shark ); |
| 362 |
|
| 363 |
procedure Finalize_17_Inner is |
| 364 |
White : Shark := new C761006_2.Fin_Check; |
| 365 |
begin |
| 366 |
Catch (White); |
| 367 |
-- Note: It is unspecified if Catch deallocates the memory |
| 368 |
-- of the allocated object, and if it ceases to exist. |
| 369 |
-- As such, it is possible that it will be finalized when |
| 370 |
-- the scope of the access type is exited. We check for this |
| 371 |
-- case below. |
| 372 |
exception |
| 373 |
when Program_Error => |
| 374 |
if not Sup.Events_Occurring(Sup.Good_Finalize) then |
| 375 |
Report.Failed("Unchecked_Deallocation: " & Fin_Not_Perf); |
| 376 |
end if; |
| 377 |
end Finalize_17_Inner; |
| 378 |
|
| 379 |
begin |
| 380 |
Finalize_17_Inner; |
| 381 |
exception |
| 382 |
when others => |
| 383 |
Report.Failed("Unchecked_Deallocation check, unwanted exception in Outer"); |
| 384 |
end Finalize_17_Outer; |
| 385 |
|
| 386 |
begin |
| 387 |
|
| 388 |
Exception_In_Finalization: begin |
| 389 |
Finalize_15; |
| 390 |
exception |
| 391 |
when Program_Error => null; -- anticipated |
| 392 |
end Exception_In_Finalization; |
| 393 |
|
| 394 |
Use_Of_Unchecked_Deallocation: begin |
| 395 |
Finalize_17_Outer; |
| 396 |
exception |
| 397 |
when Program_Error => |
| 398 |
Report.Comment("Unchecked_Deallocation check, double finalization occurred"); |
| 399 |
when others => |
| 400 |
Report.Failed("Unchecked_Deallocation check, unwanted exception in caller"); |
| 401 |
end Use_Of_Unchecked_Deallocation; |
| 402 |
|
| 403 |
end Finalize_Test; |
| 404 |
|
| 405 |
begin -- Main test procedure. |
| 406 |
|
| 407 |
Report.Test ("C761006", "Check that exceptions raised in Initialize, " & |
| 408 |
"Adjust and Finalize are processed correctly" ); |
| 409 |
|
| 410 |
Sub_Tests := (Simple_Test'Access, Initialize_Test'Access, |
| 411 |
Adjust_Test'Access, Finalize_Test'Access); |
| 412 |
|
| 413 |
for Test in Sub_Tests'Range loop |
| 414 |
begin |
| 415 |
|
| 416 |
Sup.Events_Occurring := (others => False); |
| 417 |
|
| 418 |
Sub_Tests(Test).all; |
| 419 |
|
| 420 |
case Test is |
| 421 |
when Simple | Adjust => |
| 422 |
if Sup.Events_Occurring /= Sup.Event_Array ' ( others => True ) then |
| 423 |
Report.Failed ( "Other operation missing in " & |
| 424 |
Test_ID'Image ( Test ) ); |
| 425 |
end if; |
| 426 |
when Initialize => |
| 427 |
null; |
| 428 |
when Finalize => |
| 429 |
-- Note that for Good_Adjust, we may get either True or False |
| 430 |
if Sup.Events_Occurring ( Sup.Good_Initialize ) = False or |
| 431 |
Sup.Events_Occurring ( Sup.Good_Finalize ) = False |
| 432 |
then |
| 433 |
Report.Failed ( "Other operation missing in " & |
| 434 |
Test_ID'Image ( Test ) ); |
| 435 |
end if; |
| 436 |
end case; |
| 437 |
|
| 438 |
exception |
| 439 |
when How: others => Report.Failed( Ada.Exceptions.Exception_Name( How ) |
| 440 |
& " from " & Test_ID'Image( Test ) ); |
| 441 |
end; |
| 442 |
end loop; |
| 443 |
|
| 444 |
Report.Result; |
| 445 |
|
| 446 |
end C761006; |