1 //===----------------------------------------------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 // constexpr int compare(size_type pos1, size_type n1, basic_string_view str) const;
13 #include <string_view>
17 #include "test_macros.h"
18 #include "constexpr_char_traits.h"
20 int sign ( int x
) { return x
> 0 ? 1 : ( x
< 0 ? -1 : 0 ); }
22 template<typename CharT
>
23 void test1 ( std::basic_string_view
<CharT
> sv1
, size_t pos1
, size_t n1
,
24 std::basic_string_view
<CharT
> sv2
, int expected
) {
25 #ifdef TEST_HAS_NO_EXCEPTIONS
26 if (pos1
<= sv1
.size())
27 assert(sign( sv1
.compare(pos1
, n1
, sv2
)) == sign(expected
));
30 assert(sign( sv1
.compare(pos1
, n1
, sv2
)) == sign(expected
));
31 assert(pos1
<= sv1
.size());
33 catch (const std::out_of_range
&) {
34 assert(pos1
> sv1
.size());
40 template<typename CharT
>
41 void test ( const CharT
*s1
, size_t pos1
, size_t n1
, const CharT
*s2
, int expected
) {
42 typedef std::basic_string_view
<CharT
> string_view_t
;
43 string_view_t
sv1 ( s1
);
44 string_view_t
sv2 ( s2
);
45 test1(sv1
, pos1
, n1
, sv2
, expected
);
50 test("", 0, 0, "", 0);
51 test("", 0, 0, "abcde", -5);
52 test("", 0, 0, "abcdefghij", -10);
53 test("", 0, 0, "abcdefghijklmnopqrst", -20);
54 test("", 0, 1, "", 0);
55 test("", 0, 1, "abcde", -5);
56 test("", 0, 1, "abcdefghij", -10);
57 test("", 0, 1, "abcdefghijklmnopqrst", -20);
58 test("", 1, 0, "", 0);
59 test("", 1, 0, "abcde", 0);
60 test("", 1, 0, "abcdefghij", 0);
61 test("", 1, 0, "abcdefghijklmnopqrst", 0);
62 test("abcde", 0, 0, "", 0);
63 test("abcde", 0, 0, "abcde", -5);
64 test("abcde", 0, 0, "abcdefghij", -10);
65 test("abcde", 0, 0, "abcdefghijklmnopqrst", -20);
66 test("abcde", 0, 1, "", 1);
67 test("abcde", 0, 1, "abcde", -4);
68 test("abcde", 0, 1, "abcdefghij", -9);
69 test("abcde", 0, 1, "abcdefghijklmnopqrst", -19);
70 test("abcde", 0, 2, "", 2);
71 test("abcde", 0, 2, "abcde", -3);
72 test("abcde", 0, 2, "abcdefghij", -8);
73 test("abcde", 0, 2, "abcdefghijklmnopqrst", -18);
74 test("abcde", 0, 4, "", 4);
75 test("abcde", 0, 4, "abcde", -1);
76 test("abcde", 0, 4, "abcdefghij", -6);
77 test("abcde", 0, 4, "abcdefghijklmnopqrst", -16);
78 test("abcde", 0, 5, "", 5);
79 test("abcde", 0, 5, "abcde", 0);
80 test("abcde", 0, 5, "abcdefghij", -5);
81 test("abcde", 0, 5, "abcdefghijklmnopqrst", -15);
82 test("abcde", 0, 6, "", 5);
83 test("abcde", 0, 6, "abcde", 0);
84 test("abcde", 0, 6, "abcdefghij", -5);
85 test("abcde", 0, 6, "abcdefghijklmnopqrst", -15);
86 test("abcde", 1, 0, "", 0);
87 test("abcde", 1, 0, "abcde", -5);
88 test("abcde", 1, 0, "abcdefghij", -10);
89 test("abcde", 1, 0, "abcdefghijklmnopqrst", -20);
90 test("abcde", 1, 1, "", 1);
91 test("abcde", 1, 1, "abcde", 1);
92 test("abcde", 1, 1, "abcdefghij", 1);
93 test("abcde", 1, 1, "abcdefghijklmnopqrst", 1);
94 test("abcde", 1, 2, "", 2);
95 test("abcde", 1, 2, "abcde", 1);
96 test("abcde", 1, 2, "abcdefghij", 1);
97 test("abcde", 1, 2, "abcdefghijklmnopqrst", 1);
98 test("abcde", 1, 3, "", 3);
99 test("abcde", 1, 3, "abcde", 1);
100 test("abcde", 1, 3, "abcdefghij", 1);
101 test("abcde", 1, 3, "abcdefghijklmnopqrst", 1);
102 test("abcde", 1, 4, "", 4);
103 test("abcde", 1, 4, "abcde", 1);
104 test("abcde", 1, 4, "abcdefghij", 1);
105 test("abcde", 1, 4, "abcdefghijklmnopqrst", 1);
106 test("abcde", 1, 5, "", 4);
107 test("abcde", 1, 5, "abcde", 1);
108 test("abcde", 1, 5, "abcdefghij", 1);
109 test("abcde", 1, 5, "abcdefghijklmnopqrst", 1);
110 test("abcde", 2, 0, "", 0);
111 test("abcde", 2, 0, "abcde", -5);
112 test("abcde", 2, 0, "abcdefghij", -10);
113 test("abcde", 2, 0, "abcdefghijklmnopqrst", -20);
114 test("abcde", 2, 1, "", 1);
115 test("abcde", 2, 1, "abcde", 2);
116 test("abcde", 2, 1, "abcdefghij", 2);
117 test("abcde", 2, 1, "abcdefghijklmnopqrst", 2);
118 test("abcde", 2, 2, "", 2);
119 test("abcde", 2, 2, "abcde", 2);
120 test("abcde", 2, 2, "abcdefghij", 2);
121 test("abcde", 2, 2, "abcdefghijklmnopqrst", 2);
122 test("abcde", 2, 3, "", 3);
123 test("abcde", 2, 3, "abcde", 2);
124 test("abcde", 2, 3, "abcdefghij", 2);
125 test("abcde", 2, 3, "abcdefghijklmnopqrst", 2);
126 test("abcde", 2, 4, "", 3);
127 test("abcde", 2, 4, "abcde", 2);
128 test("abcde", 2, 4, "abcdefghij", 2);
129 test("abcde", 2, 4, "abcdefghijklmnopqrst", 2);
130 test("abcde", 4, 0, "", 0);
131 test("abcde", 4, 0, "abcde", -5);
132 test("abcde", 4, 0, "abcdefghij", -10);
133 test("abcde", 4, 0, "abcdefghijklmnopqrst", -20);
134 test("abcde", 4, 1, "", 1);
135 test("abcde", 4, 1, "abcde", 4);
136 test("abcde", 4, 1, "abcdefghij", 4);
137 test("abcde", 4, 1, "abcdefghijklmnopqrst", 4);
138 test("abcde", 4, 2, "", 1);
139 test("abcde", 4, 2, "abcde", 4);
140 test("abcde", 4, 2, "abcdefghij", 4);
141 test("abcde", 4, 2, "abcdefghijklmnopqrst", 4);
142 test("abcde", 5, 0, "", 0);
143 test("abcde", 5, 0, "abcde", -5);
144 test("abcde", 5, 0, "abcdefghij", -10);
145 test("abcde", 5, 0, "abcdefghijklmnopqrst", -20);
146 test("abcde", 5, 1, "", 0);
147 test("abcde", 5, 1, "abcde", -5);
148 test("abcde", 5, 1, "abcdefghij", -10);
149 test("abcde", 5, 1, "abcdefghijklmnopqrst", -20);
154 test("abcde", 6, 0, "", 0);
155 test("abcde", 6, 0, "abcde", 0);
156 test("abcde", 6, 0, "abcdefghij", 0);
157 test("abcde", 6, 0, "abcdefghijklmnopqrst", 0);
158 test("abcdefghij", 0, 0, "", 0);
159 test("abcdefghij", 0, 0, "abcde", -5);
160 test("abcdefghij", 0, 0, "abcdefghij", -10);
161 test("abcdefghij", 0, 0, "abcdefghijklmnopqrst", -20);
162 test("abcdefghij", 0, 1, "", 1);
163 test("abcdefghij", 0, 1, "abcde", -4);
164 test("abcdefghij", 0, 1, "abcdefghij", -9);
165 test("abcdefghij", 0, 1, "abcdefghijklmnopqrst", -19);
166 test("abcdefghij", 0, 5, "", 5);
167 test("abcdefghij", 0, 5, "abcde", 0);
168 test("abcdefghij", 0, 5, "abcdefghij", -5);
169 test("abcdefghij", 0, 5, "abcdefghijklmnopqrst", -15);
170 test("abcdefghij", 0, 9, "", 9);
171 test("abcdefghij", 0, 9, "abcde", 4);
172 test("abcdefghij", 0, 9, "abcdefghij", -1);
173 test("abcdefghij", 0, 9, "abcdefghijklmnopqrst", -11);
174 test("abcdefghij", 0, 10, "", 10);
175 test("abcdefghij", 0, 10, "abcde", 5);
176 test("abcdefghij", 0, 10, "abcdefghij", 0);
177 test("abcdefghij", 0, 10, "abcdefghijklmnopqrst", -10);
178 test("abcdefghij", 0, 11, "", 10);
179 test("abcdefghij", 0, 11, "abcde", 5);
180 test("abcdefghij", 0, 11, "abcdefghij", 0);
181 test("abcdefghij", 0, 11, "abcdefghijklmnopqrst", -10);
182 test("abcdefghij", 1, 0, "", 0);
183 test("abcdefghij", 1, 0, "abcde", -5);
184 test("abcdefghij", 1, 0, "abcdefghij", -10);
185 test("abcdefghij", 1, 0, "abcdefghijklmnopqrst", -20);
186 test("abcdefghij", 1, 1, "", 1);
187 test("abcdefghij", 1, 1, "abcde", 1);
188 test("abcdefghij", 1, 1, "abcdefghij", 1);
189 test("abcdefghij", 1, 1, "abcdefghijklmnopqrst", 1);
190 test("abcdefghij", 1, 4, "", 4);
191 test("abcdefghij", 1, 4, "abcde", 1);
192 test("abcdefghij", 1, 4, "abcdefghij", 1);
193 test("abcdefghij", 1, 4, "abcdefghijklmnopqrst", 1);
194 test("abcdefghij", 1, 8, "", 8);
195 test("abcdefghij", 1, 8, "abcde", 1);
196 test("abcdefghij", 1, 8, "abcdefghij", 1);
197 test("abcdefghij", 1, 8, "abcdefghijklmnopqrst", 1);
198 test("abcdefghij", 1, 9, "", 9);
199 test("abcdefghij", 1, 9, "abcde", 1);
200 test("abcdefghij", 1, 9, "abcdefghij", 1);
201 test("abcdefghij", 1, 9, "abcdefghijklmnopqrst", 1);
202 test("abcdefghij", 1, 10, "", 9);
203 test("abcdefghij", 1, 10, "abcde", 1);
204 test("abcdefghij", 1, 10, "abcdefghij", 1);
205 test("abcdefghij", 1, 10, "abcdefghijklmnopqrst", 1);
206 test("abcdefghij", 5, 0, "", 0);
207 test("abcdefghij", 5, 0, "abcde", -5);
208 test("abcdefghij", 5, 0, "abcdefghij", -10);
209 test("abcdefghij", 5, 0, "abcdefghijklmnopqrst", -20);
210 test("abcdefghij", 5, 1, "", 1);
211 test("abcdefghij", 5, 1, "abcde", 5);
212 test("abcdefghij", 5, 1, "abcdefghij", 5);
213 test("abcdefghij", 5, 1, "abcdefghijklmnopqrst", 5);
214 test("abcdefghij", 5, 2, "", 2);
215 test("abcdefghij", 5, 2, "abcde", 5);
216 test("abcdefghij", 5, 2, "abcdefghij", 5);
217 test("abcdefghij", 5, 2, "abcdefghijklmnopqrst", 5);
218 test("abcdefghij", 5, 4, "", 4);
219 test("abcdefghij", 5, 4, "abcde", 5);
220 test("abcdefghij", 5, 4, "abcdefghij", 5);
221 test("abcdefghij", 5, 4, "abcdefghijklmnopqrst", 5);
222 test("abcdefghij", 5, 5, "", 5);
223 test("abcdefghij", 5, 5, "abcde", 5);
224 test("abcdefghij", 5, 5, "abcdefghij", 5);
225 test("abcdefghij", 5, 5, "abcdefghijklmnopqrst", 5);
226 test("abcdefghij", 5, 6, "", 5);
227 test("abcdefghij", 5, 6, "abcde", 5);
228 test("abcdefghij", 5, 6, "abcdefghij", 5);
229 test("abcdefghij", 5, 6, "abcdefghijklmnopqrst", 5);
230 test("abcdefghij", 9, 0, "", 0);
231 test("abcdefghij", 9, 0, "abcde", -5);
232 test("abcdefghij", 9, 0, "abcdefghij", -10);
233 test("abcdefghij", 9, 0, "abcdefghijklmnopqrst", -20);
234 test("abcdefghij", 9, 1, "", 1);
235 test("abcdefghij", 9, 1, "abcde", 9);
236 test("abcdefghij", 9, 1, "abcdefghij", 9);
237 test("abcdefghij", 9, 1, "abcdefghijklmnopqrst", 9);
238 test("abcdefghij", 9, 2, "", 1);
239 test("abcdefghij", 9, 2, "abcde", 9);
240 test("abcdefghij", 9, 2, "abcdefghij", 9);
241 test("abcdefghij", 9, 2, "abcdefghijklmnopqrst", 9);
242 test("abcdefghij", 10, 0, "", 0);
243 test("abcdefghij", 10, 0, "abcde", -5);
244 test("abcdefghij", 10, 0, "abcdefghij", -10);
245 test("abcdefghij", 10, 0, "abcdefghijklmnopqrst", -20);
246 test("abcdefghij", 10, 1, "", 0);
247 test("abcdefghij", 10, 1, "abcde", -5);
248 test("abcdefghij", 10, 1, "abcdefghij", -10);
249 test("abcdefghij", 10, 1, "abcdefghijklmnopqrst", -20);
250 test("abcdefghij", 11, 0, "", 0);
251 test("abcdefghij", 11, 0, "abcde", 0);
252 test("abcdefghij", 11, 0, "abcdefghij", 0);
253 test("abcdefghij", 11, 0, "abcdefghijklmnopqrst", 0);
258 test("abcdefghijklmnopqrst", 0, 0, "", 0);
259 test("abcdefghijklmnopqrst", 0, 0, "abcde", -5);
260 test("abcdefghijklmnopqrst", 0, 0, "abcdefghij", -10);
261 test("abcdefghijklmnopqrst", 0, 0, "abcdefghijklmnopqrst", -20);
262 test("abcdefghijklmnopqrst", 0, 1, "", 1);
263 test("abcdefghijklmnopqrst", 0, 1, "abcde", -4);
264 test("abcdefghijklmnopqrst", 0, 1, "abcdefghij", -9);
265 test("abcdefghijklmnopqrst", 0, 1, "abcdefghijklmnopqrst", -19);
266 test("abcdefghijklmnopqrst", 0, 10, "", 10);
267 test("abcdefghijklmnopqrst", 0, 10, "abcde", 5);
268 test("abcdefghijklmnopqrst", 0, 10, "abcdefghij", 0);
269 test("abcdefghijklmnopqrst", 0, 10, "abcdefghijklmnopqrst", -10);
270 test("abcdefghijklmnopqrst", 0, 19, "", 19);
271 test("abcdefghijklmnopqrst", 0, 19, "abcde", 14);
272 test("abcdefghijklmnopqrst", 0, 19, "abcdefghij", 9);
273 test("abcdefghijklmnopqrst", 0, 19, "abcdefghijklmnopqrst", -1);
274 test("abcdefghijklmnopqrst", 0, 20, "", 20);
275 test("abcdefghijklmnopqrst", 0, 20, "abcde", 15);
276 test("abcdefghijklmnopqrst", 0, 20, "abcdefghij", 10);
277 test("abcdefghijklmnopqrst", 0, 20, "abcdefghijklmnopqrst", 0);
278 test("abcdefghijklmnopqrst", 0, 21, "", 20);
279 test("abcdefghijklmnopqrst", 0, 21, "abcde", 15);
280 test("abcdefghijklmnopqrst", 0, 21, "abcdefghij", 10);
281 test("abcdefghijklmnopqrst", 0, 21, "abcdefghijklmnopqrst", 0);
282 test("abcdefghijklmnopqrst", 1, 0, "", 0);
283 test("abcdefghijklmnopqrst", 1, 0, "abcde", -5);
284 test("abcdefghijklmnopqrst", 1, 0, "abcdefghij", -10);
285 test("abcdefghijklmnopqrst", 1, 0, "abcdefghijklmnopqrst", -20);
286 test("abcdefghijklmnopqrst", 1, 1, "", 1);
287 test("abcdefghijklmnopqrst", 1, 1, "abcde", 1);
288 test("abcdefghijklmnopqrst", 1, 1, "abcdefghij", 1);
289 test("abcdefghijklmnopqrst", 1, 1, "abcdefghijklmnopqrst", 1);
290 test("abcdefghijklmnopqrst", 1, 9, "", 9);
291 test("abcdefghijklmnopqrst", 1, 9, "abcde", 1);
292 test("abcdefghijklmnopqrst", 1, 9, "abcdefghij", 1);
293 test("abcdefghijklmnopqrst", 1, 9, "abcdefghijklmnopqrst", 1);
294 test("abcdefghijklmnopqrst", 1, 18, "", 18);
295 test("abcdefghijklmnopqrst", 1, 18, "abcde", 1);
296 test("abcdefghijklmnopqrst", 1, 18, "abcdefghij", 1);
297 test("abcdefghijklmnopqrst", 1, 18, "abcdefghijklmnopqrst", 1);
298 test("abcdefghijklmnopqrst", 1, 19, "", 19);
299 test("abcdefghijklmnopqrst", 1, 19, "abcde", 1);
300 test("abcdefghijklmnopqrst", 1, 19, "abcdefghij", 1);
301 test("abcdefghijklmnopqrst", 1, 19, "abcdefghijklmnopqrst", 1);
302 test("abcdefghijklmnopqrst", 1, 20, "", 19);
303 test("abcdefghijklmnopqrst", 1, 20, "abcde", 1);
304 test("abcdefghijklmnopqrst", 1, 20, "abcdefghij", 1);
305 test("abcdefghijklmnopqrst", 1, 20, "abcdefghijklmnopqrst", 1);
306 test("abcdefghijklmnopqrst", 10, 0, "", 0);
307 test("abcdefghijklmnopqrst", 10, 0, "abcde", -5);
308 test("abcdefghijklmnopqrst", 10, 0, "abcdefghij", -10);
309 test("abcdefghijklmnopqrst", 10, 0, "abcdefghijklmnopqrst", -20);
310 test("abcdefghijklmnopqrst", 10, 1, "", 1);
311 test("abcdefghijklmnopqrst", 10, 1, "abcde", 10);
312 test("abcdefghijklmnopqrst", 10, 1, "abcdefghij", 10);
313 test("abcdefghijklmnopqrst", 10, 1, "abcdefghijklmnopqrst", 10);
314 test("abcdefghijklmnopqrst", 10, 5, "", 5);
315 test("abcdefghijklmnopqrst", 10, 5, "abcde", 10);
316 test("abcdefghijklmnopqrst", 10, 5, "abcdefghij", 10);
317 test("abcdefghijklmnopqrst", 10, 5, "abcdefghijklmnopqrst", 10);
318 test("abcdefghijklmnopqrst", 10, 9, "", 9);
319 test("abcdefghijklmnopqrst", 10, 9, "abcde", 10);
320 test("abcdefghijklmnopqrst", 10, 9, "abcdefghij", 10);
321 test("abcdefghijklmnopqrst", 10, 9, "abcdefghijklmnopqrst", 10);
322 test("abcdefghijklmnopqrst", 10, 10, "", 10);
323 test("abcdefghijklmnopqrst", 10, 10, "abcde", 10);
324 test("abcdefghijklmnopqrst", 10, 10, "abcdefghij", 10);
325 test("abcdefghijklmnopqrst", 10, 10, "abcdefghijklmnopqrst", 10);
326 test("abcdefghijklmnopqrst", 10, 11, "", 10);
327 test("abcdefghijklmnopqrst", 10, 11, "abcde", 10);
328 test("abcdefghijklmnopqrst", 10, 11, "abcdefghij", 10);
329 test("abcdefghijklmnopqrst", 10, 11, "abcdefghijklmnopqrst", 10);
330 test("abcdefghijklmnopqrst", 19, 0, "", 0);
331 test("abcdefghijklmnopqrst", 19, 0, "abcde", -5);
332 test("abcdefghijklmnopqrst", 19, 0, "abcdefghij", -10);
333 test("abcdefghijklmnopqrst", 19, 0, "abcdefghijklmnopqrst", -20);
334 test("abcdefghijklmnopqrst", 19, 1, "", 1);
335 test("abcdefghijklmnopqrst", 19, 1, "abcde", 19);
336 test("abcdefghijklmnopqrst", 19, 1, "abcdefghij", 19);
337 test("abcdefghijklmnopqrst", 19, 1, "abcdefghijklmnopqrst", 19);
338 test("abcdefghijklmnopqrst", 19, 2, "", 1);
339 test("abcdefghijklmnopqrst", 19, 2, "abcde", 19);
340 test("abcdefghijklmnopqrst", 19, 2, "abcdefghij", 19);
341 test("abcdefghijklmnopqrst", 19, 2, "abcdefghijklmnopqrst", 19);
342 test("abcdefghijklmnopqrst", 20, 0, "", 0);
343 test("abcdefghijklmnopqrst", 20, 0, "abcde", -5);
344 test("abcdefghijklmnopqrst", 20, 0, "abcdefghij", -10);
345 test("abcdefghijklmnopqrst", 20, 0, "abcdefghijklmnopqrst", -20);
346 test("abcdefghijklmnopqrst", 20, 1, "", 0);
347 test("abcdefghijklmnopqrst", 20, 1, "abcde", -5);
348 test("abcdefghijklmnopqrst", 20, 1, "abcdefghij", -10);
349 test("abcdefghijklmnopqrst", 20, 1, "abcdefghijklmnopqrst", -20);
350 test("abcdefghijklmnopqrst", 21, 0, "", 0);
351 test("abcdefghijklmnopqrst", 21, 0, "abcde", 0);
352 test("abcdefghijklmnopqrst", 21, 0, "abcdefghij", 0);
353 test("abcdefghijklmnopqrst", 21, 0, "abcdefghijklmnopqrst", 0);
357 int main(int, char**) {
363 test("abcde", 5, 1, "", 0);
364 test("abcde", 2, 4, "", 3);
365 test("abcde", 2, 4, "abcde", 2);
366 test("ABCde", 2, 4, "abcde", -1);
370 test(L
"abcde", 5, 1, L
"", 0);
371 test(L
"abcde", 2, 4, L
"", 3);
372 test(L
"abcde", 2, 4, L
"abcde", 2);
373 test(L
"ABCde", 2, 4, L
"abcde", -1);
376 #if TEST_STD_VER >= 11
378 test(u
"abcde", 5, 1, u
"", 0);
379 test(u
"abcde", 2, 4, u
"", 3);
380 test(u
"abcde", 2, 4, u
"abcde", 2);
381 test(u
"ABCde", 2, 4, u
"abcde", -1);
385 test(U
"abcde", 5, 1, U
"", 0);
386 test(U
"abcde", 2, 4, U
"", 3);
387 test(U
"abcde", 2, 4, U
"abcde", 2);
388 test(U
"ABCde", 2, 4, U
"abcde", -1);
392 #if TEST_STD_VER > 11
394 typedef std::basic_string_view
<char, constexpr_char_traits
<char>> SV
;
395 constexpr SV sv1
{ "abcde", 5 };
396 constexpr SV sv2
{ "abcde", 0 };
397 static_assert ( sv1
.compare(5, 1, sv2
) == 0, "" );
398 static_assert ( sv1
.compare(2, 4, sv2
) > 0, "" );