aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/testsuite/ada/acats/tests/cc/cc50a01.a
blob: 4d5dfdfd50da9eddf1524aafe4f63e09a4ed84c5 (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
-- CC50A01.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 a formal parameter of a library-level generic unit may be
--      a formal tagged private type. Check that a nonlimited tagged type may
--      be passed as an actual. Check that if the formal type is indefinite,
--      both indefinite and definite types may be passed as actuals.
--
-- TEST DESCRIPTION:
--      The generic package declares a formal tagged private type (this can
--      be considered the parent "mixin" class). This type is extended in
--      the generic to provide support for stacks of items of any nonlimited
--      tagged type. Stacks are modeled as singly linked lists, with the list
--      nodes being objects of the extended type.
--
--      A generic testing procedure pushes items onto a stack, and pops them
--      back off, verifying the state of the stack at various points along the
--      way. The push and pop routines exercise functionality important to
--      tagged types, such as type conversion toward the root of the derivation
--      class and extension aggregates.
--
--      The formal tagged private type has an unknown discriminant part, and
--      is thus indefinite. This allows both definite and indefinite types
--      to be passed as actuals. For tagged types, definite implies
--      nondiscriminated, and indefinite implies discriminated (with known
--      or unknown discriminants). 
--
-- TEST FILES:
--      This test consists of the following files:
--
--         FC50A00.A
--      -> CC50A01.A
--
--
-- CHANGE HISTORY:
--      06 Dec 94   SAIC    ACVC 2.0
--      21 Nov 95   SAIC    ACVC 2.0.1 fixes: Moved instantiations of
--                          BC50A01_0 to library level.
--      11 Aug 96   SAIC    ACVC 2.1: Updated prologue. Added pragma
--                          Elaborate to context clauses for CC50A01_2 & _3.
--
--!

with FC50A00;  -- Tagged (actual) type declarations.
generic        -- Generic stack abstraction.

   type Item (<>) is tagged private;            -- Formal tagged private type.
   TC_Default_Value : Item;                     -- Needed in View_Top (see
                                                -- below).
package CC50A01_0 is

   type Stack is private;

-- Note that because the actual type corresponding to Item may be
-- unconstrained, the functions of removing the top item from the stack and
-- returning the value of the top item of the stack have been separated into
-- Pop and View_Top, respectively. This is necessary because otherwise the
-- returned value would have to be an out parameter of Pop, which would
-- require the user (in the unconstrained case) to create an uninitialized
-- unconstrained object to serve as the actual, which is illegal.

   procedure Push     (I : in Item; S : in out Stack);
   procedure Pop      (S : in out Stack);
   function  View_Top (S : Stack) return Item;

   function Size_Of (S : Stack) return Natural;

private

   type Stack_Item;
   type Stack_Ptr is access Stack_Item;

   type Stack_Item is new Item with record      -- Extends formal type.
      Next : Stack_Ptr := null;
   end record;

   type Stack is record
      Top  : Stack_Ptr := null;
      Size : Natural   := 0;
   end record;

end CC50A01_0;


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


package body CC50A01_0 is

   -- Link NewItem in at the top of the stack (the extension aggregate within
   -- the allocator initializes the inherited portion of NewItem to equal I,
   -- and NewItem.Next to point to what S.Top points to).

   procedure Push (I : in Item; S : in out Stack) is
      NewItem : Stack_Ptr;
   begin
      NewItem := new Stack_Item'(I with S.Top);     -- Extension aggregate.
      S.Top   := NewItem;
      S.Size  := S.Size + 1;
   end Push;


   -- Remove item from top of stack. This procedure only updates the state of
   -- the stack; it does not return the value of the popped item. Hence, in
   -- order to accomplish a "true" pop, both View_Top and Pop must be called
   -- consecutively.
   --
   -- If the stack is empty, the Pop is ignored (for simplicity; in a true
   -- application this might be treated as an error condition).

   procedure Pop (S : in out Stack) is
   begin
      if S.Top = null then                          -- Stack is empty.
         null;
         -- Raise exception.
      else
         S.Top  := S.Top.Next;
         S.Size := S.Size - 1;
         -- Deallocate discarded node.
      end if;
   end Pop;


   -- Return the value of the top item on the stack. This procedure only
   -- returns the value; it does not remove the top item from the stack.
   -- Hence, in order to accomplish a "true" pop, both View_Top and Pop must
   -- be called consecutively.
   --
   -- Since items on the stack are of a type (Stack_Item) derived from Item,
   -- which is a (tagged) private type, type conversion toward the root is the
   -- only way to get a value of type Item for return to the caller.
   --
   -- If the stack is empty, View_Top returns a pre-specified default value.
   -- (In a true application, an exception might be raised instead).

   function View_Top (S : Stack) return Item is
   begin
      if S.Top = null then                          -- Stack is empty.
         return TC_Default_Value;                   -- Testing artifice.
         -- Raise exception.
      else
         return Item(S.Top.all);                    -- Type conversion.
      end if;
   end View_Top;


   function Size_Of (S : Stack) return Natural is
   begin
      return (S.Size);
   end Size_Of;


end CC50A01_0;


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


-- The formal package Stacker below is needed to gain access to the
-- appropriate version of the "generic" type Stack. It is provided with an
-- explicit actual part in order to restrict the packages that can be passed
-- as actuals to those which have been instantiated with the same actuals
-- which this generic procedure has been instantiated with.

with CC50A01_0;  -- Generic stack abstraction.
generic
   type Item_Type (<>) is tagged private;       -- Formal tagged private type.
   Default : Item_Type;
   with package Stacker is new CC50A01_0 (Item_Type, Default);
procedure CC50A01_1 (S : in out Stacker.Stack; I : in Item_Type);


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

--
-- This generic procedure performs all of the testing of the
-- stack abstraction.
--

with Report;
procedure CC50A01_1 (S : in out Stacker.Stack; I : in Item_Type) is
begin
   Stacker.Push (I, S);                    -- Push onto empty stack.
   Stacker.Push (I, S);                    -- Push onto nonempty stack.

   if Stacker.Size_Of (S) /= 2 then
      Report.Failed ("   Wrong stack size after 2 Pushes");
   end if;

   -- Calls to View_Top must initialize a declared object of type Item_Type
   -- because the type may be unconstrained.

   declare
      Buffer1 : Item_Type := Stacker.View_Top (S);
   begin
      Stacker.Pop (S);                     -- Pop item off nonempty stack.
      if Buffer1 /= I then
         Report.Failed ("   Wrong stack item value after 1st Pop");
      end if;
   end;

   declare
      Buffer2 : Item_Type := Stacker.View_Top (S);
   begin
      Stacker.Pop (S);                     -- Pop last item off stack.
      if Buffer2 /= I then
         Report.Failed ("   Wrong stack item value after 2nd Pop");
      end if;
   end;

   if Stacker.Size_Of (S) /= 0 then
      Report.Failed ("   Wrong stack size after 2 Pops");
   end if;

   declare
      Buffer3 : Item_Type := Stacker.View_Top (S);
   begin
      if Buffer3 /= Default then
         Report.Failed ("   Wrong result after Pop of empty stack");
      end if;
      Stacker.Pop (S);                     -- Pop off empty stack.
   end;

end CC50A01_1;


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


with FC50A00;

with CC50A01_0;
pragma Elaborate (CC50A01_0);

package CC50A01_2 is new CC50A01_0 (FC50A00.Count_Type,
                                    FC50A00.TC_Default_Count);


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


with FC50A00;

with CC50A01_0;
pragma Elaborate (CC50A01_0);

package CC50A01_3 is new CC50A01_0 (FC50A00.Person_Type,
                                    FC50A00.TC_Default_Person);


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


with FC50A00;   -- Tagged (actual) type declarations.
with CC50A01_0; -- Generic stack abstraction.
with CC50A01_1; -- Generic stack testing procedure.
with CC50A01_2;
with CC50A01_3;

with Report;
procedure CC50A01 is

   package Count_Stacks  renames CC50A01_2;
   package Person_Stacks renames CC50A01_3;


   procedure TC_Count_Test is new CC50A01_1 (FC50A00.Count_Type,
                                             FC50A00.TC_Default_Count,
                                             Count_Stacks);
   Count_Stack : Count_Stacks.Stack;

 
   procedure TC_Person_Test is new CC50A01_1 (FC50A00.Person_Type,
                                              FC50A00.TC_Default_Person,
                                              Person_Stacks);
   Person_Stack : Person_Stacks.Stack;

begin
   Report.Test ("CC50A01", "Check that a formal parameter of a " &
                "library-level generic unit may be a formal tagged " &
                "private type");

   Report.Comment ("Testing definite tagged type..");
   TC_Count_Test  (Count_Stack,  FC50A00.TC_Count_Item);

   Report.Comment ("Testing indefinite tagged type..");
   TC_Person_Test (Person_Stack, FC50A00.TC_Person_Item);

   Report.Result;
end CC50A01;