xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp (revision dcffa7d3e140cf2e2a80f93168b40c449bc1d230)
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& assign(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.assign(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.assign(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 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());
65     test(S("12345"), s, s+1, S("A"));
66     test(S("12345"), s, s+10, S("ABCDEFGHIJ"));
67     test(S("12345"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
68 
69     test(S("1234567890"), s, s, S());
70     test(S("1234567890"), s, s+1, S("A"));
71     test(S("1234567890"), s, s+10, S("ABCDEFGHIJ"));
72     test(S("1234567890"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
73 
74     test(S("12345678901234567890"), s, s, S());
75     test(S("12345678901234567890"), s, s+1, S("A"));
76     test(S("12345678901234567890"), s, s+10, S("ABCDEFGHIJ"));
77     test(S("12345678901234567890"), s, s+52,
78          S("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());
89     test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
90          S("A"));
91     test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
92          S("ABCDEFGHIJ"));
93     test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
94          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
95 
96     test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
97          S());
98     test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
99          S("A"));
100     test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
101          S("ABCDEFGHIJ"));
102     test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
103          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
104 
105     test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
106          S());
107     test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
108          S("A"));
109     test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
110          S("ABCDEFGHIJ"));
111     test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
112          S("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());
124     test(S("12345"), s, s+1, S("A"));
125     test(S("12345"), s, s+10, S("ABCDEFGHIJ"));
126     test(S("12345"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
127 
128     test(S("1234567890"), s, s, S());
129     test(S("1234567890"), s, s+1, S("A"));
130     test(S("1234567890"), s, s+10, S("ABCDEFGHIJ"));
131     test(S("1234567890"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
132 
133     test(S("12345678901234567890"), s, s, S());
134     test(S("12345678901234567890"), s, s+1, S("A"));
135     test(S("12345678901234567890"), s, s+10, S("ABCDEFGHIJ"));
136     test(S("12345678901234567890"), s, s+52,
137          S("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());
148     test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
149          S("A"));
150     test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
151          S("ABCDEFGHIJ"));
152     test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
153          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
154 
155     test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
156          S());
157     test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
158          S("A"));
159     test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
160          S("ABCDEFGHIJ"));
161     test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
162          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
163 
164     test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
165          S());
166     test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
167          S("A"));
168     test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
169          S("ABCDEFGHIJ"));
170     test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
171          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
172   }
173 #endif
174 #ifndef TEST_HAS_NO_EXCEPTIONS
175   { // 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 assigning 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.assign(s_short.begin(), s_short.end());
199     assert(s_short == "123/");
200     s_short.assign(s_short.begin() + 2, s_short.end());
201     assert(s_short == "3/");
202 
203     s_long.assign(s_long.begin(), s_long.end());
204     assert(s_long == "Lorem ipsum dolor sit amet, consectetur/");
205 
206     s_long.assign(s_long.begin() + 30, s_long.end());
207     assert(s_long == "nsectetur/");
208   }
209 
210   { // test assigning a different type
211     typedef std::string S;
212     const uint8_t p[] = "ABCD";
213 
214     S s;
215     s.assign(p, p + 4);
216     assert(s == "ABCD");
217   }
218 
219   { // regression-test assigning to self in sneaky ways
220     std::string sneaky = "hello";
221     sneaky.resize(sneaky.capacity(), 'x');
222     std::string expected = sneaky + std::string(1, '\0');
223     test(sneaky, sneaky.data(), sneaky.data() + sneaky.size() + 1, expected);
224   }
225 
226   return true;
227 }
228 
229 int main(int, char**)
230 {
231   test();
232 #if TEST_STD_VER > 17
233   // static_assert(test());
234 #endif
235 
236     return 0;
237 }
238