aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats-4/tests/c4/c457006.a
blob: ed76985fb3373d97ed1df004462d5c9411a96e2d (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
-- C457006.A
--
--                             Grant of Unlimited Rights
--
--     The Ada Conformity Assessment Authority (ACAA) 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, the ACAA
--     intends to confer upon all recipients unlimited rights equal to those
--     held by the ACAA. 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 ACAA 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.
--*
--
-- OBJECTIVE:
--     Check that overloaded functions can be resolved when they appear as
--     dependent expressions in a conditional expression. Part 1: Enumeration
--     literals.
--
-- TEST DESCRIPTION:
--     We declare three enumeration types with a few literals that appear
--     in more than one type. While these types aren't that realistic
--     themselves (although the list of employees is a subset of the
--     real employees of one Ada vendor, circa 1988), this sort of overloading
--     is common in practice, especially when multiple packages or types
--     appear in use clauses. Similarly, the use of conditional expressions
--     is common enough that we expect pretty much anything that can be written
--     to be written eventually. As such, we think that pretty much anything
--     meets the intent of usage-oriented testing for these features. Thus,
--     we created a rather simple test that just tries to test the objective
--     rather than creating a fancy usage scenario.
--
-- CHANGE HISTORY:
--     04 Dec 15   RLB     Created test.
--!

with Report;
procedure C457006 is

   type Base is (Bin, Oct, Dec, Hex);

   type Month is (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sep, Oct, Nov, Dec);

   type Employee is (Jan, Jen, Jim, Tim, Tom, Mary, Randy, Ike);

   type TC_Limb is (First, Second, Third);

   procedure Check (Value, First_Expected,
                    Second_Expected, Third_Expected : Base;
                    TC_This_Limb : TC_Limb; TC_Subtest : String) is
   begin
      case TC_This_Limb is
         when First =>
            if Value /= First_Expected then
               Report.Failed ("Value of " & Base'Image(Value) &
                              " not expected value of " &
                              Base'Image(First_Expected) &
                              " (" & TC_Subtest & "1)");
            end if;
         when Second =>
            if Value /= Second_Expected then
               Report.Failed ("Value of " & Base'Image(Value) &
                              " not expected value of " &
                              Base'Image(Second_Expected) &
                              " (" & TC_Subtest & "2)");
            end if;
         when Third =>
            if Value /= Third_Expected then
               Report.Failed ("Value of " & Base'Image(Value) &
                              " not expected value of " &
                              Base'Image(Third_Expected) &
                              " (" & TC_Subtest & "3)");
            end if;
      end case;
   end Check;

   procedure Check (Value, First_Expected,
                    Second_Expected, Third_Expected : Month;
                    TC_This_Limb : TC_Limb; TC_Subtest : String) is
   begin
      case TC_This_Limb is
         when First =>
            if Value /= First_Expected then
               Report.Failed ("Value of " & Month'Image(Value) &
                              " not expected value of " &
                              Month'Image(First_Expected) &
                              " (" & TC_Subtest & "1)");
            end if;
         when Second =>
            if Value /= Second_Expected then
               Report.Failed ("Value of " & Month'Image(Value) &
                              " not expected value of " &
                              Month'Image(Second_Expected) &
                              " (" & TC_Subtest & "2)");
            end if;
         when Third =>
            if Value /= Third_Expected then
               Report.Failed ("Value of " & Month'Image(Value) &
                              " not expected value of " &
                              Month'Image(Third_Expected) &
                              " (" & TC_Subtest & "3)");
            end if;
      end case;
   end Check;

   procedure Check (Value, First_Expected,
                    Second_Expected, Third_Expected : Employee;
                    TC_This_Limb : TC_Limb; TC_Subtest : String) is
   begin
      case TC_This_Limb is
         when First =>
            if Value /= First_Expected then
               Report.Failed ("Value of " & Employee'Image(Value) &
                              " not expected value of " &
                              Employee'Image(First_Expected) &
                              " (" & TC_Subtest & "1)");
            end if;
         when Second =>
            if Value /= Second_Expected then
               Report.Failed ("Value of " & Employee'Image(Value) &
                              " not expected value of " &
                              Employee'Image(Second_Expected) &
                              " (" & TC_Subtest & "2)");
            end if;
         when Third =>
            if Value /= Third_Expected then
               Report.Failed ("Value of " & Employee'Image(Value) &
                              " not expected value of " &
                              Employee'Image(Third_Expected) &
                              " (" & TC_Subtest & "3)");
            end if;
      end case;
   end Check;

   procedure SubtestA (Selector : Natural; Limb : TC_Limb) is
      -- As a qualified expression (easy: the context provides a single type).
      A_Month : Month := Dec;
   begin
      case Limb is
         when First =>
            if not (Selector < 1) then
               Report.Failed ("Selector chooses wrong limb (A1)");
            end if;
         when Second =>
            if not (Selector in 1..8) then
               Report.Failed ("Selector chooses wrong limb (A2)");
            end if;
         when Third =>
            if not (Selector > 8) then
               Report.Failed ("Selector chooses wrong limb (A3)");
            end if;
      end case;
      -- No overloaded dependent expressions:
      Check (Month'(if Selector < 1 then A_Month
                    elsif Selector in 1..8 then Jul
                    else Aug),
             A_Month, Jul, Aug, Limb, "A1");
      Check (Month'(case Selector is
                       when 0      => A_Month,
                       when 1..8   => Aug,
                       when others => Nov),
             A_Month, Aug, Nov, Limb, "A2");
      -- One overloaded dependent expression:
      Check (Month'(if Selector < 1 then Jan
                    elsif Selector in 1..8 then A_Month
                    else Apr),
             Jan, A_Month, Apr, Limb, "A3");
      Check (Month'(case Selector is
                       when 0      => Oct,
                       when 1..8   => Mar,
                       when others => A_Month),
             Oct, Mar, A_Month, Limb, "A4");
      -- All overloaded dependent expressions:
      Check (Month'(if Selector < 1 then Jan
                    elsif Selector in 1..8 then Oct
                    else Dec),
             Jan, Oct, Dec, Limb, "A5");
      Check (Month'(case Selector is
                       when 0      => Jan,
                       when 1..8   => Oct,
                       when others => Dec),
             Jan, Oct, Dec, Limb, "A6");
   end SubTestA;

   procedure SubtestB (Selector : Natural; Limb : TC_Limb) is
      -- As the parameter of an overloaded routine (harder: the context
      -- provides several separate types, only one of which works)
      A_Month : Month := Aug;

      -- Overloaded routines using global objects so that they don't
      -- affect the resolution themselves:
      procedure Hide (Value : Base; Which : Base) is
      begin
          Report.Failed ("Should not call this routine (BB)");
      end Hide;

      procedure Hide (Value : Employee; Which : Base) is
      begin
          Report.Failed ("Should not call this routine (BE)");
      end Hide;

      procedure Hide (Value : Month; Which : Base) is
      begin
          case Which is
             when Bin =>
                Check (Value, Jan, A_Month, Apr, Limb, "B7");
             when Oct =>
                Check (Value, Oct, Mar, A_Month, Limb, "B8");
             when Dec =>
                Check (Value, Jan, Oct, Dec, Limb, "B9");
             when Hex =>
                Check (Value, Jan, Oct, Dec, Limb, "BA");
          end case;
      end Hide;

   begin
      case Limb is
         when First =>
            if not (Selector < 1) then
               Report.Failed ("Selector chooses wrong limb (B1)");
            end if;
         when Second =>
            if not (Selector in 1..8) then
               Report.Failed ("Selector chooses wrong limb (B2)");
            end if;
         when Third =>
            if not (Selector > 8) then
               Report.Failed ("Selector chooses wrong limb (B3)");
            end if;
      end case;
      -- No overloaded dependent expressions:
      Check ((if Selector < 1 then A_Month
              elsif Selector in 1..8 then Jul
              else Aug),
             A_Month, Jul, Aug, Limb, "B1");
      Check ((case Selector is
                 when 0      => A_Month,
                 when 1..8   => Aug,
                 when others => Nov),
             A_Month, Aug, Nov, Limb, "B2");
      -- One overloaded dependent expression:
      Check ((if Selector < 1 then Jan
              elsif Selector in 1..8 then A_Month
              else Apr),
             Jan, A_Month, Apr, Limb, "B3");
      Check ((case Selector is
                 when 0      => Oct,
                 when 1..8   => Mar,
                 when others => A_Month),
             Oct, Mar, A_Month, Limb, "B4");
      -- All overloaded dependent expressions:
      Check ((if Selector < 1 then Jan
              elsif Selector in 1..8 then Oct
              else Dec),
             Jan, Oct, Dec, Limb, "B5");
      Check ((case Selector is
                 when 0      => Jan,
                 when 1..8   => Oct,
                 when others => Dec),
             Jan, Oct, Dec, Limb, "B6");
      -- One overloaded dependent expression, context provides no help:
      Hide ((if Selector < 1 then Jan
             elsif Selector in 1..8 then A_Month
             else Apr),
             Which => Bin);
      Hide ((case Selector is
                 when 0      => Oct,
                 when 1..8   => Mar,
                 when others => A_Month),
             Which => Oct);

      -- All overloaded dependent expressions, context provides no help:
      Hide ((if Selector < 1 then Jan
            elsif Selector in 1..8 then Oct
            else Dec), Which => Dec);
      Hide ((case Selector is
                 when 0      => Jan,
                 when 1..8   => Oct,
                 when others => Dec),
             Which => Hex);
   end SubTestB;

   procedure SubtestC (Selector : Natural; Limb : TC_Limb) is
      -- As the selector expression of a case statement (hardest: the context
      -- provides nothing, only the relationship between the dependent
      -- expressions gives an answer)

      function Convert (Value : Base) return Base is
      begin
         return Value;
      end Convert;

      function Convert (Value : Month) return Base is
      begin
         case Value is
            when Jan => return Bin;
            when Oct => return Oct;
            when Dec => return Dec;
            when others => return Hex;
         end case;
      end Convert;

      function Convert (Value : Employee) return Base is
      begin
         case Value is
            when Jan => return Bin;
            when Tim => return Oct;
            when Tom => return Dec;
            when others => return Hex;
         end case;
      end Convert;

   begin
      -- One overloaded dependent expression:
      case Convert (if Selector < 1 then Jan
                    elsif Selector in 1..8 then Tim
                    else Tom) is
         when Bin => Check (Jan, Jan, Tim, Tom, Limb, "C1");
         when Oct => Check (Tim, Jan, Tim, Tom, Limb, "C1");
         when Dec => Check (Tom, Jan, Tim, Tom, Limb, "C1");
         when Hex => Report.Failed ("Unusual result (C1)");
      end case;

      case Convert (case Selector is
                      when 0      => Jan,
                      when 1..8   => Tim,
                      when others => Tom) is
         when Bin => Check (Jan, Jan, Tim, Tom, Limb, "C2");
         when Oct => Check (Tim, Jan, Tim, Tom, Limb, "C2");
         when Dec => Check (Tom, Jan, Tim, Tom, Limb, "C2");
         when Hex => Report.Failed ("Unusual result (C2)");
      end case;

      -- All overloaded dependent expressions:
      case Convert (if Selector < 1 then Jan
                    elsif Selector in 1..8 then Oct
                    else Dec) is
         when Bin => Check (Jan, Jan, Oct, Dec, Limb, "C3");
         when Oct => Check (Oct, Jan, Oct, Dec, Limb, "C3");
         when Dec => Check (Dec, Jan, Oct, Dec, Limb, "C3");
         when Hex => Report.Failed ("Unusual result (C3)");
      end case;

      case Convert (case Selector is
                      when 0      => Jan,
                      when 1..8   => Oct,
                      when others => Dec) is
         when Bin => Check (Jan, Jan, Oct, Dec, Limb, "C4");
         when Oct => Check (Oct, Jan, Oct, Dec, Limb, "C4");
         when Dec => Check (Dec, Jan, Oct, Dec, Limb, "C4");
         when Hex => Report.Failed ("Unusual result (C4)");
      end case;

      -- Direct use of the conditional expression, all overloaded.
      case (if Selector < 1 then Jan
            elsif Selector in 1..8 then Oct
            else Dec) is
         when Jan => Check (Jan, Jan, Oct, Dec, Limb, "C5");
         when Oct => Check (Oct, Jan, Oct, Dec, Limb, "C5");
         when Dec => Check (Dec, Jan, Oct, Dec, Limb, "C5");
         when others => Report.Failed ("Unusual result (C5)");
      end case;

      case (case Selector is
               when 0      => Jan,
               when 1..8   => Oct,
               when others => Dec) is
         when Jan => Check (Jan, Jan, Oct, Dec, Limb, "C6");
         when Oct => Check (Oct, Jan, Oct, Dec, Limb, "C6");
         when Dec => Check (Dec, Jan, Oct, Dec, Limb, "C6");
         when others => Report.Failed ("Unusual result (C6)");
      end case;
   end SubTestC;

begin

   Report.Test ("C457006",
                "Check that overloaded functions can be resolved when they " &
                "appear as dependent expressions in a conditional " &
                "expression. Part 1: Enumeration literals");

   SubTestA (Report.Ident_Int(0), Limb => First);

   SubTestA (Report.Ident_Int(5), Limb => Second);

   SubTestA (Report.Ident_Int(9), Limb => Third);

   SubTestB (Report.Ident_Int(0), Limb => First);

   SubTestB (Report.Ident_Int(4), Limb => Second);

   SubTestB (Report.Ident_Int(9), Limb => Third);

   SubTestC (Report.Ident_Int(0), Limb => First);

   SubTestC (Report.Ident_Int(3), Limb => Second);

   SubTestC (Report.Ident_Int(9), Limb => Third);

   Report.Result;

end C457006;