[multiple changes]
[gcc.git] / libstdc++-v3 / testsuite / 21_strings / find.cc
1 // 1999-06-09 bkoz
2
3 // Copyright (C) 1994, 1999, 2000 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20
21 // 21.3.6.1 basic_string find
22
23 #include <string>
24 #include <stdexcept>
25 #ifdef DEBUG_ASSERT
26 #include <assert.h>
27 #endif
28
29 bool test01(void)
30 {
31 bool test = true;
32 typedef std::string::size_type csize_type;
33 typedef std::string::const_reference cref;
34 typedef std::string::reference ref;
35 csize_type npos = std::string::npos;
36 csize_type csz01, csz02;
37
38 const char str_lit01[] = "mave";
39 const std::string str01("mavericks, santa cruz");
40 std::string str02(str_lit01);
41 std::string str03("s, s");
42 std::string str04;
43
44 // size_type find(const string&, size_type pos = 0) const;
45 csz01 = str01.find(str01);
46 test &= csz01 == 0;
47 csz01 = str01.find(str01, 4);
48 test &= csz01 == npos;
49 csz01 = str01.find(str02, 0);
50 test &= csz01 == 0;
51 csz01 = str01.find(str02, 3);
52 test &= csz01 == npos;
53 csz01 = str01.find(str03, 0);
54 test &= csz01 == 8;
55 csz01 = str01.find(str03, 3);
56 test &= csz01 == 8;
57 csz01 = str01.find(str03, 12);
58 test &= csz01 == npos;
59
60 // An empty string consists of no characters
61 // therefore it should be found at every point in a string,
62 // except beyond the end
63 csz01 = str01.find(str04, 0);
64 test &= csz01 == 0;
65 csz01 = str01.find(str04, 5);
66 test &= csz01 == 5;
67 csz01 = str01.find(str04, str01.size());
68 test &= csz01 == npos;
69
70 // size_type find(const char* s, size_type pos, size_type n) const;
71 csz01 = str01.find(str_lit01, 0, 3);
72 test &= csz01 == 0;
73 csz01 = str01.find(str_lit01, 3, 0);
74 test &= csz01 == 3;
75
76 // size_type find(const char* s, size_type pos = 0) const;
77 csz01 = str01.find(str_lit01);
78 test &= csz01 == 0;
79 csz01 = str01.find(str_lit01, 3);
80 test &= csz01 == 3; // zero length string should be found at pos
81
82 // size_type find(char c, size_type pos = 0) const;
83 csz01 = str01.find('z');
84 csz02 = str01.size() - 1;
85 test &= csz01 == csz02;
86 csz01 = str01.find('/');
87 test &= csz01 == npos;
88
89 // size_type find_first_of(const string&, size_type pos = 0) const;
90 std::string str05("xena rulez");
91 csz01 = str01.find_first_of(str01);
92 test &= csz01 == 0;
93 csz01 = str01.find_first_of(str01, 4);
94 test &= csz01 == 4;
95 csz01 = str01.find_first_of(str02, 0);
96 test &= csz01 == 0;
97 csz01 = str01.find_first_of(str02, 3);
98 test &= csz01 == 3;
99 csz01 = str01.find_first_of(str03, 0);
100 test &= csz01 == 8;
101 csz01 = str01.find_first_of(str03, 3);
102 test &= csz01 == 8;
103 csz01 = str01.find_first_of(str03, 12);
104 test &= csz01 == 16;
105 csz01 = str01.find_first_of(str05, 0);
106 test &= csz01 == 1;
107 csz01 = str01.find_first_of(str05, 4);
108 test &= csz01 == 4;
109
110 // An empty string consists of no characters
111 // therefore it should be found at every point in a string,
112 // except beyond the end
113 // However, str1.find_first_of(str2,pos) finds the first character in
114 // str1 (starting at pos) that exists in str2, which is none for empty str2
115 csz01 = str01.find_first_of(str04, 0);
116 test &= csz01 == npos;
117 csz01 = str01.find_first_of(str04, 5);
118 test &= csz01 == npos;
119
120 // size_type find_first_of(const char* s, size_type pos, size_type n) const;
121 csz01 = str01.find_first_of(str_lit01, 0, 3);
122 test &= csz01 == 0;
123 csz01 = str01.find_first_of(str_lit01, 3, 0);
124 test &= csz01 == npos;
125
126 // size_type find_first_of(const char* s, size_type pos = 0) const;
127 csz01 = str01.find_first_of(str_lit01);
128 test &= csz01 == 0;
129 csz01 = str01.find_first_of(str_lit01, 3);
130 test &= csz01 == 3;
131
132 // size_type find_first_of(char c, size_type pos = 0) const;
133 csz01 = str01.find_first_of('z');
134 csz02 = str01.size() - 1;
135 test &= csz01 == csz02;
136
137 // size_type find_last_of(const string& str, size_type pos = 0) const;
138 // size_type find_last_of(const char* s, size_type pos, size_type n) const;
139 // size_type find_last_of(const char* s, size_type pos = 0) const;
140 // size_type find_last_of(char c, size_type pos = 0) const;
141
142 #if 1
143 // from tstring.cc, from jason merrill, et. al.
144 std::string x;
145 std::string::size_type pos;
146 pos = x.find_last_not_of('X');
147 test &= pos == npos;
148 pos = x.find_last_not_of("XYZ");
149 test &= pos == npos;
150
151 std::string y("a");
152 pos = y.find_last_not_of('X');
153 test &= pos == 0;
154 pos = y.find_last_not_of('a');
155 test &= pos == npos;
156 pos = y.find_last_not_of("XYZ");
157 test &= pos == 0;
158 pos = y.find_last_not_of("a");
159 test &= pos == npos;
160
161 std::string z("ab");
162 pos = z.find_last_not_of('X');
163 test &= pos == 1;
164 pos = z.find_last_not_of("XYZ");
165 test &= pos == 1;
166 pos = z.find_last_not_of('b');
167 test &= pos == 0;
168 pos = z.find_last_not_of("Xb");
169 test &= pos == 0;
170 pos = z.find_last_not_of("Xa");
171 test &= pos == 1;
172 pos = z.find_last_of("ab");
173 test &= pos == 1;
174 pos = z.find_last_of("Xa");
175 test &= pos == 0;
176 pos = z.find_last_of("Xb");
177 test &= pos == 1;
178 pos = z.find_last_of("XYZ");
179 test &= pos == std::string::npos;
180 pos = z.find_last_of('a');
181 test &= pos == 0;
182 pos = z.find_last_of('b');
183 test &= pos == 1;
184 pos = z.find_last_of('X');
185 test &= pos == std::string::npos;
186 #endif
187
188 #ifdef DEBUG_ASSERT
189 assert(test);
190 #endif
191 return test;
192 }
193
194 int main()
195 {
196 test01();
197 }
198
199
200
201
202