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& assign(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 23 test(S s, It first, It last, S expected) 24 { 25 s.assign(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 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.assign(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 int main(int, char**) 56 { 57 { 58 typedef std::string S; 59 const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 60 test(S(), s, s, S()); 61 test(S(), s, s+1, S("A")); 62 test(S(), s, s+10, S("ABCDEFGHIJ")); 63 test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 64 65 test(S("12345"), s, s, S()); 66 test(S("12345"), s, s+1, S("A")); 67 test(S("12345"), s, s+10, S("ABCDEFGHIJ")); 68 test(S("12345"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 69 70 test(S("1234567890"), s, s, S()); 71 test(S("1234567890"), s, s+1, S("A")); 72 test(S("1234567890"), s, s+10, S("ABCDEFGHIJ")); 73 test(S("1234567890"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 74 75 test(S("12345678901234567890"), s, s, S()); 76 test(S("12345678901234567890"), s, s+1, S("A")); 77 test(S("12345678901234567890"), s, s+10, S("ABCDEFGHIJ")); 78 test(S("12345678901234567890"), s, s+52, 79 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 80 81 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S()); 82 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("A")); 83 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 84 S("ABCDEFGHIJ")); 85 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 86 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 87 88 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 89 S()); 90 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 91 S("A")); 92 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 93 S("ABCDEFGHIJ")); 94 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 95 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 96 97 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 98 S()); 99 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 100 S("A")); 101 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 102 S("ABCDEFGHIJ")); 103 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 104 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 105 106 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 107 S()); 108 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 109 S("A")); 110 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 111 S("ABCDEFGHIJ")); 112 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 113 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 114 } 115 #if TEST_STD_VER >= 11 116 { 117 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; 118 const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 119 test(S(), s, s, S()); 120 test(S(), s, s+1, S("A")); 121 test(S(), s, s+10, S("ABCDEFGHIJ")); 122 test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 123 124 test(S("12345"), s, s, S()); 125 test(S("12345"), s, s+1, S("A")); 126 test(S("12345"), s, s+10, S("ABCDEFGHIJ")); 127 test(S("12345"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 128 129 test(S("1234567890"), s, s, S()); 130 test(S("1234567890"), s, s+1, S("A")); 131 test(S("1234567890"), s, s+10, S("ABCDEFGHIJ")); 132 test(S("1234567890"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 133 134 test(S("12345678901234567890"), s, s, S()); 135 test(S("12345678901234567890"), s, s+1, S("A")); 136 test(S("12345678901234567890"), s, s+10, S("ABCDEFGHIJ")); 137 test(S("12345678901234567890"), s, s+52, 138 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 139 140 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S()); 141 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("A")); 142 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 143 S("ABCDEFGHIJ")); 144 test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 145 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 146 147 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 148 S()); 149 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 150 S("A")); 151 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 152 S("ABCDEFGHIJ")); 153 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 154 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 155 156 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 157 S()); 158 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 159 S("A")); 160 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 161 S("ABCDEFGHIJ")); 162 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 163 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 164 165 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 166 S()); 167 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 168 S("A")); 169 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 170 S("ABCDEFGHIJ")); 171 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 172 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 173 } 174 #endif 175 #ifndef TEST_HAS_NO_EXCEPTIONS 176 { // test iterator operations that throw 177 typedef std::string S; 178 typedef ThrowingIterator<char> TIter; 179 typedef cpp17_input_iterator<TIter> IIter; 180 const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 181 test_exceptions(S(), IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter()); 182 test_exceptions(S(), IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter()); 183 test_exceptions(S(), IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter()); 184 185 test_exceptions(S(), TIter(s, s+10, 4, TIter::TAIncrement), TIter()); 186 test_exceptions(S(), TIter(s, s+10, 5, TIter::TADereference), TIter()); 187 test_exceptions(S(), TIter(s, s+10, 6, TIter::TAComparison), TIter()); 188 189 Widget w[100]; 190 test_exceptions(S(), w, w+100); 191 } 192 #endif 193 194 { // test assigning to self 195 typedef std::string S; 196 S s_short = "123/"; 197 S s_long = "Lorem ipsum dolor sit amet, consectetur/"; 198 199 s_short.assign(s_short.begin(), s_short.end()); 200 assert(s_short == "123/"); 201 s_short.assign(s_short.begin() + 2, s_short.end()); 202 assert(s_short == "3/"); 203 204 s_long.assign(s_long.begin(), s_long.end()); 205 assert(s_long == "Lorem ipsum dolor sit amet, consectetur/"); 206 207 s_long.assign(s_long.begin() + 30, s_long.end()); 208 assert(s_long == "nsectetur/"); 209 } 210 211 { // test assigning a different type 212 typedef std::string S; 213 const uint8_t p[] = "ABCD"; 214 215 S s; 216 s.assign(p, p + 4); 217 assert(s == "ABCD"); 218 } 219 220 { // regression-test assigning to self in sneaky ways 221 std::string sneaky = "hello"; 222 sneaky.resize(sneaky.capacity(), 'x'); 223 std::string expected = sneaky + std::string(1, '\0'); 224 test(sneaky, sneaky.data(), sneaky.data() + sneaky.size() + 1, expected); 225 } 226 227 return 0; 228 } 229