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 // XFAIL: LIBCXX-AIX-FIXME 10 11 // <string> 12 13 // template<class InputIterator> 14 // basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20 15 16 #include <string> 17 #include <cassert> 18 19 #include "test_macros.h" 20 #include "test_iterators.h" 21 #include "min_allocator.h" 22 23 template <class S, class It> 24 TEST_CONSTEXPR_CXX20 void 25 test(S s, It first, It last, S expected) 26 { 27 s.append(first, last); 28 LIBCPP_ASSERT(s.__invariants()); 29 assert(s == expected); 30 } 31 32 #ifndef TEST_HAS_NO_EXCEPTIONS 33 struct Widget { operator char() const { throw 42; } }; 34 35 template <class S, class It> 36 TEST_CONSTEXPR_CXX20 void 37 test_exceptions(S s, It first, It last) 38 { 39 S original = s; 40 typename S::iterator begin = s.begin(); 41 typename S::iterator end = s.end(); 42 43 try { 44 s.append(first, last); 45 assert(false); 46 } catch (...) {} 47 48 // Part of "no effects" is that iterators and pointers 49 // into the string must not have been invalidated. 50 LIBCPP_ASSERT(s.__invariants()); 51 assert(s == original); 52 assert(s.begin() == begin); 53 assert(s.end() == end); 54 } 55 #endif 56 57 TEST_CONSTEXPR_CXX20 bool test() { 58 { 59 typedef std::string S; 60 const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 61 test(S(), s, s, S()); 62 test(S(), s, s+1, S("A")); 63 test(S(), s, s+10, S("ABCDEFGHIJ")); 64 test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 65 66 test(S("12345"), s, s, S("12345")); 67 test(S("12345"), s, s+1, S("12345A")); 68 test(S("12345"), s, s+10, S("12345ABCDEFGHIJ")); 69 test(S("12345"), s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 70 71 test(S("1234567890"), s, s, S("1234567890")); 72 test(S("1234567890"), s, s+1, S("1234567890A")); 73 test(S("1234567890"), s, s+10, S("1234567890ABCDEFGHIJ")); 74 test(S("1234567890"), s, s+52, S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 75 76 test(S("12345678901234567890"), s, s, S("12345678901234567890")); 77 test(S("12345678901234567890"), s, s+1, S("12345678901234567890""A")); 78 test(S("12345678901234567890"), s, s+10, S("12345678901234567890""ABCDEFGHIJ")); 79 test(S("12345678901234567890"), s, s+52, 80 S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 81 82 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S()); 83 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("A")); 84 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 85 S("ABCDEFGHIJ")); 86 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 87 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 88 89 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 90 S("12345")); 91 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 92 S("12345A")); 93 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 94 S("12345ABCDEFGHIJ")); 95 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 96 S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 97 98 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 99 S("1234567890")); 100 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 101 S("1234567890A")); 102 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 103 S("1234567890ABCDEFGHIJ")); 104 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 105 S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 106 107 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 108 S("12345678901234567890")); 109 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 110 S("12345678901234567890""A")); 111 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 112 S("12345678901234567890""ABCDEFGHIJ")); 113 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 114 S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 115 } 116 #if TEST_STD_VER >= 11 117 { 118 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; 119 const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 120 test(S(), s, s, S()); 121 test(S(), s, s+1, S("A")); 122 test(S(), s, s+10, S("ABCDEFGHIJ")); 123 test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 124 125 test(S("12345"), s, s, S("12345")); 126 test(S("12345"), s, s+1, S("12345A")); 127 test(S("12345"), s, s+10, S("12345ABCDEFGHIJ")); 128 test(S("12345"), s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 129 130 test(S("1234567890"), s, s, S("1234567890")); 131 test(S("1234567890"), s, s+1, S("1234567890A")); 132 test(S("1234567890"), s, s+10, S("1234567890ABCDEFGHIJ")); 133 test(S("1234567890"), s, s+52, S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 134 135 test(S("12345678901234567890"), s, s, S("12345678901234567890")); 136 test(S("12345678901234567890"), s, s+1, S("12345678901234567890""A")); 137 test(S("12345678901234567890"), s, s+10, S("12345678901234567890""ABCDEFGHIJ")); 138 test(S("12345678901234567890"), s, s+52, 139 S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 140 141 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S()); 142 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("A")); 143 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 144 S("ABCDEFGHIJ")); 145 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 146 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 147 148 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 149 S("12345")); 150 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 151 S("12345A")); 152 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 153 S("12345ABCDEFGHIJ")); 154 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 155 S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 156 157 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 158 S("1234567890")); 159 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 160 S("1234567890A")); 161 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 162 S("1234567890ABCDEFGHIJ")); 163 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 164 S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 165 166 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 167 S("12345678901234567890")); 168 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 169 S("12345678901234567890""A")); 170 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 171 S("12345678901234567890""ABCDEFGHIJ")); 172 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 173 S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 174 } 175 #endif 176 #ifndef TEST_HAS_NO_EXCEPTIONS 177 if (!TEST_IS_CONSTANT_EVALUATED) { // test iterator operations that throw 178 typedef std::string S; 179 typedef ThrowingIterator<char> TIter; 180 typedef cpp17_input_iterator<TIter> IIter; 181 const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 182 test_exceptions(S(), IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter(TIter())); 183 test_exceptions(S(), IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter(TIter())); 184 test_exceptions(S(), IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter(TIter())); 185 186 test_exceptions(S(), TIter(s, s+10, 4, TIter::TAIncrement), TIter()); 187 test_exceptions(S(), TIter(s, s+10, 5, TIter::TADereference), TIter()); 188 test_exceptions(S(), TIter(s, s+10, 6, TIter::TAComparison), TIter()); 189 190 Widget w[100]; 191 test_exceptions(S(), w, w+100); 192 } 193 #endif 194 195 { // test appending to self 196 typedef std::string S; 197 S s_short = "123/"; 198 S s_long = "Lorem ipsum dolor sit amet, consectetur/"; 199 200 s_short.append(s_short.begin(), s_short.end()); 201 assert(s_short == "123/123/"); 202 s_short.append(s_short.begin(), s_short.end()); 203 assert(s_short == "123/123/123/123/"); 204 s_short.append(s_short.begin(), s_short.end()); 205 assert(s_short == "123/123/123/123/123/123/123/123/"); 206 207 s_long.append(s_long.begin(), s_long.end()); 208 assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/"); 209 } 210 211 { // test appending a different type 212 typedef std::string S; 213 const uint8_t p[] = "ABCD"; 214 215 S s; 216 s.append(p, p + 4); 217 assert(s == "ABCD"); 218 } 219 220 { // regression-test appending to self in sneaky ways 221 std::string s_short = "hello"; 222 std::string s_long = "Lorem ipsum dolor sit amet, consectetur/"; 223 std::string s_othertype = "hello"; 224 std::string s_sneaky = "hello"; 225 226 test(s_short, s_short.data() + s_short.size(), s_short.data() + s_short.size() + 1, 227 std::string("hello\0", 6)); 228 test(s_long, s_long.data() + s_long.size(), s_long.data() + s_long.size() + 1, 229 std::string("Lorem ipsum dolor sit amet, consectetur/\0", 41)); 230 231 s_sneaky.reserve(12); 232 test(s_sneaky, s_sneaky.data(), s_sneaky.data() + 6, std::string("hellohello\0", 11)); 233 234 if (!TEST_IS_CONSTANT_EVALUATED) { 235 const unsigned char *first = reinterpret_cast<const unsigned char*>(s_othertype.data()); 236 test(s_othertype, first + 2, first + 5, std::string("hellollo")); 237 } 238 } 239 240 { // test with a move iterator that returns char&& 241 typedef forward_iterator<const char*> It; 242 typedef std::move_iterator<It> MoveIt; 243 const char p[] = "ABCD"; 244 std::string s; 245 s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1))); 246 assert(s == "ABCD"); 247 } 248 { // test with a move iterator that returns char&& 249 typedef const char* It; 250 typedef std::move_iterator<It> MoveIt; 251 const char p[] = "ABCD"; 252 std::string s; 253 s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1))); 254 assert(s == "ABCD"); 255 } 256 257 return true; 258 } 259 260 int main(int, char**) 261 { 262 test(); 263 #if TEST_STD_VER > 17 264 static_assert(test()); 265 #endif 266 267 return 0; 268 } 269