summaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats/tests/ca/ca11d02.a
blob: 7b4f48869b25f48ffe45fb927ed2e516d217733e (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
-- CA11D02.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 an exception declared in a package can be raised by a 
--      child of a child package.  Check that it can be renamed in the 
--      child of the child package and raised with the correct effect.
--
-- TEST DESCRIPTION:
--      Declare a package which defines complex number abstraction with
--      user-defined exceptions (foundation code).
--
--      Add a public child package to the above package. Declare two 
--      subprograms for the parent type.  
--
--      Add a public grandchild package to the foundation package.  Declare
--      subprograms to raise exceptions.
--
--      In the main program, "with" the grandchild package, then check that
--      the exceptions are raised and handled as expected.  Ensure that
--      exceptions are:
--         1) raised in the public grandchild package and handled/reraised to
--            be handled by the main program.
--         2) raised and handled locally by the "others" handler in the 
--            public grandchild package.
--         3) raised in the public grandchild and propagated to the main 
--            program.
--
-- TEST FILES:
--      This test depends on the following foundation code:
--
--         FA11D00.A
--
--
-- CHANGE HISTORY:
--      06 Dec 94   SAIC    ACVC 2.0
--
--!

-- Child package of FA11D00.

package FA11D00.CA11D02_0 is     -- Basic_Complex

   function "+" (Left, Right : Complex_Type) 
     return Complex_Type;                   -- Add two complex numbers.

   function "*" (Left, Right : Complex_Type) 
     return Complex_Type;                   -- Multiply two complex numbers.

end FA11D00.CA11D02_0;     -- Basic_Complex

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

package body FA11D00.CA11D02_0 is     -- Basic_Complex

   function "+" (Left, Right : Complex_Type) return Complex_Type is
   begin
      return ( (Left.Real + Right.Real, Left.Imag + Right.Imag) );
   end "+";
   --------------------------------------------------------------
   function "*" (Left, Right : Complex_Type) return Complex_Type is
   begin
      return ( Real => (Left.Real * Right.Real),
               Imag => (Left.Imag * Right.Imag) );
   end "*";

end FA11D00.CA11D02_0;     -- Basic_Complex

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

-- Child package of FA11D00.CA11D02_0.
-- Grandchild package of FA11D00.

package FA11D00.CA11D02_0.CA11D02_1 is     -- Array_Complex

   Inverse_Error : exception renames Divide_Error;   -- Reference to exception 
                                                     -- in grandparent package.
   Array_Size    : constant := 2;

   type Complex_Array_Type is                        
      array (1 .. Array_Size) of Complex_Type;       -- Reference to type
                                                     -- in parent package.

   function Multiply (Left  : Complex_Array_Type;    -- Multiply two complex
                      Right : Complex_Array_Type)    -- arrays.
     return Complex_Array_Type;

   function Add (Left, Right : Complex_Array_Type)   -- Add two complex
     return Complex_Array_Type;                      -- arrays.

   procedure Inverse (Right : in     Complex_Array_Type;  -- Invert a complex
                      Left  : in out Complex_Array_Type); -- array.
 
end FA11D00.CA11D02_0.CA11D02_1;     -- Array_Complex

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

with Report;


package body FA11D00.CA11D02_0.CA11D02_1 is     -- Array_Complex

   function Multiply (Left  : Complex_Array_Type;
                      Right : Complex_Array_Type)
     return Complex_Array_Type is

   -- This procedure will raise an exception depending on the input
   -- parameter.  The exception will be handled locally by the
   -- "others" handler.

      Result : Complex_Array_Type := (others => Zero);

      subtype Vector_Size is Positive range Left'Range;

   begin  
      if Left = Result or else Right = Result then -- Do not multiply zero.
         raise Multiply_Error;                     -- Refence to exception in
                                                   -- grandparent package.
         Report.Failed ("Program control not transferred by raise");
      else
         for I in Vector_Size loop
           Result(I) := ( Left(I) * Right(I) );    -- Basic_Complex."*".
         end loop;
      end if;
      return (Result);
   
   exception
      when others =>
         Report.Comment ("Exception is handled by others in Multiplication");
         TC_Handled_In_Grandchild_Pkg_Func := true;
         return (Zero, Zero);
      
   end Multiply;
   --------------------------------------------------------------
   function Add (Left, Right : Complex_Array_Type)
     return Complex_Array_Type is

   -- This function will raise an exception depending on the input
   -- parameter.  The exception will be propagated and handled
   -- by the caller.

      Result : Complex_Array_Type := (others => Zero);

      subtype Vector_Size is Positive range Left'Range;

   begin  
      if Left = Result or Right = Result then     -- Do not add zero.
         raise Add_Error;                         -- Refence to exception in
                                                  -- grandparent package.
         Report.Failed ("Program control not transferred by raise");
      else
         for I in Vector_Size loop                   
           Result(I) := ( Left(I) + Right(I) );   -- Basic_Complex."+".
         end loop;
      end if;
      return (Result);
 
   end Add;
   --------------------------------------------------------------
   procedure Inverse (Right : in     Complex_Array_Type;
                      Left  : in out Complex_Array_Type) is

   -- This function will raise an exception depending on the input
   -- parameter.  The exception will be handled/reraised to be
   -- handled by the caller.

      Result : Complex_Array_Type := (others => Zero);

      Array_With_Zero : boolean := false;

   begin
      for I in 1 .. Right'Length loop
        if Right(I) = Zero then      -- Check for zero.
          Array_With_Zero := true;
        end if;
      end loop;

      If Array_With_Zero then
         raise Inverse_Error;      -- Do not inverse zero.
         Report.Failed ("Program control not transferred by raise");
      else
         for I in 1 .. Array_Size loop
           Left(I).Real := - Right(I).Real;
           Left(I).Imag := - Right(I).Imag;
        end loop;
      end if;

   exception
      when Inverse_Error  => 
         TC_Handled_In_Grandchild_Pkg_Proc := true;
         Left := Result;
         raise;     -- Reraise the Inverse_Error exception in the subtest.
         Report.Failed ("Exception not reraised in handler");

      when others => 
         Report.Failed ("Unexpected exception in procedure Inverse");
   end Inverse;

end FA11D00.CA11D02_0.CA11D02_1;     -- Array_Complex

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

with FA11D00.CA11D02_0.CA11D02_1;    -- Array_Complex,
                                     -- implicitly with Basic_Complex.
with Report;

procedure CA11D02 is

   package Complex_Pkg renames FA11D00;
   package Array_Complex_Pkg renames FA11D00.CA11D02_0.CA11D02_1;

   use Complex_Pkg;                            
   use Array_Complex_Pkg;                      

begin

   Report.Test ("CA11D02", "Check that an exception declared in a package " &
                "can be raised by a child of a child package");

   Multiply_Complex_Subtest:
   declare
      Operand_1  : Complex_Array_Type 
                 := ( Complex (Int_Type (Report.Ident_Int (3)), 
                      Int_Type (Report.Ident_Int (5))),
                      Complex (Int_Type (Report.Ident_Int (2)), 
                      Int_Type (Report.Ident_Int (8))) );
      Operand_2  : Complex_Array_Type 
                 := ( Complex (Int_Type (Report.Ident_Int (1)), 
                      Int_Type (Report.Ident_Int (2))),
                      Complex (Int_Type (Report.Ident_Int (3)), 
                      Int_Type (Report.Ident_Int (6))) );
      Operand_3  : Complex_Array_Type := ( Zero, Zero);
      Mul_Result : Complex_Array_Type 
                 := ( Complex (Int_Type (Report.Ident_Int (3)), 
                      Int_Type (Report.Ident_Int (10))),
                      Complex (Int_Type (Report.Ident_Int (6)), 
                      Int_Type (Report.Ident_Int (48))) );
      Complex_No : Complex_Array_Type := (others => Zero);

   begin
      If (Multiply (Operand_1, Operand_2) /= Mul_Result) then
         Report.Failed ("Incorrect results from multiplication");
      end if;

      -- Error is raised and exception will be handled in grandchild package.

      Complex_No := Multiply (Operand_1, Operand_3);

      if Complex_No /= (Zero, Zero) then
         Report.Failed ("Exception was not raised in multiplication");
      end if;

   exception
      when Multiply_Error     =>
         Report.Failed ("Exception raised in multiplication and " &
                        "propagated to caller");
         TC_Handled_In_Grandchild_Pkg_Func := false;  
              -- Improper exception handling in caller.

      when others => 
         Report.Failed ("Unexpected exception in multiplication");
         TC_Handled_In_Grandchild_Pkg_Func := false;  
              -- Improper exception handling in caller.

   end Multiply_Complex_Subtest;


   Add_Complex_Subtest:
   declare
      Operand_1  : Complex_Array_Type 
                 := ( Complex (Int_Type (Report.Ident_Int (2)), 
                      Int_Type (Report.Ident_Int (7))),
                      Complex (Int_Type (Report.Ident_Int (5)), 
                      Int_Type (Report.Ident_Int (8))) );
      Operand_2  : Complex_Array_Type 
                 := ( Complex (Int_Type (Report.Ident_Int (4)), 
                      Int_Type (Report.Ident_Int (1))),
                      Complex (Int_Type (Report.Ident_Int (2)), 
                      Int_Type (Report.Ident_Int (3))) );
      Operand_3  : Complex_Array_Type := ( Zero, Zero);
      Add_Result : Complex_Array_Type 
                 := ( Complex (Int_Type (Report.Ident_Int (6)), 
                      Int_Type (Report.Ident_Int (8))),
                      Complex (Int_Type (Report.Ident_Int (7)), 
                      Int_Type (Report.Ident_Int (11))) );
      Complex_No : Complex_Array_Type := (others => Zero);

   begin
      Complex_No := Add (Operand_1, Operand_2);

      If (Complex_No /= Add_Result) then
         Report.Failed ("Incorrect results from addition");
      end if;

      -- Error is raised in grandchild package and exception 
      -- will be propagated to caller.

      Complex_No := Add (Operand_1, Operand_3);

      if Complex_No = Add_Result then
         Report.Failed ("Exception was not raised in addition");
      end if;

   exception
      when Add_Error => 
         TC_Propagated_To_Caller := true;  -- Exception is propagated.

      when others => 
         Report.Failed ("Unexpected exception in addition subtest");
         TC_Propagated_To_Caller := false;  -- Improper exception handling
                                            -- in caller.
   end Add_Complex_Subtest;

   Inverse_Complex_Subtest:
   declare
      Operand_1  : Complex_Array_Type 
                 := ( Complex (Int_Type (Report.Ident_Int (1)), 
                      Int_Type (Report.Ident_Int (5))),
                      Complex (Int_Type (Report.Ident_Int (3)), 
                      Int_Type (Report.Ident_Int (11))) );
      Operand_3  : Complex_Array_Type 
                 := ( Zero, Complex (Int_Type (Report.Ident_Int (3)), 
                      Int_Type (Report.Ident_Int (6))) );
      Inv_Result : Complex_Array_Type 
                 := ( Complex (Int_Type (Report.Ident_Int (-1)), 
                      Int_Type (Report.Ident_Int (-5))),
                      Complex (Int_Type (Report.Ident_Int (-3)), 
                      Int_Type (Report.Ident_Int (-11))) );
      Complex_No : Complex_Array_Type := (others => Zero);

   begin
      Inverse (Operand_1, Complex_No);

      if (Complex_No /= Inv_Result) then
         Report.Failed ("Incorrect results from inverse");
      end if;

      -- Error is raised in grandchild package and exception 
      -- will be handled/reraised to caller.

      Inverse (Operand_3, Complex_No);

      Report.Failed ("Exception was not handled in inverse");

   exception
      when Inverse_Error => 
         if not TC_Handled_In_Grandchild_Pkg_Proc then
            Report.Failed ("Exception was not raised in inverse");
         else
            TC_Handled_In_Caller := true;  -- Exception is reraised from
                                           -- child package.
         end if;

      when others => 
         Report.Failed ("Unexpected exception in inverse");
         TC_Handled_In_Caller := false; 
                -- Improper exception handling in caller.

   end Inverse_Complex_Subtest;

   if not (TC_Handled_In_Caller               and   -- Check to see that all 
           TC_Handled_In_Grandchild_Pkg_Proc  and   -- exceptions were handled
           TC_Handled_In_Grandchild_Pkg_Func  and   -- in proper location.
           TC_Propagated_To_Caller)        
   then
      Report.Failed ("Exceptions handled in incorrect locations");
   end if;

   Report.Result;

end CA11D02;