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