aboutsummaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats/tests/cd/cd92001.a
blob: d07ff4881a5acaf8a0ab80b4372b03632cac8131 (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
-- CD92001.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 if X denotes a scalar object, X'Valid
--      yields true if an only if the object denoted by X is normal and
--      has a valid representation.
--
-- TEST DESCRIPTION:
--      Using Unchecked_Conversion, Image and Value attributes, combined
--      with string manipulation, cause valid and invalid values to be
--      stored in various objects.  Check their validity with the
--      attribute 'Valid.  Invalid objects are created in a loop which
--      performs a simplistic check to ensure that the values being used
--      are indeed not valid, then assigns the value using an instance of
--      Unchecked_Conversion.  The creation of the tables of valid values
--      is trivial.
--
-- APPLICABILITY CRITERIA:
--      All implementations must attempt to compile this test.
--
--      For implementations validating against Systems Programming Annex (C):
--        this test must execute and report PASSED.
--
--      For implementations not validating against Annex C:
--        this test may report compile time errors at one or more points
--        indicated by "-- N/A => ERROR", in which case it may be graded as
--        inapplicable. Otherwise, the test must execute and report PASSED.
--
--
-- CHANGE HISTORY:
--      10 MAY 95   SAIC    Initial version
--      07 MAY 96   SAIC    Changed U_C to Ada.U_C for 2.1
--      05 JAN 99   RLB     Added Component_Size clauses to compensate
--                          for the fact that there is no required size
--                          for either the enumeration or modular components.
--!

with Report;
with Ada.Unchecked_Conversion;
with System;
procedure CD92001 is

  type Sparse_Enumerated is
       ( Help, Home, Page_Up, Del, EndK,
         Page_Down, Up, Left, Down, Right );

  for Sparse_Enumerated use ( Help      =>    2,
                              Home      =>    4,
                              Page_Up   =>    8,
                              Del       =>   16,
                              EndK      =>   32,
                              Page_Down =>   64,
                              Up        =>  128,
                              Left      =>  256,
                              Down      =>  512,
                              Right     => 1024 );

  type Mod_10 is mod 10;

  type Default_Enumerated is ( Zero,  One, Two,   Three, Four,
                               Five,  Six, Seven, Eight, Nine,
                               Clear, '=', '/',   '*',   '-',
                               '+',   Enter );
    for Default_Enumerated'Size use 8;

  Default_Enumerated_Count : constant := 17;

  type Mod_By_Enum_Items is mod Default_Enumerated_Count;

  type Mod_Same_Size_As_Sparse_Enum is mod 2**12;
                                        -- Sparse_Enumerated 'Size;

  type Mod_Same_Size_As_Def_Enum is mod 2**8;
                                     -- Default_Enumerated'Size;

  subtype Test_Width is Positive range 1..100;

  -- Note: There is no required relationship between 'Size and 'Component_Size,
  -- so we must use component_size clauses here.
  -- We use the following expressions to insure that the component size is a
  -- multiple of the Storage_Unit.
  Sparse_Component_Size : constant := ((Sparse_Enumerated'Size / System.Storage_Unit) +
        Boolean'Pos((Sparse_Enumerated'Size mod System.Storage_Unit) /= 0)) *
        System.Storage_Unit;
  Default_Component_Size : constant := ((Default_Enumerated'Size / System.Storage_Unit) +
        Boolean'Pos((Sparse_Enumerated'Size mod System.Storage_Unit) /= 0)) *
        System.Storage_Unit;

  type Sparse_Enum_Table is array(Test_Width) of Sparse_Enumerated;
  for Sparse_Enum_Table'Component_Size use Sparse_Component_Size;  -- N/A => ERROR.
  type Def_Enum_Table is array(Test_Width) of Default_Enumerated;
  for Def_Enum_Table'Component_Size use Default_Component_Size;  -- N/A => ERROR.

  type Sparse_Mod_Table  is
       array(Test_Width) of Mod_Same_Size_As_Sparse_Enum;
  for Sparse_Mod_Table'Component_Size use Sparse_Component_Size;  -- N/A => ERROR.

  type Default_Mod_Table is
       array(Test_Width) of Mod_Same_Size_As_Def_Enum;
  for Default_Mod_Table'Component_Size use Default_Component_Size;  -- N/A => ERROR.

  function UC_Sparse_Mod_Enum is
    new Ada.Unchecked_Conversion( Sparse_Mod_Table, Sparse_Enum_Table );

  function UC_Def_Mod_Enum is
    new Ada.Unchecked_Conversion( Default_Mod_Table, Def_Enum_Table );

  Valid_Sparse_Values : Sparse_Enum_Table;
  Valid_Def_Values    : Def_Enum_Table;

  Sample_Enum_Value_Table : Sparse_Mod_Table;
  Sample_Def_Value_Table  : Default_Mod_Table;


  -- fill the Valid tables with valid values for conversion
  procedure Fill_Valid is
    K : Mod_10 := 0;
    P : Mod_By_Enum_Items := 0;
  begin
    for I in Test_Width loop
      Valid_Sparse_Values(I) := Sparse_Enumerated'Val( K );
      Valid_Def_Values(I)    := Default_Enumerated'Val( Integer(P) );
      K := K +1;
      P := P +1;
    end loop;
  end Fill_Valid;

  -- fill the Sample tables with invalid values for conversion
  procedure Fill_Invalid is
    K : Mod_Same_Size_As_Sparse_Enum := 1;
    P : Mod_Same_Size_As_Def_Enum    := 1;
  begin
    for I in Test_Width loop
      K := K +13;
      if K mod 2 = 0 then  -- oops, that would be a valid value
        K := K +1;
      end if;
      if P = Mod_Same_Size_As_Def_Enum'Last
         or P < Default_Enumerated_Count then -- that would be valid
        P := Default_Enumerated_Count + 1;
      else
        P := P +1;
      end if;
      Sample_Enum_Value_Table(I) := K;
      Sample_Def_Value_Table(I)  := P;
    end loop;

    Valid_Sparse_Values := UC_Sparse_Mod_Enum(Sample_Enum_Value_Table);
    Valid_Def_Values    := UC_Def_Mod_Enum(Sample_Def_Value_Table);

  end Fill_Invalid;

  -- fill the tables with second set of valid values for conversion
  procedure Refill_Valid is
    K : Mod_10 := 0;
    P : Mod_By_Enum_Items := 0;

    Table : Array(Mod_10) of Mod_Same_Size_As_Sparse_Enum
          := ( 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 );

  begin
    for I in Test_Width loop
      Sample_Enum_Value_Table(I) := Table(K);
      Sample_Def_Value_Table(I)  := Mod_Same_Size_As_Def_Enum(P);
      K := K +1;
      P := P +1;
    end loop;
    Valid_Sparse_Values := UC_Sparse_Mod_Enum(Sample_Enum_Value_Table);
    Valid_Def_Values    := UC_Def_Mod_Enum(Sample_Def_Value_Table);
  end Refill_Valid;

  procedure Validate(Expect_Valid: Boolean) is
  begin  -- here's where we actually use the tested attribute

    for K in Test_Width loop
      if Valid_Sparse_Values(K)'Valid /= Expect_Valid then
        Report.Failed("Expected 'Valid =" & Boolean'Image(Expect_Valid)
                    & " for Sparse item " & Integer'Image(K) );
      end if;
    end loop;

    for P in Test_Width loop
      if Valid_Def_Values(P)'Valid /= Expect_Valid then
        Report.Failed("Expected 'Valid =" & Boolean'Image(Expect_Valid)
                    & " for Default item " & Integer'Image(P) );
      end if;
    end loop;

  end Validate;

begin  -- Main test procedure.

  Report.Test ("CD92001", "Check object attribute: X'Valid" );
   
  Fill_Valid;
  Validate(True);

  Fill_Invalid;
  Validate(False);

  Refill_Valid;
  Validate(True);

  Report.Result;

end CD92001;