xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/string_view_assignment.pass.cpp (revision 1cf4113952ae3e4cc75decdf6feb3ce5dd8ca4a1)
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>& operator=(basic_string_view<charT, traits> sv); // constexpr since C++20
12 
13 #include <string>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 #include "min_allocator.h"
18 
19 template <class S, class SV>
20 TEST_CONSTEXPR_CXX20 void
21 test(S s1, SV sv)
22 {
23     typedef typename S::traits_type T;
24     s1 = sv;
25     LIBCPP_ASSERT(s1.__invariants());
26     assert(s1.size() == sv.size());
27     assert(T::compare(s1.data(), sv.data(), s1.size()) == 0);
28     assert(s1.capacity() >= s1.size());
29 }
30 
31 TEST_CONSTEXPR_CXX20 bool test() {
32   {
33     typedef std::string S;
34     typedef std::string_view SV;
35     test(S(), SV(""));
36     test(S("1"), SV(""));
37     test(S(), SV("1"));
38     test(S("1"), SV("2"));
39     test(S("1"), SV("2"));
40 
41     test(S(),
42          SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
43     test(S("123456789"),
44          SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
45     test(S("1234567890123456789012345678901234567890123456789012345678901234567890"),
46          SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
47     test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
48            "1234567890123456789012345678901234567890123456789012345678901234567890"),
49          SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
50   }
51 #if TEST_STD_VER >= 11
52   {
53     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
54     typedef std::string_view SV;
55     test(S(), SV(""));
56     test(S("1"), SV(""));
57     test(S(), SV("1"));
58     test(S("1"), SV("2"));
59     test(S("1"), SV("2"));
60 
61     test(S(),
62          SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
63     test(S("123456789"),
64          SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
65     test(S("1234567890123456789012345678901234567890123456789012345678901234567890"),
66          SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
67     test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
68            "1234567890123456789012345678901234567890123456789012345678901234567890"),
69          SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
70   }
71 #endif
72 
73   return true;
74 }
75 
76 int main(int, char**)
77 {
78   test();
79 #if TEST_STD_VER > 17
80   static_assert(test());
81 #endif
82 
83   return 0;
84 }
85