xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp (revision 57b08b0944046a6a57ee9b7b479181f548a5b9b4)
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);
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 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 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 int main()
41 {
42     {
43     typedef std::string S;
44     test(S(), S(), S());
45     test(S(), S("12345"), S("12345"));
46     test(S(), S("1234567890"), S("1234567890"));
47     test(S(), S("12345678901234567890"), S("12345678901234567890"));
48 
49     test(S("12345"), S(), S());
50     test(S("12345"), S("12345"), S("12345"));
51     test(S("12345"), S("1234567890"), S("1234567890"));
52     test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
53 
54     test(S("1234567890"), S(), S());
55     test(S("1234567890"), S("12345"), S("12345"));
56     test(S("1234567890"), S("1234567890"), S("1234567890"));
57     test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
58 
59     test(S("12345678901234567890"), S(), S());
60     test(S("12345678901234567890"), S("12345"), S("12345"));
61     test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
62     test(S("12345678901234567890"), S("12345678901234567890"),
63          S("12345678901234567890"));
64 
65     testAlloc(S(), S(), std::allocator<char>());
66     testAlloc(S(), S("12345"), std::allocator<char>());
67     testAlloc(S(), S("1234567890"), std::allocator<char>());
68     testAlloc(S(), S("12345678901234567890"), std::allocator<char>());
69     }
70 
71     { //  LWG#5579 make sure assign takes the allocators where appropriate
72     typedef other_allocator<char> A;  // has POCCA --> true
73     typedef std::basic_string<char, std::char_traits<char>, A> S;
74     testAlloc(S(A(5)), S(A(3)), A(3));
75     testAlloc(S(A(5)), S("1"), A());
76     testAlloc(S(A(5)), S("1", A(7)), A(7));
77     testAlloc(S(A(5)), S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), A(7));
78     }
79 
80 #if TEST_STD_VER >= 11
81     {
82     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
83     test(S(), S(), S());
84     test(S(), S("12345"), S("12345"));
85     test(S(), S("1234567890"), S("1234567890"));
86     test(S(), S("12345678901234567890"), S("12345678901234567890"));
87 
88     test(S("12345"), S(), S());
89     test(S("12345"), S("12345"), S("12345"));
90     test(S("12345"), S("1234567890"), S("1234567890"));
91     test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
92 
93     test(S("1234567890"), S(), S());
94     test(S("1234567890"), S("12345"), S("12345"));
95     test(S("1234567890"), S("1234567890"), S("1234567890"));
96     test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
97 
98     test(S("12345678901234567890"), S(), S());
99     test(S("12345678901234567890"), S("12345"), S("12345"));
100     test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
101     test(S("12345678901234567890"), S("12345678901234567890"),
102          S("12345678901234567890"));
103 
104     testAlloc(S(), S(), min_allocator<char>());
105     testAlloc(S(), S("12345"), min_allocator<char>());
106     testAlloc(S(), S("1234567890"), min_allocator<char>());
107     testAlloc(S(), S("12345678901234567890"), min_allocator<char>());
108     }
109 #endif
110 #if TEST_STD_VER > 14
111     {
112     typedef std::string S;
113     static_assert(noexcept(S().assign(S())), "");  // LWG#2063
114     }
115 #endif
116 }
117