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 // basic_string<charT,traits,Allocator>& 12 // assign(const basic_string<charT,traits>& str); // constexpr since C++20 13 14 #include <string> 15 #include <cassert> 16 17 #include "test_macros.h" 18 #include "nasty_string.h" 19 #include "min_allocator.h" 20 #include "test_allocator.h" 21 22 template <class S> 23 TEST_CONSTEXPR_CXX20 void test(S dest, S src) { 24 dest.assign(src); 25 LIBCPP_ASSERT(dest.__invariants()); 26 assert(dest == src); 27 } 28 29 template <class S> 30 TEST_CONSTEXPR_CXX20 void testAlloc(S dest, S src, const typename S::allocator_type& a) { 31 dest.assign(src); 32 LIBCPP_ASSERT(dest.__invariants()); 33 assert(dest == src); 34 assert(dest.get_allocator() == a); 35 } 36 37 template <class S> 38 TEST_CONSTEXPR_CXX20 void test_assign() { 39 using CharT = typename S::value_type; 40 41 test(S(), S()); 42 test(S(), S(CONVERT_TO_CSTRING(CharT, "12345"))); 43 test(S(), S(CONVERT_TO_CSTRING(CharT, "1234567890"))); 44 test(S(), S(CONVERT_TO_CSTRING(CharT, "12345678901234567890"))); 45 46 test(S(CONVERT_TO_CSTRING(CharT, "12345")), S()); 47 test(S(CONVERT_TO_CSTRING(CharT, "12345")), S(CONVERT_TO_CSTRING(CharT, "12345"))); 48 test(S(CONVERT_TO_CSTRING(CharT, "12345")), S(CONVERT_TO_CSTRING(CharT, "1234567890"))); 49 test(S(CONVERT_TO_CSTRING(CharT, "12345")), S(CONVERT_TO_CSTRING(CharT, "12345678901234567890"))); 50 51 test(S(CONVERT_TO_CSTRING(CharT, "1234567890")), S()); 52 test(S(CONVERT_TO_CSTRING(CharT, "1234567890")), S(CONVERT_TO_CSTRING(CharT, "12345"))); 53 test(S(CONVERT_TO_CSTRING(CharT, "1234567890")), S(CONVERT_TO_CSTRING(CharT, "1234567890"))); 54 test(S(CONVERT_TO_CSTRING(CharT, "1234567890")), S(CONVERT_TO_CSTRING(CharT, "12345678901234567890"))); 55 56 test(S(CONVERT_TO_CSTRING(CharT, "12345678901234567890")), S()); 57 test(S(CONVERT_TO_CSTRING(CharT, "12345678901234567890")), S(CONVERT_TO_CSTRING(CharT, "12345"))); 58 test(S(CONVERT_TO_CSTRING(CharT, "12345678901234567890")), S(CONVERT_TO_CSTRING(CharT, "1234567890"))); 59 test(S(CONVERT_TO_CSTRING(CharT, "12345678901234567890")), S(CONVERT_TO_CSTRING(CharT, "12345678901234567890"))); 60 } 61 62 TEST_CONSTEXPR_CXX20 bool test() { 63 test_assign<std::string>(); 64 #ifndef TEST_HAS_NO_WIDE_CHARACTERS 65 test_assign<std::wstring>(); 66 #endif 67 #if TEST_STD_VER >= 20 68 test_assign<std::u8string>(); 69 #endif 70 #if TEST_STD_VER >= 11 71 test_assign<std::u16string>(); 72 test_assign<std::u32string>(); 73 #endif 74 #ifndef TEST_HAS_NO_NASTY_STRING 75 test_assign<nasty_string>(); 76 #endif 77 78 { 79 typedef std::string S; 80 testAlloc(S(), S(), std::allocator<char>()); 81 testAlloc(S(), S("12345"), std::allocator<char>()); 82 testAlloc(S(), S("1234567890"), std::allocator<char>()); 83 testAlloc(S(), S("12345678901234567890"), std::allocator<char>()); 84 } 85 86 { // LWG#5579 make sure assign takes the allocators where appropriate 87 typedef other_allocator<char> A; // has POCCA --> true 88 typedef std::basic_string<char, std::char_traits<char>, A> S; 89 testAlloc(S(A(5)), S(A(3)), A(3)); 90 testAlloc(S(A(5)), S("1"), A()); 91 testAlloc(S(A(5)), S("1", A(7)), A(7)); 92 testAlloc(S(A(5)), S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), A(7)); 93 } 94 95 #if TEST_STD_VER >= 11 96 { 97 typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S; 98 test_assign<S>(); 99 testAlloc(S(), S(), min_allocator<char>()); 100 testAlloc(S(), S("12345"), min_allocator<char>()); 101 testAlloc(S(), S("1234567890"), min_allocator<char>()); 102 testAlloc(S(), S("12345678901234567890"), min_allocator<char>()); 103 } 104 #endif 105 #if TEST_STD_VER > 14 106 { 107 typedef std::string S; 108 static_assert(noexcept(S().assign(S())), ""); // LWG#2063 109 } 110 #endif 111 112 return true; 113 } 114 115 int main(int, char**) { 116 test(); 117 #if TEST_STD_VER > 17 118 static_assert(test()); 119 #endif 120 121 return 0; 122 } 123