Debian Codegraph @

libcxx/test/std/strings/basic.string/string.modifiers/string_replace/replace_with_range.pass.cpp

raw
1 //===----------------------------------------------------------------------===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10 // ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=10000000
11 // ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-ops-limit): -fconstexpr-ops-limit=70000000
12
13 // template<container-compatible-range<charT> R>
14 // constexpr basic_string& replace_with_range(const_iterator i1, const_iterator i2, R&& rg); // C++23
15
16 #include <string>
17 #include <utility>
18
19 #include "../../../../containers/sequences/insert_range_sequence_containers.h"
20 #include "test_macros.h"
21
22 template <class Range>
23 concept HasReplaceWithRange =
24 requires(std::string& c, Range&& range) { c.replace_with_range(c.end(), c.end(), std::forward<Range>(range)); };
25
26 constexpr bool test_constraints_replace_with_range() {
27 // Input range with the same value type.
28 static_assert(HasReplaceWithRange<InputRange<char>>);
29 // Input range with a convertible value type.
30 static_assert(HasReplaceWithRange<InputRange<unsigned char>>);
31 // Input range with a non-convertible value type.
32 static_assert(!HasReplaceWithRange<InputRange<Empty>>);
33 // Not an input range.
34 static_assert(!HasReplaceWithRange<InputRangeNotDerivedFrom>);
35 static_assert(!HasReplaceWithRange<InputRangeNotIndirectlyReadable>);
36 static_assert(!HasReplaceWithRange<InputRangeNotInputOrOutputIterator>);
37
38 return true;
39 }
40
41 using StrBuffer = Buffer<char, 256>;
42
43 struct TestCaseReplacement {
44 StrBuffer initial;
45 std::size_t from = 0;
46 std::size_t to = 0;
47 StrBuffer input;
48 StrBuffer expected;
49 };
50
51 // Permutation matrix:
52 // - initial string: empty / one-element / n elements;
53 // - cut starts from: beginning / middle / end;
54 // - cut size: empty / one-element / several elements / until the end;
55 // - input range: empty / one-element / middle-sized / longer than SSO / longer than the current string capacity.
56
57 // Empty string.
58
59 constexpr TestCaseReplacement EmptyString_End_EmptyCut_EmptyRange{
60 .initial = "", .from = 0, .to = 0, .input = "", .expected = ""};
61
62 constexpr TestCaseReplacement EmptyString_End_EmptyCut_OneElementRange{
63 .initial = "", .from = 0, .to = 0, .input = "a", .expected = "a"};
64
65 constexpr TestCaseReplacement EmptyString_End_EmptyCut_MidRange{
66 .initial = "", .from = 0, .to = 0, .input = "aeiou", .expected = "aeiou"};
67
68 constexpr TestCaseReplacement EmptyString_End_EmptyCut_LongRange{
69 .initial = "",
70 .from = 0,
71 .to = 0,
72 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
73 .expected = "abcdefghijklmnopqrstuvwxyz0123456789"};
74
75 // One-element string.
76
77 constexpr TestCaseReplacement OneElementString_Begin_EmptyCut_EmptyRange{
78 .initial = "B", .from = 0, .to = 0, .input = "", .expected = "B"};
79
80 constexpr TestCaseReplacement OneElementString_Begin_EmptyCut_OneElementRange{
81 .initial = "B", .from = 0, .to = 0, .input = "a", .expected = "aB"};
82
83 constexpr TestCaseReplacement OneElementString_Begin_EmptyCut_MidRange{
84 .initial = "B", .from = 0, .to = 0, .input = "aeiou", .expected = "aeiouB"};
85
86 constexpr TestCaseReplacement OneElementString_Begin_EmptyCut_LongRange{
87 .initial = "B",
88 .from = 0,
89 .to = 0,
90 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
91 .expected = "abcdefghijklmnopqrstuvwxyz0123456789B"};
92
93 constexpr TestCaseReplacement OneElementString_Begin_OneElementCut_EmptyRange{
94 .initial = "B", .from = 0, .to = 1, .input = "", .expected = ""};
95
96 constexpr TestCaseReplacement OneElementString_Begin_OneElementCut_OneElementRange{
97 .initial = "B", .from = 0, .to = 1, .input = "a", .expected = "a"};
98
99 constexpr TestCaseReplacement OneElementString_Begin_OneElementCut_MidRange{
100 .initial = "B", .from = 0, .to = 1, .input = "aeiou", .expected = "aeiou"};
101
102 constexpr TestCaseReplacement OneElementString_Begin_OneElementCut_LongRange{
103 .initial = "B",
104 .from = 0,
105 .to = 1,
106 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
107 .expected = "abcdefghijklmnopqrstuvwxyz0123456789"};
108
109 constexpr TestCaseReplacement OneElementString_End_EmptyCut_EmptyRange{
110 .initial = "B", .from = 1, .to = 1, .input = "", .expected = "B"};
111
112 constexpr TestCaseReplacement OneElementString_End_EmptyCut_OneElementRange{
113 .initial = "B", .from = 1, .to = 1, .input = "a", .expected = "Ba"};
114
115 constexpr TestCaseReplacement OneElementString_End_EmptyCut_MidRange{
116 .initial = "B", .from = 1, .to = 1, .input = "aeiou", .expected = "Baeiou"};
117
118 constexpr TestCaseReplacement OneElementString_End_EmptyCut_LongRange{
119 .initial = "B",
120 .from = 1,
121 .to = 1,
122 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
123 .expected = "Babcdefghijklmnopqrstuvwxyz0123456789"};
124
125 // Short string (using SSO).
126
127 // Replace at the beginning.
128
129 constexpr TestCaseReplacement ShortString_Begin_EmptyCut_EmptyRange{
130 .initial = "_BCDFGHJ_", .from = 0, .to = 0, .input = "", .expected = "_BCDFGHJ_"};
131
132 constexpr TestCaseReplacement ShortString_Begin_EmptyCut_OneElementRange{
133 .initial = "_BCDFGHJ_", .from = 0, .to = 0, .input = "a", .expected = "a_BCDFGHJ_"};
134
135 constexpr TestCaseReplacement ShortString_Begin_EmptyCut_MidRange{
136 .initial = "_BCDFGHJ_", .from = 0, .to = 0, .input = "aeiou", .expected = "aeiou_BCDFGHJ_"};
137
138 constexpr TestCaseReplacement ShortString_Begin_EmptyCut_LongRange{
139 .initial = "_BCDFGHJ_",
140 .from = 0,
141 .to = 0,
142 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
143 .expected = "abcdefghijklmnopqrstuvwxyz0123456789_BCDFGHJ_"};
144
145 constexpr TestCaseReplacement ShortString_Begin_OneElementCut_EmptyRange{
146 .initial = "_BCDFGHJ_", .from = 0, .to = 1, .input = "", .expected = "BCDFGHJ_"};
147
148 constexpr TestCaseReplacement ShortString_Begin_OneElementCut_OneElementRange{
149 .initial = "_BCDFGHJ_", .from = 0, .to = 1, .input = "a", .expected = "aBCDFGHJ_"};
150
151 constexpr TestCaseReplacement ShortString_Begin_OneElementCut_MidRange{
152 .initial = "_BCDFGHJ_", .from = 0, .to = 1, .input = "aeiou", .expected = "aeiouBCDFGHJ_"};
153
154 constexpr TestCaseReplacement ShortString_Begin_OneElementCut_LongRange{
155 .initial = "_BCDFGHJ_",
156 .from = 0,
157 .to = 1,
158 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
159 .expected = "abcdefghijklmnopqrstuvwxyz0123456789BCDFGHJ_"};
160
161 constexpr TestCaseReplacement ShortString_Begin_MidSizedCut_EmptyRange{
162 .initial = "_BCDFGHJ_", .from = 0, .to = 3, .input = "", .expected = "DFGHJ_"};
163
164 constexpr TestCaseReplacement ShortString_Begin_MidSizedCut_OneElementRange{
165 .initial = "_BCDFGHJ_", .from = 0, .to = 3, .input = "a", .expected = "aDFGHJ_"};
166
167 constexpr TestCaseReplacement ShortString_Begin_MidSizedCut_MidRange{
168 .initial = "_BCDFGHJ_", .from = 0, .to = 3, .input = "aeiou", .expected = "aeiouDFGHJ_"};
169
170 // Note: this test case switches from SSO to non-SSO.
171 constexpr TestCaseReplacement ShortString_Begin_MidSizedCut_LongRange{
172 .initial = "_BCDFGHJ_",
173 .from = 0,
174 .to = 3,
175 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
176 .expected = "abcdefghijklmnopqrstuvwxyz0123456789DFGHJ_"};
177
178 constexpr TestCaseReplacement ShortString_Begin_CutToEnd_EmptyRange{
179 .initial = "_BCDFGHJ_", .from = 0, .to = 9, .input = "", .expected = ""};
180
181 constexpr TestCaseReplacement ShortString_Begin_CutToEnd_OneElementRange{
182 .initial = "_BCDFGHJ_", .from = 0, .to = 9, .input = "a", .expected = "a"};
183
184 constexpr TestCaseReplacement ShortString_Begin_CutToEnd_MidRange{
185 .initial = "_BCDFGHJ_", .from = 0, .to = 9, .input = "aeiou", .expected = "aeiou"};
186
187 // Note: this test case switches from SSO to non-SSO.
188 constexpr TestCaseReplacement ShortString_Begin_CutToEnd_LongRange{
189 .initial = "_BCDFGHJ_",
190 .from = 0,
191 .to = 9,
192 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
193 .expected = "abcdefghijklmnopqrstuvwxyz0123456789"};
194
195 // Replace in the middle.
196
197 constexpr TestCaseReplacement ShortString_Mid_EmptyCut_EmptyRange{
198 .initial = "_BCDFGHJ_", .from = 4, .to = 4, .input = "", .expected = "_BCDFGHJ_"};
199
200 constexpr TestCaseReplacement ShortString_Mid_EmptyCut_OneElementRange{
201 .initial = "_BCDFGHJ_", .from = 4, .to = 4, .input = "a", .expected = "_BCDaFGHJ_"};
202
203 constexpr TestCaseReplacement ShortString_Mid_EmptyCut_MidRange{
204 .initial = "_BCDFGHJ_", .from = 4, .to = 4, .input = "aeiou", .expected = "_BCDaeiouFGHJ_"};
205
206 constexpr TestCaseReplacement ShortString_Mid_EmptyCut_LongRange{
207 .initial = "_BCDFGHJ_",
208 .from = 4,
209 .to = 4,
210 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
211 .expected = "_BCDabcdefghijklmnopqrstuvwxyz0123456789FGHJ_"};
212
213 constexpr TestCaseReplacement ShortString_Mid_OneElementCut_EmptyRange{
214 .initial = "_BCDFGHJ_", .from = 4, .to = 5, .input = "", .expected = "_BCDGHJ_"};
215
216 constexpr TestCaseReplacement ShortString_Mid_OneElementCut_OneElementRange{
217 .initial = "_BCDFGHJ_", .from = 4, .to = 5, .input = "a", .expected = "_BCDaGHJ_"};
218
219 constexpr TestCaseReplacement ShortString_Mid_OneElementCut_MidRange{
220 .initial = "_BCDFGHJ_", .from = 4, .to = 5, .input = "aeiou", .expected = "_BCDaeiouGHJ_"};
221
222 constexpr TestCaseReplacement ShortString_Mid_OneElementCut_LongRange{
223 .initial = "_BCDFGHJ_",
224 .from = 4,
225 .to = 5,
226 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
227 .expected = "_BCDabcdefghijklmnopqrstuvwxyz0123456789GHJ_"};
228
229 constexpr TestCaseReplacement ShortString_Mid_MidSizedCut_EmptyRange{
230 .initial = "_BCDFGHJ_", .from = 4, .to = 7, .input = "", .expected = "_BCDJ_"};
231
232 constexpr TestCaseReplacement ShortString_Mid_MidSizedCut_OneElementRange{
233 .initial = "_BCDFGHJ_", .from = 4, .to = 7, .input = "a", .expected = "_BCDaJ_"};
234
235 constexpr TestCaseReplacement ShortString_Mid_MidSizedCut_MidRange{
236 .initial = "_BCDFGHJ_", .from = 4, .to = 7, .input = "aeiou", .expected = "_BCDaeiouJ_"};
237
238 constexpr TestCaseReplacement ShortString_Mid_MidSizedCut_LongRange{
239 .initial = "_BCDFGHJ_",
240 .from = 4,
241 .to = 7,
242 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
243 .expected = "_BCDabcdefghijklmnopqrstuvwxyz0123456789J_"};
244
245 constexpr TestCaseReplacement ShortString_Mid_CutToEnd_EmptyRange{
246 .initial = "_BCDFGHJ_", .from = 4, .to = 9, .input = "", .expected = "_BCD"};
247
248 constexpr TestCaseReplacement ShortString_Mid_CutToEnd_OneElementRange{
249 .initial = "_BCDFGHJ_", .from = 4, .to = 9, .input = "a", .expected = "_BCDa"};
250
251 constexpr TestCaseReplacement ShortString_Mid_CutToEnd_MidRange{
252 .initial = "_BCDFGHJ_", .from = 4, .to = 9, .input = "aeiou", .expected = "_BCDaeiou"};
253
254 constexpr TestCaseReplacement ShortString_Mid_CutToEnd_LongRange{
255 .initial = "_BCDFGHJ_",
256 .from = 4,
257 .to = 9,
258 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
259 .expected = "_BCDabcdefghijklmnopqrstuvwxyz0123456789"};
260
261 // Replace at the end.
262
263 constexpr TestCaseReplacement ShortString_End_EmptyCut_EmptyRange{
264 .initial = "_BCDFGHJ_", .from = 9, .to = 9, .input = "", .expected = "_BCDFGHJ_"};
265
266 constexpr TestCaseReplacement ShortString_End_EmptyCut_OneElementRange{
267 .initial = "_BCDFGHJ_", .from = 9, .to = 9, .input = "a", .expected = "_BCDFGHJ_a"};
268
269 constexpr TestCaseReplacement ShortString_End_EmptyCut_MidRange{
270 .initial = "_BCDFGHJ_", .from = 9, .to = 9, .input = "aeiou", .expected = "_BCDFGHJ_aeiou"};
271
272 constexpr TestCaseReplacement ShortString_End_EmptyCut_LongRange{
273 .initial = "_BCDFGHJ_",
274 .from = 9,
275 .to = 9,
276 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
277 .expected = "_BCDFGHJ_abcdefghijklmnopqrstuvwxyz0123456789"};
278
279 // Long string (no SSO).
280
281 // Replace at the beginning.
282
283 constexpr TestCaseReplacement LongString_Begin_EmptyCut_EmptyRange{
284 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
285 .from = 0,
286 .to = 0,
287 .input = "",
288 .expected = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
289
290 constexpr TestCaseReplacement LongString_Begin_EmptyCut_OneElementRange{
291 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
292 .from = 0,
293 .to = 0,
294 .input = "a",
295 .expected = "a_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
296
297 constexpr TestCaseReplacement LongString_Begin_EmptyCut_MidRange{
298 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
299 .from = 0,
300 .to = 0,
301 .input = "aeiou",
302 .expected = "aeiou_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
303
304 constexpr TestCaseReplacement LongString_Begin_EmptyCut_LongRange{
305 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
306 .from = 0,
307 .to = 0,
308 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
309 .expected = "abcdefghijklmnopqrstuvwxyz0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
310
311 constexpr TestCaseReplacement LongString_Begin_EmptyCut_LongerThanCapacityRange{
312 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
313 .from = 0,
314 .to = 0,
315 .input = "abcdefghijklmnopqrstuvwxyz0123456789_"
316 "abcdefghijklmnopqrstuvwxyz0123456789_"
317 "abcdefghijklmnopqrstuvwxyz0123456789",
318 .expected = "abcdefghijklmnopqrstuvwxyz0123456789_"
319 "abcdefghijklmnopqrstuvwxyz0123456789_"
320 "abcdefghijklmnopqrstuvwxyz0123456789"
321 "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
322
323 constexpr TestCaseReplacement LongString_Begin_OneElementCut_EmptyRange{
324 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
325 .from = 0,
326 .to = 1,
327 .input = "",
328 .expected = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
329
330 constexpr TestCaseReplacement LongString_Begin_OneElementCut_OneElementRange{
331 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
332 .from = 0,
333 .to = 1,
334 .input = "a",
335 .expected = "aABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
336
337 constexpr TestCaseReplacement LongString_Begin_OneElementCut_MidRange{
338 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
339 .from = 0,
340 .to = 1,
341 .input = "aeiou",
342 .expected = "aeiouABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
343
344 constexpr TestCaseReplacement LongString_Begin_OneElementCut_LongRange{
345 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
346 .from = 0,
347 .to = 1,
348 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
349 .expected = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
350
351 constexpr TestCaseReplacement LongString_Begin_OneElementCut_LongerThanCapacityRange{
352 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
353 .from = 0,
354 .to = 1,
355 .input = "abcdefghijklmnopqrstuvwxyz0123456789_"
356 "abcdefghijklmnopqrstuvwxyz0123456789_"
357 "abcdefghijklmnopqrstuvwxyz0123456789",
358 .expected = "abcdefghijklmnopqrstuvwxyz0123456789_"
359 "abcdefghijklmnopqrstuvwxyz0123456789_"
360 "abcdefghijklmnopqrstuvwxyz0123456789"
361 "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
362
363 constexpr TestCaseReplacement LongString_Begin_MidSizedCut_EmptyRange{
364 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
365 .from = 0,
366 .to = 3,
367 .input = "",
368 .expected = "CDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
369
370 constexpr TestCaseReplacement LongString_Begin_MidSizedCut_OneElementRange{
371 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
372 .from = 0,
373 .to = 3,
374 .input = "a",
375 .expected = "aCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
376
377 constexpr TestCaseReplacement LongString_Begin_MidSizedCut_MidRange{
378 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
379 .from = 0,
380 .to = 3,
381 .input = "aeiou",
382 .expected = "aeiouCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
383
384 constexpr TestCaseReplacement LongString_Begin_MidSizedCut_LongRange{
385 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
386 .from = 0,
387 .to = 3,
388 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
389 .expected = "abcdefghijklmnopqrstuvwxyz0123456789CDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
390
391 constexpr TestCaseReplacement LongString_Begin_MidSizedCut_LongerThanCapacityRange{
392 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
393 .from = 0,
394 .to = 3,
395 .input = "abcdefghijklmnopqrstuvwxyz0123456789_"
396 "abcdefghijklmnopqrstuvwxyz0123456789_"
397 "abcdefghijklmnopqrstuvwxyz0123456789",
398 .expected = "abcdefghijklmnopqrstuvwxyz0123456789_"
399 "abcdefghijklmnopqrstuvwxyz0123456789_"
400 "abcdefghijklmnopqrstuvwxyz0123456789"
401 "CDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
402
403 constexpr TestCaseReplacement LongString_Begin_CutToEnd_EmptyRange{
404 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_", .from = 0, .to = 38, .input = "", .expected = ""};
405
406 constexpr TestCaseReplacement LongString_Begin_CutToEnd_OneElementRange{
407 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_", .from = 0, .to = 38, .input = "a", .expected = "a"};
408
409 constexpr TestCaseReplacement LongString_Begin_CutToEnd_MidRange{
410 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_", .from = 0, .to = 38, .input = "aeiou", .expected = "aeiou"};
411
412 constexpr TestCaseReplacement LongString_Begin_CutToEnd_LongRange{
413 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
414 .from = 0,
415 .to = 38,
416 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
417 .expected = "abcdefghijklmnopqrstuvwxyz0123456789"};
418
419 constexpr TestCaseReplacement LongString_Begin_CutToEnd_LongerThanCapacityRange{
420 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
421 .from = 0,
422 .to = 38,
423 .input = "abcdefghijklmnopqrstuvwxyz0123456789_"
424 "abcdefghijklmnopqrstuvwxyz0123456789_"
425 "abcdefghijklmnopqrstuvwxyz0123456789",
426 .expected = "abcdefghijklmnopqrstuvwxyz0123456789_"
427 "abcdefghijklmnopqrstuvwxyz0123456789_"
428 "abcdefghijklmnopqrstuvwxyz0123456789"};
429
430 // Replace in the middle.
431
432 constexpr TestCaseReplacement LongString_Mid_EmptyCut_EmptyRange{
433 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
434 .from = 18,
435 .to = 18,
436 .input = "",
437 .expected = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
438
439 constexpr TestCaseReplacement LongString_Mid_EmptyCut_OneElementRange{
440 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
441 .from = 18,
442 .to = 18,
443 .input = "a",
444 .expected = "_ABCDEFGHIJKLMNOPQaRSTUVWXYZ0123456789_"};
445
446 constexpr TestCaseReplacement LongString_Mid_EmptyCut_MidRange{
447 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
448 .from = 18,
449 .to = 18,
450 .input = "aeiou",
451 .expected = "_ABCDEFGHIJKLMNOPQaeiouRSTUVWXYZ0123456789_"};
452
453 constexpr TestCaseReplacement LongString_Mid_EmptyCut_LongRange{
454 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
455 .from = 18,
456 .to = 18,
457 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
458 .expected = "_ABCDEFGHIJKLMNOPQabcdefghijklmnopqrstuvwxyz0123456789RSTUVWXYZ0123456789_"};
459
460 constexpr TestCaseReplacement LongString_Mid_EmptyCut_LongerThanCapacityRange{
461 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
462 .from = 18,
463 .to = 18,
464 .input = "abcdefghijklmnopqrstuvwxyz0123456789_"
465 "abcdefghijklmnopqrstuvwxyz0123456789_"
466 "abcdefghijklmnopqrstuvwxyz0123456789",
467 .expected =
468 "_ABCDEFGHIJKLMNOPQ"
469 "abcdefghijklmnopqrstuvwxyz0123456789_"
470 "abcdefghijklmnopqrstuvwxyz0123456789_"
471 "abcdefghijklmnopqrstuvwxyz0123456789"
472 "RSTUVWXYZ0123456789_"};
473
474 constexpr TestCaseReplacement LongString_Mid_OneElementCut_EmptyRange{
475 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
476 .from = 18,
477 .to = 19,
478 .input = "",
479 .expected = "_ABCDEFGHIJKLMNOPQSTUVWXYZ0123456789_"};
480
481 constexpr TestCaseReplacement LongString_Mid_OneElementCut_OneElementRange{
482 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
483 .from = 18,
484 .to = 19,
485 .input = "a",
486 .expected = "_ABCDEFGHIJKLMNOPQaSTUVWXYZ0123456789_"};
487
488 constexpr TestCaseReplacement LongString_Mid_OneElementCut_MidRange{
489 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
490 .from = 18,
491 .to = 19,
492 .input = "aeiou",
493 .expected = "_ABCDEFGHIJKLMNOPQaeiouSTUVWXYZ0123456789_"};
494
495 constexpr TestCaseReplacement LongString_Mid_OneElementCut_LongRange{
496 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
497 .from = 18,
498 .to = 19,
499 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
500 .expected = "_ABCDEFGHIJKLMNOPQabcdefghijklmnopqrstuvwxyz0123456789STUVWXYZ0123456789_"};
501
502 constexpr TestCaseReplacement LongString_Mid_OneElementCut_LongerThanCapacityRange{
503 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
504 .from = 18,
505 .to = 19,
506 .input = "abcdefghijklmnopqrstuvwxyz0123456789_"
507 "abcdefghijklmnopqrstuvwxyz0123456789_"
508 "abcdefghijklmnopqrstuvwxyz0123456789",
509 .expected =
510 "_ABCDEFGHIJKLMNOPQ"
511 "abcdefghijklmnopqrstuvwxyz0123456789_"
512 "abcdefghijklmnopqrstuvwxyz0123456789_"
513 "abcdefghijklmnopqrstuvwxyz0123456789"
514 "STUVWXYZ0123456789_"};
515
516 constexpr TestCaseReplacement LongString_Mid_MidSizedCut_EmptyRange{
517 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
518 .from = 18,
519 .to = 21,
520 .input = "",
521 .expected = "_ABCDEFGHIJKLMNOPQUVWXYZ0123456789_"};
522
523 constexpr TestCaseReplacement LongString_Mid_MidSizedCut_OneElementRange{
524 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
525 .from = 18,
526 .to = 21,
527 .input = "a",
528 .expected = "_ABCDEFGHIJKLMNOPQaUVWXYZ0123456789_"};
529
530 constexpr TestCaseReplacement LongString_Mid_MidSizedCut_MidRange{
531 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
532 .from = 18,
533 .to = 21,
534 .input = "aeiou",
535 .expected = "_ABCDEFGHIJKLMNOPQaeiouUVWXYZ0123456789_"};
536
537 constexpr TestCaseReplacement LongString_Mid_MidSizedCut_LongRange{
538 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
539 .from = 18,
540 .to = 21,
541 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
542 .expected = "_ABCDEFGHIJKLMNOPQabcdefghijklmnopqrstuvwxyz0123456789UVWXYZ0123456789_"};
543
544 constexpr TestCaseReplacement LongString_Mid_MidSizedCut_LongerThanCapacityRange{
545 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
546 .from = 18,
547 .to = 21,
548 .input = "abcdefghijklmnopqrstuvwxyz0123456789_"
549 "abcdefghijklmnopqrstuvwxyz0123456789_"
550 "abcdefghijklmnopqrstuvwxyz0123456789",
551 .expected =
552 "_ABCDEFGHIJKLMNOPQ"
553 "abcdefghijklmnopqrstuvwxyz0123456789_"
554 "abcdefghijklmnopqrstuvwxyz0123456789_"
555 "abcdefghijklmnopqrstuvwxyz0123456789"
556 "UVWXYZ0123456789_"};
557
558 constexpr TestCaseReplacement LongString_Mid_CutToEnd_EmptyRange{
559 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
560 .from = 18,
561 .to = 38,
562 .input = "",
563 .expected = "_ABCDEFGHIJKLMNOPQ"};
564
565 constexpr TestCaseReplacement LongString_Mid_CutToEnd_OneElementRange{
566 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
567 .from = 18,
568 .to = 38,
569 .input = "a",
570 .expected = "_ABCDEFGHIJKLMNOPQa"};
571
572 constexpr TestCaseReplacement LongString_Mid_CutToEnd_MidRange{
573 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
574 .from = 18,
575 .to = 38,
576 .input = "aeiou",
577 .expected = "_ABCDEFGHIJKLMNOPQaeiou"};
578
579 constexpr TestCaseReplacement LongString_Mid_CutToEnd_LongRange{
580 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
581 .from = 18,
582 .to = 38,
583 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
584 .expected = "_ABCDEFGHIJKLMNOPQabcdefghijklmnopqrstuvwxyz0123456789"};
585
586 constexpr TestCaseReplacement LongString_Mid_CutToEnd_LongerThanCapacityRange{
587 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
588 .from = 18,
589 .to = 38,
590 .input = "abcdefghijklmnopqrstuvwxyz0123456789_"
591 "abcdefghijklmnopqrstuvwxyz0123456789_"
592 "abcdefghijklmnopqrstuvwxyz0123456789",
593 .expected = "_ABCDEFGHIJKLMNOPQ"
594 "abcdefghijklmnopqrstuvwxyz0123456789_"
595 "abcdefghijklmnopqrstuvwxyz0123456789_"
596 "abcdefghijklmnopqrstuvwxyz0123456789"};
597
598 // Replace at the end.
599
600 constexpr TestCaseReplacement LongString_End_EmptyCut_EmptyRange{
601 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
602 .from = 38,
603 .to = 38,
604 .input = "",
605 .expected = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"};
606
607 constexpr TestCaseReplacement LongString_End_EmptyCut_OneElementRange{
608 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
609 .from = 38,
610 .to = 38,
611 .input = "a",
612 .expected = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_a"};
613
614 constexpr TestCaseReplacement LongString_End_EmptyCut_MidRange{
615 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
616 .from = 38,
617 .to = 38,
618 .input = "aeiou",
619 .expected = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_aeiou"};
620
621 constexpr TestCaseReplacement LongString_End_EmptyCut_LongRange{
622 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
623 .from = 38,
624 .to = 38,
625 .input = "abcdefghijklmnopqrstuvwxyz0123456789",
626 .expected = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_abcdefghijklmnopqrstuvwxyz0123456789"};
627
628 constexpr TestCaseReplacement LongString_End_EmptyCut_LongerThanCapacityRange{
629 .initial = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_",
630 .from = 38,
631 .to = 38,
632 .input = "abcdefghijklmnopqrstuvwxyz0123456789_"
633 "abcdefghijklmnopqrstuvwxyz0123456789_"
634 "abcdefghijklmnopqrstuvwxyz0123456789",
635 .expected = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"
636 "abcdefghijklmnopqrstuvwxyz0123456789_"
637 "abcdefghijklmnopqrstuvwxyz0123456789_"
638 "abcdefghijklmnopqrstuvwxyz0123456789"};
639
640 template <class Iter, class Sent, class Alloc>
641 constexpr void test_string_replace_with_range() {
642 auto test = [&](const TestCaseReplacement& test_case) {
643 using Container = std::basic_string<char, std::char_traits<char>, Alloc>;
644
645 auto get_pos = [](auto& c, auto index) { return std::ranges::next(c.begin(), index); };
646 Container c(test_case.initial.begin(), test_case.initial.end());
647 auto in = wrap_input<Iter, Sent>(test_case.input);
648 auto from = get_pos(c, test_case.from);
649 auto to = get_pos(c, test_case.to);
650
651 Container& result = c.replace_with_range(from, to, in);
652 assert(&result == &c);
653 LIBCPP_ASSERT(c.__invariants());
654 return std::ranges::equal(c, test_case.expected);
655 };
656
657 { // Empty string.
658 // empty_str.replace_with_range(end, end, empty_range)
659 assert(test(EmptyString_End_EmptyCut_EmptyRange));
660 // empty_str.replace_with_range(end, end, one_element_range)
661 assert(test(EmptyString_End_EmptyCut_OneElementRange));
662 // empty_str.replace_with_range(end, end, mid_range)
663 assert(test(EmptyString_End_EmptyCut_MidRange));
664 // empty_str.replace_with_range(end, end, long_range)
665 assert(test(EmptyString_End_EmptyCut_LongRange));
666 }
667
668 { // One-element string.
669 // one_element_str.replace_with_range(begin, begin, empty_range)
670 assert(test(OneElementString_Begin_EmptyCut_EmptyRange));
671 // one_element_str.replace_with_range(begin, begin, one_element_range)
672 assert(test(OneElementString_Begin_EmptyCut_OneElementRange));
673 // one_element_str.replace_with_range(begin, begin, mid_range)
674 assert(test(OneElementString_Begin_EmptyCut_MidRange));
675 // one_element_str.replace_with_range(begin, begin, long_range)
676 assert(test(OneElementString_Begin_EmptyCut_LongRange));
677 // one_element_str.replace_with_range(begin, begin + 1, empty_range)
678 assert(test(OneElementString_Begin_OneElementCut_EmptyRange));
679 // one_element_str.replace_with_range(begin, begin + 1, one_element_range)
680 assert(test(OneElementString_Begin_OneElementCut_OneElementRange));
681 // one_element_str.replace_with_range(begin, begin + 1, mid_range)
682 assert(test(OneElementString_Begin_OneElementCut_MidRange));
683 // one_element_str.replace_with_range(begin, begin + 1, long_range)
684 assert(test(OneElementString_Begin_OneElementCut_LongRange));
685 // one_element_str.replace_with_range(end, end, empty_range)
686 assert(test(OneElementString_End_EmptyCut_EmptyRange));
687 // one_element_str.replace_with_range(end, end, one_element_range)
688 assert(test(OneElementString_End_EmptyCut_OneElementRange));
689 // one_element_str.replace_with_range(end, end, mid_range)
690 assert(test(OneElementString_End_EmptyCut_MidRange));
691 // one_element_str.replace_with_range(end, end, long_range)
692 assert(test(OneElementString_End_EmptyCut_LongRange));
693 }
694
695 { // Short string.
696 // Replace at the beginning.
697
698 // short_str.replace_with_range(begin, begin, empty_range)
699 assert(test(ShortString_Begin_EmptyCut_EmptyRange));
700 // short_str.replace_with_range(begin, begin, one_element_range)
701 assert(test(ShortString_Begin_EmptyCut_OneElementRange));
702 // short_str.replace_with_range(begin, begin, mid_range)
703 assert(test(ShortString_Begin_EmptyCut_MidRange));
704 // short_str.replace_with_range(begin, begin, long_range)
705 assert(test(ShortString_Begin_EmptyCut_LongRange));
706 // short_str.replace_with_range(begin, begin + 1, empty_range)
707 assert(test(ShortString_Begin_OneElementCut_EmptyRange));
708 // short_str.replace_with_range(begin, begin + 1, one_element_range)
709 assert(test(ShortString_Begin_OneElementCut_OneElementRange));
710 // short_str.replace_with_range(begin, begin + 1, mid_range)
711 assert(test(ShortString_Begin_OneElementCut_MidRange));
712 // short_str.replace_with_range(begin, begin + 1, long_range)
713 assert(test(ShortString_Begin_OneElementCut_LongRange));
714 // short_str.replace_with_range(begin, begin + 3, empty_range)
715 assert(test(ShortString_Begin_MidSizedCut_EmptyRange));
716 // short_str.replace_with_range(begin, begin + 3, one_element_range)
717 assert(test(ShortString_Begin_MidSizedCut_OneElementRange));
718 // short_str.replace_with_range(begin, begin + 3, mid_range)
719 assert(test(ShortString_Begin_MidSizedCut_MidRange));
720 // short_str.replace_with_range(begin, begin + 3, long_range)
721 assert(test(ShortString_Begin_MidSizedCut_LongRange));
722 // short_str.replace_with_range(begin, end, empty_range)
723 assert(test(ShortString_Begin_CutToEnd_EmptyRange));
724 // short_str.replace_with_range(begin, end, one_element_range)
725 assert(test(ShortString_Begin_CutToEnd_OneElementRange));
726 // short_str.replace_with_range(begin, end, mid_range)
727 assert(test(ShortString_Begin_CutToEnd_MidRange));
728 // short_str.replace_with_range(begin, end, long_range)
729 assert(test(ShortString_Begin_CutToEnd_LongRange));
730
731 // Replace in the middle.
732
733 // short_str.replace_with_range(mid, mid, empty_range)
734 assert(test(ShortString_Mid_EmptyCut_EmptyRange));
735 // short_str.replace_with_range(mid, mid, one_element_range)
736 assert(test(ShortString_Mid_EmptyCut_OneElementRange));
737 // short_str.replace_with_range(mid, mid, mid_range)
738 assert(test(ShortString_Mid_EmptyCut_MidRange));
739 // short_str.replace_with_range(mid, mid, long_range)
740 assert(test(ShortString_Mid_EmptyCut_LongRange));
741 // short_str.replace_with_range(mid, mid + 1, empty_range)
742 assert(test(ShortString_Mid_OneElementCut_EmptyRange));
743 // short_str.replace_with_range(mid, mid + 1, one_element_range)
744 assert(test(ShortString_Mid_OneElementCut_OneElementRange));
745 // short_str.replace_with_range(mid, mid + 1, mid_range)
746 assert(test(ShortString_Mid_OneElementCut_MidRange));
747 // short_str.replace_with_range(mid, mid + 1, long_range)
748 assert(test(ShortString_Mid_OneElementCut_LongRange));
749 // short_str.replace_with_range(mid, mid + 3, empty_range)
750 assert(test(ShortString_Mid_MidSizedCut_EmptyRange));
751 // short_str.replace_with_range(mid, mid + 3, one_element_range)
752 assert(test(ShortString_Mid_MidSizedCut_OneElementRange));
753 // short_str.replace_with_range(mid, mid + 3, mid_range)
754 assert(test(ShortString_Mid_MidSizedCut_MidRange));
755 // short_str.replace_with_range(mid, mid + 3, long_range)
756 assert(test(ShortString_Mid_MidSizedCut_LongRange));
757 // short_str.replace_with_range(mid, end, empty_range)
758 assert(test(ShortString_Mid_CutToEnd_EmptyRange));
759 // short_str.replace_with_range(mid, end, one_element_range)
760 assert(test(ShortString_Mid_CutToEnd_OneElementRange));
761 // short_str.replace_with_range(mid, end, mid_range)
762 assert(test(ShortString_Mid_CutToEnd_MidRange));
763 // short_str.replace_with_range(mid, end, long_range)
764 assert(test(ShortString_Mid_CutToEnd_LongRange));
765
766 // Replace at the end.
767
768 // short_str.replace_with_range(end, end, empty_range)
769 assert(test(ShortString_End_EmptyCut_EmptyRange));
770 // short_str.replace_with_range(end, end, one_element_range)
771 assert(test(ShortString_End_EmptyCut_OneElementRange));
772 // short_str.replace_with_range(end, end, mid_range)
773 assert(test(ShortString_End_EmptyCut_MidRange));
774 // short_str.replace_with_range(end, end, long_range)
775 assert(test(ShortString_End_EmptyCut_LongRange));
776 }
777
778 { // Long string.
779 // Replace at the beginning.
780
781 // long_str.replace_with_range(begin, begin, empty_range)
782 assert(test(LongString_Begin_EmptyCut_EmptyRange));
783 // long_str.replace_with_range(begin, begin, one_element_range)
784 assert(test(LongString_Begin_EmptyCut_OneElementRange));
785 // long_str.replace_with_range(begin, begin, mid_range)
786 assert(test(LongString_Begin_EmptyCut_MidRange));
787 // long_str.replace_with_range(begin, begin, long_range)
788 assert(test(LongString_Begin_EmptyCut_LongRange));
789 // long_str.replace_with_range(begin, begin, longer_than_capacity_range)
790 assert(test(LongString_Begin_EmptyCut_LongerThanCapacityRange));
791 // long_str.replace_with_range(begin, begin + 1, empty_range)
792 assert(test(LongString_Begin_OneElementCut_EmptyRange));
793 // long_str.replace_with_range(begin, begin + 1, one_element_range)
794 assert(test(LongString_Begin_OneElementCut_OneElementRange));
795 // long_str.replace_with_range(begin, begin + 1, mid_range)
796 assert(test(LongString_Begin_OneElementCut_MidRange));
797 // long_str.replace_with_range(begin, begin + 1, long_range)
798 assert(test(LongString_Begin_OneElementCut_LongRange));
799 // long_str.replace_with_range(begin, begin + 1, longer_than_capacity_range)
800 assert(test(LongString_Begin_OneElementCut_LongerThanCapacityRange));
801 // long_str.replace_with_range(begin, begin + 3, empty_range)
802 assert(test(LongString_Begin_MidSizedCut_EmptyRange));
803 // long_str.replace_with_range(begin, begin + 3, one_element_range)
804 assert(test(LongString_Begin_MidSizedCut_OneElementRange));
805 // long_str.replace_with_range(begin, begin + 3, mid_range)
806 assert(test(LongString_Begin_MidSizedCut_MidRange));
807 // long_str.replace_with_range(begin, begin + 3, long_range)
808 assert(test(LongString_Begin_MidSizedCut_LongRange));
809 // long_str.replace_with_range(begin, begin + 3, longer_than_capacity_range)
810 assert(test(LongString_Begin_MidSizedCut_LongerThanCapacityRange));
811 // long_str.replace_with_range(begin, end, empty_range)
812 assert(test(LongString_Begin_CutToEnd_EmptyRange));
813 // long_str.replace_with_range(begin, end, one_element_range)
814 assert(test(LongString_Begin_CutToEnd_OneElementRange));
815 // long_str.replace_with_range(begin, end, mid_range)
816 assert(test(LongString_Begin_CutToEnd_MidRange));
817 // long_str.replace_with_range(begin, end, long_range)
818 assert(test(LongString_Begin_CutToEnd_LongRange));
819 // long_str.replace_with_range(begin, end, longer_than_capacity_range)
820 assert(test(LongString_Begin_CutToEnd_LongerThanCapacityRange));
821
822 // Replace in the middle.
823
824 // long_str.replace_with_range(mid, mid, empty_range)
825 assert(test(LongString_Mid_EmptyCut_EmptyRange));
826 // long_str.replace_with_range(mid, mid, one_element_range)
827 assert(test(LongString_Mid_EmptyCut_OneElementRange));
828 // long_str.replace_with_range(mid, mid, mid_range)
829 assert(test(LongString_Mid_EmptyCut_MidRange));
830 // long_str.replace_with_range(mid, mid, long_range)
831 assert(test(LongString_Mid_EmptyCut_LongRange));
832 // long_str.replace_with_range(mid, mid, longer_than_capacity_range)
833 assert(test(LongString_Mid_EmptyCut_LongerThanCapacityRange));
834 // long_str.replace_with_range(mid, mid + 1, empty_range)
835 assert(test(LongString_Mid_OneElementCut_EmptyRange));
836 // long_str.replace_with_range(mid, mid + 1, one_element_range)
837 assert(test(LongString_Mid_OneElementCut_OneElementRange));
838 // long_str.replace_with_range(mid, mid + 1, mid_range)
839 assert(test(LongString_Mid_OneElementCut_MidRange));
840 // long_str.replace_with_range(mid, mid + 1, long_range)
841 assert(test(LongString_Mid_OneElementCut_LongRange));
842 // long_str.replace_with_range(mid, mid + 1, longer_than_capacity_range)
843 assert(test(LongString_Mid_OneElementCut_LongerThanCapacityRange));
844 // long_str.replace_with_range(mid, mid + 3, empty_range)
845 assert(test(LongString_Mid_MidSizedCut_EmptyRange));
846 // long_str.replace_with_range(mid, mid + 3, one_element_range)
847 assert(test(LongString_Mid_MidSizedCut_OneElementRange));
848 // long_str.replace_with_range(mid, mid + 3, mid_range)
849 assert(test(LongString_Mid_MidSizedCut_MidRange));
850 // long_str.replace_with_range(mid, mid + 3, long_range)
851 assert(test(LongString_Mid_MidSizedCut_LongRange));
852 // long_str.replace_with_range(mid, mid + 3, longer_than_capacity_range)
853 assert(test(LongString_Mid_MidSizedCut_LongerThanCapacityRange));
854 // long_str.replace_with_range(mid, end, empty_range)
855 assert(test(LongString_Mid_CutToEnd_EmptyRange));
856 // long_str.replace_with_range(mid, end, one_element_range)
857 assert(test(LongString_Mid_CutToEnd_OneElementRange));
858 // long_str.replace_with_range(mid, end, mid_range)
859 assert(test(LongString_Mid_CutToEnd_MidRange));
860 // long_str.replace_with_range(mid, end, long_range)
861 assert(test(LongString_Mid_CutToEnd_LongRange));
862 // long_str.replace_with_range(mid, end, longer_than_capacity_range)
863 assert(test(LongString_Mid_CutToEnd_LongerThanCapacityRange));
864
865 // Replace at the end.
866
867 // long_str.replace_with_range(end, end, empty_range)
868 assert(test(LongString_End_EmptyCut_EmptyRange));
869 // long_str.replace_with_range(end, end, one_element_range)
870 assert(test(LongString_End_EmptyCut_OneElementRange));
871 // long_str.replace_with_range(end, end, mid_range)
872 assert(test(LongString_End_EmptyCut_MidRange));
873 // long_str.replace_with_range(end, end, long_range)
874 assert(test(LongString_End_EmptyCut_LongRange));
875 // long_str.replace_with_range(end, end, longer_than_capacity_range)
876 assert(test(LongString_End_EmptyCut_LongerThanCapacityRange));
877 }
878 }
879
880 constexpr void test_string_replace_with_range_rvalue_range() {
881 // Make sure that the input range can be passed by both an lvalue and an rvalue reference and regardless of constness.
882
883 { // Lvalue.
884 std::string in = "abc";
885 std::string c = "123";
886 c.replace_with_range(c.begin(), c.end(), in);
887 }
888
889 { // Const lvalue.
890 const std::string in = "abc";
891 std::string c = "123";
892 c.replace_with_range(c.begin(), c.end(), in);
893 }
894
895 { // Rvalue.
896 std::string in = "abc";
897 std::string c = "123";
898 c.replace_with_range(c.begin(), c.end(), std::move(in));
899 }
900
901 { // Const rvalue.
902 const std::string in = "abc";
903 std::string c = "123";
904 c.replace_with_range(c.begin(), c.end(), std::move(in));
905 }
906 }
907
908 constexpr bool test_constexpr() {
909 for_all_iterators_and_allocators_constexpr<char, const char*>([]<class Iter, class Sent, class Alloc>() {
910 test_string_replace_with_range<Iter, Sent, Alloc>();
911 });
912 test_string_replace_with_range_rvalue_range();
913
914 return true;
915 }
916
917 // Tested cases:
918 // - different kinds of replacements (varying the size of the initial string, the cut point and the cut size, and the
919 // size of the input range);
920 // - an exception is thrown when allocating new elements.
921 int main(int, char**) {
922 static_assert(test_constraints_replace_with_range());
923
924 for_all_iterators_and_allocators<char, const char*>([]<class Iter, class Sent, class Alloc>() {
925 test_string_replace_with_range<Iter, Sent, Alloc>();
926 });
927 test_string_replace_with_range_rvalue_range();
928 static_assert(test_constexpr());
929
930 // Note: `test_string_replace_with_range_exception_safety_throwing_copy` doesn't apply because copying chars cannot
931 // throw.
932 {
933 #if !defined(TEST_HAS_NO_EXCEPTIONS)
934 // Note: the input string must be long enough to prevent SSO, otherwise the allocator won't be used.
935 std::string in(64, 'a');
936
937 try {
938 ThrowingAllocator<char> alloc;
939
940 globalMemCounter.reset();
941 std::basic_string<char, std::char_traits<char>, ThrowingAllocator<char>> c(alloc);
942 c.replace_with_range(c.end(), c.end(), in);
943 assert(false); // The function call above should throw.
944
945 } catch (int) {
946 assert(globalMemCounter.new_called == globalMemCounter.delete_called);
947 }
948 #endif
949 }
950
951 return 0;
952 }