]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string_view/operations/find/char/2.cc
1 // { dg-options "-std=gnu++17" }
2 // { dg-do run { target c++17 } }
4 // Copyright (C) 2013-2021 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING3. If not see
19 // <http://www.gnu.org/licenses/>.
21 // basic_string_view find_first_of
23 #include <string_view>
24 #include <testsuite_hooks.h>
29 typedef std::string_view::size_type csize_type
;
30 csize_type npos
= std::string_view::npos
;
31 csize_type csz01
, csz02
;
33 const char str_lit01
[] = "mave";
34 const std::string_view
str01("mavericks, santa cruz");
35 std::string_view
str02(str_lit01
);
36 std::string_view
str03("s, s");
37 std::string_view str04
;
39 // size_type find_first_of(const string_view&, size_type pos = 0) const;
40 std::string_view
str05("xena rulez");
41 csz01
= str01
.find_first_of(str01
);
43 csz01
= str01
.find_first_of(str01
, 4);
45 csz01
= str01
.find_first_of(str02
, 0);
47 csz01
= str01
.find_first_of(str02
, 3);
49 csz01
= str01
.find_first_of(str03
, 0);
51 csz01
= str01
.find_first_of(str03
, 3);
53 csz01
= str01
.find_first_of(str03
, 12);
54 VERIFY( csz01
== 16 );
55 csz01
= str01
.find_first_of(str05
, 0);
57 csz01
= str01
.find_first_of(str05
, 4);
60 // An empty string_view consists of no characters
61 // therefore it should be found at every point in a string_view,
62 // except beyond the end
63 // However, str1.find_first_of(str2,pos) finds the first character in
64 // str1 (starting at pos) that exists in str2, which is none for empty str2
65 csz01
= str01
.find_first_of(str04
, 0);
66 VERIFY( csz01
== npos
);
67 csz01
= str01
.find_first_of(str04
, 5);
68 VERIFY( csz01
== npos
);
70 // size_type find_first_of(const char* s, size_type pos, size_type n) const;
71 csz01
= str01
.find_first_of(str_lit01
, 0, 3);
73 csz01
= str01
.find_first_of(str_lit01
, 3, 0);
74 VERIFY( csz01
== npos
);
76 // size_type find_first_of(const char* s, size_type pos = 0) const;
77 csz01
= str01
.find_first_of(str_lit01
);
79 csz01
= str01
.find_first_of(str_lit01
, 3);
82 // size_type find_first_of(char c, size_type pos = 0) const;
83 csz01
= str01
.find_first_of('z');
84 csz02
= str01
.size() - 1;
85 VERIFY( csz01
== csz02
);
91 typedef std::string_view::size_type csize_type
;
92 csize_type npos
= std::string_view::npos
;
93 csize_type csz01
= 0, csz02
= 0;
95 const char str_lit01
[] = "mave";
96 const std::string_view
str01("mavericks, santa cruz");
97 std::string_view
str02(str_lit01
);
98 std::string_view
str03("s, s");
99 std::string_view str04
;
102 #define VERIFY(x) if(!(x)) return false
104 // size_type find_first_of(const string_view&, size_type pos = 0) const;
105 std::string_view
str05("xena rulez");
106 csz01
= str01
.find_first_of(str01
);
107 VERIFY( csz01
== 0 );
108 csz01
= str01
.find_first_of(str01
, 4);
109 VERIFY( csz01
== 4 );
110 csz01
= str01
.find_first_of(str02
, 0);
111 VERIFY( csz01
== 0 );
112 csz01
= str01
.find_first_of(str02
, 3);
113 VERIFY( csz01
== 3 );
114 csz01
= str01
.find_first_of(str03
, 0);
115 VERIFY( csz01
== 8 );
116 csz01
= str01
.find_first_of(str03
, 3);
117 VERIFY( csz01
== 8 );
118 csz01
= str01
.find_first_of(str03
, 12);
119 VERIFY( csz01
== 16 );
120 csz01
= str01
.find_first_of(str05
, 0);
121 VERIFY( csz01
== 1 );
122 csz01
= str01
.find_first_of(str05
, 4);
123 VERIFY( csz01
== 4 );
125 // An empty string_view consists of no characters
126 // therefore it should be found at every point in a string_view,
127 // except beyond the end
128 // However, str1.find_first_of(str2,pos) finds the first character in
129 // str1 (starting at pos) that exists in str2, which is none for empty str2
130 csz01
= str01
.find_first_of(str04
, 0);
131 VERIFY( csz01
== npos
);
132 csz01
= str01
.find_first_of(str04
, 5);
133 VERIFY( csz01
== npos
);
135 // size_type find_first_of(const char* s, size_type pos, size_type n) const;
136 csz01
= str01
.find_first_of(str_lit01
, 0, 3);
137 VERIFY( csz01
== 0 );
138 csz01
= str01
.find_first_of(str_lit01
, 3, 0);
139 VERIFY( csz01
== npos
);
141 // size_type find_first_of(const char* s, size_type pos = 0) const;
142 csz01
= str01
.find_first_of(str_lit01
);
143 VERIFY( csz01
== 0 );
144 csz01
= str01
.find_first_of(str_lit01
, 3);
145 VERIFY( csz01
== 3 );
147 // size_type find_first_of(char c, size_type pos = 0) const;
148 csz01
= str01
.find_first_of('z');
149 csz02
= str01
.size() - 1;
150 VERIFY( csz01
== csz02
);
159 static_assert( test03() );