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