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