aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats-4/tests/c3/c3a0029.a
blob: 6e9f8000856070c37d37b332ad4ebb8ab4e9730b (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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
-- C3A0029.A
--
--                            Grant of Unlimited Rights
--
--    AdaCore holds unlimited rights in the software and documentation
--    contained herein. Unlimited rights are the same as those granted
--    by the U.S. Government for older parts of the Ada Conformity
--    Assessment Test Suite, and are defined in DFAR 252.227-7013(a)(19).
--    By making this public release, AdaCore intends to confer upon all
--    recipients unlimited rights equal to those held by the Ada Conformity
--    Assessment Authority. 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. ADACORE 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.
--
--                                    Notice
--
--    The ACAA has created and maintains the Ada Conformity Assessment Test
--    Suite for the purpose of conformity assessments conducted in accordance
--    with the International Standard ISO/IEC 18009 - Ada: Conformity
--    assessment of a language processor. This test suite should not be used
--    to make claims of conformance unless used in accordance with
--    ISO/IEC 18009 and any applicable ACAA procedures.
--
--    This test is based on one submitted by AdaCore; AdaCore retains the
--    copyright on the test.
--*
--  OBJECTIVE:
--    Check that Constraint_Error is raised when a null access value is
--    converted to a null excluding subtype of a named access-to-subprogram
--    type. Case B: null_exclusion given at point of use of the access-
--    to-normal-subprogram type.
--
--    Check that a null_exclusion can be given in a subtype_indication,
--    discriminant_specification, parameter_specification, function result
--    subtype, and formal_object_declaration if the subtype_mark is a
--    access-to-subprogram subtype that does not exclude null.
--
--    Check that Constraint_Error is raised when a null access value is
--    converted to a null excluding anonynous access-to-protected-subprogram
--    type.
--
--  CHANGE HISTORY:
--    02 May 2008 RLB Created ACATS test based on C3A0027.
--    18 Jul 2008 RLB Fixed return statement error.
--!
with Report;
procedure C3A0029 is

   protected PO is
      function Double (X : Float) return Float;
      function Triple (X : Float) return Float;
   end PO;

   protected body PO is
      function Double (X : Float) return Float is
      begin
         return 2.0 * X;
      end Double;

      function Triple (X : Float) return Float is
      begin
         return 3.0 * X;
      end ;
   end PO;

   type Acc_Func_Ptr is access protected function (X : Float) return Float;

   function Null_Init return Acc_Func_Ptr is
   begin
      return null;
   end Null_Init;

   function Double_Init return Acc_Func_Ptr is
   begin
      return PO.Double'Access;
   end Double_Init;

   procedure Use_It (Obj : Acc_Func_Ptr) is
      -- Use Obj so that the compiler cannot remove the actual from
      -- the program as a dead object.
   begin
      if Obj = null then
         null;
      elsif Report.Equal (Integer(Obj(2.0)), 0) then
         Report.Comment ("Don't optimize Obj");
      end if;
   end Use_It;

begin
   Report.Test ("C3A0029", "Check that Constraint_Error is raised when a null " &
                           "access value is converted to a null excluding " &
                           "subtype of a named access-to-protected-subprogram type");

   --  Object declaration tests --------------------------------------------

   begin
      declare
         O_1 : not null Acc_Func_Ptr;    -- Test (default initialization).
      begin
         Report.Failed ("O_1: Wrong value");
         Use_It (O_1);
      end;
   exception
      when Constraint_Error =>
         null;
   end;

   begin
      declare
         O_2 : not null Acc_Func_Ptr := Null_Init;      -- Test (initialization).
      begin
         Report.Failed ("O_2: Wrong value");
         Use_It (O_2);
      end;
   exception
      when Constraint_Error =>
         null;
   end;

   declare
      O_3 : not null Acc_Func_Ptr := Double_Init;
   begin
      O_3 := Null_Init;                            -- Test (assignment).
      Report.Failed ("O_3: Wrong value");
      Use_It (O_3);
   exception
      when Constraint_Error =>
         null;
   end;

   declare
      O_4 : not null Acc_Func_Ptr := Double_Init;
   begin
      if O_4 (5.0) /= 10.0 then
         Report.Failed ("Wrong result from O_4");
      end if;
   end;


   --  Array component and aggregate tests ------------------------------

   declare
      type Table is array (Integer range <>) of not null Acc_Func_Ptr;
      T      : Table (1 .. 2) := (PO.Double'Access, PO.Triple'Access);
      Result : Float;
   begin
      Result := T(1) (3.0);
      if Result /= 6.0 then
         Report.Failed ("Wrong value at T(1)");
      end if;

      Result := T(2) (3.0);
      if Result /= 9.0 then
         Report.Failed ("Wrong value at T(2)");
      end if;

      begin
         T(1) := Null_Init;  -- Test (component assignment)
         Report.Failed ("Missing null-exclusion check (AO_1)");
         Use_It (T(1));
      exception
         when Constraint_Error =>
            null;
      end;

      begin
         declare
            AO_2 : Table (1 .. 2) := (Double_Init, Null_Init); -- Test (aggregate)
         begin
            Report.Failed ("Missing null-exclusion check (AO_2)");
            Use_It (AO_2(2));
         end;
      exception
         when Constraint_Error =>
            null;
      end;

      begin
         declare
            AO_3 : Table (1 .. 2); -- Test (default initialization)
         begin
            Report.Failed ("Missing null-exclusion check (AO_3)");
            Use_It (AO_3(2));
         end;
      exception
         when Constraint_Error =>
            null;
      end;
   end;


   --  Record component and aggregate tests -----------------------------

   begin
      declare
         type Rec is record
            C : not null Acc_Func_Ptr;
         end record;
         RO_1 : Rec;       -- Test (implicit record component default initialization).
      begin
         Report.Failed ("RO_1: Wrong value");
         Use_It (RO_1.C);
      end;
   exception
      when Constraint_Error =>
         null;
   end;

   begin
      declare
         type Rec is record
            C : not null Acc_Func_Ptr;
         end record;
         RO_2 : Rec := (C => Null_Init);   -- Test (record aggregate).
      begin
         Report.Failed ("RO_2: Wrong value");
         Use_It (RO_2.C);
      end;
   exception
      when Constraint_Error =>
         null;
   end;

   begin
      declare
         type Rec is record
            C : not null Acc_Func_Ptr := Null_Init;
         end record;
         RO_3 : Rec;       -- Test (explicit record component default initialization).
      begin
         Report.Failed ("RO_3: Wrong value");
         Use_It (RO_3.C);
      end;
   exception
      when Constraint_Error =>
         null;
   end;

   begin
      declare
         type Rec is record
            C : not null Acc_Func_Ptr := Null_Init;
         end record;
         RO_4 : Rec := (C => Double_Init); -- Default expression not evaluated.
      begin
         if RO_4.C (4.5) /= 9.0 then
            Report.Failed ("Wrong value at RO_4.C");
         end if;
         RO_4.C := Null_Init;           -- Test (record component assignment).
         Report.Failed ("RO_4: Wrong value");
         Use_It (RO_4.C);
      exception
         when Constraint_Error =>
            null;
      end;
   exception
      when Constraint_Error =>
         Report.Failed ("RO_4: Constraint_Error raised");
   end;

   declare
      type Rec is record
         C : not null Acc_Func_Ptr := Double_Init;
      end record;
      RO_5 : Rec;
   begin
      RO_5.C := Null_Init;           -- Test (record component assignment).
      Report.Failed ("RO_5: Wrong value");
      Use_It (RO_5.C);
   exception
      when Constraint_Error =>
         null;
   end;


   --  Discriminant tests -----------------------------------------------

   begin
      declare
         type Rec (D : not null Acc_Func_Ptr) is
            record
               Dummy : Integer := 0;
            end record;
         RO_6 : Rec (Null_Init);        -- Test (discriminant constraint).
      begin
         RO_6.Dummy := 0;
         Report.Failed ("RO_6: Wrong value");
         Use_It (RO_6.D);
      end;
   exception
      when Constraint_Error =>
         null;
   end;

   begin
      declare
         type Rec (D : not null Acc_Func_Ptr := Null_Init) is
            limited record
               Dummy : Integer := 0;
            end record;
         RO_7 : Rec;           -- Test (defaulted discriminant).
      begin
         RO_7.Dummy := 0;
         Report.Failed ("RO_7: Wrong value");
         Use_It (RO_7.D);
      end;
   exception
      when Constraint_Error =>
         null;
   end;

   declare
      type Rec (D : not null Acc_Func_Ptr := Null_Init) is
         limited record
            Dummy : Integer := 0;
         end record;
      RO_8 : Rec (Double_Init);
   begin
      if RO_8.D (1.5) /= 3.0 then
         Report.Failed ("Wrong value at RO_8.D");
      end if;
   end;


   --  Parameter tests --------------------------------------------------

   declare
      procedure Proc_1 (F : not null Acc_Func_Ptr) is
      begin
         Use_It (F);
      end Proc_1;
   begin
      begin
         Proc_1 (Null_Init);
         Report.Failed ("Proc_1: Wrong value");
      exception
         when Constraint_Error =>
            null;
      end;
   end;

   begin
      declare
         procedure Proc_2 (F :
            not null Acc_Func_Ptr := Null_Init) is
         begin
            Use_It (F);
         end Proc_2;
      begin
         Proc_2;
         Report.Failed ("Proc_2: Wrong value");
      end;
   exception
      when Constraint_Error =>
         null;
   end;

   declare
      function Evaluate
        (Fn : not null Acc_Func_Ptr; X  : Float) return Float is
      begin
         return Fn (X);
      end Evaluate;

      Result : Float;
   begin
      Result := Evaluate (Double_Init, 3.0);
      if Result /= 6.0 then
         Report.Failed ("Wrong value in called function");
      end if;

      begin
         Result := Evaluate (Null_Init, 3.0);
         Report.Failed ("Missing null-exclusion check - function");
         if Result /= 6.0 then
            Report.Comment ("Don't optimize Result");
         end if;
      exception
         when Constraint_Error =>
            null;
      end;
   end;


   --  Result subtype tests ---------------------------------------------

   declare
      function Func_1 (F : Acc_Func_Ptr) return not null Acc_Func_Ptr is
      begin
         return F;
      end Func_1;
      Obj : Acc_Func_Ptr;
   begin
      begin
         Obj := Func_1 (Null_Init);
         Report.Failed ("Func_1: Wrong value");
         Use_It (Obj);
      exception
         when Constraint_Error =>
            null;
      end;
      Obj := Func_1 (Double_Init);
      if Obj(4.0) /= 8.0 then
         Report.Failed ("Func_1_OK: Wrong value");
      end if;
   end;

   declare
      function Func_2 (F : Acc_Func_Ptr) return not null Acc_Func_Ptr is
      begin
         return Result : not null Acc_Func_Ptr := PO.Double'Access do
            Result := F;
         end return;
      end Func_2;
      Obj : Acc_Func_Ptr;
   begin
      begin
         Obj := Func_2 (Null_Init);
         Report.Failed ("Func_2: Wrong value");
         Use_It (Obj);
      exception
         when Constraint_Error =>
            null;
      end;
      Obj := Func_2 (Double_Init);
      if Obj(7.0) /= 14.0 then
         Report.Failed ("Func_2_OK: Wrong value");
      end if;
   end;


   --  Formal object tests ----------------------------------------------

   declare
      generic
         F : not null Acc_Func_Ptr := Null_Init;
      package GPack is
         procedure User;
      end GPack;

      package body GPack is
         procedure User is
         begin
            Use_It (F);
         end User;
      end GPack;
   begin
      begin
         declare
            package Inst_1 is new GPack; -- Object default.
         begin
            Report.Failed ("Inst_1: Wrong value");
            Inst_1.User;
         end;
      exception
         when Constraint_Error =>
            null;
      end;
      begin
         declare
            package Inst_2 is new GPack (Null_Init);
         begin
            Report.Failed ("Inst_2: Wrong value");
            Inst_2.User;
         end;
      exception
         when Constraint_Error =>
            null;
      end;
      begin
         declare
            package Inst_3 is new GPack (Double_Init);
         begin
            Inst_3.User;
         end;
      exception
         when Constraint_Error =>
            Report.Failed ("Inst_3: Constraint_Error raised");
      end;
   end;

   Report.Result;
end C3A0029;