1 // Tests for the -*- C++ -*- string classes.
2 // Copyright (C) 1994 Free Software Foundation
4 // This file is part of the GNU ANSI C++ Library. This library is free
5 // software; you can redistribute it and/or modify it under the terms of
6 // the GNU General Public License as published by the Free Software
7 // Foundation; either version 2, or (at your option) any later version.
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
33 cout
<< "an empty string:" << x
<< "\n";
37 cout
<< "A string initialized to Hello:" << y
<< "\n";
40 if (y
[y
.length()-1] == 'o')
42 assert(y
== "Hello\n");
46 cout
<< "A string initialized to previous string:" << a
<< "\n";
51 cout
<< "A string initialized to (previous string, 1, 2):" << b
<< "\n";
56 cout
<< "A string initialized to @:" << z
<< "\n";
60 cout
<< "A string initialized to 20:" << n
<< "\n";
63 int i
= atoi(n
.c_str ());
64 double f
= atof(n
.c_str ());
65 cout
<< "n = " << n
<< " atoi(n) = " << i
<< " atof(n) = " << f
<< "\n";
69 int ar
[] = { 'H', 'e', 'l', 'l', 'o' };
70 string
is (ar
, ar
+sizeof(ar
)/sizeof(ar
[0]));
71 cout
<< "is = " << is
<< endl
;
72 assert (is
== "Hello");
80 cout
<< "z = x + y = " << z
<< "\n";
81 assert(z
== "Helloworld");
84 cout
<< "x += y; x = " << x
<< "\n";
85 assert(x
== "Helloworld");
90 cout
<< "y.insert (0, x); y = " << y
<< "\n";
91 assert(y
== "Helloworld");
96 cout
<< "x = x + y + x; x = " << x
<< "\n";
97 assert(x
== "HelloworldHello");
102 cout
<< "x = y + x + x; x = " << x
<< "\n";
103 assert(x
== "worldHelloHello");
107 z
= x
+ s
+ ' ' + y
.substr (y
.find ('w'), 1) + y
.substr (y
.find ('w') + 1) + ".";
108 cout
<< "z = x + s + + y.substr (y.find (w), 1) + y.substr (y.find (w) + 1) + . = " << z
<< "\n";
109 assert(z
== "Hello, world.");
116 string::size_type pos
;
117 pos
= x
.find_last_not_of('X');
118 assert(pos
== string::npos
);
119 pos
= x
.find_last_not_of("XYZ");
120 assert(pos
== string::npos
);
123 pos
= y
.find_last_not_of('X');
125 pos
= y
.find_last_not_of('a');
126 assert(pos
== string::npos
);
127 pos
= y
.find_last_not_of("XYZ");
129 pos
= y
.find_last_not_of("a");
130 assert(pos
== string::npos
);
133 pos
= z
.find_last_not_of('X');
135 pos
= z
.find_last_not_of("XYZ");
137 pos
= z
.find_last_not_of('b');
139 pos
= z
.find_last_not_of("Xb");
141 pos
= z
.find_last_not_of("Xa");
143 pos
= z
.find_last_of("ab");
145 pos
= z
.find_last_of("Xa");
147 pos
= z
.find_last_of("Xb");
149 pos
= z
.find_last_of("XYZ");
150 assert(pos
== string::npos
);
151 pos
= z
.find_last_of('a');
153 pos
= z
.find_last_of('b');
155 pos
= z
.find_last_of('X');
156 assert(pos
== string::npos
);
167 assert(x
== "Hello");
168 assert(x
!= z
.substr (0, 4));
169 assert(x
.compare (y
) < 0);
170 assert(x
.compare (z
.substr (0, 6)) < 0);
172 assert(x
.find ("lo") == 3);
173 assert(x
.find ("l", 2) == 2);
174 assert(x
.rfind ("l") == 3);
182 cout
<< "ch = x[0] = " << ch
<< "\n";
185 string z
= x
.substr (2, 3);
186 cout
<< "z = x.substr (2, 3) = " << z
<< "\n";
189 x
.replace (2, 2, "r");
190 cout
<< "x.replace (2, 2, r); x = " << x
<< "\n";
194 x
.replace (0, 1, 'j');
195 cout
<< "x.replace (0, 1, 'j'); x = " << x
<< "\n";
196 assert(x
== "jello");
198 int ar
[] = { 'H', 'e', 'l', 'l', 'o' };
199 x
.replace (find (x
.begin (), x
.end (), 'l'),
200 find (x
.rbegin (), x
.rend (), 'l').base (),
201 ar
, ar
+sizeof(ar
)/sizeof(ar
[0]));
202 cout
<< "x = " << x
<< endl
;
203 assert (x
== "jeHelloo");
209 cout
<< "enter a word:";
211 cout
<< "word =" << z
<< " ";
212 cout
<< "length = " << z
.length() << "\n";
215 void identitytest(string a
, string b
)
221 assert((a
+ b
) == x
);
222 assert((a
+ b
) == y
);
225 assert((a
+ b
+ a
) == (a
+ (b
+ a
)));
227 x
.erase (x
.rfind (b
));
230 y
.replace (0, y
.rfind (b
), b
);
231 assert(y
== (b
+ b
));
232 y
.replace (y
.find (b
), b
.length (), a
);
233 assert(y
== (a
+ b
));
245 identitytest(X
+Y
+N
+X
+Y
+N
, "A string that will be used in identitytest but is otherwise just another useless string.");
247 cout
<< "\nEnd of test\n";