Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / basic.string / string.modifiers / string_append / iterator.pass.cpp
blob2982cfe77042c0d6cbafb7f2adda89bb01f984df
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 //===----------------------------------------------------------------------===//
9 // <string>
11 // template<class InputIterator>
12 // basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20
14 #include <string>
15 #include <cassert>
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
29 struct Widget {
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) {
35 S original = s;
36 typename S::iterator begin = s.begin();
37 typename S::iterator end = s.end();
39 try {
40 s.append(first, last);
41 assert(false);
42 } catch (...) {
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);
52 #endif
54 template <class S>
55 TEST_CONSTEXPR_CXX20 void test_string() {
57 const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
58 test(S(), s, s, S());
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"),
76 s + 1,
77 S("12345678901234567890"
78 "A"));
79 test(S("12345678901234567890"),
81 s + 10,
82 S("12345678901234567890"
83 "ABCDEFGHIJ"));
84 test(S("12345678901234567890"),
86 s + 52,
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"));
93 test(S(),
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"));
100 test(S("12345"),
101 cpp17_input_iterator<const char*>(s),
102 cpp17_input_iterator<const char*>(s + 10),
103 S("12345ABCDEFGHIJ"));
104 test(S("12345"),
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),
113 S("1234567890A"));
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"
131 "A"));
132 test(S("12345678901234567890"),
133 cpp17_input_iterator<const char*>(s),
134 cpp17_input_iterator<const char*>(s + 10),
135 S("12345678901234567890"
136 "ABCDEFGHIJ"));
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());
157 Widget w[100];
158 test_exceptions(S(), w, w + 100);
160 #endif
162 { // test appending to self
163 S s_short = "123/";
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";
180 S s;
181 s.append(p, p + 4);
182 assert(s == "ABCD");
185 { // regression-test appending to self in sneaky ways
186 S s_short = "hello";
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));
192 test(s_long,
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";
210 S s;
211 s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1)));
212 assert(s == "ABCD");
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 S s;
219 s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1)));
220 assert(s == "ABCD");
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> > >();
228 #endif
230 return true;
233 int main(int, char**) {
234 test();
235 #if TEST_STD_VER > 17
236 static_assert(test());
237 #endif
239 return 0;