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 // insert(size_type pos, string_view sv);
18 #include "test_macros.h"
19 #include "min_allocator.h"
21 template <class S
, class SV
>
23 test(S s
, typename
S::size_type pos
, SV sv
, S expected
)
25 const typename
S::size_type old_size
= s
.size();
30 LIBCPP_ASSERT(s
.__invariants());
31 assert(s
== expected
);
33 #ifndef TEST_HAS_NO_EXCEPTIONS
41 catch (std::out_of_range
&)
43 assert(pos
> old_size
);
53 typedef std::string S
;
54 typedef std::string_view SV
;
55 test(S(""), 0, SV(""), S(""));
56 test(S(""), 0, SV("12345"), S("12345"));
57 test(S(""), 0, SV("1234567890"), S("1234567890"));
58 test(S(""), 0, SV("12345678901234567890"), S("12345678901234567890"));
59 test(S(""), 1, SV(""), S("can't happen"));
60 test(S(""), 1, SV("12345"), S("can't happen"));
61 test(S(""), 1, SV("1234567890"), S("can't happen"));
62 test(S(""), 1, SV("12345678901234567890"), S("can't happen"));
63 test(S("abcde"), 0, SV(""), S("abcde"));
64 test(S("abcde"), 0, SV("12345"), S("12345abcde"));
65 test(S("abcde"), 0, SV("1234567890"), S("1234567890abcde"));
66 test(S("abcde"), 0, SV("12345678901234567890"), S("12345678901234567890abcde"));
67 test(S("abcde"), 1, SV(""), S("abcde"));
68 test(S("abcde"), 1, SV("12345"), S("a12345bcde"));
69 test(S("abcde"), 1, SV("1234567890"), S("a1234567890bcde"));
70 test(S("abcde"), 1, SV("12345678901234567890"), S("a12345678901234567890bcde"));
71 test(S("abcde"), 2, SV(""), S("abcde"));
72 test(S("abcde"), 2, SV("12345"), S("ab12345cde"));
73 test(S("abcde"), 2, SV("1234567890"), S("ab1234567890cde"));
74 test(S("abcde"), 2, SV("12345678901234567890"), S("ab12345678901234567890cde"));
75 test(S("abcde"), 4, SV(""), S("abcde"));
76 test(S("abcde"), 4, SV("12345"), S("abcd12345e"));
77 test(S("abcde"), 4, SV("1234567890"), S("abcd1234567890e"));
78 test(S("abcde"), 4, SV("12345678901234567890"), S("abcd12345678901234567890e"));
79 test(S("abcde"), 5, SV(""), S("abcde"));
80 test(S("abcde"), 5, SV("12345"), S("abcde12345"));
81 test(S("abcde"), 5, SV("1234567890"), S("abcde1234567890"));
82 test(S("abcde"), 5, SV("12345678901234567890"), S("abcde12345678901234567890"));
83 test(S("abcde"), 6, SV(""), S("can't happen"));
84 test(S("abcde"), 6, SV("12345"), S("can't happen"));
85 test(S("abcde"), 6, SV("1234567890"), S("can't happen"));
86 test(S("abcde"), 6, SV("12345678901234567890"), S("can't happen"));
87 test(S("abcdefghij"), 0, SV(""), S("abcdefghij"));
88 test(S("abcdefghij"), 0, SV("12345"), S("12345abcdefghij"));
89 test(S("abcdefghij"), 0, SV("1234567890"), S("1234567890abcdefghij"));
90 test(S("abcdefghij"), 0, SV("12345678901234567890"), S("12345678901234567890abcdefghij"));
91 test(S("abcdefghij"), 1, SV(""), S("abcdefghij"));
92 test(S("abcdefghij"), 1, SV("12345"), S("a12345bcdefghij"));
93 test(S("abcdefghij"), 1, SV("1234567890"), S("a1234567890bcdefghij"));
94 test(S("abcdefghij"), 1, SV("12345678901234567890"), S("a12345678901234567890bcdefghij"));
95 test(S("abcdefghij"), 5, SV(""), S("abcdefghij"));
96 test(S("abcdefghij"), 5, SV("12345"), S("abcde12345fghij"));
97 test(S("abcdefghij"), 5, SV("1234567890"), S("abcde1234567890fghij"));
98 test(S("abcdefghij"), 5, SV("12345678901234567890"), S("abcde12345678901234567890fghij"));
99 test(S("abcdefghij"), 9, SV(""), S("abcdefghij"));
100 test(S("abcdefghij"), 9, SV("12345"), S("abcdefghi12345j"));
101 test(S("abcdefghij"), 9, SV("1234567890"), S("abcdefghi1234567890j"));
102 test(S("abcdefghij"), 9, SV("12345678901234567890"), S("abcdefghi12345678901234567890j"));
103 test(S("abcdefghij"), 10, SV(""), S("abcdefghij"));
104 test(S("abcdefghij"), 10, SV("12345"), S("abcdefghij12345"));
105 test(S("abcdefghij"), 10, SV("1234567890"), S("abcdefghij1234567890"));
106 test(S("abcdefghij"), 10, SV("12345678901234567890"), S("abcdefghij12345678901234567890"));
107 test(S("abcdefghij"), 11, SV(""), S("can't happen"));
108 test(S("abcdefghij"), 11, SV("12345"), S("can't happen"));
109 test(S("abcdefghij"), 11, SV("1234567890"), S("can't happen"));
110 test(S("abcdefghij"), 11, SV("12345678901234567890"), S("can't happen"));
111 test(S("abcdefghijklmnopqrst"), 0, SV(""), S("abcdefghijklmnopqrst"));
112 test(S("abcdefghijklmnopqrst"), 0, SV("12345"), S("12345abcdefghijklmnopqrst"));
113 test(S("abcdefghijklmnopqrst"), 0, SV("1234567890"), S("1234567890abcdefghijklmnopqrst"));
114 test(S("abcdefghijklmnopqrst"), 0, SV("12345678901234567890"), S("12345678901234567890abcdefghijklmnopqrst"));
115 test(S("abcdefghijklmnopqrst"), 1, SV(""), S("abcdefghijklmnopqrst"));
116 test(S("abcdefghijklmnopqrst"), 1, SV("12345"), S("a12345bcdefghijklmnopqrst"));
117 test(S("abcdefghijklmnopqrst"), 1, SV("1234567890"), S("a1234567890bcdefghijklmnopqrst"));
118 test(S("abcdefghijklmnopqrst"), 1, SV("12345678901234567890"), S("a12345678901234567890bcdefghijklmnopqrst"));
119 test(S("abcdefghijklmnopqrst"), 10, SV(""), S("abcdefghijklmnopqrst"));
120 test(S("abcdefghijklmnopqrst"), 10, SV("12345"), S("abcdefghij12345klmnopqrst"));
121 test(S("abcdefghijklmnopqrst"), 10, SV("1234567890"), S("abcdefghij1234567890klmnopqrst"));
122 test(S("abcdefghijklmnopqrst"), 10, SV("12345678901234567890"), S("abcdefghij12345678901234567890klmnopqrst"));
123 test(S("abcdefghijklmnopqrst"), 19, SV(""), S("abcdefghijklmnopqrst"));
124 test(S("abcdefghijklmnopqrst"), 19, SV("12345"), S("abcdefghijklmnopqrs12345t"));
125 test(S("abcdefghijklmnopqrst"), 19, SV("1234567890"), S("abcdefghijklmnopqrs1234567890t"));
126 test(S("abcdefghijklmnopqrst"), 19, SV("12345678901234567890"), S("abcdefghijklmnopqrs12345678901234567890t"));
127 test(S("abcdefghijklmnopqrst"), 20, SV(""), S("abcdefghijklmnopqrst"));
128 test(S("abcdefghijklmnopqrst"), 20, SV("12345"), S("abcdefghijklmnopqrst12345"));
129 test(S("abcdefghijklmnopqrst"), 20, SV("1234567890"), S("abcdefghijklmnopqrst1234567890"));
130 test(S("abcdefghijklmnopqrst"), 20, SV("12345678901234567890"), S("abcdefghijklmnopqrst12345678901234567890"));
131 test(S("abcdefghijklmnopqrst"), 21, SV(""), S("can't happen"));
132 test(S("abcdefghijklmnopqrst"), 21, SV("12345"), S("can't happen"));
133 test(S("abcdefghijklmnopqrst"), 21, SV("1234567890"), S("can't happen"));
134 test(S("abcdefghijklmnopqrst"), 21, SV("12345678901234567890"), S("can't happen"));
136 #if TEST_STD_VER >= 11
138 typedef std::basic_string
<char, std::char_traits
<char>, min_allocator
<char>> S
;
139 typedef std::string_view SV
;
140 test(S(""), 0, SV(""), S(""));
141 test(S(""), 0, SV("12345"), S("12345"));
142 test(S(""), 0, SV("1234567890"), S("1234567890"));
143 test(S(""), 0, SV("12345678901234567890"), S("12345678901234567890"));
144 test(S(""), 1, SV(""), S("can't happen"));
145 test(S(""), 1, SV("12345"), S("can't happen"));
146 test(S(""), 1, SV("1234567890"), S("can't happen"));
147 test(S(""), 1, SV("12345678901234567890"), S("can't happen"));
148 test(S("abcde"), 0, SV(""), S("abcde"));
149 test(S("abcde"), 0, SV("12345"), S("12345abcde"));
150 test(S("abcde"), 0, SV("1234567890"), S("1234567890abcde"));
151 test(S("abcde"), 0, SV("12345678901234567890"), S("12345678901234567890abcde"));
152 test(S("abcde"), 1, SV(""), S("abcde"));
153 test(S("abcde"), 1, SV("12345"), S("a12345bcde"));
154 test(S("abcde"), 1, SV("1234567890"), S("a1234567890bcde"));
155 test(S("abcde"), 1, SV("12345678901234567890"), S("a12345678901234567890bcde"));
156 test(S("abcde"), 2, SV(""), S("abcde"));
157 test(S("abcde"), 2, SV("12345"), S("ab12345cde"));
158 test(S("abcde"), 2, SV("1234567890"), S("ab1234567890cde"));
159 test(S("abcde"), 2, SV("12345678901234567890"), S("ab12345678901234567890cde"));
160 test(S("abcde"), 4, SV(""), S("abcde"));
161 test(S("abcde"), 4, SV("12345"), S("abcd12345e"));
162 test(S("abcde"), 4, SV("1234567890"), S("abcd1234567890e"));
163 test(S("abcde"), 4, SV("12345678901234567890"), S("abcd12345678901234567890e"));
164 test(S("abcde"), 5, SV(""), S("abcde"));
165 test(S("abcde"), 5, SV("12345"), S("abcde12345"));
166 test(S("abcde"), 5, SV("1234567890"), S("abcde1234567890"));
167 test(S("abcde"), 5, SV("12345678901234567890"), S("abcde12345678901234567890"));
168 test(S("abcde"), 6, SV(""), S("can't happen"));
169 test(S("abcde"), 6, SV("12345"), S("can't happen"));
170 test(S("abcde"), 6, SV("1234567890"), S("can't happen"));
171 test(S("abcde"), 6, SV("12345678901234567890"), S("can't happen"));
172 test(S("abcdefghij"), 0, SV(""), S("abcdefghij"));
173 test(S("abcdefghij"), 0, SV("12345"), S("12345abcdefghij"));
174 test(S("abcdefghij"), 0, SV("1234567890"), S("1234567890abcdefghij"));
175 test(S("abcdefghij"), 0, SV("12345678901234567890"), S("12345678901234567890abcdefghij"));
176 test(S("abcdefghij"), 1, SV(""), S("abcdefghij"));
177 test(S("abcdefghij"), 1, SV("12345"), S("a12345bcdefghij"));
178 test(S("abcdefghij"), 1, SV("1234567890"), S("a1234567890bcdefghij"));
179 test(S("abcdefghij"), 1, SV("12345678901234567890"), S("a12345678901234567890bcdefghij"));
180 test(S("abcdefghij"), 5, SV(""), S("abcdefghij"));
181 test(S("abcdefghij"), 5, SV("12345"), S("abcde12345fghij"));
182 test(S("abcdefghij"), 5, SV("1234567890"), S("abcde1234567890fghij"));
183 test(S("abcdefghij"), 5, SV("12345678901234567890"), S("abcde12345678901234567890fghij"));
184 test(S("abcdefghij"), 9, SV(""), S("abcdefghij"));
185 test(S("abcdefghij"), 9, SV("12345"), S("abcdefghi12345j"));
186 test(S("abcdefghij"), 9, SV("1234567890"), S("abcdefghi1234567890j"));
187 test(S("abcdefghij"), 9, SV("12345678901234567890"), S("abcdefghi12345678901234567890j"));
188 test(S("abcdefghij"), 10, SV(""), S("abcdefghij"));
189 test(S("abcdefghij"), 10, SV("12345"), S("abcdefghij12345"));
190 test(S("abcdefghij"), 10, SV("1234567890"), S("abcdefghij1234567890"));
191 test(S("abcdefghij"), 10, SV("12345678901234567890"), S("abcdefghij12345678901234567890"));
192 test(S("abcdefghij"), 11, SV(""), S("can't happen"));
193 test(S("abcdefghij"), 11, SV("12345"), S("can't happen"));
194 test(S("abcdefghij"), 11, SV("1234567890"), S("can't happen"));
195 test(S("abcdefghij"), 11, SV("12345678901234567890"), S("can't happen"));
196 test(S("abcdefghijklmnopqrst"), 0, SV(""), S("abcdefghijklmnopqrst"));
197 test(S("abcdefghijklmnopqrst"), 0, SV("12345"), S("12345abcdefghijklmnopqrst"));
198 test(S("abcdefghijklmnopqrst"), 0, SV("1234567890"), S("1234567890abcdefghijklmnopqrst"));
199 test(S("abcdefghijklmnopqrst"), 0, SV("12345678901234567890"), S("12345678901234567890abcdefghijklmnopqrst"));
200 test(S("abcdefghijklmnopqrst"), 1, SV(""), S("abcdefghijklmnopqrst"));
201 test(S("abcdefghijklmnopqrst"), 1, SV("12345"), S("a12345bcdefghijklmnopqrst"));
202 test(S("abcdefghijklmnopqrst"), 1, SV("1234567890"), S("a1234567890bcdefghijklmnopqrst"));
203 test(S("abcdefghijklmnopqrst"), 1, SV("12345678901234567890"), S("a12345678901234567890bcdefghijklmnopqrst"));
204 test(S("abcdefghijklmnopqrst"), 10, SV(""), S("abcdefghijklmnopqrst"));
205 test(S("abcdefghijklmnopqrst"), 10, SV("12345"), S("abcdefghij12345klmnopqrst"));
206 test(S("abcdefghijklmnopqrst"), 10, SV("1234567890"), S("abcdefghij1234567890klmnopqrst"));
207 test(S("abcdefghijklmnopqrst"), 10, SV("12345678901234567890"), S("abcdefghij12345678901234567890klmnopqrst"));
208 test(S("abcdefghijklmnopqrst"), 19, SV(""), S("abcdefghijklmnopqrst"));
209 test(S("abcdefghijklmnopqrst"), 19, SV("12345"), S("abcdefghijklmnopqrs12345t"));
210 test(S("abcdefghijklmnopqrst"), 19, SV("1234567890"), S("abcdefghijklmnopqrs1234567890t"));
211 test(S("abcdefghijklmnopqrst"), 19, SV("12345678901234567890"), S("abcdefghijklmnopqrs12345678901234567890t"));
212 test(S("abcdefghijklmnopqrst"), 20, SV(""), S("abcdefghijklmnopqrst"));
213 test(S("abcdefghijklmnopqrst"), 20, SV("12345"), S("abcdefghijklmnopqrst12345"));
214 test(S("abcdefghijklmnopqrst"), 20, SV("1234567890"), S("abcdefghijklmnopqrst1234567890"));
215 test(S("abcdefghijklmnopqrst"), 20, SV("12345678901234567890"), S("abcdefghijklmnopqrst12345678901234567890"));
216 test(S("abcdefghijklmnopqrst"), 21, SV(""), S("can't happen"));
217 test(S("abcdefghijklmnopqrst"), 21, SV("12345"), S("can't happen"));
218 test(S("abcdefghijklmnopqrst"), 21, SV("1234567890"), S("can't happen"));
219 test(S("abcdefghijklmnopqrst"), 21, SV("12345678901234567890"), S("can't happen"));
223 { // test inserting into self
224 typedef std::string S
;
226 S s_long
= "Lorem ipsum dolor sit amet, consectetur/";
228 s_short
.insert(0, s_short
.c_str());
229 assert(s_short
== "123/123/");
230 s_short
.insert(0, s_short
.c_str());
231 assert(s_short
== "123/123/123/123/");
232 s_short
.insert(0, s_short
.c_str());
233 assert(s_short
== "123/123/123/123/123/123/123/123/");
235 s_long
.insert(0, s_long
.c_str());
236 assert(s_long
== "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");