]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++/tests/tstring.cc
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.
32 cout
<< "an empty string:" << x
<< "\n";
36 cout
<< "A string initialized to Hello:" << y
<< "\n";
39 if (y
[y
.length()-1] == 'o')
41 assert(y
== "Hello\n");
45 cout
<< "A string initialized to previous string:" << a
<< "\n";
50 cout
<< "A string initialized to (previous string, 1, 2):" << b
<< "\n";
55 cout
<< "A string initialized to @:" << z
<< "\n";
59 cout
<< "A string initialized to 20:" << n
<< "\n";
62 int i
= atoi(n
.c_str ());
63 double f
= atof(n
.c_str ());
64 cout
<< "n = " << n
<< " atoi(n) = " << i
<< " atof(n) = " << f
<< "\n";
75 cout
<< "z = x + y = " << z
<< "\n";
76 assert(z
== "Helloworld");
79 cout
<< "x += y; x = " << x
<< "\n";
80 assert(x
== "Helloworld");
85 cout
<< "y.insert (0, x); y = " << y
<< "\n";
86 assert(y
== "Helloworld");
91 cout
<< "x = x + y + x; x = " << x
<< "\n";
92 assert(x
== "HelloworldHello");
97 cout
<< "x = y + x + x; x = " << x
<< "\n";
98 assert(x
== "worldHelloHello");
102 z
= x
+ s
+ ' ' + y
.substr (y
.find ('w'), 1) + y
.substr (y
.find ('w') + 1) + ".";
103 cout
<< "z = x + s + + y.substr (y.find (w), 1) + y.substr (y.find (w) + 1) + . = " << z
<< "\n";
104 assert(z
== "Hello, world.");
115 assert(x
== "Hello");
116 assert(x
!= z
.substr (0, 4));
117 assert(x
.compare (y
) < 0);
118 assert(x
.compare (z
.substr (0, 6)) < 0);
120 assert(x
.find ("lo") == 3);
121 assert(x
.find ("l", 2) == 2);
122 assert(x
.rfind ("l") == 3);
130 cout
<< "ch = x[0] = " << ch
<< "\n";
133 string z
= x
.substr (2, 3);
134 cout
<< "z = x.substr (2, 3) = " << z
<< "\n";
137 x
.replace (2, 2, "r");
138 cout
<< "x.replace (2, 2, r); x = " << x
<< "\n";
142 x
.replace (0, 1, 'j');
143 cout
<< "x.replace (0, 1, 'j'); x = " << x
<< "\n";
144 assert(x
== "jello");
150 cout
<< "enter a word:";
152 cout
<< "word =" << z
<< " ";
153 cout
<< "length = " << z
.length() << "\n";
156 void identitytest(string a
, string b
)
162 assert((a
+ b
) == x
);
163 assert((a
+ b
) == y
);
166 assert((a
+ b
+ a
) == (a
+ (b
+ a
)));
168 x
.remove (x
.rfind (b
));
171 y
.replace (0, y
.rfind (b
), b
);
172 assert(y
== (b
+ b
));
173 y
.replace (y
.find (b
), b
.length (), a
);
174 assert(y
== (a
+ b
));
185 identitytest(X
+Y
+N
+X
+Y
+N
, "A string that will be used in identitytest but is otherwise just another useless string.");
187 cout
<< "\nEnd of test\n";