xref: /llvm-project/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string_view.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(basic_string_view<charT,traits> sv); // constexpr since C++20
15 
16 #include <string>
17 #include <string_view>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "min_allocator.h"
22 #include "test_allocator.h"
23 
24 template <class S, class SV>
25 TEST_CONSTEXPR_CXX20 void
26 test(S s, SV sv, S expected)
27 {
28     s.assign(sv);
29     LIBCPP_ASSERT(s.__invariants());
30     assert(s == expected);
31 }
32 
33 template <class S, class SV>
34 TEST_CONSTEXPR_CXX20 void
35 testAlloc(S s, SV sv, const typename S::allocator_type& a)
36 {
37     s.assign(sv);
38     LIBCPP_ASSERT(s.__invariants());
39     assert(s == sv);
40     assert(s.get_allocator() == a);
41 }
42 
43 TEST_CONSTEXPR_CXX20 bool test() {
44   {
45     typedef std::string S;
46     typedef std::string_view SV;
47     test(S(), SV(), S());
48     test(S(), SV("12345"), S("12345"));
49     test(S(), SV("1234567890"), S("1234567890"));
50     test(S(), SV("12345678901234567890"), S("12345678901234567890"));
51 
52     test(S("12345"), SV(), S());
53     test(S("12345"), SV("12345"), S("12345"));
54     test(S("12345"), SV("1234567890"), S("1234567890"));
55     test(S("12345"), SV("12345678901234567890"), S("12345678901234567890"));
56 
57     test(S("1234567890"), SV(), S());
58     test(S("1234567890"), SV("12345"), S("12345"));
59     test(S("1234567890"), SV("1234567890"), S("1234567890"));
60     test(S("1234567890"), SV("12345678901234567890"), S("12345678901234567890"));
61 
62     test(S("12345678901234567890"), SV(), S());
63     test(S("12345678901234567890"), SV("12345"), S("12345"));
64     test(S("12345678901234567890"), SV("1234567890"), S("1234567890"));
65     test(S("12345678901234567890"), SV("12345678901234567890"),
66          S("12345678901234567890"));
67 
68     testAlloc(S(), SV(), std::allocator<char>());
69     testAlloc(S(), SV("12345"), std::allocator<char>());
70     testAlloc(S(), SV("1234567890"), std::allocator<char>());
71     testAlloc(S(), SV("12345678901234567890"), std::allocator<char>());
72   }
73 
74 #if TEST_STD_VER >= 11
75   {
76     typedef std::basic_string     <char, std::char_traits<char>, min_allocator<char>> S;
77     typedef std::basic_string_view<char, std::char_traits<char> > SV;
78     test(S(), SV(), S());
79     test(S(), SV("12345"), S("12345"));
80     test(S(), SV("1234567890"), S("1234567890"));
81     test(S(), SV("12345678901234567890"), S("12345678901234567890"));
82 
83     test(S("12345"), SV(), S());
84     test(S("12345"), SV("12345"), S("12345"));
85     test(S("12345"), SV("1234567890"), S("1234567890"));
86     test(S("12345"), SV("12345678901234567890"), S("12345678901234567890"));
87 
88     test(S("1234567890"), SV(), S());
89     test(S("1234567890"), SV("12345"), S("12345"));
90     test(S("1234567890"), SV("1234567890"), S("1234567890"));
91     test(S("1234567890"), SV("12345678901234567890"), S("12345678901234567890"));
92 
93     test(S("12345678901234567890"), SV(), S());
94     test(S("12345678901234567890"), SV("12345"), S("12345"));
95     test(S("12345678901234567890"), SV("1234567890"), S("1234567890"));
96     test(S("12345678901234567890"), SV("12345678901234567890"),
97          S("12345678901234567890"));
98 
99     testAlloc(S(), SV(), min_allocator<char>());
100     testAlloc(S(), SV("12345"), min_allocator<char>());
101     testAlloc(S(), SV("1234567890"), min_allocator<char>());
102     testAlloc(S(), SV("12345678901234567890"), min_allocator<char>());
103   }
104 #endif
105 
106   return true;
107 }
108 
109 int main(int, char**)
110 {
111   test();
112 #if TEST_STD_VER > 17
113   static_assert(test());
114 #endif
115 
116   return 0;
117 }
118