1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // template<class InputIterator>
12 // basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20
17 #include "test_macros.h"
18 #include "test_iterators.h"
19 #include "min_allocator.h"
21 template <class S
, class It
>
22 TEST_CONSTEXPR_CXX20
void test(S s
, It first
, It last
, S expected
) {
23 s
.append(first
, last
);
24 LIBCPP_ASSERT(s
.__invariants());
25 assert(s
== expected
);
28 #ifndef TEST_HAS_NO_EXCEPTIONS
30 operator char() const { throw 42; }
33 template <class S
, class It
>
34 TEST_CONSTEXPR_CXX20
void test_exceptions(S s
, It first
, It last
) {
36 typename
S::iterator begin
= s
.begin();
37 typename
S::iterator end
= s
.end();
40 s
.append(first
, last
);
45 // Part of "no effects" is that iterators and pointers
46 // into the string must not have been invalidated.
47 LIBCPP_ASSERT(s
.__invariants());
48 assert(s
== original
);
49 assert(s
.begin() == begin
);
50 assert(s
.end() == end
);
55 TEST_CONSTEXPR_CXX20
void test_string() {
57 const char* s
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
59 test(S(), s
, s
+ 1, S("A"));
60 test(S(), s
, s
+ 10, S("ABCDEFGHIJ"));
61 test(S(), s
, s
+ 52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
63 test(S("12345"), s
, s
, S("12345"));
64 test(S("12345"), s
, s
+ 1, S("12345A"));
65 test(S("12345"), s
, s
+ 10, S("12345ABCDEFGHIJ"));
66 test(S("12345"), s
, s
+ 52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
68 test(S("1234567890"), s
, s
, S("1234567890"));
69 test(S("1234567890"), s
, s
+ 1, S("1234567890A"));
70 test(S("1234567890"), s
, s
+ 10, S("1234567890ABCDEFGHIJ"));
71 test(S("1234567890"), s
, s
+ 52, S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
73 test(S("12345678901234567890"), s
, s
, S("12345678901234567890"));
74 test(S("12345678901234567890"),
77 S("12345678901234567890"
79 test(S("12345678901234567890"),
82 S("12345678901234567890"
84 test(S("12345678901234567890"),
87 S("12345678901234567890"
88 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
90 test(S(), cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
), S());
91 test(S(), cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 1), S("A"));
92 test(S(), cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 10), S("ABCDEFGHIJ"));
94 cpp17_input_iterator
<const char*>(s
),
95 cpp17_input_iterator
<const char*>(s
+ 52),
96 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
98 test(S("12345"), cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
), S("12345"));
99 test(S("12345"), cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
+ 1), S("12345A"));
101 cpp17_input_iterator
<const char*>(s
),
102 cpp17_input_iterator
<const char*>(s
+ 10),
103 S("12345ABCDEFGHIJ"));
105 cpp17_input_iterator
<const char*>(s
),
106 cpp17_input_iterator
<const char*>(s
+ 52),
107 S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
109 test(S("1234567890"), cpp17_input_iterator
<const char*>(s
), cpp17_input_iterator
<const char*>(s
), S("1234567890"));
110 test(S("1234567890"),
111 cpp17_input_iterator
<const char*>(s
),
112 cpp17_input_iterator
<const char*>(s
+ 1),
114 test(S("1234567890"),
115 cpp17_input_iterator
<const char*>(s
),
116 cpp17_input_iterator
<const char*>(s
+ 10),
117 S("1234567890ABCDEFGHIJ"));
118 test(S("1234567890"),
119 cpp17_input_iterator
<const char*>(s
),
120 cpp17_input_iterator
<const char*>(s
+ 52),
121 S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
123 test(S("12345678901234567890"),
124 cpp17_input_iterator
<const char*>(s
),
125 cpp17_input_iterator
<const char*>(s
),
126 S("12345678901234567890"));
127 test(S("12345678901234567890"),
128 cpp17_input_iterator
<const char*>(s
),
129 cpp17_input_iterator
<const char*>(s
+ 1),
130 S("12345678901234567890"
132 test(S("12345678901234567890"),
133 cpp17_input_iterator
<const char*>(s
),
134 cpp17_input_iterator
<const char*>(s
+ 10),
135 S("12345678901234567890"
137 test(S("12345678901234567890"),
138 cpp17_input_iterator
<const char*>(s
),
139 cpp17_input_iterator
<const char*>(s
+ 52),
140 S("12345678901234567890"
141 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
144 #ifndef TEST_HAS_NO_EXCEPTIONS
145 if (!TEST_IS_CONSTANT_EVALUATED
) { // test iterator operations that throw
146 typedef ThrowingIterator
<char> TIter
;
147 typedef cpp17_input_iterator
<TIter
> IIter
;
148 const char* s
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
149 test_exceptions(S(), IIter(TIter(s
, s
+ 10, 4, TIter::TAIncrement
)), IIter(TIter()));
150 test_exceptions(S(), IIter(TIter(s
, s
+ 10, 5, TIter::TADereference
)), IIter(TIter()));
151 test_exceptions(S(), IIter(TIter(s
, s
+ 10, 6, TIter::TAComparison
)), IIter(TIter()));
153 test_exceptions(S(), TIter(s
, s
+ 10, 4, TIter::TAIncrement
), TIter());
154 test_exceptions(S(), TIter(s
, s
+ 10, 5, TIter::TADereference
), TIter());
155 test_exceptions(S(), TIter(s
, s
+ 10, 6, TIter::TAComparison
), TIter());
158 test_exceptions(S(), w
, w
+ 100);
162 { // test appending to self
164 S s_long
= "Lorem ipsum dolor sit amet, consectetur/";
166 s_short
.append(s_short
.begin(), s_short
.end());
167 assert(s_short
== "123/123/");
168 s_short
.append(s_short
.begin(), s_short
.end());
169 assert(s_short
== "123/123/123/123/");
170 s_short
.append(s_short
.begin(), s_short
.end());
171 assert(s_short
== "123/123/123/123/123/123/123/123/");
173 s_long
.append(s_long
.begin(), s_long
.end());
174 assert(s_long
== "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
177 { // test appending a different type
178 const std::uint8_t p
[] = "ABCD";
185 { // regression-test appending to self in sneaky ways
187 S s_long
= "Lorem ipsum dolor sit amet, consectetur/";
188 S s_othertype
= "hello";
189 S s_sneaky
= "hello";
191 test(s_short
, s_short
.data() + s_short
.size(), s_short
.data() + s_short
.size() + 1, S("hello\0", 6));
193 s_long
.data() + s_long
.size(),
194 s_long
.data() + s_long
.size() + 1,
195 S("Lorem ipsum dolor sit amet, consectetur/\0", 41));
197 s_sneaky
.reserve(12);
198 test(s_sneaky
, s_sneaky
.data(), s_sneaky
.data() + 6, S("hellohello\0", 11));
200 if (!TEST_IS_CONSTANT_EVALUATED
) {
201 const unsigned char* first
= reinterpret_cast<const unsigned char*>(s_othertype
.data());
202 test(s_othertype
, first
+ 2, first
+ 5, S("hellollo"));
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";
211 s
.append(MoveIt(It(std::begin(p
))), MoveIt(It(std::end(p
) - 1)));
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";
219 s
.append(MoveIt(It(std::begin(p
))), MoveIt(It(std::end(p
) - 1)));
224 TEST_CONSTEXPR_CXX20
bool test() {
225 test_string
<std::string
>();
226 #if TEST_STD_VER >= 11
227 test_string
<std::basic_string
<char, std::char_traits
<char>, min_allocator
<char> > >();
233 int main(int, char**) {
235 #if TEST_STD_VER > 17
236 static_assert(test());