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.");
120 assert(x == "Hello");
121 assert(x != z.substr (0, 4));
122 assert(x.compare (y) < 0);
123 assert(x.compare (z.substr (0, 6)) < 0);
125 assert(x.find ("lo") == 3);
126 assert(x.find ("l", 2) == 2);
127 assert(x.rfind ("l") == 3);
135 cout << "ch = x[0] = " << ch << "\n";
138 string z = x.substr (2, 3);
139 cout << "z = x.substr (2, 3) = " << z << "\n";
142 x.replace (2, 2, "r");
143 cout << "x.replace (2, 2, r); x = " << x << "\n";
147 x.replace (0, 1, 'j');
148 cout << "x.replace (0, 1, 'j'); x = " << x << "\n";
149 assert(x == "jello");
151 int ar[] = { 'H', 'e', 'l', 'l', 'o' };
152 x.replace (find (x.begin (), x.end (), 'l'),
153 find (x.rbegin (), x.rend (), 'l').base (),
154 ar, ar+sizeof(ar)/sizeof(ar[0]));
155 cout << "x = " << x << endl;
156 assert (x == "jeHelloo");
162 cout << "enter a word:";
164 cout << "word =" << z << " ";
165 cout << "length = " << z.length() << "\n";
168 void identitytest(string a, string b)
174 assert((a + b) == x);
175 assert((a + b) == y);
178 assert((a + b + a) == (a + (b + a)));
180 x.erase (x.rfind (b));
183 y.replace (0, y.rfind (b), b);
184 assert(y == (b + b));
185 y.replace (y.find (b), b.length (), a);
186 assert(y == (a + b));
197 identitytest(X+Y+N+X+Y+N, "A string that will be used in identitytest but is otherwise just another useless string.");
199 cout << "\nEnd of test\n";