xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp (revision e87479b00fcc852a54b79d2fd7f8d779e2b75f68)
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 aCopy = s;
38     try {
39         s.assign(first, last);
40         assert(false);
41     }
42     catch (...) {}
43     LIBCPP_ASSERT(s.__invariants());
44     assert(s == aCopy);
45 }
46 #endif
47 
48 int main(int, char**)
49 {
50     {
51     typedef std::string S;
52     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
53     test(S(), s, s, S());
54     test(S(), s, s+1, S("A"));
55     test(S(), s, s+10, S("ABCDEFGHIJ"));
56     test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
57 
58     test(S("12345"), s, s, S());
59     test(S("12345"), s, s+1, S("A"));
60     test(S("12345"), s, s+10, S("ABCDEFGHIJ"));
61     test(S("12345"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
62 
63     test(S("1234567890"), s, s, S());
64     test(S("1234567890"), s, s+1, S("A"));
65     test(S("1234567890"), s, s+10, S("ABCDEFGHIJ"));
66     test(S("1234567890"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
67 
68     test(S("12345678901234567890"), s, s, S());
69     test(S("12345678901234567890"), s, s+1, S("A"));
70     test(S("12345678901234567890"), s, s+10, S("ABCDEFGHIJ"));
71     test(S("12345678901234567890"), s, s+52,
72          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
73 
74     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s), S());
75     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("A"));
76     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
77          S("ABCDEFGHIJ"));
78     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
79          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
80 
81     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s),
82          S());
83     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
84          S("A"));
85     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
86          S("ABCDEFGHIJ"));
87     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
88          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
89 
90     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
91          S());
92     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
93          S("A"));
94     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
95          S("ABCDEFGHIJ"));
96     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
97          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
98 
99     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
100          S());
101     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
102          S("A"));
103     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
104          S("ABCDEFGHIJ"));
105     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
106          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
107     }
108 #if TEST_STD_VER >= 11
109     {
110     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
111     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
112     test(S(), s, s, S());
113     test(S(), s, s+1, S("A"));
114     test(S(), s, s+10, S("ABCDEFGHIJ"));
115     test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
116 
117     test(S("12345"), s, s, S());
118     test(S("12345"), s, s+1, S("A"));
119     test(S("12345"), s, s+10, S("ABCDEFGHIJ"));
120     test(S("12345"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
121 
122     test(S("1234567890"), s, s, S());
123     test(S("1234567890"), s, s+1, S("A"));
124     test(S("1234567890"), s, s+10, S("ABCDEFGHIJ"));
125     test(S("1234567890"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
126 
127     test(S("12345678901234567890"), s, s, S());
128     test(S("12345678901234567890"), s, s+1, S("A"));
129     test(S("12345678901234567890"), s, s+10, S("ABCDEFGHIJ"));
130     test(S("12345678901234567890"), s, s+52,
131          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
132 
133     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s), S());
134     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+1), S("A"));
135     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
136          S("ABCDEFGHIJ"));
137     test(S(), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
138          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
139 
140     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s),
141          S());
142     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
143          S("A"));
144     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
145          S("ABCDEFGHIJ"));
146     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
147          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
148 
149     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
150          S());
151     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
152          S("A"));
153     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
154          S("ABCDEFGHIJ"));
155     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
156          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
157 
158     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
159          S());
160     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
161          S("A"));
162     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
163          S("ABCDEFGHIJ"));
164     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
165          S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
166     }
167 #endif
168 #ifndef TEST_HAS_NO_EXCEPTIONS
169     { // test iterator operations that throw
170     typedef std::string S;
171     typedef ThrowingIterator<char> TIter;
172     typedef input_iterator<TIter> IIter;
173     const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
174     test_exceptions(S(), IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter());
175     test_exceptions(S(), IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter());
176     test_exceptions(S(), IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter());
177 
178     test_exceptions(S(), TIter(s, s+10, 4, TIter::TAIncrement), TIter());
179     test_exceptions(S(), TIter(s, s+10, 5, TIter::TADereference), TIter());
180     test_exceptions(S(), TIter(s, s+10, 6, TIter::TAComparison), TIter());
181 
182     Widget w[100];
183     test_exceptions(S(), w, w+100);
184     }
185 #endif
186 
187     { // test assigning to self
188     typedef std::string S;
189     S s_short = "123/";
190     S s_long  = "Lorem ipsum dolor sit amet, consectetur/";
191 
192     s_short.assign(s_short.begin(), s_short.end());
193     assert(s_short == "123/");
194     s_short.assign(s_short.begin() + 2, s_short.end());
195     assert(s_short == "3/");
196 
197     s_long.assign(s_long.begin(), s_long.end());
198     assert(s_long == "Lorem ipsum dolor sit amet, consectetur/");
199 
200     s_long.assign(s_long.begin() + 30, s_long.end());
201     assert(s_long == "nsectetur/");
202     }
203 
204     { // test assigning a different type
205     typedef std::string S;
206     const uint8_t p[] = "ABCD";
207 
208     S s;
209     s.assign(p, p + 4);
210     assert(s == "ABCD");
211     }
212 
213     { // regression-test assigning to self in sneaky ways
214     std::string sneaky = "hello";
215     sneaky.resize(sneaky.capacity(), 'x');
216     std::string expected = sneaky + std::string(1, '\0');
217     test(sneaky, sneaky.data(), sneaky.data() + sneaky.size() + 1, expected);
218     }
219 
220     return 0;
221 }
222