summaryrefslogtreecommitdiff
path: root/gcc/testsuite/ada/acats/tests/cxa/cxa4011.a
blob: 05388a04ba7767c3ac407b45c80880fd218878e6 (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
-- CXA4011.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 the subprograms defined in package Ada.Strings.Unbounded
--      are available, and that they produce correct results. Specifically, 
--      check the subprograms To_Unbounded_String, "&", ">", "<", Element, 
--      Replace_Element, Count, Find_Token, Translate, Trim, Delete, and
--      "*".
--      
-- TEST DESCRIPTION:
--      This test demonstrates the uses of many of the subprograms defined
--      in package Ada.Strings.Unbounded for use with unbounded strings.
--      The test simulates how unbounded strings could be processed in a
--      user environment, using the subprograms provided in this package.
--
--      This test uses a variety of the subprograms defined in the unbounded 
--      string package in ways typical of common usage, with different 
--      combinations of available subprograms being used to accomplish 
--      similar unbounded string processing goals.
--      
--       
-- CHANGE HISTORY:
--      06 Dec 94   SAIC    ACVC 2.0
--      27 Feb 95   SAIC    Test description modification.
--      01 Nov 95   SAIC    Update and repair for ACVC 2.0.1.
--
--!

with Report;
with Ada.Strings.Maps;
with Ada.Strings.Unbounded;

procedure CXA4011 is
begin

   Report.Test ("CXA4011", "Check that the subprograms defined in "        &
                           "package Ada.Strings.Unbounded are available, " &
                           "and that they produce correct results");

   Test_Block:
   declare

      package ASUnb renames Ada.Strings.Unbounded;
      use Ada.Strings;
      use type Maps.Character_Set;
      use type ASUnb.Unbounded_String;

      Cad_String        : ASUnb.Unbounded_String := 
                            ASUnb.To_Unbounded_String("cad");

      Complete_String   : ASUnb.Unbounded_String := 
                            ASUnb.To_Unbounded_String("Incomplete") &
                            Ada.Strings.Space &
                            ASUnb.To_Unbounded_String("String");

      Incomplete_String  : ASUnb.Unbounded_String :=
                             ASUnb.To_Unbounded_String("ncomplete Strin");

      Incorrect_Spelling : ASUnb.Unbounded_String :=
                             ASUnb.To_Unbounded_String("Guob Dai");

      Magic_String       : ASUnb.Unbounded_String :=
                             ASUnb.To_Unbounded_String("abracadabra");

      Incantation        : ASUnb.Unbounded_String := Magic_String;


      A_Small_G : Character := 'g';
      A_Small_D : Character := 'd';

      ABCD_Set : Maps.Character_Set := Maps.To_Set("abcd");
      B_Set    : Maps.Character_Set := Maps.To_Set('b');
      AB_Set   : Maps.Character_Set := Maps."OR"(Maps.To_Set('a'), B_Set);

      Code_Map         : Maps.Character_Mapping := 
                           Maps.To_Mapping(From => "abcd", To => "wxyz");
      Reverse_Code_Map : Maps.Character_Mapping :=
                           Maps.To_Mapping(From => "wxyz", To => "abcd");
      Non_Existent_Map : Maps.Character_Mapping :=
                           Maps.To_Mapping(From => "jkl",  To => "mno");


      Token_Start      : Positive;
      Token_End        : Natural := 0;
      Matching_Letters : Natural := 0;


   begin

      -- "&"

      -- Prepend an 'I' and append a 'g' to the string.
      Incomplete_String := ASUnb."&"('I', Incomplete_String);  -- Char & Unb
      Incomplete_String := ASUnb."&"(Incomplete_String, 
                                     A_Small_G);               -- Unb & Char

      if Incomplete_String  < Complete_String  or
         Incomplete_String  > Complete_String  or
         Incomplete_String /= Complete_String
      then
         Report.Failed("Incorrect result from use of ""&"" operator");
      end if;


      -- Element

      -- Last element of the unbounded string should be a 'g'.
      if ASUnb.Element(Incomplete_String, ASUnb.Length(Incomplete_String)) /=
         A_Small_G
      then
         Report.Failed("Incorrect result from use of Function Element - 1");
      end if;

      if ASUnb.Element(Incomplete_String, 2)                 /=
         ASUnb.Element(ASUnb.Tail(Incomplete_String, 2), 1)     or
         ASUnb.Element(ASUnb.Head(Incomplete_String, 4), 2)  /=
         ASUnb.Element(ASUnb.To_Unbounded_String("wnqz"), 2)
      then
         Report.Failed("Incorrect result from use of Function Element - 2");
      end if;
    

      -- Replace_Element

      -- The unbounded string Incorrect_Spelling starts as "Guob Dai", and
      -- is transformed by the following three procedure calls to "Good Day".

      ASUnb.Replace_Element(Incorrect_Spelling, 2, 'o');

      ASUnb.Replace_Element(Incorrect_Spelling, 
                            ASUnb.Index(Incorrect_Spelling, B_Set),
                            A_Small_D);

      ASUnb.Replace_Element(Source => Incorrect_Spelling,
                            Index  => ASUnb.Length(Incorrect_Spelling),
                            By     => 'y');

      if Incorrect_Spelling /= ASUnb.To_Unbounded_String("Good Day") then
         Report.Failed("Incorrect result from Procedure Replace_Element");
      end if;


      -- Count

      -- Determine the number of characters in the unbounded string that
      -- are contained in the set.

      Matching_Letters := ASUnb.Count(Source => Magic_String,
                                      Set    => ABCD_Set);

      if Matching_Letters /= 9 then
         Report.Failed
            ("Incorrect result from Function Count with Set parameter");
      end if;

      -- Determine the number of occurrences of the following pattern strings
      -- in the unbounded string Magic_String.

      if ASUnb.Count(Magic_String, "ab") /=
         (ASUnb.Count(Magic_String, "ac") + ASUnb.Count(Magic_String, "ad")) or
         ASUnb.Count(Magic_String, "ab") /= 2
      then
         Report.Failed
            ("Incorrect result from Function Count with String parameter");
      end if;


      -- Find_Token

      ASUnb.Find_Token(Magic_String,      -- Find location of first "ab".
                       AB_Set,            -- Should be (1..2).
                       Ada.Strings.Inside,
                       Token_Start,
                       Token_End);

      if Natural(Token_Start) /= ASUnb.To_String(Magic_String)'First  or
         Token_End            /= ASUnb.Index(Magic_String, B_Set)
      then 
         Report.Failed("Incorrect result from Procedure Find_Token - 1");
      end if;


      ASUnb.Find_Token(Source => Magic_String, -- Find location of char 'r'
                       Set    => ABCD_Set,     -- in string, should be (3..3)
                       Test   => Ada.Strings.Outside,
                       First  => Token_Start,
                       Last   => Token_End);

      if Natural(Token_Start) /= 3 or
         Token_End            /= 3 then
         Report.Failed("Incorrect result from Procedure Find_Token - 2");
      end if;


      ASUnb.Find_Token(Magic_String,           -- No 'g' is in the string, so
                       Maps.To_Set(A_Small_G), -- the result parameters should
                       Ada.Strings.Inside,     -- be First = Source'First and
                       First => Token_Start,   -- Last = 0.
                       Last  => Token_End);

      if Token_Start /= ASUnb.To_String(Magic_String)'First  or
         Token_End   /= 0
      then
         Report.Failed("Incorrect result from Procedure Find_Token - 3");
      end if;
 

      -- Translate

      -- Use a mapping ("abcd" -> "wxyz") to transform the contents of 
      -- the unbounded string.
      -- Magic_String = "abracadabra"

      Incantation := ASUnb.Translate(Magic_String, Code_Map); 

      if Incantation /= ASUnb.To_Unbounded_String("wxrwywzwxrw") then
         Report.Failed("Incorrect result from Function Translate");
      end if;

      -- Use the inverse mapping of the one above to return the "translated"
      -- unbounded string to its original form.

      ASUnb.Translate(Incantation, Reverse_Code_Map);

      -- The map contained in the following call to Translate contains one
      -- element, and this element is not found in the unbounded string, so
      -- this call to Translate should have no effect on the unbounded string.

      if Incantation /= ASUnb.Translate(Magic_String, Non_Existent_Map) then
         Report.Failed("Incorrect result from Procedure Translate");
      end if;

    
      -- Trim

      Trim_Block:
      declare

         XYZ_Set     : Maps.Character_Set := Maps.To_Set("xyz");
         PQR_Set     : Maps.Character_Set := Maps.To_Set("pqr");

         Pad         : constant ASUnb.Unbounded_String := 
                                  ASUnb.To_Unbounded_String("Pad");

         The_New_Ada : constant ASUnb.Unbounded_String :=
                                  ASUnb.To_Unbounded_String("Ada9X");

         Space_Array : array (1..4) of ASUnb.Unbounded_String :=
                         (ASUnb.To_Unbounded_String("  Pad    "),
                          ASUnb.To_Unbounded_String("Pad   "),
                          ASUnb.To_Unbounded_String("     Pad"),
                          Pad);

         String_Array : array (1..5) of ASUnb.Unbounded_String := 
                          (ASUnb.To_Unbounded_String("xyzxAda9Xpqr"),
                           ASUnb.To_Unbounded_String("Ada9Xqqrp"),
                           ASUnb.To_Unbounded_String("zxyxAda9Xqpqr"),
                           ASUnb.To_Unbounded_String("xxxyAda9X"),
                           The_New_Ada);
           
      begin

         -- Examine the version of Trim that removes blanks from
         -- the left and/or right of a string.

         for i in 1..4 loop
            if ASUnb.Trim(Space_Array(i), Ada.Strings.Both) /= Pad then
               Report.Failed("Incorrect result from Trim for spaces - " &
                             Integer'Image(i));
            end if;
         end loop;   

         -- Examine the version of Trim that removes set characters from
         -- the left and right of a string.

         for i in 1..5 loop
            if ASUnb.Trim(String_Array(i),
                          Left   => XYZ_Set,
                          Right  => PQR_Set) /= The_New_Ada then
               Report.Failed
                 ("Incorrect result from Trim for set characters - " &
                  Integer'Image(i));
            end if;
         end loop;   

      end Trim_Block;


      -- Delete

      -- Use the Delete function to remove the first four and last four
      -- characters from the string.

      if ASUnb.Delete(Source  => ASUnb.Delete(Magic_String,
                                              8,
                                              ASUnb.Length(Magic_String)),
                      From    => ASUnb.To_String(Magic_String)'First,
                      Through => 4)   /= 
         Cad_String
      then
         Report.Failed("Incorrect results from Function Delete");
      end if;


      -- Constructors ("*")

      Constructor_Block:
      declare
      
         SOS      : ASUnb.Unbounded_String;

         Dot      : constant ASUnb.Unbounded_String := 
                               ASUnb.To_Unbounded_String("Dot_");
         Dash     : constant String := "Dash_";

         Distress : ASUnb.Unbounded_String := 
                      ASUnb.To_Unbounded_String("Dot_Dot_Dot_")    &
                      ASUnb.To_Unbounded_String("Dash_Dash_Dash_") &
                      ASUnb.To_Unbounded_String("Dot_Dot_Dot");
                      
         Repeat        : constant Natural   := 3;
         Separator     : constant Character := '_';

         Separator_Set : Maps.Character_Set := Maps.To_Set(Separator);

      begin

         -- Use the following constructor forms to construct the string
         -- "Dot_Dot_Dot_Dash_Dash_Dash_Dot_Dot_Dot".  Note that the 
         -- trailing underscore in the string is removed in the call to 
         -- Trim in the If statement condition.

         SOS := ASUnb."*"(Repeat, Dot);        -- "*"(#, Unb Str)

         SOS := SOS                     &
                ASUnb."*"(Repeat, Dash) &      -- "*"(#, Str)
                ASUnb."*"(Repeat, Dot);        -- "*"(#, Unb Str)

         if ASUnb.Trim(SOS, Maps.Null_Set, Separator_Set) /= Distress then
            Report.Failed("Incorrect results from Function ""*""");
         end if;

      end Constructor_Block;
      

   exception
      when others => Report.Failed ("Exception raised in Test_Block");
   end Test_Block;


   Report.Result;

end CXA4011;