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