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); // constexpr since C++20 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 TEST_CONSTEXPR_CXX20 void 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 struct Widget { operator char() const { throw 42; } }; 32 33 template <class S, class It> 34 TEST_CONSTEXPR_CXX20 void 35 test_exceptions(S s, It first, It last) 36 { 37 S original = s; 38 typename S::iterator begin = s.begin(); 39 typename S::iterator end = s.end(); 40 41 try { 42 s.append(first, last); 43 assert(false); 44 } catch (...) {} 45 46 // Part of "no effects" is that iterators and pointers 47 // into the string must not have been invalidated. 48 LIBCPP_ASSERT(s.__invariants()); 49 assert(s == original); 50 assert(s.begin() == begin); 51 assert(s.end() == end); 52 } 53 #endif 54 55 template <class S> 56 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")); 62 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")); 67 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")); 72 73 test(S("12345678901234567890"), s, s, S("12345678901234567890")); 74 test(S("12345678901234567890"), s, s+1, S("12345678901234567890""A")); 75 test(S("12345678901234567890"), s, s+10, S("12345678901234567890""ABCDEFGHIJ")); 76 test(S("12345678901234567890"), s, s+52, 77 S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 78 79 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S()); 80 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("A")); 81 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 82 S("ABCDEFGHIJ")); 83 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 84 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 85 86 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 87 S("12345")); 88 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 89 S("12345A")); 90 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 91 S("12345ABCDEFGHIJ")); 92 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 93 S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 94 95 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 96 S("1234567890")); 97 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 98 S("1234567890A")); 99 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 100 S("1234567890ABCDEFGHIJ")); 101 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 102 S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 103 104 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 105 S("12345678901234567890")); 106 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 107 S("12345678901234567890""A")); 108 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 109 S("12345678901234567890""ABCDEFGHIJ")); 110 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 111 S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 112 } 113 114 TEST_CONSTEXPR_CXX20 bool test() { 115 test_string<std::string>(); 116 #if TEST_STD_VER >= 11 117 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>(); 118 #endif 119 120 #ifndef TEST_HAS_NO_EXCEPTIONS 121 if (!TEST_IS_CONSTANT_EVALUATED) { // test iterator operations that throw 122 typedef std::string S; 123 typedef ThrowingIterator<char> TIter; 124 typedef cpp17_input_iterator<TIter> IIter; 125 const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 126 test_exceptions(S(), IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter(TIter())); 127 test_exceptions(S(), IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter(TIter())); 128 test_exceptions(S(), IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter(TIter())); 129 130 test_exceptions(S(), TIter(s, s+10, 4, TIter::TAIncrement), TIter()); 131 test_exceptions(S(), TIter(s, s+10, 5, TIter::TADereference), TIter()); 132 test_exceptions(S(), TIter(s, s+10, 6, TIter::TAComparison), TIter()); 133 134 Widget w[100]; 135 test_exceptions(S(), w, w+100); 136 } 137 #endif 138 139 { // test appending to self 140 typedef std::string S; 141 S s_short = "123/"; 142 S s_long = "Lorem ipsum dolor sit amet, consectetur/"; 143 144 s_short.append(s_short.begin(), s_short.end()); 145 assert(s_short == "123/123/"); 146 s_short.append(s_short.begin(), s_short.end()); 147 assert(s_short == "123/123/123/123/"); 148 s_short.append(s_short.begin(), s_short.end()); 149 assert(s_short == "123/123/123/123/123/123/123/123/"); 150 151 s_long.append(s_long.begin(), s_long.end()); 152 assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/"); 153 } 154 155 { // test appending a different type 156 typedef std::string S; 157 const std::uint8_t p[] = "ABCD"; 158 159 S s; 160 s.append(p, p + 4); 161 assert(s == "ABCD"); 162 } 163 164 { // regression-test appending to self in sneaky ways 165 std::string s_short = "hello"; 166 std::string s_long = "Lorem ipsum dolor sit amet, consectetur/"; 167 std::string s_othertype = "hello"; 168 std::string s_sneaky = "hello"; 169 170 test(s_short, s_short.data() + s_short.size(), s_short.data() + s_short.size() + 1, 171 std::string("hello\0", 6)); 172 test(s_long, s_long.data() + s_long.size(), s_long.data() + s_long.size() + 1, 173 std::string("Lorem ipsum dolor sit amet, consectetur/\0", 41)); 174 175 s_sneaky.reserve(12); 176 test(s_sneaky, s_sneaky.data(), s_sneaky.data() + 6, std::string("hellohello\0", 11)); 177 178 if (!TEST_IS_CONSTANT_EVALUATED) { 179 const unsigned char *first = reinterpret_cast<const unsigned char*>(s_othertype.data()); 180 test(s_othertype, first + 2, first + 5, std::string("hellollo")); 181 } 182 } 183 184 { // test with a move iterator that returns char&& 185 typedef forward_iterator<const char*> It; 186 typedef std::move_iterator<It> MoveIt; 187 const char p[] = "ABCD"; 188 std::string s; 189 s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1))); 190 assert(s == "ABCD"); 191 } 192 { // test with a move iterator that returns char&& 193 typedef const char* It; 194 typedef std::move_iterator<It> MoveIt; 195 const char p[] = "ABCD"; 196 std::string s; 197 s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1))); 198 assert(s == "ABCD"); 199 } 200 201 return true; 202 } 203 204 int main(int, char**) 205 { 206 test(); 207 #if TEST_STD_VER > 17 208 static_assert(test()); 209 #endif 210 211 return 0; 212 } 213