3 -- Grant of Unlimited Rights
5 -- Under contracts F33600-87-D-0337, F33600-84-D-0280, MDA903-79-C-0687,
6 -- F08630-91-C-0015, and DCA100-97-D-0025, the U.S. Government obtained
7 -- unlimited rights in the software and documentation contained herein.
8 -- Unlimited rights are defined in DFAR 252.227-7013(a)(19). By making
9 -- this public release, the Government intends to confer upon all
10 -- recipients unlimited rights equal to those held by the Government.
11 -- These rights include rights to use, duplicate, release or disclose the
12 -- released technical data and computer software in whole or in part, in
13 -- any manner and for any purpose whatsoever, and to have or permit others
18 -- ALL MATERIALS OR INFORMATION HEREIN RELEASED, MADE AVAILABLE OR
19 -- DISCLOSED ARE AS IS. THE GOVERNMENT MAKES NO EXPRESS OR IMPLIED
20 -- WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING THE CONDITIONS OF THE
21 -- SOFTWARE, DOCUMENTATION OR OTHER INFORMATION RELEASED, MADE AVAILABLE
22 -- OR DISCLOSED, OR THE OWNERSHIP, MERCHANTABILITY, OR FITNESS FOR A
23 -- PARTICULAR PURPOSE OF SAID MATERIAL.
27 -- Check that a child package of a library level instantiation
28 -- of a generic can be the instantiation of a child package of
29 -- the generic. Check that the child instance can use its parent's
30 -- declarations and operations, including a formal type of the parent.
33 -- Declare a generic package which simulates an integer complex
34 -- abstraction. Declare a generic child package of this package
35 -- which defines additional complex operations.
37 -- Instantiate the first generic package, then instantiate the child
38 -- generic package as a child unit of the first instance. In the main
39 -- program, check that the operations in both instances perform as
44 -- 06 Dec 94 SAIC ACVC 2.0
45 -- 21 Dec 94 SAIC Corrected visibility errors for literals
46 -- 27 Feb 97 PWB.CTA Added elaboration pragma at package CA11012_3
49 generic -- Complex number abstraction.
50 type Int_Type
is range <>;
54 -- Simulate a generic complex number support package. Complex numbers
55 -- are treated as coordinates in the Cartesian plane.
57 type Complex_Type
is private;
59 Zero
: constant Complex_Type
; -- Real number (0,0).
61 function Complex
(Real
, Imag
: Int_Type
) -- Create a complex
62 return Complex_Type
; -- number.
64 function "-" (Right
: Complex_Type
) -- Invert a complex
65 return Complex_Type
; -- number.
67 function "+" (Left
, Right
: Complex_Type
) -- Add two complex
68 return Complex_Type
; -- numbers.
71 type Complex_Type
is record
76 Zero
: constant Complex_Type
:= (Real
=> 0, Imag
=> 0);
80 --==================================================================--
82 package body CA11012_0
is
84 function Complex
(Real
, Imag
: Int_Type
) return Complex_Type
is
88 ---------------------------------------------------------------
89 function "-" (Right
: Complex_Type
) return Complex_Type
is
91 return (-Right
.Real
, -Right
.Imag
);
93 ---------------------------------------------------------------
94 function "+" (Left
, Right
: Complex_Type
) return Complex_Type
is
96 return ( (Left
.Real
+ Right
.Real
, Left
.Imag
+ Right
.Imag
) );
101 --==================================================================--
103 -- Generic child of complex number package. Child must be generic since
104 -- parent is generic.
106 generic -- Complex additional operations
108 package CA11012_0
.CA11012_1
is
110 -- More operations on complex number. This child adds a layer of
111 -- functionality to the parent generic.
113 function Real_Part
(Complex_No
: Complex_Type
)
116 function Imag_Part
(Complex_No
: Complex_Type
)
119 function "*" (Factor
: Int_Type
;
120 C
: Complex_Type
) return Complex_Type
;
122 function Vector_Magnitude
(Complex_No
: Complex_Type
)
125 end CA11012_0
.CA11012_1
;
127 --==================================================================--
129 package body CA11012_0
.CA11012_1
is
131 function Real_Part
(Complex_No
: Complex_Type
) return Int_Type
is
133 return (Complex_No
.Real
);
135 ---------------------------------------------------------------
136 function Imag_Part
(Complex_No
: Complex_Type
) return Int_Type
is
138 return (Complex_No
.Imag
);
140 ---------------------------------------------------------------
141 function "*" (Factor
: Int_Type
;
142 C
: Complex_Type
) return Complex_Type
is
143 Result
: Complex_Type
:= Zero
; -- Zero is declared in parent,
146 for I
in 1 .. abs (Factor
) loop
147 Result
:= Result
+ C
; -- Complex_Number "+"
151 Result
:= - Result
; -- Complex_Number "-"
156 ---------------------------------------------------------------
157 function Vector_Magnitude
(Complex_No
: Complex_Type
)
158 return Int_Type
is -- Not a real vector magnitude.
160 return (Complex_No
.Real
+ Complex_No
.Imag
);
161 end Vector_Magnitude
;
163 end CA11012_0
.CA11012_1
;
165 --==================================================================--
169 subtype My_Integer
is integer range -100 .. 100;
171 -- ... Various other types used by the application.
175 -- No body for CA11012_2;
177 --==================================================================--
179 -- Declare instances of the generic complex packages for integer type.
180 -- The instance of the child must itself be declared as a child of the
181 -- instance of the parent.
183 with CA11012_0
; -- Complex number abstraction
184 with CA11012_2
; -- Package containing integer type
185 pragma Elaborate
(CA11012_0
);
186 package CA11012_3
is new CA11012_0
(Int_Type
=> CA11012_2
.My_Integer
);
188 with CA11012_0
.CA11012_1
; -- Complex additional operations
190 package CA11012_3
.CA11012_4
is new CA11012_3
.CA11012_1
;
192 --==================================================================--
194 with CA11012_2
; -- Package containing integer type
195 with CA11012_3
.CA11012_4
; -- Complex abstraction + additional operations
200 package My_Complex_Pkg
renames CA11012_3
;
202 package My_Complex_Operation
renames CA11012_3
.CA11012_4
;
204 use My_Complex_Pkg
, -- All user-defined
205 My_Complex_Operation
; -- operators directly
207 Complex_One
, Complex_Two
: Complex_Type
;
211 Report
.Test
("CA11012", "Check that child instance can use its parent's " &
212 "declarations and operations, including a formal " &
213 "type of the parent");
217 My_Literal
: CA11012_2
.My_Integer
:= -3;
220 Complex_One
:= Complex
(-4, 7); -- Operation from the generic
223 Complex_Two
:= My_Literal
* Complex_One
; -- Operation from the generic
226 if Real_Part
(Complex_Two
) /= 12 -- Operation from the generic
227 or Imag_Part
(Complex_Two
) /= -21 -- child package.
229 Report
.Failed
("Incorrect results from complex operation");
232 end Correct_Range_Test
;
234 ---------------------------------------------------------------
238 My_Vector
: CA11012_2
.My_Integer
;
241 Complex_One
:= Complex
(70, 70); -- Operation from the generic
243 My_Vector
:= Vector_Magnitude
(Complex_One
);
244 -- Operation from the generic child package.
246 Report
.Failed
("Exception not raised in child package");
249 when Constraint_Error
=>
250 Report
.Comment
("Exception is raised as expected");
253 Report
.Failed
("Others exception is raised");
255 end Out_Of_Range_Test
;