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(basic_string_view<charT,traits> sv); // constexpr since C++20 13 14 #include <string> 15 #include <string_view> 16 #include <cassert> 17 18 #include "test_macros.h" 19 #include "min_allocator.h" 20 #include "test_allocator.h" 21 22 template <class S, class SV> 23 TEST_CONSTEXPR_CXX20 void 24 test(S s, SV sv, S expected) 25 { 26 s.assign(sv); 27 LIBCPP_ASSERT(s.__invariants()); 28 assert(s == expected); 29 } 30 31 template <class S, class SV> 32 TEST_CONSTEXPR_CXX20 void 33 testAlloc(S s, SV sv, const typename S::allocator_type& a) 34 { 35 s.assign(sv); 36 LIBCPP_ASSERT(s.__invariants()); 37 assert(s == sv); 38 assert(s.get_allocator() == a); 39 } 40 41 template <class S> 42 TEST_CONSTEXPR_CXX20 void test_string() { 43 typedef std::string_view SV; 44 test(S(), SV(), S()); 45 test(S(), SV("12345"), S("12345")); 46 test(S(), SV("1234567890"), S("1234567890")); 47 test(S(), SV("12345678901234567890"), S("12345678901234567890")); 48 49 test(S("12345"), SV(), S()); 50 test(S("12345"), SV("12345"), S("12345")); 51 test(S("12345"), SV("1234567890"), S("1234567890")); 52 test(S("12345"), SV("12345678901234567890"), S("12345678901234567890")); 53 54 test(S("1234567890"), SV(), S()); 55 test(S("1234567890"), SV("12345"), S("12345")); 56 test(S("1234567890"), SV("1234567890"), S("1234567890")); 57 test(S("1234567890"), SV("12345678901234567890"), S("12345678901234567890")); 58 59 test(S("12345678901234567890"), SV(), S()); 60 test(S("12345678901234567890"), SV("12345"), S("12345")); 61 test(S("12345678901234567890"), SV("1234567890"), S("1234567890")); 62 test(S("12345678901234567890"), SV("12345678901234567890"), 63 S("12345678901234567890")); 64 65 using A = typename S::allocator_type; 66 67 testAlloc(S(), SV(), A()); 68 testAlloc(S(), SV("12345"), A()); 69 testAlloc(S(), SV("1234567890"), A()); 70 testAlloc(S(), SV("12345678901234567890"), A()); 71 } 72 73 TEST_CONSTEXPR_CXX20 bool test() { 74 test_string<std::string>(); 75 #if TEST_STD_VER >= 11 76 test_string<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>(); 77 #endif 78 79 return true; 80 } 81 82 int main(int, char**) 83 { 84 test(); 85 #if TEST_STD_VER > 17 86 static_assert(test()); 87 #endif 88 89 return 0; 90 } 91