* c-decl.c (c_expand_body): Check TYPE_SIZE_UNIT (ret_type)
[official-gcc.git] / libstdc++ / tests / tstring.cc
blob833d3d964604fe112e20988051c4ea4a448cb6ca
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.
18 #include <string>
19 #include <algorithm>
20 #include <iostream.h>
21 #include <stdlib.h>
22 #include <assert.h>
24 string X = "Hello";
25 string Y = "world";
26 string N = "123";
27 string c;
28 const char* s = ",";
30 void decltest()
32 string x;
33 cout << "an empty string:" << x << "\n";
34 assert(x == "");
36 string y = "Hello";
37 cout << "A string initialized to Hello:" << y << "\n";
38 assert(y == "Hello");
40 if (y[y.length()-1] == 'o')
41 y = y + '\n';
42 assert(y == "Hello\n");
43 y = "Hello";
45 string a = y;
46 cout << "A string initialized to previous string:" << a << "\n";
47 assert(a == "Hello");
48 assert(a == y);
50 string b (a, 1, 2);
51 cout << "A string initialized to (previous string, 1, 2):" << b << "\n";
52 assert(b == "el");
54 char ch = '@';
55 string z (1, ch);
56 cout << "A string initialized to @:" << z << "\n";
57 assert (z == "@");
59 string n ("20");
60 cout << "A string initialized to 20:" << n << "\n";
61 assert(n == "20");
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);
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");
75 void cattest()
77 string x = X;
78 string y = Y;
79 string z = x + y;
80 cout << "z = x + y = " << z << "\n";
81 assert(z == "Helloworld");
83 x += y;
84 cout << "x += y; x = " << x << "\n";
85 assert(x == "Helloworld");
87 y = Y;
88 x = X;
89 y.insert (0, x);
90 cout << "y.insert (0, x); y = " << y << "\n";
91 assert(y == "Helloworld");
93 y = Y;
94 x = X;
95 x = x + y + x;
96 cout << "x = x + y + x; x = " << x << "\n";
97 assert(x == "HelloworldHello");
99 y = Y;
100 x = X;
101 x = y + x + x;
102 cout << "x = y + x + x; x = " << x << "\n";
103 assert(x == "worldHelloHello");
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.");
112 void
113 findtest()
115 string x;
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);
122 string y("a");
123 pos = y.find_last_not_of('X');
124 assert(pos == 0);
125 pos = y.find_last_not_of('a');
126 assert(pos == string::npos);
127 pos = y.find_last_not_of("XYZ");
128 assert(pos == 0);
129 pos = y.find_last_not_of("a");
130 assert(pos == string::npos);
132 string z("ab");
133 pos = z.find_last_not_of('X');
134 assert(pos == 1);
135 pos = z.find_last_not_of("XYZ");
136 assert(pos == 1);
137 pos = z.find_last_not_of('b');
138 assert(pos == 0);
139 pos = z.find_last_not_of("Xb");
140 assert(pos == 0);
141 pos = z.find_last_not_of("Xa");
142 assert(pos == 1);
143 pos = z.find_last_of("ab");
144 assert(pos == 1);
145 pos = z.find_last_of("Xa");
146 assert(pos == 0);
147 pos = z.find_last_of("Xb");
148 assert(pos == 1);
149 pos = z.find_last_of("XYZ");
150 assert(pos == string::npos);
151 pos = z.find_last_of('a');
152 assert(pos == 0);
153 pos = z.find_last_of('b');
154 assert(pos == 1);
155 pos = z.find_last_of('X');
156 assert(pos == string::npos);
159 void comparetest()
161 string x = X;
162 string y = Y;
163 string n = N;
164 string z = x + y;
166 assert(x != y);
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);
177 void substrtest()
179 string x = X;
181 char ch = x[0];
182 cout << "ch = x[0] = " << ch << "\n";
183 assert(ch == 'H');
185 string z = x.substr (2, 3);
186 cout << "z = x.substr (2, 3) = " << z << "\n";
187 assert(z == "llo");
189 x.replace (2, 2, "r");
190 cout << "x.replace (2, 2, r); x = " << x << "\n";
191 assert(x == "Hero");
193 x = X;
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");
206 void iotest()
208 string z;
209 cout << "enter a word:";
210 cin >> z;
211 cout << "word =" << z << " ";
212 cout << "length = " << z.length() << "\n";
215 void identitytest(string a, string b)
217 string x = a;
218 string y = b;
219 x += b;
220 y.insert (0, a);
221 assert((a + b) == x);
222 assert((a + b) == y);
223 assert(x == y);
225 assert((a + b + a) == (a + (b + a)));
227 x.erase (x.rfind (b));
228 assert(x == a);
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));
236 int main()
238 decltest();
239 cattest();
240 comparetest();
241 findtest();
242 substrtest();
243 identitytest(X, X);
244 identitytest(X, Y);
245 identitytest(X+Y+N+X+Y+N, "A string that will be used in identitytest but is otherwise just another useless string.");
246 iotest();
247 cout << "\nEnd of test\n";
248 return 0;