]> gcc.gnu.org Git - gcc.git/blame - libstdc++/tests/tstring.cc
* tstring.cc (identitytest): s/remove/erase/.
[gcc.git] / libstdc++ / tests / tstring.cc
CommitLineData
6599da04
JM
1// Tests for the -*- C++ -*- string classes.
2// Copyright (C) 1994 Free Software Foundation
3
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.
8
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.
13
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.
5a4a879c 17
6599da04 18#include <string>
5a4a879c 19#include <algorithm>
6599da04
JM
20#include <iostream.h>
21#include <stdlib.h>
22#include <assert.h>
23
24string X = "Hello";
25string Y = "world";
26string N = "123";
27string c;
28const char* s = ",";
29
30void decltest()
31{
32 string x;
33 cout << "an empty string:" << x << "\n";
34 assert(x == "");
35
36 string y = "Hello";
37 cout << "A string initialized to Hello:" << y << "\n";
38 assert(y == "Hello");
39
40 if (y[y.length()-1] == 'o')
41 y = y + '\n';
42 assert(y == "Hello\n");
43 y = "Hello";
44
45 string a = y;
46 cout << "A string initialized to previous string:" << a << "\n";
47 assert(a == "Hello");
48 assert(a == y);
49
50 string b (a, 1, 2);
51 cout << "A string initialized to (previous string, 1, 2):" << b << "\n";
52 assert(b == "el");
53
54 char ch = '@';
55 string z (1, ch);
56 cout << "A string initialized to @:" << z << "\n";
57 assert (z == "@");
58
59 string n ("20");
60 cout << "A string initialized to 20:" << n << "\n";
61 assert(n == "20");
62
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";
66 assert(i == 20);
67 assert(f == 20);
68
5a4a879c
JM
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");
6599da04
JM
73}
74
75void cattest()
76{
77 string x = X;
78 string y = Y;
79 string z = x + y;
80 cout << "z = x + y = " << z << "\n";
81 assert(z == "Helloworld");
82
83 x += y;
84 cout << "x += y; x = " << x << "\n";
85 assert(x == "Helloworld");
86
87 y = Y;
88 x = X;
89 y.insert (0, x);
90 cout << "y.insert (0, x); y = " << y << "\n";
91 assert(y == "Helloworld");
92
93 y = Y;
94 x = X;
95 x = x + y + x;
96 cout << "x = x + y + x; x = " << x << "\n";
97 assert(x == "HelloworldHello");
98
99 y = Y;
100 x = X;
101 x = y + x + x;
102 cout << "x = y + x + x; x = " << x << "\n";
103 assert(x == "worldHelloHello");
104
105 x = X;
106 y = Y;
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.");
110}
111
112void comparetest()
113{
114 string x = X;
115 string y = Y;
116 string n = N;
117 string z = x + y;
118
119 assert(x != y);
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);
124
125 assert(x.find ("lo") == 3);
126 assert(x.find ("l", 2) == 2);
127 assert(x.rfind ("l") == 3);
128}
129
130void substrtest()
131{
132 string x = X;
133
134 char ch = x[0];
135 cout << "ch = x[0] = " << ch << "\n";
136 assert(ch == 'H');
137
138 string z = x.substr (2, 3);
139 cout << "z = x.substr (2, 3) = " << z << "\n";
140 assert(z == "llo");
141
142 x.replace (2, 2, "r");
143 cout << "x.replace (2, 2, r); x = " << x << "\n";
144 assert(x == "Hero");
145
146 x = X;
147 x.replace (0, 1, 'j');
148 cout << "x.replace (0, 1, 'j'); x = " << x << "\n";
149 assert(x == "jello");
5a4a879c
JM
150
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");
6599da04
JM
157}
158
159void iotest()
160{
161 string z;
162 cout << "enter a word:";
163 cin >> z;
164 cout << "word =" << z << " ";
165 cout << "length = " << z.length() << "\n";
166}
167
168void identitytest(string a, string b)
169{
170 string x = a;
171 string y = b;
172 x += b;
173 y.insert (0, a);
174 assert((a + b) == x);
175 assert((a + b) == y);
176 assert(x == y);
177
178 assert((a + b + a) == (a + (b + a)));
179
38bab5d4 180 x.erase (x.rfind (b));
6599da04
JM
181 assert(x == a);
182
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));
187}
188
189int main()
190{
191 decltest();
192 cattest();
193 comparetest();
194 substrtest();
195 identitytest(X, X);
196 identitytest(X, Y);
197 identitytest(X+Y+N+X+Y+N, "A string that will be used in identitytest but is otherwise just another useless string.");
198 iotest();
199 cout << "\nEnd of test\n";
200 return 0;
201}
This page took 0.059041 seconds and 5 git commands to generate.