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