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 // <string>
10
11 // basic_string<charT,traits,Allocator>&
12 // replace(const_iterator i1, const_iterator i2, size_type n, charT c);
13
14 #include <string>
15 #include <algorithm>
16 #include <cassert>
17
18 #include "test_macros.h"
19 #include "min_allocator.h"
20
21 template <class S>
22 void
test(S s,typename S::size_type pos1,typename S::size_type n1,typename S::size_type n2,typename S::value_type c,S expected)23 test(S s, typename S::size_type pos1, typename S::size_type n1, typename S::size_type n2,
24 typename S::value_type c, S expected)
25 {
26 typename S::size_type old_size = s.size();
27 typename S::const_iterator first = s.begin() + pos1;
28 typename S::const_iterator last = s.begin() + pos1 + n1;
29 typename S::size_type xlen = last - first;
30 s.replace(first, last, n2, c);
31 LIBCPP_ASSERT(s.__invariants());
32 assert(s == expected);
33 typename S::size_type rlen = n2;
34 assert(s.size() == old_size - xlen + rlen);
35 }
36
37 template <class S>
test0()38 void test0()
39 {
40 test(S(""), 0, 0, 0, '3', S(""));
41 test(S(""), 0, 0, 5, '3', S("33333"));
42 test(S(""), 0, 0, 10, '3', S("3333333333"));
43 test(S(""), 0, 0, 20, '3', S("33333333333333333333"));
44 test(S("abcde"), 0, 0, 0, '3', S("abcde"));
45 test(S("abcde"), 0, 0, 5, '3', S("33333abcde"));
46 test(S("abcde"), 0, 0, 10, '3', S("3333333333abcde"));
47 test(S("abcde"), 0, 0, 20, '3', S("33333333333333333333abcde"));
48 test(S("abcde"), 0, 1, 0, '3', S("bcde"));
49 test(S("abcde"), 0, 1, 5, '3', S("33333bcde"));
50 test(S("abcde"), 0, 1, 10, '3', S("3333333333bcde"));
51 test(S("abcde"), 0, 1, 20, '3', S("33333333333333333333bcde"));
52 test(S("abcde"), 0, 2, 0, '3', S("cde"));
53 test(S("abcde"), 0, 2, 5, '3', S("33333cde"));
54 test(S("abcde"), 0, 2, 10, '3', S("3333333333cde"));
55 test(S("abcde"), 0, 2, 20, '3', S("33333333333333333333cde"));
56 test(S("abcde"), 0, 4, 0, '3', S("e"));
57 test(S("abcde"), 0, 4, 5, '3', S("33333e"));
58 test(S("abcde"), 0, 4, 10, '3', S("3333333333e"));
59 test(S("abcde"), 0, 4, 20, '3', S("33333333333333333333e"));
60 test(S("abcde"), 0, 5, 0, '3', S(""));
61 test(S("abcde"), 0, 5, 5, '3', S("33333"));
62 test(S("abcde"), 0, 5, 10, '3', S("3333333333"));
63 test(S("abcde"), 0, 5, 20, '3', S("33333333333333333333"));
64 test(S("abcde"), 1, 0, 0, '3', S("abcde"));
65 test(S("abcde"), 1, 0, 5, '3', S("a33333bcde"));
66 test(S("abcde"), 1, 0, 10, '3', S("a3333333333bcde"));
67 test(S("abcde"), 1, 0, 20, '3', S("a33333333333333333333bcde"));
68 test(S("abcde"), 1, 1, 0, '3', S("acde"));
69 test(S("abcde"), 1, 1, 5, '3', S("a33333cde"));
70 test(S("abcde"), 1, 1, 10, '3', S("a3333333333cde"));
71 test(S("abcde"), 1, 1, 20, '3', S("a33333333333333333333cde"));
72 test(S("abcde"), 1, 2, 0, '3', S("ade"));
73 test(S("abcde"), 1, 2, 5, '3', S("a33333de"));
74 test(S("abcde"), 1, 2, 10, '3', S("a3333333333de"));
75 test(S("abcde"), 1, 2, 20, '3', S("a33333333333333333333de"));
76 test(S("abcde"), 1, 3, 0, '3', S("ae"));
77 test(S("abcde"), 1, 3, 5, '3', S("a33333e"));
78 test(S("abcde"), 1, 3, 10, '3', S("a3333333333e"));
79 test(S("abcde"), 1, 3, 20, '3', S("a33333333333333333333e"));
80 test(S("abcde"), 1, 4, 0, '3', S("a"));
81 test(S("abcde"), 1, 4, 5, '3', S("a33333"));
82 test(S("abcde"), 1, 4, 10, '3', S("a3333333333"));
83 test(S("abcde"), 1, 4, 20, '3', S("a33333333333333333333"));
84 test(S("abcde"), 2, 0, 0, '3', S("abcde"));
85 test(S("abcde"), 2, 0, 5, '3', S("ab33333cde"));
86 test(S("abcde"), 2, 0, 10, '3', S("ab3333333333cde"));
87 test(S("abcde"), 2, 0, 20, '3', S("ab33333333333333333333cde"));
88 test(S("abcde"), 2, 1, 0, '3', S("abde"));
89 test(S("abcde"), 2, 1, 5, '3', S("ab33333de"));
90 test(S("abcde"), 2, 1, 10, '3', S("ab3333333333de"));
91 test(S("abcde"), 2, 1, 20, '3', S("ab33333333333333333333de"));
92 test(S("abcde"), 2, 2, 0, '3', S("abe"));
93 test(S("abcde"), 2, 2, 5, '3', S("ab33333e"));
94 test(S("abcde"), 2, 2, 10, '3', S("ab3333333333e"));
95 test(S("abcde"), 2, 2, 20, '3', S("ab33333333333333333333e"));
96 test(S("abcde"), 2, 3, 0, '3', S("ab"));
97 test(S("abcde"), 2, 3, 5, '3', S("ab33333"));
98 test(S("abcde"), 2, 3, 10, '3', S("ab3333333333"));
99 test(S("abcde"), 2, 3, 20, '3', S("ab33333333333333333333"));
100 test(S("abcde"), 4, 0, 0, '3', S("abcde"));
101 test(S("abcde"), 4, 0, 5, '3', S("abcd33333e"));
102 test(S("abcde"), 4, 0, 10, '3', S("abcd3333333333e"));
103 test(S("abcde"), 4, 0, 20, '3', S("abcd33333333333333333333e"));
104 test(S("abcde"), 4, 1, 0, '3', S("abcd"));
105 test(S("abcde"), 4, 1, 5, '3', S("abcd33333"));
106 test(S("abcde"), 4, 1, 10, '3', S("abcd3333333333"));
107 test(S("abcde"), 4, 1, 20, '3', S("abcd33333333333333333333"));
108 test(S("abcde"), 5, 0, 0, '3', S("abcde"));
109 test(S("abcde"), 5, 0, 5, '3', S("abcde33333"));
110 test(S("abcde"), 5, 0, 10, '3', S("abcde3333333333"));
111 test(S("abcde"), 5, 0, 20, '3', S("abcde33333333333333333333"));
112 test(S("abcdefghij"), 0, 0, 0, '3', S("abcdefghij"));
113 test(S("abcdefghij"), 0, 0, 5, '3', S("33333abcdefghij"));
114 test(S("abcdefghij"), 0, 0, 10, '3', S("3333333333abcdefghij"));
115 test(S("abcdefghij"), 0, 0, 20, '3', S("33333333333333333333abcdefghij"));
116 test(S("abcdefghij"), 0, 1, 0, '3', S("bcdefghij"));
117 test(S("abcdefghij"), 0, 1, 5, '3', S("33333bcdefghij"));
118 test(S("abcdefghij"), 0, 1, 10, '3', S("3333333333bcdefghij"));
119 test(S("abcdefghij"), 0, 1, 20, '3', S("33333333333333333333bcdefghij"));
120 test(S("abcdefghij"), 0, 5, 0, '3', S("fghij"));
121 test(S("abcdefghij"), 0, 5, 5, '3', S("33333fghij"));
122 test(S("abcdefghij"), 0, 5, 10, '3', S("3333333333fghij"));
123 test(S("abcdefghij"), 0, 5, 20, '3', S("33333333333333333333fghij"));
124 test(S("abcdefghij"), 0, 9, 0, '3', S("j"));
125 test(S("abcdefghij"), 0, 9, 5, '3', S("33333j"));
126 test(S("abcdefghij"), 0, 9, 10, '3', S("3333333333j"));
127 test(S("abcdefghij"), 0, 9, 20, '3', S("33333333333333333333j"));
128 test(S("abcdefghij"), 0, 10, 0, '3', S(""));
129 test(S("abcdefghij"), 0, 10, 5, '3', S("33333"));
130 test(S("abcdefghij"), 0, 10, 10, '3', S("3333333333"));
131 test(S("abcdefghij"), 0, 10, 20, '3', S("33333333333333333333"));
132 test(S("abcdefghij"), 1, 0, 0, '3', S("abcdefghij"));
133 test(S("abcdefghij"), 1, 0, 5, '3', S("a33333bcdefghij"));
134 test(S("abcdefghij"), 1, 0, 10, '3', S("a3333333333bcdefghij"));
135 test(S("abcdefghij"), 1, 0, 20, '3', S("a33333333333333333333bcdefghij"));
136 test(S("abcdefghij"), 1, 1, 0, '3', S("acdefghij"));
137 test(S("abcdefghij"), 1, 1, 5, '3', S("a33333cdefghij"));
138 test(S("abcdefghij"), 1, 1, 10, '3', S("a3333333333cdefghij"));
139 test(S("abcdefghij"), 1, 1, 20, '3', S("a33333333333333333333cdefghij"));
140 }
141
142 template <class S>
test1()143 void test1()
144 {
145 test(S("abcdefghij"), 1, 4, 0, '3', S("afghij"));
146 test(S("abcdefghij"), 1, 4, 5, '3', S("a33333fghij"));
147 test(S("abcdefghij"), 1, 4, 10, '3', S("a3333333333fghij"));
148 test(S("abcdefghij"), 1, 4, 20, '3', S("a33333333333333333333fghij"));
149 test(S("abcdefghij"), 1, 8, 0, '3', S("aj"));
150 test(S("abcdefghij"), 1, 8, 5, '3', S("a33333j"));
151 test(S("abcdefghij"), 1, 8, 10, '3', S("a3333333333j"));
152 test(S("abcdefghij"), 1, 8, 20, '3', S("a33333333333333333333j"));
153 test(S("abcdefghij"), 1, 9, 0, '3', S("a"));
154 test(S("abcdefghij"), 1, 9, 5, '3', S("a33333"));
155 test(S("abcdefghij"), 1, 9, 10, '3', S("a3333333333"));
156 test(S("abcdefghij"), 1, 9, 20, '3', S("a33333333333333333333"));
157 test(S("abcdefghij"), 5, 0, 0, '3', S("abcdefghij"));
158 test(S("abcdefghij"), 5, 0, 5, '3', S("abcde33333fghij"));
159 test(S("abcdefghij"), 5, 0, 10, '3', S("abcde3333333333fghij"));
160 test(S("abcdefghij"), 5, 0, 20, '3', S("abcde33333333333333333333fghij"));
161 test(S("abcdefghij"), 5, 1, 0, '3', S("abcdeghij"));
162 test(S("abcdefghij"), 5, 1, 5, '3', S("abcde33333ghij"));
163 test(S("abcdefghij"), 5, 1, 10, '3', S("abcde3333333333ghij"));
164 test(S("abcdefghij"), 5, 1, 20, '3', S("abcde33333333333333333333ghij"));
165 test(S("abcdefghij"), 5, 2, 0, '3', S("abcdehij"));
166 test(S("abcdefghij"), 5, 2, 5, '3', S("abcde33333hij"));
167 test(S("abcdefghij"), 5, 2, 10, '3', S("abcde3333333333hij"));
168 test(S("abcdefghij"), 5, 2, 20, '3', S("abcde33333333333333333333hij"));
169 test(S("abcdefghij"), 5, 4, 0, '3', S("abcdej"));
170 test(S("abcdefghij"), 5, 4, 5, '3', S("abcde33333j"));
171 test(S("abcdefghij"), 5, 4, 10, '3', S("abcde3333333333j"));
172 test(S("abcdefghij"), 5, 4, 20, '3', S("abcde33333333333333333333j"));
173 test(S("abcdefghij"), 5, 5, 0, '3', S("abcde"));
174 test(S("abcdefghij"), 5, 5, 5, '3', S("abcde33333"));
175 test(S("abcdefghij"), 5, 5, 10, '3', S("abcde3333333333"));
176 test(S("abcdefghij"), 5, 5, 20, '3', S("abcde33333333333333333333"));
177 test(S("abcdefghij"), 9, 0, 0, '3', S("abcdefghij"));
178 test(S("abcdefghij"), 9, 0, 5, '3', S("abcdefghi33333j"));
179 test(S("abcdefghij"), 9, 0, 10, '3', S("abcdefghi3333333333j"));
180 test(S("abcdefghij"), 9, 0, 20, '3', S("abcdefghi33333333333333333333j"));
181 test(S("abcdefghij"), 9, 1, 0, '3', S("abcdefghi"));
182 test(S("abcdefghij"), 9, 1, 5, '3', S("abcdefghi33333"));
183 test(S("abcdefghij"), 9, 1, 10, '3', S("abcdefghi3333333333"));
184 test(S("abcdefghij"), 9, 1, 20, '3', S("abcdefghi33333333333333333333"));
185 test(S("abcdefghij"), 10, 0, 0, '3', S("abcdefghij"));
186 test(S("abcdefghij"), 10, 0, 5, '3', S("abcdefghij33333"));
187 test(S("abcdefghij"), 10, 0, 10, '3', S("abcdefghij3333333333"));
188 test(S("abcdefghij"), 10, 0, 20, '3', S("abcdefghij33333333333333333333"));
189 test(S("abcdefghijklmnopqrst"), 0, 0, 0, '3', S("abcdefghijklmnopqrst"));
190 test(S("abcdefghijklmnopqrst"), 0, 0, 5, '3', S("33333abcdefghijklmnopqrst"));
191 test(S("abcdefghijklmnopqrst"), 0, 0, 10, '3', S("3333333333abcdefghijklmnopqrst"));
192 test(S("abcdefghijklmnopqrst"), 0, 0, 20, '3', S("33333333333333333333abcdefghijklmnopqrst"));
193 test(S("abcdefghijklmnopqrst"), 0, 1, 0, '3', S("bcdefghijklmnopqrst"));
194 test(S("abcdefghijklmnopqrst"), 0, 1, 5, '3', S("33333bcdefghijklmnopqrst"));
195 test(S("abcdefghijklmnopqrst"), 0, 1, 10, '3', S("3333333333bcdefghijklmnopqrst"));
196 test(S("abcdefghijklmnopqrst"), 0, 1, 20, '3', S("33333333333333333333bcdefghijklmnopqrst"));
197 test(S("abcdefghijklmnopqrst"), 0, 10, 0, '3', S("klmnopqrst"));
198 test(S("abcdefghijklmnopqrst"), 0, 10, 5, '3', S("33333klmnopqrst"));
199 test(S("abcdefghijklmnopqrst"), 0, 10, 10, '3', S("3333333333klmnopqrst"));
200 test(S("abcdefghijklmnopqrst"), 0, 10, 20, '3', S("33333333333333333333klmnopqrst"));
201 test(S("abcdefghijklmnopqrst"), 0, 19, 0, '3', S("t"));
202 test(S("abcdefghijklmnopqrst"), 0, 19, 5, '3', S("33333t"));
203 test(S("abcdefghijklmnopqrst"), 0, 19, 10, '3', S("3333333333t"));
204 test(S("abcdefghijklmnopqrst"), 0, 19, 20, '3', S("33333333333333333333t"));
205 test(S("abcdefghijklmnopqrst"), 0, 20, 0, '3', S(""));
206 test(S("abcdefghijklmnopqrst"), 0, 20, 5, '3', S("33333"));
207 test(S("abcdefghijklmnopqrst"), 0, 20, 10, '3', S("3333333333"));
208 test(S("abcdefghijklmnopqrst"), 0, 20, 20, '3', S("33333333333333333333"));
209 test(S("abcdefghijklmnopqrst"), 1, 0, 0, '3', S("abcdefghijklmnopqrst"));
210 test(S("abcdefghijklmnopqrst"), 1, 0, 5, '3', S("a33333bcdefghijklmnopqrst"));
211 test(S("abcdefghijklmnopqrst"), 1, 0, 10, '3', S("a3333333333bcdefghijklmnopqrst"));
212 test(S("abcdefghijklmnopqrst"), 1, 0, 20, '3', S("a33333333333333333333bcdefghijklmnopqrst"));
213 test(S("abcdefghijklmnopqrst"), 1, 1, 0, '3', S("acdefghijklmnopqrst"));
214 test(S("abcdefghijklmnopqrst"), 1, 1, 5, '3', S("a33333cdefghijklmnopqrst"));
215 test(S("abcdefghijklmnopqrst"), 1, 1, 10, '3', S("a3333333333cdefghijklmnopqrst"));
216 test(S("abcdefghijklmnopqrst"), 1, 1, 20, '3', S("a33333333333333333333cdefghijklmnopqrst"));
217 test(S("abcdefghijklmnopqrst"), 1, 9, 0, '3', S("aklmnopqrst"));
218 test(S("abcdefghijklmnopqrst"), 1, 9, 5, '3', S("a33333klmnopqrst"));
219 test(S("abcdefghijklmnopqrst"), 1, 9, 10, '3', S("a3333333333klmnopqrst"));
220 test(S("abcdefghijklmnopqrst"), 1, 9, 20, '3', S("a33333333333333333333klmnopqrst"));
221 test(S("abcdefghijklmnopqrst"), 1, 18, 0, '3', S("at"));
222 test(S("abcdefghijklmnopqrst"), 1, 18, 5, '3', S("a33333t"));
223 test(S("abcdefghijklmnopqrst"), 1, 18, 10, '3', S("a3333333333t"));
224 test(S("abcdefghijklmnopqrst"), 1, 18, 20, '3', S("a33333333333333333333t"));
225 test(S("abcdefghijklmnopqrst"), 1, 19, 0, '3', S("a"));
226 test(S("abcdefghijklmnopqrst"), 1, 19, 5, '3', S("a33333"));
227 test(S("abcdefghijklmnopqrst"), 1, 19, 10, '3', S("a3333333333"));
228 test(S("abcdefghijklmnopqrst"), 1, 19, 20, '3', S("a33333333333333333333"));
229 test(S("abcdefghijklmnopqrst"), 10, 0, 0, '3', S("abcdefghijklmnopqrst"));
230 test(S("abcdefghijklmnopqrst"), 10, 0, 5, '3', S("abcdefghij33333klmnopqrst"));
231 test(S("abcdefghijklmnopqrst"), 10, 0, 10, '3', S("abcdefghij3333333333klmnopqrst"));
232 test(S("abcdefghijklmnopqrst"), 10, 0, 20, '3', S("abcdefghij33333333333333333333klmnopqrst"));
233 test(S("abcdefghijklmnopqrst"), 10, 1, 0, '3', S("abcdefghijlmnopqrst"));
234 test(S("abcdefghijklmnopqrst"), 10, 1, 5, '3', S("abcdefghij33333lmnopqrst"));
235 test(S("abcdefghijklmnopqrst"), 10, 1, 10, '3', S("abcdefghij3333333333lmnopqrst"));
236 test(S("abcdefghijklmnopqrst"), 10, 1, 20, '3', S("abcdefghij33333333333333333333lmnopqrst"));
237 test(S("abcdefghijklmnopqrst"), 10, 5, 0, '3', S("abcdefghijpqrst"));
238 test(S("abcdefghijklmnopqrst"), 10, 5, 5, '3', S("abcdefghij33333pqrst"));
239 test(S("abcdefghijklmnopqrst"), 10, 5, 10, '3', S("abcdefghij3333333333pqrst"));
240 test(S("abcdefghijklmnopqrst"), 10, 5, 20, '3', S("abcdefghij33333333333333333333pqrst"));
241 test(S("abcdefghijklmnopqrst"), 10, 9, 0, '3', S("abcdefghijt"));
242 test(S("abcdefghijklmnopqrst"), 10, 9, 5, '3', S("abcdefghij33333t"));
243 test(S("abcdefghijklmnopqrst"), 10, 9, 10, '3', S("abcdefghij3333333333t"));
244 test(S("abcdefghijklmnopqrst"), 10, 9, 20, '3', S("abcdefghij33333333333333333333t"));
245 }
246
247 template <class S>
test2()248 void test2()
249 {
250 test(S("abcdefghijklmnopqrst"), 10, 10, 0, '3', S("abcdefghij"));
251 test(S("abcdefghijklmnopqrst"), 10, 10, 5, '3', S("abcdefghij33333"));
252 test(S("abcdefghijklmnopqrst"), 10, 10, 10, '3', S("abcdefghij3333333333"));
253 test(S("abcdefghijklmnopqrst"), 10, 10, 20, '3', S("abcdefghij33333333333333333333"));
254 test(S("abcdefghijklmnopqrst"), 19, 0, 0, '3', S("abcdefghijklmnopqrst"));
255 test(S("abcdefghijklmnopqrst"), 19, 0, 5, '3', S("abcdefghijklmnopqrs33333t"));
256 test(S("abcdefghijklmnopqrst"), 19, 0, 10, '3', S("abcdefghijklmnopqrs3333333333t"));
257 test(S("abcdefghijklmnopqrst"), 19, 0, 20, '3', S("abcdefghijklmnopqrs33333333333333333333t"));
258 test(S("abcdefghijklmnopqrst"), 19, 1, 0, '3', S("abcdefghijklmnopqrs"));
259 test(S("abcdefghijklmnopqrst"), 19, 1, 5, '3', S("abcdefghijklmnopqrs33333"));
260 test(S("abcdefghijklmnopqrst"), 19, 1, 10, '3', S("abcdefghijklmnopqrs3333333333"));
261 test(S("abcdefghijklmnopqrst"), 19, 1, 20, '3', S("abcdefghijklmnopqrs33333333333333333333"));
262 test(S("abcdefghijklmnopqrst"), 20, 0, 0, '3', S("abcdefghijklmnopqrst"));
263 test(S("abcdefghijklmnopqrst"), 20, 0, 5, '3', S("abcdefghijklmnopqrst33333"));
264 test(S("abcdefghijklmnopqrst"), 20, 0, 10, '3', S("abcdefghijklmnopqrst3333333333"));
265 test(S("abcdefghijklmnopqrst"), 20, 0, 20, '3', S("abcdefghijklmnopqrst33333333333333333333"));
266 }
267
main(int,char **)268 int main(int, char**)
269 {
270 {
271 typedef std::string S;
272 test0<S>();
273 test1<S>();
274 test2<S>();
275 }
276 #if TEST_STD_VER >= 11
277 {
278 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
279 test0<S>();
280 test1<S>();
281 test2<S>();
282 }
283 #endif
284
285 return 0;
286 }
287