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); // 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.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 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()); 64 test(S("12345"), s, s+1, S("A")); 65 test(S("12345"), s, s+10, S("ABCDEFGHIJ")); 66 test(S("12345"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 67 68 test(S("1234567890"), s, s, S()); 69 test(S("1234567890"), s, s+1, S("A")); 70 test(S("1234567890"), s, s+10, S("ABCDEFGHIJ")); 71 test(S("1234567890"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 72 73 test(S("12345678901234567890"), s, s, S()); 74 test(S("12345678901234567890"), s, s+1, S("A")); 75 test(S("12345678901234567890"), s, s+10, S("ABCDEFGHIJ")); 76 test(S("12345678901234567890"), s, s+52, 77 S("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()); 88 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 89 S("A")); 90 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 91 S("ABCDEFGHIJ")); 92 test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 93 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 94 95 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 96 S()); 97 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 98 S("A")); 99 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 100 S("ABCDEFGHIJ")); 101 test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 102 S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")); 103 104 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), 105 S()); 106 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), 107 S("A")); 108 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), 109 S("ABCDEFGHIJ")); 110 test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), 111 S("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 assigning 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.assign(s_short.begin(), s_short.end()); 145 assert(s_short == "123/"); 146 s_short.assign(s_short.begin() + 2, s_short.end()); 147 assert(s_short == "3/"); 148 149 s_long.assign(s_long.begin(), s_long.end()); 150 assert(s_long == "Lorem ipsum dolor sit amet, consectetur/"); 151 152 s_long.assign(s_long.begin() + 30, s_long.end()); 153 assert(s_long == "nsectetur/"); 154 } 155 156 { // test assigning a different type 157 typedef std::string S; 158 const std::uint8_t p[] = "ABCD"; 159 160 S s; 161 s.assign(p, p + 4); 162 assert(s == "ABCD"); 163 } 164 165 { // regression-test assigning to self in sneaky ways 166 std::string sneaky = "hello"; 167 sneaky.resize(sneaky.capacity(), 'x'); 168 std::string expected = sneaky + std::string(1, '\0'); 169 test(sneaky, sneaky.data(), sneaky.data() + sneaky.size() + 1, expected); 170 } 171 return true; 172 } 173 174 int main(int, char**) 175 { 176 test(); 177 #if TEST_STD_VER > 17 178 static_assert(test()); 179 #endif 180 181 return 0; 182 } 183