xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp (revision 425620ccdd47e56b59512913cdc71e116f951e4e)
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 // XFAIL: LIBCXX-AIX-FIXME
10 
11 // <string>
12 
13 // basic_string<charT,traits,Allocator>&
14 //   assign(const basic_string<charT,traits>& str); // constexpr since C++20
15 
16 #include <string>
17 #include <cassert>
18 
19 #include "test_macros.h"
20 #include "min_allocator.h"
21 #include "test_allocator.h"
22 
23 template <class S>
24 TEST_CONSTEXPR_CXX20 void
25 test(S s, S str, S expected)
26 {
27     s.assign(str);
28     LIBCPP_ASSERT(s.__invariants());
29     assert(s == expected);
30 }
31 
32 template <class S>
33 TEST_CONSTEXPR_CXX20 void
34 testAlloc(S s, S str, const typename S::allocator_type& a)
35 {
36     s.assign(str);
37     LIBCPP_ASSERT(s.__invariants());
38     assert(s == str);
39     assert(s.get_allocator() == a);
40 }
41 
42 TEST_CONSTEXPR_CXX20 bool test() {
43   {
44     typedef std::string S;
45     test(S(), S(), S());
46     test(S(), S("12345"), S("12345"));
47     test(S(), S("1234567890"), S("1234567890"));
48     test(S(), S("12345678901234567890"), S("12345678901234567890"));
49 
50     test(S("12345"), S(), S());
51     test(S("12345"), S("12345"), S("12345"));
52     test(S("12345"), S("1234567890"), S("1234567890"));
53     test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
54 
55     test(S("1234567890"), S(), S());
56     test(S("1234567890"), S("12345"), S("12345"));
57     test(S("1234567890"), S("1234567890"), S("1234567890"));
58     test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
59 
60     test(S("12345678901234567890"), S(), S());
61     test(S("12345678901234567890"), S("12345"), S("12345"));
62     test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
63     test(S("12345678901234567890"), S("12345678901234567890"),
64          S("12345678901234567890"));
65 
66     testAlloc(S(), S(), std::allocator<char>());
67     testAlloc(S(), S("12345"), std::allocator<char>());
68     testAlloc(S(), S("1234567890"), std::allocator<char>());
69     testAlloc(S(), S("12345678901234567890"), std::allocator<char>());
70   }
71 
72   { //  LWG#5579 make sure assign takes the allocators where appropriate
73     typedef other_allocator<char> A;  // has POCCA --> true
74     typedef std::basic_string<char, std::char_traits<char>, A> S;
75     testAlloc(S(A(5)), S(A(3)), A(3));
76     testAlloc(S(A(5)), S("1"), A());
77     testAlloc(S(A(5)), S("1", A(7)), A(7));
78     testAlloc(S(A(5)), S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), A(7));
79   }
80 
81 #if TEST_STD_VER >= 11
82   {
83     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
84     test(S(), S(), S());
85     test(S(), S("12345"), S("12345"));
86     test(S(), S("1234567890"), S("1234567890"));
87     test(S(), S("12345678901234567890"), S("12345678901234567890"));
88 
89     test(S("12345"), S(), S());
90     test(S("12345"), S("12345"), S("12345"));
91     test(S("12345"), S("1234567890"), S("1234567890"));
92     test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
93 
94     test(S("1234567890"), S(), S());
95     test(S("1234567890"), S("12345"), S("12345"));
96     test(S("1234567890"), S("1234567890"), S("1234567890"));
97     test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
98 
99     test(S("12345678901234567890"), S(), S());
100     test(S("12345678901234567890"), S("12345"), S("12345"));
101     test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
102     test(S("12345678901234567890"), S("12345678901234567890"),
103          S("12345678901234567890"));
104 
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 
121 int main(int, char**)
122 {
123   test();
124 #if TEST_STD_VER > 17
125   static_assert(test());
126 #endif
127 
128   return 0;
129 }
130