aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats/tests/cb/cb20003.a
blob: daaf9ffe5c5d31a98ee857652fee5b107a081f86 (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
-- CB20003.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 can be raised, reraised, and handled in an
--      accessed subprogram.
--
--
-- TEST DESCRIPTION:
--      Declare a record type, with one component being an access to
--      subprogram type.  Various subprograms are defined to fit the profile 
--      of this access type, such that the record component can refer to 
--      any of the subprograms.  
--
--      Each of the subprograms raises a different exception, based on the
--      value of an input parameter.  Exceptions are 1) raised, handled with
--      an others handler, reraised and propagated to main to be handled in
--      a specific handler; 2) raised, handled in a specific handler, reraised 
--      and propagated to the main to be handled in an others handler there,
--      and 3) raised and propagated directly to the caller by the subprogram.
--
--      Boolean variables are set throughout the test to ensure that correct
--      exception processing has occurred, and these variables are verified at
--      the conclusion of the test.
--
--       
-- CHANGE HISTORY:
--      06 Dec 94   SAIC    ACVC 2.0
--
--!

package CB20003_0 is                          -- package Push_Buttons 


   Non_Default_Priority,
   Non_Alert_Priority,
   Non_Emergency_Priority : exception;

   Handled_With_Others,
   Reraised_In_Subprogram,
   Handled_In_Caller      : Boolean := False;

   subtype Priority_Type is Integer range 1 .. 10;

   Default_Priority   : Priority_Type := 1;
   Alert_Priority     : Priority_Type := 3;
   Emergency_Priority : Priority_Type := 5;


   type Button is tagged private;                  -- Private tagged type.

   type Button_Response_Ptr is access procedure (P : in     Priority_Type;
                                                 B : in out Button);


   -- Procedures accessible with Button_Response_Ptr type.

   procedure Default_Response   (P : in     Priority_Type;
                                 B : in out Button);

   procedure Alert_Response     (P : in     Priority_Type;
                                 B : in out Button);

   procedure Emergency_Response (P : in     Priority_Type;
                                 B : in out Button);



   procedure Push (B : in out Button;
                   P : in     Priority_Type);

   procedure Set_Response (B : in out Button;       
                           R : in     Button_Response_Ptr);

private

   type Button is tagged                   
      record
         Priority :  Priority_Type       := Default_Priority;
         Response :  Button_Response_Ptr := Default_Response'Access;   
      end record;


end CB20003_0;                                -- package Push_Buttons


     --=================================================================--


with Report;

package body CB20003_0 is                     -- package Push_Buttons 


   procedure Push (B : in out Button;
                   P : in     Priority_Type) is
   begin                                  -- Invoking subprogram designated 
      B.Response (P, B);                  -- by access value.
   end Push;


   procedure Set_Response (B : in out Button;
                           R : in     Button_Response_Ptr) is
   begin
      B.Response := R;      -- Set procedure value in record
   end Set_Response;


   procedure Default_Response (P : in     Priority_Type;
                               B : in out Button) is
   begin
      if (P > Default_Priority) then
         raise Non_Default_Priority;
         Report.Failed ("Exception not raised in procedure body");
      else
         B.Priority := P;
      end if;
   exception
      when others =>                    -- Catch exception with others handler
         Handled_With_Others := True;   -- Successfully caught with "others"
         raise;
         Report.Failed ("Exception not reraised in handler");
   end Default_Response;



   procedure Alert_Response (P : in     Priority_Type;
                             B : in out Button) is
   begin
      if (P > Alert_Priority) then
         raise Non_Alert_Priority;
         Report.Failed ("Exception not raised in procedure body");
      else
         B.Priority := P;
      end if;
   exception
      when Non_Alert_Priority =>
         Reraised_In_Subprogram := True;
         raise;                                  -- Propagate to caller.
         Report.Failed ("Exception not reraised in procedure excpt handler");
      when others =>
         Report.Failed ("Incorrect exception raised/handled");
   end Alert_Response;



   procedure Emergency_Response (P : in     Priority_type;
                                 B : in out Button) is
   begin
      if (P > Emergency_Priority) then
         raise Non_Emergency_Priority;
         Report.Failed ("Exception not raised in procedure body");
      else
         B.Priority := P;
      end if;
      -- No exception handler here, exception will be propagated to caller.
   end Emergency_Response;


end CB20003_0;                                -- package Push_Buttons


     --=================================================================--


with Report;
with CB20003_0;                               -- package Push_Buttons

procedure CB20003 is
      
   package Push_Buttons renames CB20003_0;

   Console_Button : Push_Buttons.Button;

begin

   Report.Test ("CB20003", "Check that exceptions can be raised, "  &
                           "reraised, and handled in a subprogram " &
                           "referenced by an access to subprogram value");


   Default_Response_Processing:                 -- The exception
                                                -- Handled_With_Others is to 
                                                -- be caught with an others
                                                -- handler in Default_Resp.,
                                                -- reraised, and handled with
                                                -- a specific handler here.
   begin
   
      Push_Buttons.Push (Console_Button,        -- Raise exception that will
                         Report.Ident_Int(2));  -- be handled in procedure.
   exception
      when Push_Buttons.Non_Default_Priority =>
         if not Push_Buttons.Handled_With_Others then   -- Not reraised in 
                                                        -- procedure.
            Report.Failed 
              ("Exception not handled/reraised in procedure");
         end if;
      when others =>
         Report.Failed ("Exception handled in " &
                        " Default_Response_Processing block");
   end Default_Response_Processing;



   Alert_Response_Processing:
   begin
   
      Push_Buttons.Set_Response (Console_Button,
                                 Push_Buttons.Alert_Response'access);

      Push_Buttons.Push (Console_Button,        -- Raise exception that will
                         Report.Ident_Int(4));  -- be handled in procedure, 
                                                -- reraised, and propagated
                                                -- to caller.
      Report.Failed ("Exception not propagated to caller " &
                     "in Alert_Response_Processing block");

   exception                                    
      when Push_Buttons.Non_Alert_Priority =>
         if not Push_Buttons.Reraised_In_Subprogram then  -- Not reraised in 
                                                          -- procedure.
            Report.Failed ("Exception not reraised in procedure");
         end if;
      when others =>
         Report.Failed ("Exception handled in " &
                        " Alert_Response_Processing block");
   end Alert_Response_Processing;



   Emergency_Response_Processing:
   begin
   
      Push_Buttons.Set_Response (Console_Button,
                                 Push_Buttons.Emergency_Response'access);

      Push_Buttons.Push (Console_Button,        -- Raise exception that will
                         Report.Ident_Int(6));  -- be propagated directly to
                                                -- caller.
      Report.Failed ("Exception not propagated to caller " &
                     "in Emergency_Response_Processing block");

   exception                                   
      when Push_Buttons.Non_Emergency_Priority =>
         Push_Buttons.Handled_In_Caller := True;
      when others =>
         Report.Failed ("Exception handled in " &
                        " Emergency_Response_Processing block");
   end Emergency_Response_Processing;



   if not (Push_Buttons.Handled_With_Others and
           Push_Buttons.Reraised_In_Subprogram and
           Push_Buttons.Handled_In_Caller ) 
   then
      Report.Failed ("Incorrect exception handling in referenced subprograms");
   end if;


   Report.Result;

end CB20003;