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_macros.h"
18 #include "test_iterators.h"
19 #include "min_allocator.h"
20 
21 template <class S, class It>
22 void
test(S s,It first,It last,S expected)23 test(S s, It first, It last, S expected)
24 {
25     s.append(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
test_exceptions(S s,It first,It last)33 test_exceptions(S s, It first, It last)
34 {
35     S aCopy = s;
36     try {
37         s.append(first, last);
38         assert(false);
39         }
40     catch (...) {}
41     LIBCPP_ASSERT(s.__invariants());
42     assert(s == aCopy);
43 }
44 #endif
45 
main(int,char **)46 int main(int, char**)
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("12345"));
57     test(S("12345"), s, s+1, S("12345A"));
58     test(S("12345"), s, s+10, S("12345ABCDEFGHIJ"));
59     test(S("12345"), s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
60 
61     test(S("1234567890"), s, s, S("1234567890"));
62     test(S("1234567890"), s, s+1, S("1234567890A"));
63     test(S("1234567890"), s, s+10, S("1234567890ABCDEFGHIJ"));
64     test(S("1234567890"), s, s+52, S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
65 
66     test(S("12345678901234567890"), s, s, S("12345678901234567890"));
67     test(S("12345678901234567890"), s, s+1, S("12345678901234567890""A"));
68     test(S("12345678901234567890"), s, s+10, S("12345678901234567890""ABCDEFGHIJ"));
69     test(S("12345678901234567890"), s, s+52,
70          S("12345678901234567890""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("12345"));
81     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
82          S("12345A"));
83     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
84          S("12345ABCDEFGHIJ"));
85     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
86          S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
87 
88     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
89          S("1234567890"));
90     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
91          S("1234567890A"));
92     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
93          S("1234567890ABCDEFGHIJ"));
94     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
95          S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
96 
97     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
98          S("12345678901234567890"));
99     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
100          S("12345678901234567890""A"));
101     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
102          S("12345678901234567890""ABCDEFGHIJ"));
103     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
104          S("12345678901234567890""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("12345"));
116     test(S("12345"), s, s+1, S("12345A"));
117     test(S("12345"), s, s+10, S("12345ABCDEFGHIJ"));
118     test(S("12345"), s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
119 
120     test(S("1234567890"), s, s, S("1234567890"));
121     test(S("1234567890"), s, s+1, S("1234567890A"));
122     test(S("1234567890"), s, s+10, S("1234567890ABCDEFGHIJ"));
123     test(S("1234567890"), s, s+52, S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
124 
125     test(S("12345678901234567890"), s, s, S("12345678901234567890"));
126     test(S("12345678901234567890"), s, s+1, S("12345678901234567890""A"));
127     test(S("12345678901234567890"), s, s+10, S("12345678901234567890""ABCDEFGHIJ"));
128     test(S("12345678901234567890"), s, s+52,
129          S("12345678901234567890""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("12345"));
140     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
141          S("12345A"));
142     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
143          S("12345ABCDEFGHIJ"));
144     test(S("12345"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
145          S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
146 
147     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
148          S("1234567890"));
149     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
150          S("1234567890A"));
151     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
152          S("1234567890ABCDEFGHIJ"));
153     test(S("1234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
154          S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
155 
156     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s),
157          S("12345678901234567890"));
158     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+1),
159          S("12345678901234567890""A"));
160     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+10),
161          S("12345678901234567890""ABCDEFGHIJ"));
162     test(S("12345678901234567890"), input_iterator<const char*>(s), input_iterator<const char*>(s+52),
163          S("12345678901234567890""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 appending 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.append(s_short.begin(), s_short.end());
188     assert(s_short == "123/123/");
189     s_short.append(s_short.begin(), s_short.end());
190     assert(s_short == "123/123/123/123/");
191     s_short.append(s_short.begin(), s_short.end());
192     assert(s_short == "123/123/123/123/123/123/123/123/");
193 
194     s_long.append(s_long.begin(), s_long.end());
195     assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
196     }
197 
198     { // test appending a different type
199     typedef std::string S;
200     const uint8_t p[] = "ABCD";
201 
202     S s;
203     s.append(p, p + 4);
204     assert(s == "ABCD");
205     }
206 
207   { // test with a move iterator that returns char&&
208     typedef forward_iterator<const char*> It;
209     typedef std::move_iterator<It> MoveIt;
210     const char p[] = "ABCD";
211     std::string s;
212     s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1)));
213     assert(s == "ABCD");
214   }
215   { // test with a move iterator that returns char&&
216     typedef const char* It;
217     typedef std::move_iterator<It> MoveIt;
218     const char p[] = "ABCD";
219     std::string s;
220     s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1)));
221     assert(s == "ABCD");
222   }
223 
224   return 0;
225 }
226