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