2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / testsuite / ada / acats / tests / cxa / cxa4002.a
blob583621ab4d97f75f85ccb5be1595c612d64dfbb8
1 -- CXA4002.A
2 --
3 -- Grant of Unlimited Rights
4 --
5 -- Under contracts F33600-87-D-0337, F33600-84-D-0280, MDA903-79-C-0687,
6 -- F08630-91-C-0015, and DCA100-97-D-0025, the U.S. Government obtained
7 -- unlimited rights in the software and documentation contained herein.
8 -- Unlimited rights are defined in DFAR 252.227-7013(a)(19). By making
9 -- this public release, the Government intends to confer upon all
10 -- recipients unlimited rights equal to those held by the Government.
11 -- These rights include rights to use, duplicate, release or disclose the
12 -- released technical data and computer software in whole or in part, in
13 -- any manner and for any purpose whatsoever, and to have or permit others
14 -- to do so.
16 -- DISCLAIMER
18 -- ALL MATERIALS OR INFORMATION HEREIN RELEASED, MADE AVAILABLE OR
19 -- DISCLOSED ARE AS IS. THE GOVERNMENT MAKES NO EXPRESS OR IMPLIED
20 -- WARRANTY AS TO ANY MATTER WHATSOEVER, INCLUDING THE CONDITIONS OF THE
21 -- SOFTWARE, DOCUMENTATION OR OTHER INFORMATION RELEASED, MADE AVAILABLE
22 -- OR DISCLOSED, OR THE OWNERSHIP, MERCHANTABILITY, OR FITNESS FOR A
23 -- PARTICULAR PURPOSE OF SAID MATERIAL.
24 --*
26 -- OBJECTIVE:
27 -- Check that the subprograms defined in package Ada.Strings.Fixed are
28 -- available, and that they produce correct results. Specifically,
29 -- check the subprograms Index, "*" (string constructor function),
30 -- Count, Trim, and Replace_Slice.
32 -- TEST DESCRIPTION:
33 -- This test demonstrates how certain Fixed string functions are used
34 -- to eliminate specific substrings from portions of text. A procedure
35 -- is defined that will take as parameters a source string along with
36 -- a substring that is to be completely removed from the source string.
37 -- The source string is parsed using the Index function, and any substring
38 -- slices are replaced in the source string by a series of X's (based on
39 -- the length of the substring.)
40 -- Three lines of text are provided to this procedure, and the resulting
41 -- substitutions are compared with expected results to validate the
42 -- string processing.
43 -- A global accumulator is updated with the number of occurrences of the
44 -- substring in the source string.
46 --
47 -- CHANGE HISTORY:
48 -- 06 Dec 94 SAIC ACVC 2.0
50 --!
52 with Ada.Strings;
53 with Ada.Strings.Fixed;
54 with Ada.Strings.Maps;
55 with Report;
57 procedure CXA4002 is
59 begin
61 Report.Test ("CXA4002", "Check that the subprograms defined in package " &
62 "Ada.Strings.Fixed are available, and that " &
63 "they produce correct results");
65 Test_Block:
66 declare
68 TC_Total : Natural := 0;
69 Number_Of_Lines : constant := 3;
71 type Restricted_Words_Array_Type is array (1..10) of String (1..10);
73 Restricted_Words : Restricted_Words_Array_Type :=
74 (" platoon", " marines ", " Marines ",
75 "north ", "south ", " east",
76 " beach ", " airport", "airfield ",
77 " road ");
79 subtype Line_Of_Text_Type is String(1..25);
80 type Page_Of_Text_Type is array (1..Number_Of_Lines)
81 of Line_Of_Text_Type;
83 Text_Page : Page_Of_Text_Type := ("The platoon of Marines ",
84 "moved south on the south ",
85 "road to the airfield. ");
87 TC_Revised_Line_1 : constant String := "The XXXXXXX of XXXXXXX ";
88 TC_Revised_Line_2 : constant String := "moved XXXXX on the XXXXX ";
89 TC_Revised_Line_3 : constant String := "XXXX to the XXXXXXXX. ";
91 ---
93 procedure Censor (Source_String : in out String;
94 Pattern_String : in String) is
96 -- Create a replacement string that is the same length as the
97 -- pattern string being removed.
98 Replacement : constant String := -- "*"
99 Ada.Strings.Fixed."*"(Pattern_String'Length, 'X');
101 Going : Ada.Strings.Direction := Ada.Strings.Forward;
102 Map : constant Ada.Strings.Maps.Character_Mapping :=
103 Ada.Strings.Maps.Identity;
104 Start_Pos,
105 Index : Natural := Source_String'First;
108 begin -- Censor
110 -- Accumulate count of total replacement operations.
112 TC_Total := TC_Total + -- Count
113 Ada.Strings.Fixed.Count (Source => Source_String,
114 Pattern => Pattern_String,
115 Mapping => Map);
116 loop
118 Index := Ada.Strings.Fixed.Index -- Index
119 (Source_String(Start_Pos..Source_String'Last),
120 Pattern_String,
121 Going,
122 Map);
124 exit when Index = 0; -- No matches, exit loop.
126 -- if a match was found, modify the substring.
127 Ada.Strings.Fixed.Replace_Slice -- Replace_Slice
128 (Source_String,
129 Index,
130 Index + Pattern_String'Length - 1,
131 Replacement);
132 Start_Pos := Index + Pattern_String'Length;
134 end loop;
136 end Censor;
139 begin
141 -- Invoke Censor subprogram to cleanse text.
142 -- Loop through each line of text, and check for the presence of each
143 -- restricted word.
144 -- Use the Trim function to eliminate leading or trailing blanks from
145 -- the restricted word parameters.
147 for Line in 1..Number_Of_Lines loop
148 for Word in Restricted_Words'Range loop
149 Censor (Text_Page(Line),
150 Ada.Strings.Fixed.Trim(Restricted_Words(Word), -- Trim
151 Ada.Strings.Both));
152 end loop;
153 end loop;
156 -- Validate results.
158 if TC_Total /= 6 then
159 Report.Failed ("Incorrect number of substitutions performed");
160 end if;
162 if Text_Page(1) /= TC_Revised_Line_1 then
163 Report.Failed ("Incorrect substitutions on Line 1");
164 end if;
166 if Text_Page(2) /= TC_Revised_Line_2 then
167 Report.Failed ("Incorrect substitutions on Line 2");
168 end if;
170 if Text_Page(3) /= TC_Revised_Line_3 then
171 Report.Failed ("Incorrect substitutions on Line 3");
172 end if;
175 exception
176 when others => Report.Failed ("Exception raised in Test_Block");
177 end Test_Block;
180 Report.Result;
182 end CXA4002;