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 // basic_string<charT,traits,Allocator>&
12 // replace(const_iterator i1, const_iterator i2, const basic_string& str); // constexpr since C++20
18 #include "test_macros.h"
19 #include "min_allocator.h"
20 #include "asan_testing.h"
23 TEST_CONSTEXPR_CXX20
void test(S s
, typename
S::size_type pos1
, typename
S::size_type n1
, S str
, S expected
) {
24 typename
S::size_type old_size
= s
.size();
25 typename
S::const_iterator first
= s
.begin() + pos1
;
26 typename
S::const_iterator last
= s
.begin() + pos1
+ n1
;
27 typename
S::size_type xlen
= last
- first
;
28 s
.replace(first
, last
, str
);
29 LIBCPP_ASSERT(s
.__invariants());
30 assert(s
== expected
);
31 typename
S::size_type rlen
= str
.size();
32 assert(s
.size() == old_size
- xlen
+ rlen
);
33 LIBCPP_ASSERT(is_string_asan_correct(s
));
37 TEST_CONSTEXPR_CXX20
bool test0() {
38 test(S(""), 0, 0, S(""), S(""));
39 test(S(""), 0, 0, S("12345"), S("12345"));
40 test(S(""), 0, 0, S("1234567890"), S("1234567890"));
41 test(S(""), 0, 0, S("12345678901234567890"), S("12345678901234567890"));
42 test(S("abcde"), 0, 0, S(""), S("abcde"));
43 test(S("abcde"), 0, 0, S("12345"), S("12345abcde"));
44 test(S("abcde"), 0, 0, S("1234567890"), S("1234567890abcde"));
45 test(S("abcde"), 0, 0, S("12345678901234567890"), S("12345678901234567890abcde"));
46 test(S("abcde"), 0, 1, S(""), S("bcde"));
47 test(S("abcde"), 0, 1, S("12345"), S("12345bcde"));
48 test(S("abcde"), 0, 1, S("1234567890"), S("1234567890bcde"));
49 test(S("abcde"), 0, 1, S("12345678901234567890"), S("12345678901234567890bcde"));
50 test(S("abcde"), 0, 2, S(""), S("cde"));
51 test(S("abcde"), 0, 2, S("12345"), S("12345cde"));
52 test(S("abcde"), 0, 2, S("1234567890"), S("1234567890cde"));
53 test(S("abcde"), 0, 2, S("12345678901234567890"), S("12345678901234567890cde"));
54 test(S("abcde"), 0, 4, S(""), S("e"));
55 test(S("abcde"), 0, 4, S("12345"), S("12345e"));
56 test(S("abcde"), 0, 4, S("1234567890"), S("1234567890e"));
57 test(S("abcde"), 0, 4, S("12345678901234567890"), S("12345678901234567890e"));
58 test(S("abcde"), 0, 5, S(""), S(""));
59 test(S("abcde"), 0, 5, S("12345"), S("12345"));
60 test(S("abcde"), 0, 5, S("1234567890"), S("1234567890"));
61 test(S("abcde"), 0, 5, S("12345678901234567890"), S("12345678901234567890"));
62 test(S("abcde"), 1, 0, S(""), S("abcde"));
63 test(S("abcde"), 1, 0, S("12345"), S("a12345bcde"));
64 test(S("abcde"), 1, 0, S("1234567890"), S("a1234567890bcde"));
65 test(S("abcde"), 1, 0, S("12345678901234567890"), S("a12345678901234567890bcde"));
66 test(S("abcde"), 1, 1, S(""), S("acde"));
67 test(S("abcde"), 1, 1, S("12345"), S("a12345cde"));
68 test(S("abcde"), 1, 1, S("1234567890"), S("a1234567890cde"));
69 test(S("abcde"), 1, 1, S("12345678901234567890"), S("a12345678901234567890cde"));
70 test(S("abcde"), 1, 2, S(""), S("ade"));
71 test(S("abcde"), 1, 2, S("12345"), S("a12345de"));
72 test(S("abcde"), 1, 2, S("1234567890"), S("a1234567890de"));
73 test(S("abcde"), 1, 2, S("12345678901234567890"), S("a12345678901234567890de"));
74 test(S("abcde"), 1, 3, S(""), S("ae"));
75 test(S("abcde"), 1, 3, S("12345"), S("a12345e"));
76 test(S("abcde"), 1, 3, S("1234567890"), S("a1234567890e"));
77 test(S("abcde"), 1, 3, S("12345678901234567890"), S("a12345678901234567890e"));
78 test(S("abcde"), 1, 4, S(""), S("a"));
79 test(S("abcde"), 1, 4, S("12345"), S("a12345"));
80 test(S("abcde"), 1, 4, S("1234567890"), S("a1234567890"));
81 test(S("abcde"), 1, 4, S("12345678901234567890"), S("a12345678901234567890"));
82 test(S("abcde"), 2, 0, S(""), S("abcde"));
83 test(S("abcde"), 2, 0, S("12345"), S("ab12345cde"));
84 test(S("abcde"), 2, 0, S("1234567890"), S("ab1234567890cde"));
85 test(S("abcde"), 2, 0, S("12345678901234567890"), S("ab12345678901234567890cde"));
86 test(S("abcde"), 2, 1, S(""), S("abde"));
87 test(S("abcde"), 2, 1, S("12345"), S("ab12345de"));
88 test(S("abcde"), 2, 1, S("1234567890"), S("ab1234567890de"));
89 test(S("abcde"), 2, 1, S("12345678901234567890"), S("ab12345678901234567890de"));
90 test(S("abcde"), 2, 2, S(""), S("abe"));
91 test(S("abcde"), 2, 2, S("12345"), S("ab12345e"));
92 test(S("abcde"), 2, 2, S("1234567890"), S("ab1234567890e"));
93 test(S("abcde"), 2, 2, S("12345678901234567890"), S("ab12345678901234567890e"));
94 test(S("abcde"), 2, 3, S(""), S("ab"));
95 test(S("abcde"), 2, 3, S("12345"), S("ab12345"));
96 test(S("abcde"), 2, 3, S("1234567890"), S("ab1234567890"));
97 test(S("abcde"), 2, 3, S("12345678901234567890"), S("ab12345678901234567890"));
98 test(S("abcde"), 4, 0, S(""), S("abcde"));
99 test(S("abcde"), 4, 0, S("12345"), S("abcd12345e"));
100 test(S("abcde"), 4, 0, S("1234567890"), S("abcd1234567890e"));
101 test(S("abcde"), 4, 0, S("12345678901234567890"), S("abcd12345678901234567890e"));
102 test(S("abcde"), 4, 1, S(""), S("abcd"));
103 test(S("abcde"), 4, 1, S("12345"), S("abcd12345"));
104 test(S("abcde"), 4, 1, S("1234567890"), S("abcd1234567890"));
105 test(S("abcde"), 4, 1, S("12345678901234567890"), S("abcd12345678901234567890"));
106 test(S("abcde"), 5, 0, S(""), S("abcde"));
107 test(S("abcde"), 5, 0, S("12345"), S("abcde12345"));
108 test(S("abcde"), 5, 0, S("1234567890"), S("abcde1234567890"));
109 test(S("abcde"), 5, 0, S("12345678901234567890"), S("abcde12345678901234567890"));
110 test(S("abcdefghij"), 0, 0, S(""), S("abcdefghij"));
111 test(S("abcdefghij"), 0, 0, S("12345"), S("12345abcdefghij"));
112 test(S("abcdefghij"), 0, 0, S("1234567890"), S("1234567890abcdefghij"));
113 test(S("abcdefghij"), 0, 0, S("12345678901234567890"), S("12345678901234567890abcdefghij"));
114 test(S("abcdefghij"), 0, 1, S(""), S("bcdefghij"));
115 test(S("abcdefghij"), 0, 1, S("12345"), S("12345bcdefghij"));
116 test(S("abcdefghij"), 0, 1, S("1234567890"), S("1234567890bcdefghij"));
117 test(S("abcdefghij"), 0, 1, S("12345678901234567890"), S("12345678901234567890bcdefghij"));
118 test(S("abcdefghij"), 0, 5, S(""), S("fghij"));
119 test(S("abcdefghij"), 0, 5, S("12345"), S("12345fghij"));
120 test(S("abcdefghij"), 0, 5, S("1234567890"), S("1234567890fghij"));
121 test(S("abcdefghij"), 0, 5, S("12345678901234567890"), S("12345678901234567890fghij"));
122 test(S("abcdefghij"), 0, 9, S(""), S("j"));
123 test(S("abcdefghij"), 0, 9, S("12345"), S("12345j"));
124 test(S("abcdefghij"), 0, 9, S("1234567890"), S("1234567890j"));
125 test(S("abcdefghij"), 0, 9, S("12345678901234567890"), S("12345678901234567890j"));
126 test(S("abcdefghij"), 0, 10, S(""), S(""));
127 test(S("abcdefghij"), 0, 10, S("12345"), S("12345"));
128 test(S("abcdefghij"), 0, 10, S("1234567890"), S("1234567890"));
129 test(S("abcdefghij"), 0, 10, S("12345678901234567890"), S("12345678901234567890"));
130 test(S("abcdefghij"), 1, 0, S(""), S("abcdefghij"));
131 test(S("abcdefghij"), 1, 0, S("12345"), S("a12345bcdefghij"));
132 test(S("abcdefghij"), 1, 0, S("1234567890"), S("a1234567890bcdefghij"));
133 test(S("abcdefghij"), 1, 0, S("12345678901234567890"), S("a12345678901234567890bcdefghij"));
134 test(S("abcdefghij"), 1, 1, S(""), S("acdefghij"));
135 test(S("abcdefghij"), 1, 1, S("12345"), S("a12345cdefghij"));
136 test(S("abcdefghij"), 1, 1, S("1234567890"), S("a1234567890cdefghij"));
137 test(S("abcdefghij"), 1, 1, S("12345678901234567890"), S("a12345678901234567890cdefghij"));
143 TEST_CONSTEXPR_CXX20
bool test1() {
144 test(S("abcdefghij"), 1, 4, S(""), S("afghij"));
145 test(S("abcdefghij"), 1, 4, S("12345"), S("a12345fghij"));
146 test(S("abcdefghij"), 1, 4, S("1234567890"), S("a1234567890fghij"));
147 test(S("abcdefghij"), 1, 4, S("12345678901234567890"), S("a12345678901234567890fghij"));
148 test(S("abcdefghij"), 1, 8, S(""), S("aj"));
149 test(S("abcdefghij"), 1, 8, S("12345"), S("a12345j"));
150 test(S("abcdefghij"), 1, 8, S("1234567890"), S("a1234567890j"));
151 test(S("abcdefghij"), 1, 8, S("12345678901234567890"), S("a12345678901234567890j"));
152 test(S("abcdefghij"), 1, 9, S(""), S("a"));
153 test(S("abcdefghij"), 1, 9, S("12345"), S("a12345"));
154 test(S("abcdefghij"), 1, 9, S("1234567890"), S("a1234567890"));
155 test(S("abcdefghij"), 1, 9, S("12345678901234567890"), S("a12345678901234567890"));
156 test(S("abcdefghij"), 5, 0, S(""), S("abcdefghij"));
157 test(S("abcdefghij"), 5, 0, S("12345"), S("abcde12345fghij"));
158 test(S("abcdefghij"), 5, 0, S("1234567890"), S("abcde1234567890fghij"));
159 test(S("abcdefghij"), 5, 0, S("12345678901234567890"), S("abcde12345678901234567890fghij"));
160 test(S("abcdefghij"), 5, 1, S(""), S("abcdeghij"));
161 test(S("abcdefghij"), 5, 1, S("12345"), S("abcde12345ghij"));
162 test(S("abcdefghij"), 5, 1, S("1234567890"), S("abcde1234567890ghij"));
163 test(S("abcdefghij"), 5, 1, S("12345678901234567890"), S("abcde12345678901234567890ghij"));
164 test(S("abcdefghij"), 5, 2, S(""), S("abcdehij"));
165 test(S("abcdefghij"), 5, 2, S("12345"), S("abcde12345hij"));
166 test(S("abcdefghij"), 5, 2, S("1234567890"), S("abcde1234567890hij"));
167 test(S("abcdefghij"), 5, 2, S("12345678901234567890"), S("abcde12345678901234567890hij"));
168 test(S("abcdefghij"), 5, 4, S(""), S("abcdej"));
169 test(S("abcdefghij"), 5, 4, S("12345"), S("abcde12345j"));
170 test(S("abcdefghij"), 5, 4, S("1234567890"), S("abcde1234567890j"));
171 test(S("abcdefghij"), 5, 4, S("12345678901234567890"), S("abcde12345678901234567890j"));
172 test(S("abcdefghij"), 5, 5, S(""), S("abcde"));
173 test(S("abcdefghij"), 5, 5, S("12345"), S("abcde12345"));
174 test(S("abcdefghij"), 5, 5, S("1234567890"), S("abcde1234567890"));
175 test(S("abcdefghij"), 5, 5, S("12345678901234567890"), S("abcde12345678901234567890"));
176 test(S("abcdefghij"), 9, 0, S(""), S("abcdefghij"));
177 test(S("abcdefghij"), 9, 0, S("12345"), S("abcdefghi12345j"));
178 test(S("abcdefghij"), 9, 0, S("1234567890"), S("abcdefghi1234567890j"));
179 test(S("abcdefghij"), 9, 0, S("12345678901234567890"), S("abcdefghi12345678901234567890j"));
180 test(S("abcdefghij"), 9, 1, S(""), S("abcdefghi"));
181 test(S("abcdefghij"), 9, 1, S("12345"), S("abcdefghi12345"));
182 test(S("abcdefghij"), 9, 1, S("1234567890"), S("abcdefghi1234567890"));
183 test(S("abcdefghij"), 9, 1, S("12345678901234567890"), S("abcdefghi12345678901234567890"));
184 test(S("abcdefghij"), 10, 0, S(""), S("abcdefghij"));
185 test(S("abcdefghij"), 10, 0, S("12345"), S("abcdefghij12345"));
186 test(S("abcdefghij"), 10, 0, S("1234567890"), S("abcdefghij1234567890"));
187 test(S("abcdefghij"), 10, 0, S("12345678901234567890"), S("abcdefghij12345678901234567890"));
188 test(S("abcdefghijklmnopqrst"), 0, 0, S(""), S("abcdefghijklmnopqrst"));
189 test(S("abcdefghijklmnopqrst"), 0, 0, S("12345"), S("12345abcdefghijklmnopqrst"));
190 test(S("abcdefghijklmnopqrst"), 0, 0, S("1234567890"), S("1234567890abcdefghijklmnopqrst"));
191 test(S("abcdefghijklmnopqrst"), 0, 0, S("12345678901234567890"), S("12345678901234567890abcdefghijklmnopqrst"));
192 test(S("abcdefghijklmnopqrst"), 0, 1, S(""), S("bcdefghijklmnopqrst"));
193 test(S("abcdefghijklmnopqrst"), 0, 1, S("12345"), S("12345bcdefghijklmnopqrst"));
194 test(S("abcdefghijklmnopqrst"), 0, 1, S("1234567890"), S("1234567890bcdefghijklmnopqrst"));
195 test(S("abcdefghijklmnopqrst"), 0, 1, S("12345678901234567890"), S("12345678901234567890bcdefghijklmnopqrst"));
196 test(S("abcdefghijklmnopqrst"), 0, 10, S(""), S("klmnopqrst"));
197 test(S("abcdefghijklmnopqrst"), 0, 10, S("12345"), S("12345klmnopqrst"));
198 test(S("abcdefghijklmnopqrst"), 0, 10, S("1234567890"), S("1234567890klmnopqrst"));
199 test(S("abcdefghijklmnopqrst"), 0, 10, S("12345678901234567890"), S("12345678901234567890klmnopqrst"));
200 test(S("abcdefghijklmnopqrst"), 0, 19, S(""), S("t"));
201 test(S("abcdefghijklmnopqrst"), 0, 19, S("12345"), S("12345t"));
202 test(S("abcdefghijklmnopqrst"), 0, 19, S("1234567890"), S("1234567890t"));
203 test(S("abcdefghijklmnopqrst"), 0, 19, S("12345678901234567890"), S("12345678901234567890t"));
204 test(S("abcdefghijklmnopqrst"), 0, 20, S(""), S(""));
205 test(S("abcdefghijklmnopqrst"), 0, 20, S("12345"), S("12345"));
206 test(S("abcdefghijklmnopqrst"), 0, 20, S("1234567890"), S("1234567890"));
207 test(S("abcdefghijklmnopqrst"), 0, 20, S("12345678901234567890"), S("12345678901234567890"));
208 test(S("abcdefghijklmnopqrst"), 1, 0, S(""), S("abcdefghijklmnopqrst"));
209 test(S("abcdefghijklmnopqrst"), 1, 0, S("12345"), S("a12345bcdefghijklmnopqrst"));
210 test(S("abcdefghijklmnopqrst"), 1, 0, S("1234567890"), S("a1234567890bcdefghijklmnopqrst"));
211 test(S("abcdefghijklmnopqrst"), 1, 0, S("12345678901234567890"), S("a12345678901234567890bcdefghijklmnopqrst"));
212 test(S("abcdefghijklmnopqrst"), 1, 1, S(""), S("acdefghijklmnopqrst"));
213 test(S("abcdefghijklmnopqrst"), 1, 1, S("12345"), S("a12345cdefghijklmnopqrst"));
214 test(S("abcdefghijklmnopqrst"), 1, 1, S("1234567890"), S("a1234567890cdefghijklmnopqrst"));
215 test(S("abcdefghijklmnopqrst"), 1, 1, S("12345678901234567890"), S("a12345678901234567890cdefghijklmnopqrst"));
216 test(S("abcdefghijklmnopqrst"), 1, 9, S(""), S("aklmnopqrst"));
217 test(S("abcdefghijklmnopqrst"), 1, 9, S("12345"), S("a12345klmnopqrst"));
218 test(S("abcdefghijklmnopqrst"), 1, 9, S("1234567890"), S("a1234567890klmnopqrst"));
219 test(S("abcdefghijklmnopqrst"), 1, 9, S("12345678901234567890"), S("a12345678901234567890klmnopqrst"));
220 test(S("abcdefghijklmnopqrst"), 1, 18, S(""), S("at"));
221 test(S("abcdefghijklmnopqrst"), 1, 18, S("12345"), S("a12345t"));
222 test(S("abcdefghijklmnopqrst"), 1, 18, S("1234567890"), S("a1234567890t"));
223 test(S("abcdefghijklmnopqrst"), 1, 18, S("12345678901234567890"), S("a12345678901234567890t"));
224 test(S("abcdefghijklmnopqrst"), 1, 19, S(""), S("a"));
225 test(S("abcdefghijklmnopqrst"), 1, 19, S("12345"), S("a12345"));
226 test(S("abcdefghijklmnopqrst"), 1, 19, S("1234567890"), S("a1234567890"));
227 test(S("abcdefghijklmnopqrst"), 1, 19, S("12345678901234567890"), S("a12345678901234567890"));
228 test(S("abcdefghijklmnopqrst"), 10, 0, S(""), S("abcdefghijklmnopqrst"));
229 test(S("abcdefghijklmnopqrst"), 10, 0, S("12345"), S("abcdefghij12345klmnopqrst"));
230 test(S("abcdefghijklmnopqrst"), 10, 0, S("1234567890"), S("abcdefghij1234567890klmnopqrst"));
231 test(S("abcdefghijklmnopqrst"), 10, 0, S("12345678901234567890"), S("abcdefghij12345678901234567890klmnopqrst"));
232 test(S("abcdefghijklmnopqrst"), 10, 1, S(""), S("abcdefghijlmnopqrst"));
233 test(S("abcdefghijklmnopqrst"), 10, 1, S("12345"), S("abcdefghij12345lmnopqrst"));
234 test(S("abcdefghijklmnopqrst"), 10, 1, S("1234567890"), S("abcdefghij1234567890lmnopqrst"));
235 test(S("abcdefghijklmnopqrst"), 10, 1, S("12345678901234567890"), S("abcdefghij12345678901234567890lmnopqrst"));
236 test(S("abcdefghijklmnopqrst"), 10, 5, S(""), S("abcdefghijpqrst"));
237 test(S("abcdefghijklmnopqrst"), 10, 5, S("12345"), S("abcdefghij12345pqrst"));
238 test(S("abcdefghijklmnopqrst"), 10, 5, S("1234567890"), S("abcdefghij1234567890pqrst"));
239 test(S("abcdefghijklmnopqrst"), 10, 5, S("12345678901234567890"), S("abcdefghij12345678901234567890pqrst"));
240 test(S("abcdefghijklmnopqrst"), 10, 9, S(""), S("abcdefghijt"));
241 test(S("abcdefghijklmnopqrst"), 10, 9, S("12345"), S("abcdefghij12345t"));
242 test(S("abcdefghijklmnopqrst"), 10, 9, S("1234567890"), S("abcdefghij1234567890t"));
243 test(S("abcdefghijklmnopqrst"), 10, 9, S("12345678901234567890"), S("abcdefghij12345678901234567890t"));
249 TEST_CONSTEXPR_CXX20
bool test2() {
250 test(S("abcdefghijklmnopqrst"), 10, 10, S(""), S("abcdefghij"));
251 test(S("abcdefghijklmnopqrst"), 10, 10, S("12345"), S("abcdefghij12345"));
252 test(S("abcdefghijklmnopqrst"), 10, 10, S("1234567890"), S("abcdefghij1234567890"));
253 test(S("abcdefghijklmnopqrst"), 10, 10, S("12345678901234567890"), S("abcdefghij12345678901234567890"));
254 test(S("abcdefghijklmnopqrst"), 19, 0, S(""), S("abcdefghijklmnopqrst"));
255 test(S("abcdefghijklmnopqrst"), 19, 0, S("12345"), S("abcdefghijklmnopqrs12345t"));
256 test(S("abcdefghijklmnopqrst"), 19, 0, S("1234567890"), S("abcdefghijklmnopqrs1234567890t"));
257 test(S("abcdefghijklmnopqrst"), 19, 0, S("12345678901234567890"), S("abcdefghijklmnopqrs12345678901234567890t"));
258 test(S("abcdefghijklmnopqrst"), 19, 1, S(""), S("abcdefghijklmnopqrs"));
259 test(S("abcdefghijklmnopqrst"), 19, 1, S("12345"), S("abcdefghijklmnopqrs12345"));
260 test(S("abcdefghijklmnopqrst"), 19, 1, S("1234567890"), S("abcdefghijklmnopqrs1234567890"));
261 test(S("abcdefghijklmnopqrst"), 19, 1, S("12345678901234567890"), S("abcdefghijklmnopqrs12345678901234567890"));
262 test(S("abcdefghijklmnopqrst"), 20, 0, S(""), S("abcdefghijklmnopqrst"));
263 test(S("abcdefghijklmnopqrst"), 20, 0, S("12345"), S("abcdefghijklmnopqrst12345"));
264 test(S("abcdefghijklmnopqrst"), 20, 0, S("1234567890"), S("abcdefghijklmnopqrst1234567890"));
265 test(S("abcdefghijklmnopqrst"), 20, 0, S("12345678901234567890"), S("abcdefghijklmnopqrst12345678901234567890"));
276 #if TEST_STD_VER > 17
277 static_assert(test0
<S
>());
278 static_assert(test1
<S
>());
279 static_assert(test2
<S
>());
283 #if TEST_STD_VER >= 11
286 s
.replace(s
.cbegin(), s
.cend(), {"abc", 1});
287 assert(s
.size() == 1);
293 int main(int, char**) {
295 #if TEST_STD_VER >= 11
296 test
<std::basic_string
<char, std::char_traits
<char>, min_allocator
<char>>>();
297 test
<std::basic_string
<char, std::char_traits
<char>, safe_allocator
<char>>>();