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