Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / libstdc++ / tests / tstring.cc
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.
17
18 #include <string>
19 #include <algorithm>
20 #include <iostream.h>
21 #include <stdlib.h>
22 #include <assert.h>
23
24 string X = "Hello";
25 string Y = "world";
26 string N = "123";
27 string c;
28 const char*  s = ",";
29
30 void 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
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");
73 }
74
75 void 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
112 void
113 findtest()
114 {
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);
121
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);
131
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);
157 }
158
159 void comparetest()
160 {  
161   string x = X;
162   string y = Y;
163   string n = N;
164   string z = x + y;
165
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);
171
172   assert(x.find ("lo") == 3);
173   assert(x.find ("l", 2) == 2);
174   assert(x.rfind ("l") == 3);
175 }
176
177 void substrtest()
178 {
179   string x = X;
180
181   char ch = x[0];
182   cout << "ch = x[0] = " << ch << "\n";
183   assert(ch == 'H');
184
185   string z = x.substr (2, 3);
186   cout << "z = x.substr (2, 3) = " << z << "\n";
187   assert(z == "llo");
188
189   x.replace (2, 2, "r");
190   cout << "x.replace (2, 2, r); x = " << x << "\n";
191   assert(x == "Hero");
192
193   x = X;
194   x.replace (0, 1, 'j');
195   cout << "x.replace (0, 1, 'j'); x = " << x << "\n";
196   assert(x == "jello");
197
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");
204 }
205
206 void iotest()
207 {
208   string z;
209   cout << "enter a word:";
210   cin >> z;
211   cout << "word =" << z << " ";
212   cout << "length = " << z.length() << "\n";
213 }
214
215 void identitytest(string a, string b)
216 {
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);
224   
225   assert((a + b + a) == (a + (b + a)));
226
227   x.erase (x.rfind (b));
228   assert(x == a);
229
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));
234 }
235
236 int main()
237 {
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;
249 }