summaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats/tests/c7/c761006.a
blob: 771e625d10f666f5865bb27567e733792614eed7 (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
-- C761006.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 Program_Error is raised when:
--        * an exception is raised if Finalize invoked as part of an
--          assignment operation; or
--        * an exception is raised if Adjust invoked as part of an assignment
--          operation, after any other adjustment due to be performed are
--          performed; or
--        * an exception is raised if Finalize invoked as part of a call on
--          Unchecked_Deallocation, after any other finalizations to be
--          performed are performed.
--
-- TEST DESCRIPTION:
--      This test defines these four controlled types:
--        Good
--        Bad_Initialize
--        Bad_Adjust
--        Bad_Finalize
--      The type name conveys the associated failure.  The operations in type
--      good will "touch" the boolean array indicating correct path
--      utilization for the purposes of checking "other <operations> are
--      performed", where <operations> ::= initialization, adjusting, and
--      finalization
--
--
--
-- CHANGE HISTORY:
--      12 APR 94   SAIC   Initial version
--      02 MAY 96   SAIC   Visibility fixed for 2.1
--      13 FEB 97   PWB.CTA Corrected value of Events_Occurring at line 286
--      01 DEC 97   EDS    Made correction wrt RM 7.6(21)
--      16 MAR 01   RLB    Corrected Adjust cases to avoid problems with
--                         RM 7.6.1(16/1) from Technical Corrigendum 1.
--
--!

------------------------------------------------------------- C761006_Support

package C761006_Support is

  type Events is ( Good_Initialize, Good_Adjust, Good_Finalize );

  type Event_Array is array(Events) of Boolean;

  Events_Occurring : Event_Array := (others => False);

  Propagating_Exception : exception;

  procedure Raise_Propagating_Exception(Do_It: Boolean);

  function Unique_Value return Natural;

end C761006_Support;

------------------------------------------------------------- C761006_Support

with Report;
package body C761006_Support is

  procedure Raise_Propagating_Exception(Do_It: Boolean) is
  begin
     if Report.Ident_Bool(Do_It) then
       raise Propagating_Exception;
     end if;
  end Raise_Propagating_Exception;

  Seed : Natural := 0;

  function Unique_Value return Natural is
  begin
    Seed := Seed +1;
    return Seed;
  end Unique_Value;

end C761006_Support;

------------------------------------------------------------------- C761006_0

with Ada.Finalization;
with C761006_Support;
package C761006_0 is

  type Good is new Ada.Finalization.Controlled
    with record
      Initialized : Boolean := False;
      Adjusted    : Boolean := False;
      Unique      : Natural := C761006_Support.Unique_Value;
    end record;

  procedure Initialize( It: in out Good );
  procedure Adjust    ( It: in out Good );
  procedure Finalize  ( It: in out Good );

  type Bad_Initialize is private;

  type Bad_Adjust     is private;

  type Bad_Finalize   is private;

  Inits_Order  : String(1..255);
  Inits_Called : Natural := 0;
private
  type Bad_Initialize is new Ada.Finalization.Controlled
                                             with null record;
  procedure Initialize( It: in out Bad_Initialize );

  type Bad_Adjust is new Ada.Finalization.Controlled
                                         with null record;
  procedure Adjust    ( It: in out Bad_Adjust );

  type Bad_Finalize is
       new Ada.Finalization.Controlled with null record;
  procedure Finalize  ( It: in out Bad_Finalize );
end C761006_0;

------------------------------------------------------------------- C761006_1

with Ada.Finalization;
with C761006_0;
package C761006_1 is

  type Init_Check_Root is new Ada.Finalization.Controlled with record
    Good_Component : C761006_0.Good;
    Init_Fails     : C761006_0.Bad_Initialize;
  end record;

  type Adj_Check_Root is new Ada.Finalization.Controlled with record
    Good_Component : C761006_0.Good;
    Adj_Fails      : C761006_0.Bad_Adjust;
  end record;

  type Fin_Check_Root is new Ada.Finalization.Controlled with record
    Good_Component : C761006_0.Good;
    Fin_Fails      : C761006_0.Bad_Finalize;
  end record;

end C761006_1;

------------------------------------------------------------------- C761006_2

with C761006_1;
package C761006_2 is

  type Init_Check is new C761006_1.Init_Check_Root with null record;
  type Adj_Check is  new C761006_1.Adj_Check_Root  with null record;
  type Fin_Check is  new C761006_1.Fin_Check_Root  with null record;

end C761006_2;

------------------------------------------------------------------- C761006_0

with Report;
with C761006_Support;
package body C761006_0 is

  package Sup renames C761006_Support;

  procedure Initialize( It: in out Good ) is
  begin
    Sup.Events_Occurring( Sup.Good_Initialize ) := True;
    It.Initialized := True;
  end Initialize;

  procedure Adjust    ( It: in out Good ) is
  begin
    Sup.Events_Occurring( Sup.Good_Adjust ) := True;
    It.Adjusted := True;
    It.Unique := C761006_Support.Unique_Value;
  end Adjust;

  procedure Finalize  ( It: in out Good ) is
  begin
    Sup.Events_Occurring( Sup.Good_Finalize ) := True;
  end Finalize;

  procedure Initialize( It: in out Bad_Initialize ) is
  begin
    Sup.Raise_Propagating_Exception(Report.Ident_Bool(True));
  end Initialize;

  procedure Adjust( It: in out Bad_Adjust ) is
  begin
    Sup.Raise_Propagating_Exception(Report.Ident_Bool(True));
  end Adjust;

  procedure Finalize( It: in out Bad_Finalize ) is
  begin
    Sup.Raise_Propagating_Exception(Report.Ident_Bool(True));
  end Finalize;

end C761006_0;

--------------------------------------------------------------------- C761006

with Report;
with C761006_0;
with C761006_2;
with C761006_Support;
with Ada.Exceptions;
with Ada.Finalization;
with Unchecked_Deallocation;
procedure C761006 is

  package Sup renames C761006_Support;
  use type Sup.Event_Array;

  type Procedure_Handle is access procedure;

  type Test_ID is ( Simple, Initialize, Adjust, Finalize );

  Sub_Tests : array(Test_ID) of Procedure_Handle;

  procedure Simple_Test is
    A_Good_Object : C761006_0.Good; -- should call Initialize
  begin
    if not A_Good_Object.Initialized then
      Report.Failed("Good object not initialized");
    end if;

    -- should call Adjust
    A_Good_Object := ( Ada.Finalization.Controlled
                       with Unique => 0, others => False );
    if not A_Good_Object.Adjusted then
      Report.Failed("Good object not adjusted");
    end if;

    -- should call Finalize before end of scope
  end Simple_Test;

  procedure Initialize_Test is
  begin
    declare
      This_Object_Fails_In_Initialize : C761006_2.Init_Check;
    begin
      Report.Failed("Exception in Initialize did not occur");
    exception
      when others =>
        Report.Failed("Initialize caused exception at wrong lex");
    end;

    Report.Failed("Error in execution sequence");

  exception
    when Sup.Propagating_Exception => -- this is correct
      if not Sup.Events_Occurring(Sup.Good_Initialize) then
        Report.Failed("Initialization of Good Component did not occur");
      end if;
  end Initialize_Test;

  procedure Adjust_Test is
    This_Object_OK     : C761006_2.Adj_Check;
    This_Object_Target : C761006_2.Adj_Check;
  begin

    Check_Adjust_Due_To_Assignment: begin
      This_Object_Target := This_Object_OK;
      Report.Failed("Adjust did not propagate any exception");
    exception
      when Program_Error =>  -- expected case
             if not This_Object_Target.Good_Component.Adjusted then
               Report.Failed("other adjustment not performed");
             end if;
      when others =>
             Report.Failed("Adjust propagated wrong exception");
    end Check_Adjust_Due_To_Assignment;

    C761006_Support.Events_Occurring := (True, False, False);

    Check_Adjust_Due_To_Initial_Assignment: declare
      Another_Target : C761006_2.Adj_Check := This_Object_OK;
    begin
      Report.Failed("Adjust did not propagate any exception");
    exception
      when others => Report.Failed("Adjust caused exception at wrong lex");
    end Check_Adjust_Due_To_Initial_Assignment;

  exception
    when Program_Error =>  -- expected case
           if Sup.Events_Occurring(Sup.Good_Finalize) /=
              Sup.Events_Occurring(Sup.Good_Adjust) then
              -- RM 7.6.1(16/1) says that the good Adjust may or may not
              -- be performed; but if it is, then the Finalize must be
              -- performed; and if it is not, then the Finalize must not
              -- performed.
              if Sup.Events_Occurring(Sup.Good_Finalize) then
                 Report.Failed("Good adjust not performed with bad adjust, " &
                               "but good finalize was");
              else
                 Report.Failed("Good adjust performed with bad adjust, " &
                               "but good finalize was not");
              end if;
           end if;
    when others =>
           Report.Failed("Adjust propagated wrong exception");
  end Adjust_Test;

  procedure Finalize_Test is

    Fin_Not_Perf : constant String := "other finalizations not performed";

    procedure Finalize_15 is
      Item   : C761006_2.Fin_Check;
      Target : C761006_2.Fin_Check;
    begin

      Item := Target;
      -- finalization of Item should cause PE
      -- ARM7.6:21 allows the implementation to omit the assignment of the
      -- value into an anonymous object, which is the point at which Adjust
      -- is normally called.  However, this would result in Program_Error's
      -- being raised before the call to Adjust, with the consequence that
      -- Adjust is never called.

    exception
      when Program_Error => -- expected case
             if not Sup.Events_Occurring(Sup.Good_Finalize) then
               Report.Failed("Assignment: " & Fin_Not_Perf);
             end if;
      when others =>
             Report.Failed("Other exception in Finalize_15");

    -- finalization of Item/Target should cause PE
    end Finalize_15;

  -- check failure in finalize due to Unchecked_Deallocation

  type Shark is access C761006_2.Fin_Check;

  procedure Catch is
    new Unchecked_Deallocation( C761006_2.Fin_Check, Shark );

  procedure Finalize_17 is
    White : Shark := new C761006_2.Fin_Check;
  begin
    Catch( White );
  exception
    when Program_Error =>
           if not Sup.Events_Occurring(Sup.Good_Finalize) then
             Report.Failed("Unchecked_Deallocation: " & Fin_Not_Perf);
           end if;
  end Finalize_17;

  begin

    Exception_In_Finalization: begin
      Finalize_15;
    exception
      when Program_Error => null; -- anticipated
    end Exception_In_Finalization;

    Use_Of_Unchecked_Deallocation: begin
      Finalize_17;
    exception
      when others =>
        Report.Failed("Unchecked_Deallocation check, unwanted exception");
    end Use_Of_Unchecked_Deallocation;

  end Finalize_Test;

begin  -- Main test procedure.

  Report.Test ("C761006", "Check that exceptions raised in Initialize, " &
                          "Adjust and Finalize are processed correctly" );

  Sub_Tests := (Simple_Test'Access, Initialize_Test'Access,
                Adjust_Test'Access, Finalize_Test'Access);

  for Test in Sub_Tests'Range loop
    begin

      Sup.Events_Occurring := (others => False);

      Sub_Tests(Test).all;

      case Test is
        when Simple | Adjust =>
          if Sup.Events_Occurring /= Sup.Event_Array ' ( others => True ) then
            Report.Failed ( "Other operation missing in " &
                            Test_ID'Image ( Test ) );
          end if;
        when  Initialize =>
          null;
        when Finalize  =>
          -- Note that for Good_Adjust, we may get either True or False
          if Sup.Events_Occurring ( Sup.Good_Initialize ) = False or
             Sup.Events_Occurring ( Sup.Good_Finalize ) = False
          then
            Report.Failed ( "Other operation missing in " &
                            Test_ID'Image ( Test ) );
          end if;
      end case;

    exception
       when How: others => Report.Failed( Ada.Exceptions.Exception_Name( How )
                                        & " from " & Test_ID'Image( Test ) );
    end;
  end loop;

  Report.Result;

end C761006;