summaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats/tests/cb/cb20004.a
blob: 42c0d7672544db6281e836dbd432b56d6d46fa6e (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
-- CB20004.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 propagate correctly from objects of 
--      protected types. Check propagation from protected entry bodies.
--
-- TEST DESCRIPTION:
--      Declare a package with a protected type, including entries and private
--      data, simulating a bounded buffer abstraction.  In the main procedure,
--      perform entry calls on an object of the protected type that raises
--      exceptions.
--      Ensure that the exceptions are: 
--         1) raised and handled locally in the entry body
--         2) raised in the entry body and handled/reraised to be handled 
--            by the caller.
--         3) raised in the entry body and propagated directly to the calling 
--            procedure. 
--
--       
-- CHANGE HISTORY:
--      06 Dec 94   SAIC    ACVC 2.0
--
--!

package CB20004_0 is               -- Package Buffer.

   Max_Buffer_Size       : constant := 2;

   Handled_In_Body,
   Propagated_To_Caller,
   Handled_In_Caller     : Boolean := False;

   Data_Over_5,
   Data_Degradation      : exception;

   type Data_Item is range 0 .. 100;

   type Item_Array_Type is array (1 .. Max_Buffer_Size) of Data_Item;

   protected type Bounded_Buffer is
      entry Put (Item : in     Data_Item);
      entry Get (Item :    out Data_Item);
   private
      Item_Array : Item_Array_Type;
      I, J       : Integer range 1 .. Max_Buffer_Size := 1;
      Count      : Integer range 0 .. Max_Buffer_Size := 0;
   end Bounded_Buffer;

end CB20004_0;

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

with Report;

package body CB20004_0 is               -- Package Buffer.

   protected body Bounded_Buffer is

      entry Put (Item : in Data_Item) when Count < Max_Buffer_Size is
      begin
         if Item > 10 then
            Item_Array (I) := Item * 8;  -- Constraint_Error will be raised
         elsif Item > 5 then             -- and handled in entry body.
            raise Data_Over_5;           -- Exception  handled/reraised in
         else                            -- entry body, propagated to caller.
            Item_Array (I) := Item;      -- Store data item in buffer.
            I := (I mod Max_Buffer_Size) + 1;
            Count := Count + 1;
         end if;
      exception
         when Constraint_Error =>
            Handled_In_Body := True;
         when Data_Over_5 =>
            Propagated_To_Caller := True;
            raise;    -- Propagate the exception to the caller.
      end Put;


      entry Get (Item : out Data_Item) when Count > 0 is
      begin
         Item := Item_Array(J);
         J := (J mod Max_Buffer_Size) + 1;
         Count := Count - 1;
         if Count = 0 then
            raise Data_Degradation;   -- Exception to propagate to caller.
         end if;
      end Get;

   end Bounded_Buffer;

end CB20004_0;


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


with CB20004_0;                   -- Package Buffer.
with Report;

procedure CB20004 is

   package Buffer renames CB20004_0;

   Data        : Buffer.Data_Item := Buffer.Data_Item'First;
   Data_Buffer : Buffer.Bounded_Buffer;   -- an object of protected type.

   Handled_In_Caller : Boolean := False;  -- same name as boolean declared 
                                          -- in package Buffer.
begin

   Report.Test ("CB20004", "Check that exceptions propagate correctly " &
                           "from objects of protected types" );

   Initial_Data_Block:
   begin                                    -- Data causes Constraint_Error.
      Data_Buffer.Put (CB20004_0.Data_Item(Report.Ident_Int(51)));

   exception                                  
      when Constraint_Error => 
         Buffer.Handled_In_Body := False;     -- Improper exception handling
                                              -- in entry body.
         Report.Failed ("Exception propagated to caller " &
                        " from Initial_Data_Block");
      when others =>
         Report.Failed ("Exception raised in processing and " &
                        "propagated to caller from Initial_Data_Block");
   end Initial_Data_Block;


   Data_Entry_Block:
   begin
                                              -- Valid data. No exception.
      Data_Buffer.Put (CB20004_0.Data_Item(Report.Ident_Int(3)));  

                                              -- Data will cause exception.
      Data_Buffer.Put (7);                    -- Call protected object entry, 
                                              -- exception to be handled/
                                              -- reraised in entry body.
      Report.Failed ("Data_Over_5 Exception not raised in processing");
   exception
      when Buffer.Data_Over_5 =>
         if Buffer.Propagated_To_Caller then   -- Reraised in entry body?
            Buffer.Handled_In_Caller := True;
         else
            Report.Failed ("Exception not reraised in entry body");
         end if;
      when others =>
         Report.Failed ("Exception raised in processing and propagated " &
                        "to caller from Data_Entry_Block");
   end Data_Entry_Block;


   Data_Retrieval_Block:
   begin

      Data_Buffer.Get (Data);  -- Retrieval of buffer data, buffer now empty.
                               -- Exception will be raised in entry body, with
                               -- propagation to caller.
      Report.Failed ("Data_Degradation Exception not raised in processing");
   exception
      when Buffer.Data_Degradation =>
         Handled_In_Caller := True;   -- Local Boolean used here.
      when others =>
         Report.Failed ("Exception raised in processing and propagated " &
                        "to caller from Data_Retrieval_Block");
   end Data_Retrieval_Block;


   if not (Buffer.Handled_In_Body      and    -- Validate proper exception
           Buffer.Propagated_To_Caller and    -- handling in entry bodies.
           Buffer.Handled_In_Caller    and
           Handled_In_Caller)
   then
      Report.Failed ("Improper exception handling by entry bodies");
   end if;


   Report.Result;

end CB20004;