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