aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats/tests/cb/cb40005.a
blob: 681ec18ff28037877f3f5cab9a5411d9af16f791 (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
-- CB40005.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 exceptions raised in non-generic code can be handled by
--      a procedure in a generic package.  Check that the exception identity
--      can be properly retrieved from the generic code and used by the
--      non-generic code.
--
-- TEST DESCRIPTION:
--      This test models a possible usage paradigm for the type:
--        Ada.Exceptions.Exception_Occurrence.
--
--      A generic package takes access to procedure types (allowing it to
--      be used at any accessibility level) and defines a "fail soft"
--      procedure that takes designators to a procedure to call, a
--      procedure to call in the event that it fails, and a function to
--      call to determine the next action.
--
--      In the event an exception occurs on the call to the first procedure,
--      the exception is stored in a stack; along with the designator to the
--      procedure that caused it; allowing the procedure to be called again, 
--      or the exception to be re-raised.
--
--      A full implementation of such a tool would use a more robust storage
--      mechanism, and would provide a more flexible interface.
--
--
-- CHANGE HISTORY:
--      29 MAR 96   SAIC   Initial version
--      12 NOV 96   SAIC   Revised for 2.1 release
--
--!

----------------------------------------------------------------- CB40005_0

with Ada.Exceptions;
generic
  type Proc_Pointer is access procedure;
  type Func_Pointer is access function return Proc_Pointer;
package CB40005_0 is -- Fail_Soft


  procedure Fail_Soft_Call( Proc_To_Call : Proc_Pointer;
                            Proc_To_Call_On_Exception : Proc_Pointer := null;
                            Retry_Routine : Func_Pointer := null );

  function Top_Event_Exception return Ada.Exceptions.Exception_Occurrence;

  function Top_Event_Procedure return Proc_Pointer;

  procedure Pop_Event;

  function Event_Stack_Size return Natural;

end CB40005_0; -- Fail_Soft

-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- CB40005_0

with Report;
package body CB40005_0 is

  type History_Event is record
    Exception_Event  : Ada.Exceptions.Exception_Occurrence_Access;
    Procedure_Called : Proc_Pointer;
  end record;

  procedure Store_Event( Proc_Called : Proc_Pointer;
                         Error       : Ada.Exceptions.Exception_Occurrence );

  procedure Fail_Soft_Call( Proc_To_Call : Proc_Pointer;
                            Proc_To_Call_On_Exception : Proc_Pointer := null;
                            Retry_Routine : Func_Pointer := null ) is

    Current_Proc_To_Call : Proc_Pointer := Proc_To_Call;

  begin
    while Current_Proc_To_Call /= null loop
      begin
        Current_Proc_To_Call.all;  -- call procedure through pointer
        Current_Proc_To_Call := null;
      exception
        when Capture: others =>
          Store_Event( Current_Proc_To_Call, Capture );
          if Proc_To_Call_On_Exception /= null then
            Proc_To_Call_On_Exception.all;
          end if;
          if Retry_Routine /= null then
            Current_Proc_To_Call := Retry_Routine.all;
          else
            Current_Proc_To_Call := null;
          end if;
      end;
    end loop;
  end Fail_Soft_Call;
  
  Stack : array(1..10) of History_Event;  -- minimal, sufficient for testing

  Stack_Top : Natural := 0;

  procedure Store_Event( Proc_Called : Proc_Pointer;
                         Error       : Ada.Exceptions.Exception_Occurrence )
  is
  begin
    Stack_Top := Stack_Top +1;
    Stack(Stack_Top) := ( Ada.Exceptions.Save_Occurrence(Error),
                          Proc_Called );
  end Store_Event;

  function Top_Event_Exception return Ada.Exceptions.Exception_Occurrence is
  begin
    if Stack_Top > 0 then
      return Stack(Stack_Top).Exception_Event.all;
    else
      return Ada.Exceptions.Null_Occurrence;
    end if;
  end Top_Event_Exception;

  function Top_Event_Procedure return Proc_Pointer is
  begin
    if Stack_Top > 0 then
      return Stack(Stack_Top).Procedure_Called;
    else
      return null;
    end if;
  end Top_Event_Procedure;

  procedure Pop_Event is
  begin
    if Stack_Top > 0 then
      Stack_Top := Stack_Top -1;
    else
      Report.Failed("Stack Error");
    end if;
  end Pop_Event;

  function Event_Stack_Size return Natural is
  begin
    return Stack_Top;
  end Event_Stack_Size;

end CB40005_0;

------------------------------------------------------------------- CB40005

with Report;
with TCTouch;
with CB40005_0;
with Ada.Exceptions;
procedure CB40005 is

  type Proc_Pointer is access procedure;
  type Func_Pointer is access function return Proc_Pointer;

  package Fail_Soft is new CB40005_0(Proc_Pointer, Func_Pointer);

  procedure Cause_Standard_Exception;

  procedure Cause_Visible_Exception;

  procedure Cause_Invisible_Exception;

  Exception_Procedure_Pointer : Proc_Pointer;

  Visible_Exception : exception;

  procedure Action_On_Exception;

  function Retry_Procedure return Proc_Pointer;

  Raise_Error : Boolean;

  -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

  procedure Cause_Standard_Exception is
  begin
    TCTouch.Touch('S');  --------------------------------------------------- S
    if Raise_Error then
      raise Constraint_Error;
    end if;
  end Cause_Standard_Exception;

  procedure Cause_Visible_Exception is
  begin
    TCTouch.Touch('V');  --------------------------------------------------- V
    if Raise_Error then
      raise Visible_Exception;
    end if;
  end Cause_Visible_Exception;

  procedure Cause_Invisible_Exception is
    Invisible_Exception : exception;
  begin
    TCTouch.Touch('I');  --------------------------------------------------- I
    if Raise_Error then
      raise Invisible_Exception;
    end if;
  end Cause_Invisible_Exception;

  procedure Action_On_Exception is
  begin
    TCTouch.Touch('A');  --------------------------------------------------- A
  end Action_On_Exception;

  function Retry_Procedure return Proc_Pointer is
  begin
    TCTouch.Touch('R');  --------------------------------------------------- R
    return Action_On_Exception'Access;
  end Retry_Procedure;

         -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 

begin  -- Main test procedure.

  Report.Test ("CB40005", "Check that exceptions raised in non-generic " &
                          "code can be handled by a procedure in a generic " &
                          "package.  Check that the exception identity can " &
                          "be properly retrieved from the generic code and " &
                          "used by the non-generic code" );

  -- first, check that the no exception cases cause no action on the stack
  Raise_Error := False;

  Fail_Soft.Fail_Soft_Call( Cause_Standard_Exception'Access );    -- S

  Fail_Soft.Fail_Soft_Call( Cause_Visible_Exception'Access,       -- V
                            Action_On_Exception'Access,
                            Retry_Procedure'Access );

  Fail_Soft.Fail_Soft_Call( Cause_Invisible_Exception'Access,     -- I
                            null,
                            Retry_Procedure'Access );

  TCTouch.Assert( Fail_Soft.Event_Stack_Size = 0, "Empty stack");

  TCTouch.Validate( "SVI", "Non error case check" );

  -- second, check that error cases add to the stack
  Raise_Error := True;

  Fail_Soft.Fail_Soft_Call( Cause_Standard_Exception'Access );    -- S

  Fail_Soft.Fail_Soft_Call( Cause_Visible_Exception'Access,       -- V
                            Action_On_Exception'Access,           -- A
                            Retry_Procedure'Access );             -- RA

  Fail_Soft.Fail_Soft_Call( Cause_Invisible_Exception'Access,     -- I
                            null,
                            Retry_Procedure'Access );             -- RA

  TCTouch.Assert( Fail_Soft.Event_Stack_Size = 3, "Stack = 3");

  TCTouch.Validate( "SVARAIRA", "Error case check" );

  -- check that the exceptions and procedure were stored correctly
  -- on the stack
  Raise_Error := False;

  -- return procedure pointer from top of stack and call the procedure
  -- through that pointer:

  Fail_Soft.Top_Event_Procedure.all;

  TCTouch.Validate( "I", "Invisible case unwind" );

  begin
    Ada.Exceptions.Raise_Exception( 
      Ada.Exceptions.Exception_Identity(Fail_Soft.Top_Event_Exception) );
    Report.Failed("1: Exception not raised");
  exception
    when Constraint_Error  => Report.Failed("1: Raised Constraint_Error");
    when Visible_Exception => Report.Failed("1: Raised Visible_Exception");
    when others            => null; -- expected case
  end;

  Fail_Soft.Pop_Event;

  -- return procedure pointer from top of stack and call the procedure
  -- through that pointer:

  Fail_Soft.Top_Event_Procedure.all;

  TCTouch.Validate( "V", "Visible case unwind" );

  begin
    Ada.Exceptions.Raise_Exception( 
      Ada.Exceptions.Exception_Identity(Fail_Soft.Top_Event_Exception) );
    Report.Failed("2: Exception not raised");
  exception
    when Constraint_Error  => Report.Failed("2: Raised Constraint_Error");
    when Visible_Exception => null; -- expected case
    when others            => Report.Failed("2: Raised Invisible_Exception");
  end;

  Fail_Soft.Pop_Event;

  Fail_Soft.Top_Event_Procedure.all;

  TCTouch.Validate( "S", "Standard case unwind" );

  begin
    Ada.Exceptions.Raise_Exception( 
      Ada.Exceptions.Exception_Identity(Fail_Soft.Top_Event_Exception) );
    Report.Failed("3: Exception not raised");
  exception
    when Constraint_Error  => null; -- expected case
    when Visible_Exception => Report.Failed("3: Raised Visible_Exception");
    when others            => Report.Failed("3: Raised Invisible_Exception");
  end;

  Fail_Soft.Pop_Event;

  TCTouch.Assert( Fail_Soft.Event_Stack_Size = 0, "Stack empty after pops");

  Report.Result;

end CB40005;