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