]>
Commit | Line | Data |
---|---|---|
ca8f2cb1 | 1 | // { dg-options "-std=gnu++17" } |
6458742a | 2 | // { dg-do run { target c++17 } } |
ca8f2cb1 | 3 | |
8d9254fc | 4 | // Copyright (C) 2013-2020 Free Software Foundation, Inc. |
ca8f2cb1 VV |
5 | // |
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) | |
10 | // any later version. | |
11 | ||
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. | |
16 | ||
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/>. | |
20 | ||
21 | // basic_string_view find_first_of | |
22 | ||
23 | #include <string_view> | |
24 | #include <testsuite_hooks.h> | |
25 | ||
118c8424 | 26 | void |
ca8f2cb1 VV |
27 | test02() |
28 | { | |
ca8f2cb1 VV |
29 | typedef std::string_view::size_type csize_type; |
30 | csize_type npos = std::string_view::npos; | |
31 | csize_type csz01, csz02; | |
32 | ||
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; | |
38 | ||
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); | |
42 | VERIFY( csz01 == 0 ); | |
43 | csz01 = str01.find_first_of(str01, 4); | |
44 | VERIFY( csz01 == 4 ); | |
45 | csz01 = str01.find_first_of(str02, 0); | |
46 | VERIFY( csz01 == 0 ); | |
47 | csz01 = str01.find_first_of(str02, 3); | |
48 | VERIFY( csz01 == 3 ); | |
49 | csz01 = str01.find_first_of(str03, 0); | |
50 | VERIFY( csz01 == 8 ); | |
51 | csz01 = str01.find_first_of(str03, 3); | |
52 | VERIFY( csz01 == 8 ); | |
53 | csz01 = str01.find_first_of(str03, 12); | |
54 | VERIFY( csz01 == 16 ); | |
55 | csz01 = str01.find_first_of(str05, 0); | |
56 | VERIFY( csz01 == 1 ); | |
57 | csz01 = str01.find_first_of(str05, 4); | |
58 | VERIFY( csz01 == 4 ); | |
59 | ||
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 | |
c5ae1a27 | 63 | // However, str1.find_first_of(str2,pos) finds the first character in |
ca8f2cb1 VV |
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 ); | |
c5ae1a27 JW |
69 | |
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); | |
72 | VERIFY( csz01 == 0 ); | |
73 | csz01 = str01.find_first_of(str_lit01, 3, 0); | |
74 | VERIFY( csz01 == npos ); | |
75 | ||
76 | // size_type find_first_of(const char* s, size_type pos = 0) const; | |
77 | csz01 = str01.find_first_of(str_lit01); | |
78 | VERIFY( csz01 == 0 ); | |
79 | csz01 = str01.find_first_of(str_lit01, 3); | |
80 | VERIFY( csz01 == 3 ); | |
81 | ||
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 ); | |
86 | } | |
87 | ||
88 | constexpr bool | |
89 | test03() | |
90 | { | |
91 | typedef std::string_view::size_type csize_type; | |
92 | csize_type npos = std::string_view::npos; | |
93 | csize_type csz01 = 0, csz02 = 0; | |
94 | ||
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; | |
100 | ||
101 | #undef VERIFY | |
102 | #define VERIFY(x) if(!(x)) return false | |
103 | ||
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 ); | |
124 | ||
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 ); | |
134 | ||
ca8f2cb1 VV |
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 ); | |
140 | ||
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 ); | |
146 | ||
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 ); | |
c5ae1a27 JW |
151 | |
152 | return true; | |
ca8f2cb1 VV |
153 | } |
154 | ||
155 | int | |
156 | main() | |
c5ae1a27 | 157 | { |
ca8f2cb1 | 158 | test02(); |
c5ae1a27 | 159 | static_assert( test03() ); |
ca8f2cb1 VV |
160 | |
161 | return 0; | |
162 | } |