summaryrefslogtreecommitdiff
path: root/gnu/egcs/libstdc++/tests/tstring.cc
blob: 833d3d964604fe112e20988051c4ea4a448cb6ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Tests for the -*- C++ -*- string classes.
// Copyright (C) 1994 Free Software Foundation

// This file is part of the GNU ANSI C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the terms of
// the GNU General Public License as published by the Free Software
// Foundation; either version 2, or (at your option) any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

#include <string>
#include <algorithm>
#include <iostream.h>
#include <stdlib.h>
#include <assert.h>

string X = "Hello";
string Y = "world";
string N = "123";
string c;
const char*  s = ",";

void decltest()
{
  string x;
  cout << "an empty string:" << x << "\n";
  assert(x == "");

  string y = "Hello";
  cout << "A string initialized to Hello:" << y << "\n";
  assert(y == "Hello");

  if (y[y.length()-1] == 'o')
	y = y + '\n';
  assert(y == "Hello\n");
  y = "Hello";

  string a = y;
  cout << "A string initialized to previous string:" << a << "\n";
  assert(a == "Hello");
  assert(a == y);

  string b (a, 1, 2);
  cout << "A string initialized to (previous string, 1, 2):" << b << "\n";
  assert(b == "el");

  char ch = '@';
  string z (1, ch);
  cout << "A string initialized to @:" << z << "\n";
  assert (z == "@");

  string n ("20");
  cout << "A string initialized to 20:" << n << "\n";
  assert(n == "20");

  int i = atoi(n.c_str ());
  double f = atof(n.c_str ());
  cout << "n = " << n << " atoi(n) = " << i << " atof(n) = " << f << "\n";
  assert(i == 20);
  assert(f == 20);

  int ar[] = { 'H', 'e', 'l', 'l', 'o' };
  string is (ar, ar+sizeof(ar)/sizeof(ar[0]));
  cout << "is = " << is << endl;
  assert (is == "Hello");
}

void cattest()
{
  string x = X;
  string y = Y;
  string z = x + y;
  cout << "z = x + y = " << z << "\n";
  assert(z == "Helloworld");

  x += y;
  cout << "x += y; x = " << x << "\n";
  assert(x == "Helloworld");

  y = Y;
  x = X;
  y.insert (0, x);
  cout << "y.insert (0, x); y = " << y << "\n";
  assert(y == "Helloworld");

  y = Y;
  x = X;
  x = x + y + x;
  cout << "x = x + y + x; x = " << x << "\n";
  assert(x == "HelloworldHello");

  y = Y;
  x = X;
  x = y + x + x;
  cout << "x = y + x + x; x = " << x << "\n";
  assert(x == "worldHelloHello");

  x = X;
  y = Y;
  z = x + s + ' ' + y.substr (y.find ('w'), 1) + y.substr (y.find ('w') + 1) + ".";
  cout << "z = x + s +  + y.substr (y.find (w), 1) + y.substr (y.find (w) + 1) + . = " << z << "\n";
  assert(z == "Hello, world.");
}

void
findtest()
{
  string x;
  string::size_type pos;
  pos = x.find_last_not_of('X');
  assert(pos == string::npos);
  pos = x.find_last_not_of("XYZ");
  assert(pos == string::npos);

  string y("a");
  pos = y.find_last_not_of('X');
  assert(pos == 0);
  pos = y.find_last_not_of('a');
  assert(pos == string::npos);
  pos = y.find_last_not_of("XYZ");
  assert(pos == 0);
  pos = y.find_last_not_of("a");
  assert(pos == string::npos);

  string z("ab");
  pos = z.find_last_not_of('X');
  assert(pos == 1);
  pos = z.find_last_not_of("XYZ");
  assert(pos == 1);
  pos = z.find_last_not_of('b');
  assert(pos == 0);
  pos = z.find_last_not_of("Xb");
  assert(pos == 0);
  pos = z.find_last_not_of("Xa");
  assert(pos == 1);
  pos = z.find_last_of("ab");
  assert(pos == 1);
  pos = z.find_last_of("Xa");
  assert(pos == 0);
  pos = z.find_last_of("Xb");
  assert(pos == 1);
  pos = z.find_last_of("XYZ");
  assert(pos == string::npos);
  pos = z.find_last_of('a');
  assert(pos == 0);
  pos = z.find_last_of('b');
  assert(pos == 1);
  pos = z.find_last_of('X');
  assert(pos == string::npos);
}

void comparetest()
{  
  string x = X;
  string y = Y;
  string n = N;
  string z = x + y;

  assert(x != y);
  assert(x == "Hello");
  assert(x != z.substr (0, 4));
  assert(x.compare (y) < 0);
  assert(x.compare (z.substr (0, 6)) < 0);

  assert(x.find ("lo") == 3);
  assert(x.find ("l", 2) == 2);
  assert(x.rfind ("l") == 3);
}

void substrtest()
{
  string x = X;

  char ch = x[0];
  cout << "ch = x[0] = " << ch << "\n";
  assert(ch == 'H');

  string z = x.substr (2, 3);
  cout << "z = x.substr (2, 3) = " << z << "\n";
  assert(z == "llo");

  x.replace (2, 2, "r");
  cout << "x.replace (2, 2, r); x = " << x << "\n";
  assert(x == "Hero");

  x = X;
  x.replace (0, 1, 'j');
  cout << "x.replace (0, 1, 'j'); x = " << x << "\n";
  assert(x == "jello");

  int ar[] = { 'H', 'e', 'l', 'l', 'o' };
  x.replace (find (x.begin (), x.end (), 'l'),
	     find (x.rbegin (), x.rend (), 'l').base (),
	     ar, ar+sizeof(ar)/sizeof(ar[0]));
  cout << "x = " << x << endl;
  assert (x == "jeHelloo");
}

void iotest()
{
  string z;
  cout << "enter a word:";
  cin >> z;
  cout << "word =" << z << " ";
  cout << "length = " << z.length() << "\n";
}

void identitytest(string a, string b)
{
  string x = a;
  string y = b;
  x += b;
  y.insert (0, a);
  assert((a + b) == x);
  assert((a + b) == y);
  assert(x == y);
  
  assert((a + b + a) == (a + (b + a)));

  x.erase (x.rfind (b));
  assert(x == a);

  y.replace (0, y.rfind (b), b);
  assert(y == (b + b));
  y.replace (y.find (b), b.length (), a);
  assert(y == (a + b));
}

int main()
{
  decltest();
  cattest();
  comparetest();
  findtest();
  substrtest();
  identitytest(X, X);
  identitytest(X, Y);
  identitytest(X+Y+N+X+Y+N, "A string that will be used in identitytest but is otherwise just another useless string.");
  iotest();
  cout << "\nEnd of test\n";
  return 0;
}