xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_append/iterator.pass.cpp (revision 036b80fcbbccee8199851158fd349230aa77d75e)
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);
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 void
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
31 struct Widget { operator char() const { throw 42; } };
32 
33 template <class S, class It>
34 void
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 
55 int main(int, char**)
56 {
57     {
58     typedef std::string S;
59     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
60     test(S(), s, s, S());
61     test(S(), s, s+1, S("A"));
62     test(S(), s, s+10, S("ABCDEFGHIJ"));
63     test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
64 
65     test(S("12345"), s, s, S("12345"));
66     test(S("12345"), s, s+1, S("12345A"));
67     test(S("12345"), s, s+10, S("12345ABCDEFGHIJ"));
68     test(S("12345"), s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
69 
70     test(S("1234567890"), s, s, S("1234567890"));
71     test(S("1234567890"), s, s+1, S("1234567890A"));
72     test(S("1234567890"), s, s+10, S("1234567890ABCDEFGHIJ"));
73     test(S("1234567890"), s, s+52, S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
74 
75     test(S("12345678901234567890"), s, s, S("12345678901234567890"));
76     test(S("12345678901234567890"), s, s+1, S("12345678901234567890""A"));
77     test(S("12345678901234567890"), s, s+10, S("12345678901234567890""ABCDEFGHIJ"));
78     test(S("12345678901234567890"), s, s+52,
79          S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
80 
81     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s), S());
82     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("A"));
83     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
84          S("ABCDEFGHIJ"));
85     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
86          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
87 
88     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s),
89          S("12345"));
90     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
91          S("12345A"));
92     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
93          S("12345ABCDEFGHIJ"));
94     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
95          S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
96 
97     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
98          S("1234567890"));
99     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
100          S("1234567890A"));
101     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
102          S("1234567890ABCDEFGHIJ"));
103     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
104          S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
105 
106     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
107          S("12345678901234567890"));
108     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
109          S("12345678901234567890""A"));
110     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
111          S("12345678901234567890""ABCDEFGHIJ"));
112     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
113          S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
114     }
115 #if TEST_STD_VER >= 11
116     {
117     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
118     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
119     test(S(), s, s, S());
120     test(S(), s, s+1, S("A"));
121     test(S(), s, s+10, S("ABCDEFGHIJ"));
122     test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
123 
124     test(S("12345"), s, s, S("12345"));
125     test(S("12345"), s, s+1, S("12345A"));
126     test(S("12345"), s, s+10, S("12345ABCDEFGHIJ"));
127     test(S("12345"), s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
128 
129     test(S("1234567890"), s, s, S("1234567890"));
130     test(S("1234567890"), s, s+1, S("1234567890A"));
131     test(S("1234567890"), s, s+10, S("1234567890ABCDEFGHIJ"));
132     test(S("1234567890"), s, s+52, S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
133 
134     test(S("12345678901234567890"), s, s, S("12345678901234567890"));
135     test(S("12345678901234567890"), s, s+1, S("12345678901234567890""A"));
136     test(S("12345678901234567890"), s, s+10, S("12345678901234567890""ABCDEFGHIJ"));
137     test(S("12345678901234567890"), s, s+52,
138          S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
139 
140     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s), S());
141     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("A"));
142     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
143          S("ABCDEFGHIJ"));
144     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
145          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
146 
147     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s),
148          S("12345"));
149     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
150          S("12345A"));
151     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
152          S("12345ABCDEFGHIJ"));
153     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
154          S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
155 
156     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
157          S("1234567890"));
158     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
159          S("1234567890A"));
160     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
161          S("1234567890ABCDEFGHIJ"));
162     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
163          S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
164 
165     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
166          S("12345678901234567890"));
167     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
168          S("12345678901234567890""A"));
169     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
170          S("12345678901234567890""ABCDEFGHIJ"));
171     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
172          S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
173     }
174 #endif
175 #ifndef TEST_HAS_NO_EXCEPTIONS
176     { // test iterator operations that throw
177     typedef std::string S;
178     typedef ThrowingIterator<char> TIter;
179     typedef input_iterator<TIter> IIter;
180     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
181     test_exceptions(S(), IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter());
182     test_exceptions(S(), IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter());
183     test_exceptions(S(), IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter());
184 
185     test_exceptions(S(), TIter(s, s+10, 4, TIter::TAIncrement), TIter());
186     test_exceptions(S(), TIter(s, s+10, 5, TIter::TADereference), TIter());
187     test_exceptions(S(), TIter(s, s+10, 6, TIter::TAComparison), TIter());
188 
189     Widget w[100];
190     test_exceptions(S(), w, w+100);
191     }
192 #endif
193 
194     { // test appending to self
195     typedef std::string S;
196     S s_short = "123/";
197     S s_long  = "Lorem ipsum dolor sit amet, consectetur/";
198 
199     s_short.append(s_short.begin(), s_short.end());
200     assert(s_short == "123/123/");
201     s_short.append(s_short.begin(), s_short.end());
202     assert(s_short == "123/123/123/123/");
203     s_short.append(s_short.begin(), s_short.end());
204     assert(s_short == "123/123/123/123/123/123/123/123/");
205 
206     s_long.append(s_long.begin(), s_long.end());
207     assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
208     }
209 
210     { // test appending a different type
211     typedef std::string S;
212     const uint8_t p[] = "ABCD";
213 
214     S s;
215     s.append(p, p + 4);
216     assert(s == "ABCD");
217     }
218 
219     { // regression-test appending to self in sneaky ways
220     std::string s_short = "hello";
221     std::string s_long = "Lorem ipsum dolor sit amet, consectetur/";
222     std::string s_othertype = "hello";
223     const unsigned char *first = reinterpret_cast<const unsigned char*>(s_othertype.data());
224     std::string s_sneaky = "hello";
225 
226     test(s_short, s_short.data() + s_short.size(), s_short.data() + s_short.size() + 1,
227          std::string("hello\0", 6));
228     test(s_long, s_long.data() + s_long.size(), s_long.data() + s_long.size() + 1,
229          std::string("Lorem ipsum dolor sit amet, consectetur/\0", 41));
230     test(s_othertype, first + 2, first + 5, std::string("hellollo"));
231 
232     s_sneaky.reserve(12);
233     test(s_sneaky, s_sneaky.data(), s_sneaky.data() + 6, std::string("hellohello\0", 11));
234     }
235 
236   { // test with a move iterator that returns char&&
237     typedef forward_iterator<const char*> It;
238     typedef std::move_iterator<It> MoveIt;
239     const char p[] = "ABCD";
240     std::string s;
241     s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1)));
242     assert(s == "ABCD");
243   }
244   { // test with a move iterator that returns char&&
245     typedef const char* It;
246     typedef std::move_iterator<It> MoveIt;
247     const char p[] = "ABCD";
248     std::string s;
249     s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1)));
250     assert(s == "ABCD");
251   }
252 
253   return 0;
254 }
255