]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/21_strings/find.cc
*.cc: Remove spaces, make sure testcases return zero.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / find.cc
CommitLineData
b2dad0e3
BK
1// 1999-06-09 bkoz
2
7626c237 3// Copyright (C) 1994, 1999, 2000 Free Software Foundation, Inc.
b2dad0e3
BK
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>
aa1b2f7d 25#include <debug_assert.h>
b2dad0e3
BK
26
27bool test01(void)
28{
29 bool test = true;
30 typedef std::string::size_type csize_type;
31 typedef std::string::const_reference cref;
32 typedef std::string::reference ref;
33 csize_type npos = std::string::npos;
34 csize_type csz01, csz02;
35
36 const char str_lit01[] = "mave";
37 const std::string str01("mavericks, santa cruz");
38 std::string str02(str_lit01);
39 std::string str03("s, s");
40 std::string str04;
41
42 // size_type find(const string&, size_type pos = 0) const;
43 csz01 = str01.find(str01);
aa1b2f7d 44 VERIFY( csz01 == 0 );
b2dad0e3 45 csz01 = str01.find(str01, 4);
aa1b2f7d 46 VERIFY( csz01 == npos );
b2dad0e3 47 csz01 = str01.find(str02, 0);
aa1b2f7d 48 VERIFY( csz01 == 0 );
b2dad0e3 49 csz01 = str01.find(str02, 3);
aa1b2f7d 50 VERIFY( csz01 == npos );
b2dad0e3 51 csz01 = str01.find(str03, 0);
aa1b2f7d 52 VERIFY( csz01 == 8 );
b2dad0e3 53 csz01 = str01.find(str03, 3);
aa1b2f7d 54 VERIFY( csz01 == 8 );
b2dad0e3 55 csz01 = str01.find(str03, 12);
aa1b2f7d 56 VERIFY( csz01 == npos );
60df8b81
BK
57
58 // An empty string consists of no characters
59 // therefore it should be found at every point in a string,
60 // except beyond the end
b2dad0e3 61 csz01 = str01.find(str04, 0);
aa1b2f7d 62 VERIFY( csz01 == 0 );
b2dad0e3 63 csz01 = str01.find(str04, 5);
aa1b2f7d 64 VERIFY( csz01 == 5 );
60df8b81 65 csz01 = str01.find(str04, str01.size());
aa1b2f7d 66 VERIFY( csz01 == str01.size() );
5f349042 67 csz01 = str01.find(str04, str01.size()+1);
aa1b2f7d 68 VERIFY( csz01 == npos );
b2dad0e3
BK
69
70 // size_type find(const char* s, size_type pos, size_type n) const;
71 csz01 = str01.find(str_lit01, 0, 3);
aa1b2f7d 72 VERIFY( csz01 == 0 );
b2dad0e3 73 csz01 = str01.find(str_lit01, 3, 0);
aa1b2f7d 74 VERIFY( csz01 == 3 );
b2dad0e3
BK
75
76 // size_type find(const char* s, size_type pos = 0) const;
77 csz01 = str01.find(str_lit01);
aa1b2f7d 78 VERIFY( csz01 == 0 );
b2dad0e3 79 csz01 = str01.find(str_lit01, 3);
aa1b2f7d 80 VERIFY( csz01 == npos );
b2dad0e3
BK
81
82 // size_type find(char c, size_type pos = 0) const;
83 csz01 = str01.find('z');
84 csz02 = str01.size() - 1;
aa1b2f7d 85 VERIFY( csz01 == csz02 );
b2dad0e3 86 csz01 = str01.find('/');
aa1b2f7d 87 VERIFY( csz01 == npos );
b2dad0e3 88
b2dad0e3
BK
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);
aa1b2f7d 92 VERIFY( csz01 == 0 );
b2dad0e3 93 csz01 = str01.find_first_of(str01, 4);
aa1b2f7d 94 VERIFY( csz01 == 4 );
b2dad0e3 95 csz01 = str01.find_first_of(str02, 0);
aa1b2f7d 96 VERIFY( csz01 == 0 );
b2dad0e3 97 csz01 = str01.find_first_of(str02, 3);
aa1b2f7d 98 VERIFY( csz01 == 3 );
b2dad0e3 99 csz01 = str01.find_first_of(str03, 0);
aa1b2f7d 100 VERIFY( csz01 == 8 );
b2dad0e3 101 csz01 = str01.find_first_of(str03, 3);
aa1b2f7d 102 VERIFY( csz01 == 8 );
b2dad0e3 103 csz01 = str01.find_first_of(str03, 12);
aa1b2f7d 104 VERIFY( csz01 == 16 );
b2dad0e3 105 csz01 = str01.find_first_of(str05, 0);
aa1b2f7d 106 VERIFY( csz01 == 1 );
b2dad0e3 107 csz01 = str01.find_first_of(str05, 4);
aa1b2f7d 108 VERIFY( csz01 == 4 );
b2dad0e3 109
7626c237
BK
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
60df8b81
BK
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
b2dad0e3 115 csz01 = str01.find_first_of(str04, 0);
aa1b2f7d 116 VERIFY( csz01 == npos );
b2dad0e3 117 csz01 = str01.find_first_of(str04, 5);
aa1b2f7d 118 VERIFY( csz01 == npos );
b2dad0e3
BK
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);
aa1b2f7d 122 VERIFY( csz01 == 0 );
b2dad0e3 123 csz01 = str01.find_first_of(str_lit01, 3, 0);
aa1b2f7d 124 VERIFY( csz01 == npos );
b2dad0e3
BK
125
126 // size_type find_first_of(const char* s, size_type pos = 0) const;
127 csz01 = str01.find_first_of(str_lit01);
aa1b2f7d 128 VERIFY( csz01 == 0 );
b2dad0e3 129 csz01 = str01.find_first_of(str_lit01, 3);
aa1b2f7d 130 VERIFY( csz01 == 3 );
b2dad0e3
BK
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;
aa1b2f7d 135 VERIFY( csz01 == csz02 );
b2dad0e3
BK
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');
aa1b2f7d 147 VERIFY( pos == npos );
b2dad0e3 148 pos = x.find_last_not_of("XYZ");
aa1b2f7d 149 VERIFY( pos == npos );
b2dad0e3
BK
150
151 std::string y("a");
152 pos = y.find_last_not_of('X');
aa1b2f7d 153 VERIFY( pos == 0 );
b2dad0e3 154 pos = y.find_last_not_of('a');
aa1b2f7d 155 VERIFY( pos == npos );
b2dad0e3 156 pos = y.find_last_not_of("XYZ");
aa1b2f7d 157 VERIFY( pos == 0 );
b2dad0e3 158 pos = y.find_last_not_of("a");
aa1b2f7d 159 VERIFY( pos == npos );
b2dad0e3
BK
160
161 std::string z("ab");
162 pos = z.find_last_not_of('X');
aa1b2f7d 163 VERIFY( pos == 1 );
b2dad0e3 164 pos = z.find_last_not_of("XYZ");
aa1b2f7d 165 VERIFY( pos == 1 );
b2dad0e3 166 pos = z.find_last_not_of('b');
aa1b2f7d 167 VERIFY( pos == 0 );
b2dad0e3 168 pos = z.find_last_not_of("Xb");
aa1b2f7d 169 VERIFY( pos == 0 );
b2dad0e3 170 pos = z.find_last_not_of("Xa");
aa1b2f7d 171 VERIFY( pos == 1 );
b2dad0e3 172 pos = z.find_last_of("ab");
aa1b2f7d 173 VERIFY( pos == 1 );
b2dad0e3 174 pos = z.find_last_of("Xa");
aa1b2f7d 175 VERIFY( pos == 0 );
b2dad0e3 176 pos = z.find_last_of("Xb");
aa1b2f7d 177 VERIFY( pos == 1 );
b2dad0e3 178 pos = z.find_last_of("XYZ");
aa1b2f7d 179 VERIFY( pos == std::string::npos );
b2dad0e3 180 pos = z.find_last_of('a');
aa1b2f7d 181 VERIFY( pos == 0 );
b2dad0e3 182 pos = z.find_last_of('b');
aa1b2f7d 183 VERIFY( pos == 1 );
b2dad0e3 184 pos = z.find_last_of('X');
aa1b2f7d 185 VERIFY( pos == std::string::npos );
b2dad0e3
BK
186#endif
187
188#ifdef DEBUG_ASSERT
189 assert(test);
190#endif
191 return test;
192}
193
194int main()
195{
196 test01();
04d930e6 197 return 0;
b2dad0e3 198}