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