aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.9/gcc/testsuite/ada/acats/tests/cxa/cxa4013.a
blob: 0f93e9dc8d1c9e38dc6417d0619f426ab0794af2 (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
-- CXA4013.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.Wide_Fixed
--      are available, and that they produce correct results.  Specifically,
--      check the subprograms Index, "*" (Wide_String constructor function),
--      Count, Trim, and Replace_Slice.
--
-- TEST DESCRIPTION:
--      This test demonstrates how certain Wide_Fixed string functions
--      are used to eliminate specific substrings from portions of text.  
--      A procedure is defined that will take as parameters a source 
--      Wide_String along with a substring that is to be completely removed
--      from the source string.  The source Wide_String is parsed using the
--      Index function, and any substring slices are replaced in the source
--      Wide_String by a series of X's (based on the length of the substring.)  
--      Three lines of text are provided to this procedure, and the resulting
--      substitutions are compared with expected results to validate the 
--      string processing.
--      A global accumulator is updated with the number of occurrences of the
--      substring in the source string.  
--
--       
-- CHANGE HISTORY:
--      06 Dec 94   SAIC    ACVC 2.0
--
--!

with Ada.Strings;
with Ada.Strings.Wide_Fixed;
with Ada.Strings.Wide_Maps;
with Report;

procedure CXA4013 is

begin

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

   Test_Block:
   declare

      TC_Total        : Natural  := 0;
      Number_Of_Lines : constant := 3;
      WC              : Wide_Character := 
                          Wide_Character'Val(Character'Pos('X')            + 
                                             Character'Pos(Character'Last) + 
                                             1 );

      subtype WS is Wide_String (1..25);

      type Restricted_Words_Array_Type is 
        array (1..10) of Wide_String (1..10);

      Restricted_Words : Restricted_Words_Array_Type := 
                           ("   platoon", " marines  ", "  Marines ",
                            "north     ", "south     ", "      east", 
                            "  beach   ", "   airport", "airfield  ", 
                            "     road ");

      type Page_Of_Text_Type is array (1..Number_Of_Lines) of WS;

      Text_Page : Page_Of_Text_Type := ("The platoon of Marines   ",
                                        "moved south on the south ",
                                        "road to the airfield.    ");

      TC_Revised_Line_1 : constant Wide_String := "The XXXXXXX of XXXXXXX   ";
      TC_Revised_Line_2 : constant Wide_String := "moved XXXXX on the XXXXX ";
      TC_Revised_Line_3 : constant Wide_String := "XXXX to the XXXXXXXX.    ";


      function Equivalent (Left : WS;  Right : Wide_String) 
        return Boolean is
      begin
         for i in WS'range loop
            if Left(i) /= Right(i) then
               if Left(i) /= WC   or  Right(i) /= 'X' then
                  return False;
               end if;
            end if;
         end loop;
         return True;
      end Equivalent;

      ---

      procedure Censor (Source_String  : in out Wide_String;
                        Pattern_String : in     Wide_String) is
                        
         use Ada.Strings.Wide_Fixed; -- allows infix notation of "*" below.

         -- Create a replacement string that is the same length as the
         -- pattern string being removed.  Use the infix notation of the 
         -- wide string constructor function.

         Replacement : constant Wide_String :=                       
                         Pattern_String'Length * WC;                 -- "*"

         Going     : Ada.Strings.Direction := Ada.Strings.Forward;   
         Start_Pos,
         Index     : Natural := Source_String'First;

      begin  -- Censor

         -- Accumulate count of total replacement operations.

         TC_Total := TC_Total +                                     
                      Ada.Strings.Wide_Fixed.Count                  -- Count
                        (Source  => Source_String,
                         Pattern => Pattern_String,
                         Mapping => Ada.Strings.Wide_Maps.Identity);
         loop

            Index := Ada.Strings.Wide_Fixed.Index                   -- Index
                       (Source_String(Start_Pos..Source_String'Last),
                        Pattern_String, 
                        Going, 
                        Ada.Strings.Wide_Maps.Identity);

            exit when Index = 0;   -- No matches, exit loop.

            -- if a match was found, modify the substring.
            Ada.Strings.Wide_Fixed.Replace_Slice              -- Replace_Slice
                                     (Source_String,
                                      Index,
                                      Index + Pattern_String'Length - 1,
                                      Replacement);
            Start_Pos := Index + Pattern_String'Length;

         end loop;

      end Censor;


   begin

      -- Invoke Censor subprogram to cleanse text.
      -- Loop through each line of text, and check for the presence of each
      -- restricted word.
      -- Use the Trim function to eliminate leading or trailing blanks from
      -- the restricted word parameters.

      for Line in 1..Number_Of_Lines loop
         for Word in Restricted_Words'Range loop
             Censor (Text_Page(Line),                                 -- Trim
                     Ada.Strings.Wide_Fixed.Trim(Restricted_Words(Word),
                                                 Ada.Strings.Both));
         end loop;
      end loop;


      -- Validate results.

      if TC_Total /= 6 then
         Report.Failed ("Incorrect number of substitutions performed");
      end if;

      if not Equivalent (Text_Page(1), TC_Revised_Line_1) then
         Report.Failed ("Incorrect substitutions on Line 1");
      end if;

      if not Equivalent (Text_Page(2), TC_Revised_Line_2) then
         Report.Failed ("Incorrect substitutions on Line 2");
      end if;

      if not Equivalent (Text_Page(3), TC_Revised_Line_3) then
         Report.Failed ("Incorrect substitutions on Line 3");
      end if;


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


   Report.Result;

end CXA4013;