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 // template<class InputIterator>
12 // basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20
13
14 #include <string>
15 #include <cassert>
16
17 #include "test_macros.h"
18 #include "test_iterators.h"
19 #include "min_allocator.h"
20
21 template <class S, class It>
22 TEST_CONSTEXPR_CXX20 void
test(S s,It first,It last,S expected)23 test(S s, It first, It last, S expected)
24 {
25 s.append(first, last);
26 LIBCPP_ASSERT(s.__invariants());
27 assert(s == expected);
28 }
29
30 #ifndef TEST_HAS_NO_EXCEPTIONS
operator charWidget31 struct Widget { operator char() const { throw 42; } };
32
33 template <class S, class It>
34 TEST_CONSTEXPR_CXX20 void
test_exceptions(S s,It first,It last)35 test_exceptions(S s, It first, It last)
36 {
37 S original = s;
38 typename S::iterator begin = s.begin();
39 typename S::iterator end = s.end();
40
41 try {
42 s.append(first, last);
43 assert(false);
44 } catch (...) {}
45
46 // Part of "no effects" is that iterators and pointers
47 // into the string must not have been invalidated.
48 LIBCPP_ASSERT(s.__invariants());
49 assert(s == original);
50 assert(s.begin() == begin);
51 assert(s.end() == end);
52 }
53 #endif
54
test()55 TEST_CONSTEXPR_CXX20 bool test() {
56 {
57 typedef std::string S;
58 const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
59 test(S(), s, s, S());
60 test(S(), s, s+1, S("A"));
61 test(S(), s, s+10, S("ABCDEFGHIJ"));
62 test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
63
64 test(S("12345"), s, s, S("12345"));
65 test(S("12345"), s, s+1, S("12345A"));
66 test(S("12345"), s, s+10, S("12345ABCDEFGHIJ"));
67 test(S("12345"), s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
68
69 test(S("1234567890"), s, s, S("1234567890"));
70 test(S("1234567890"), s, s+1, S("1234567890A"));
71 test(S("1234567890"), s, s+10, S("1234567890ABCDEFGHIJ"));
72 test(S("1234567890"), s, s+52, S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
73
74 test(S("12345678901234567890"), s, s, S("12345678901234567890"));
75 test(S("12345678901234567890"), s, s+1, S("12345678901234567890""A"));
76 test(S("12345678901234567890"), s, s+10, S("12345678901234567890""ABCDEFGHIJ"));
77 test(S("12345678901234567890"), s, s+52,
78 S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
79
80 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
81 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("A"));
82 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
83 S("ABCDEFGHIJ"));
84 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
85 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
86
87 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
88 S("12345"));
89 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
90 S("12345A"));
91 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
92 S("12345ABCDEFGHIJ"));
93 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
94 S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
95
96 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
97 S("1234567890"));
98 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
99 S("1234567890A"));
100 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
101 S("1234567890ABCDEFGHIJ"));
102 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
103 S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
104
105 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
106 S("12345678901234567890"));
107 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
108 S("12345678901234567890""A"));
109 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
110 S("12345678901234567890""ABCDEFGHIJ"));
111 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
112 S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
113 }
114 #if TEST_STD_VER >= 11
115 {
116 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
117 const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
118 test(S(), s, s, S());
119 test(S(), s, s+1, S("A"));
120 test(S(), s, s+10, S("ABCDEFGHIJ"));
121 test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
122
123 test(S("12345"), s, s, S("12345"));
124 test(S("12345"), s, s+1, S("12345A"));
125 test(S("12345"), s, s+10, S("12345ABCDEFGHIJ"));
126 test(S("12345"), s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
127
128 test(S("1234567890"), s, s, S("1234567890"));
129 test(S("1234567890"), s, s+1, S("1234567890A"));
130 test(S("1234567890"), s, s+10, S("1234567890ABCDEFGHIJ"));
131 test(S("1234567890"), s, s+52, S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
132
133 test(S("12345678901234567890"), s, s, S("12345678901234567890"));
134 test(S("12345678901234567890"), s, s+1, S("12345678901234567890""A"));
135 test(S("12345678901234567890"), s, s+10, S("12345678901234567890""ABCDEFGHIJ"));
136 test(S("12345678901234567890"), s, s+52,
137 S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
138
139 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
140 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("A"));
141 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
142 S("ABCDEFGHIJ"));
143 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
144 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
145
146 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
147 S("12345"));
148 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
149 S("12345A"));
150 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
151 S("12345ABCDEFGHIJ"));
152 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
153 S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
154
155 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
156 S("1234567890"));
157 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
158 S("1234567890A"));
159 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
160 S("1234567890ABCDEFGHIJ"));
161 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
162 S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
163
164 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
165 S("12345678901234567890"));
166 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
167 S("12345678901234567890""A"));
168 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
169 S("12345678901234567890""ABCDEFGHIJ"));
170 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
171 S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
172 }
173 #endif
174 #ifndef TEST_HAS_NO_EXCEPTIONS
175 if (!TEST_IS_CONSTANT_EVALUATED) { // test iterator operations that throw
176 typedef std::string S;
177 typedef ThrowingIterator<char> TIter;
178 typedef cpp17_input_iterator<TIter> IIter;
179 const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
180 test_exceptions(S(), IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter(TIter()));
181 test_exceptions(S(), IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter(TIter()));
182 test_exceptions(S(), IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter(TIter()));
183
184 test_exceptions(S(), TIter(s, s+10, 4, TIter::TAIncrement), TIter());
185 test_exceptions(S(), TIter(s, s+10, 5, TIter::TADereference), TIter());
186 test_exceptions(S(), TIter(s, s+10, 6, TIter::TAComparison), TIter());
187
188 Widget w[100];
189 test_exceptions(S(), w, w+100);
190 }
191 #endif
192
193 { // test appending to self
194 typedef std::string S;
195 S s_short = "123/";
196 S s_long = "Lorem ipsum dolor sit amet, consectetur/";
197
198 s_short.append(s_short.begin(), s_short.end());
199 assert(s_short == "123/123/");
200 s_short.append(s_short.begin(), s_short.end());
201 assert(s_short == "123/123/123/123/");
202 s_short.append(s_short.begin(), s_short.end());
203 assert(s_short == "123/123/123/123/123/123/123/123/");
204
205 s_long.append(s_long.begin(), s_long.end());
206 assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
207 }
208
209 { // test appending a different type
210 typedef std::string S;
211 const uint8_t p[] = "ABCD";
212
213 S s;
214 s.append(p, p + 4);
215 assert(s == "ABCD");
216 }
217
218 { // regression-test appending to self in sneaky ways
219 std::string s_short = "hello";
220 std::string s_long = "Lorem ipsum dolor sit amet, consectetur/";
221 std::string s_othertype = "hello";
222 std::string s_sneaky = "hello";
223
224 test(s_short, s_short.data() + s_short.size(), s_short.data() + s_short.size() + 1,
225 std::string("hello\0", 6));
226 test(s_long, s_long.data() + s_long.size(), s_long.data() + s_long.size() + 1,
227 std::string("Lorem ipsum dolor sit amet, consectetur/\0", 41));
228
229 s_sneaky.reserve(12);
230 test(s_sneaky, s_sneaky.data(), s_sneaky.data() + 6, std::string("hellohello\0", 11));
231
232 if (!TEST_IS_CONSTANT_EVALUATED) {
233 const unsigned char *first = reinterpret_cast<const unsigned char*>(s_othertype.data());
234 test(s_othertype, first + 2, first + 5, std::string("hellollo"));
235 }
236 }
237
238 { // test with a move iterator that returns char&&
239 typedef forward_iterator<const char*> It;
240 typedef std::move_iterator<It> MoveIt;
241 const char p[] = "ABCD";
242 std::string s;
243 s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1)));
244 assert(s == "ABCD");
245 }
246 { // test with a move iterator that returns char&&
247 typedef const char* It;
248 typedef std::move_iterator<It> MoveIt;
249 const char p[] = "ABCD";
250 std::string s;
251 s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1)));
252 assert(s == "ABCD");
253 }
254
255 return true;
256 }
257
main(int,char **)258 int main(int, char**)
259 {
260 test();
261 #if TEST_STD_VER > 17
262 static_assert(test());
263 #endif
264
265 return 0;
266 }
267