summaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats/tests/c9/c980003.a
blob: dd69fc7ee68a6b3fa300a0bc47aee934bc7de561 (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
-- C980003.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.
--*
--
-- TEST OBJECTIVE:
--      Check that aborts are deferred during the execution of an 
--      Initialize procedure (as the last step of the default 
--      initialization of a controlled object), during the execution 
--      of a Finalize procedure (as part of the finalization of a 
--      controlled object), and during an assignment operation to an
--      object with a controlled part.
--
-- TEST DESCRIPTION:
--      A controlled type is created with Initialize, Adjust, and 
--      Finalize operations.  These operations note in a protected
--      object when the operation starts and completes.  This change
--      in state of the protected object will open the barrier for
--      the entry in the protected object.
--      The test contains declarations of objects of the controlled
--      type.  An asynchronous select is used to attempt to abort
--      the operations on the controlled type.  The asynchronous select
--      makes use of the state change to the protected object to 
--      trigger the abort.
--
--
-- CHANGE HISTORY:
--      11 Jan 96   SAIC    Initial Release for 2.1
--       5 May 96   SAIC    Incorporated Reviewer comments.
--      10 Oct 96   SAIC    Addressed issue where assignment statement
--                          can be 2 assignment operations.
--
--!

with Ada.Finalization;
package C980003_0 is
    Verbose : constant Boolean := False;

    -- the following flag is set true whenever the
    -- Initialize operation is called.
    Init_Occurred : Boolean;

    type Is_Controlled is new Ada.Finalization.Controlled with
         record
             Id : Integer;
         end record;

     procedure Initialize (Object : in out Is_Controlled);
     procedure Finalize   (Object : in out Is_Controlled);
     procedure Adjust     (Object : in out Is_Controlled);

     type States is (Unknown,
                     Start_Init,   Finished_Init, 
                     Start_Adjust, Finished_Adjust,
                     Start_Final,  Finished_Final);

     protected State_Manager is
        procedure Reset;
        procedure Set (New_State : States);
        function Current return States;
        entry Wait_For_Change;
     private
        Current_State : States := Unknown;
        Changed : Boolean := False;
     end State_Manager;

end C980003_0;


with Report;
with ImpDef;
package body C980003_0 is
     protected body State_Manager is
         procedure Reset is
         begin
             Current_State := Unknown;
             Changed := False;
         end Reset;

         procedure Set (New_State : States) is
         begin
             Changed := True;
             Current_State := New_State;
         end Set;

         function Current return States is
         begin
             return Current_State;
         end Current;

         entry Wait_For_Change when Changed is
         begin
             Changed := False;
         end Wait_For_Change;
     end State_Manager;

     procedure Initialize (Object : in out Is_Controlled) is
     begin
        if Verbose then
            Report.Comment ("starting initialize");
        end if;
        State_Manager.Set (Start_Init);
        if Verbose then
            Report.Comment ("in initialize");
        end if;
        delay ImpDef.Switch_To_New_Task;  -- tempting place for abort
        State_Manager.Set (Finished_Init);
        if Verbose then
            Report.Comment ("finished initialize");
        end if;
        Init_Occurred := True;
     end Initialize;

     procedure Finalize   (Object : in out Is_Controlled) is
     begin
        if Verbose then
            Report.Comment ("starting finalize");
        end if;
        State_Manager.Set (Start_Final);
        if Verbose then
            Report.Comment ("in finalize");
        end if;
        delay ImpDef.Switch_To_New_Task; -- tempting place for abort
        State_Manager.Set (Finished_Final);
        if Verbose then
            Report.Comment ("finished finalize");
        end if;
     end Finalize;

     procedure Adjust     (Object : in out Is_Controlled) is
     begin
        if Verbose then
            Report.Comment ("starting adjust");
        end if;
        State_Manager.Set (Start_Adjust);
        if Verbose then
            Report.Comment ("in adjust");
        end if;
        delay ImpDef.Switch_To_New_Task; -- tempting place for abort
        State_Manager.Set (Finished_Adjust);
        if Verbose then
            Report.Comment ("finished adjust");
        end if;
     end Adjust;
end C980003_0;


with Report;
with ImpDef;
with C980003_0;  use C980003_0;
with Ada.Unchecked_Deallocation;
procedure C980003 is

    procedure Check_State (Should_Be : States;
                           Msg       : String) is
        Cur : States := State_Manager.Current;
    begin
        if Cur /= Should_Be then
            Report.Failed (Msg);
            Report.Comment ("expected: " & States'Image (Should_Be) &
                            "  found: " & States'Image (Cur));
        elsif Verbose then
            Report.Comment ("passed: " & Msg);
        end if;
    end Check_State;

begin
 
    Report.Test ("C980003", "Check that aborts are deferred during" &
                            " initialization, finalization, and assignment" &
                            " operations on controlled objects");

    Check_State (Unknown, "initial condition");

    -- check that initialization and finalization take place
    Init_Occurred := False;
    select
        State_Manager.Wait_For_Change;
    then abort
        declare
            My_Controlled_Obj : Is_Controlled;
        begin
            delay 0.0;   -- abort completion point
            Report.Failed ("state change did not occur");
        end; 
    end select;
    if not Init_Occurred then
        Report.Failed ("Initialize did not complete");
    end if;
    Check_State (Finished_Final, "init/final for declared item");

    -- check adjust
    State_Manager.Reset;
    declare
        Source, Dest : Is_Controlled;
    begin
        Check_State (Finished_Init, "adjust initial state");
        Source.Id := 3;
        Dest.Id := 4;
        State_Manager.Reset;  -- so we will wait for change
        select
            State_Manager.Wait_For_Change;
        then abort
            Dest := Source;
        end select;

        -- there are two implementation methods for the 
        -- assignment statement:
        --   1.  no temporary was used in the assignment statement 
        --        thus the entire
        --        assignment statement is abort deferred.  
        --   2.  a temporary was used in the assignment statement so
        --        there are two assignment operations.  An abort may
        --        occur between the assignment operations
        -- Various optimizations are allowed by 7.6 that can affect
        -- how many times Adjust and Finalize are called. 
        -- Depending upon the implementation, the state can be either
        -- Finished_Adjust or Finished_Finalize.   If it is any other
        -- state then the abort took place at the wrong time.

        case State_Manager.Current is
        when Finished_Adjust =>
            if Verbose then
                Report.Comment ("assignment aborted after adjust");
            end if;
        when Finished_Final =>
            if Verbose then
                Report.Comment ("assignment aborted after finalize");
            end if;
        when Start_Adjust =>
            Report.Failed ("assignment aborted in adjust");
        when Start_Final =>
            Report.Failed ("assignment aborted in finalize");
        when Start_Init =>
            Report.Failed ("assignment aborted in initialize");
        when Finished_Init =>
            Report.Failed ("assignment aborted after initialize");
        when Unknown =>
            Report.Failed ("assignment aborted in unknown state");
        end case;


        if Dest.Id /= 3 then
            if Verbose then
                Report.Comment ("assignment not performed");
            end if;
        end if;
    end;


     -- check dynamically allocated objects
    State_Manager.Reset;
    declare
        type Pointer_Type is access Is_Controlled;
        procedure Free is new Ada.Unchecked_Deallocation (
              Is_Controlled, Pointer_Type);
        Ptr : Pointer_Type;
    begin
      -- make sure initialize is done when object is allocated
      Ptr := new Is_Controlled;
      Check_State (Finished_Init, "init when item allocated");
      -- now try aborting the finalize
      State_Manager.Reset;
      select
             State_Manager.Wait_For_Change;
      then abort
             Free (Ptr);
      end select;
      Check_State (Finished_Final, "finalization in dealloc");
    end;

    Report.Result;
 
end C980003;