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