xref: /llvm-project/libcxx/test/std/strings/basic.string/string.cons/string_view.pass.cpp (revision 053d81ceebe9a54255c49801b88ab61b5d03060a)
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // <string>
11 
12 // explicit basic_string(basic_string_view<CharT, traits> sv, const Allocator& a = Allocator());
13 
14 #include <string>
15 #include <string_view>
16 #include <stdexcept>
17 #include <algorithm>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "test_allocator.h"
22 #include "min_allocator.h"
23 
24 template <class charT>
25 void
26 test(std::basic_string_view<charT> sv)
27 {
28     typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
29     typedef typename S::traits_type T;
30     typedef typename S::allocator_type A;
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 template <class charT, class A>
40 void
41 test(std::basic_string_view<charT> sv, const A& a)
42 {
43     typedef std::basic_string<charT, std::char_traits<charT>, A> S;
44     typedef typename S::traits_type T;
45     S s2(sv, a);
46     LIBCPP_ASSERT(s2.__invariants());
47     assert(s2.size() == sv.size());
48     assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
49     assert(s2.get_allocator() == a);
50     assert(s2.capacity() >= s2.size());
51 }
52 
53 int main()
54 {
55     {
56     typedef test_allocator<char> A;
57     typedef std::basic_string_view<char, std::char_traits<char> > SV;
58 
59     test(SV(""));
60     test(SV(""), A(2));
61 
62     test(SV("1"));
63     test(SV("1") ,A(2));
64 
65     test(SV("1234567980"));
66     test(SV("1234567980"), A(2));
67 
68     test(SV("123456798012345679801234567980123456798012345679801234567980"));
69     test(SV("123456798012345679801234567980123456798012345679801234567980"), A(2));
70     }
71 #if TEST_STD_VER >= 11
72     {
73     typedef min_allocator<char> A;
74     typedef std::basic_string_view<char, std::char_traits<char> > SV;
75 
76     test(SV(""));
77     test(SV(""), A());
78 
79     test(SV("1"));
80     test(SV("1") ,A());
81 
82     test(SV("1234567980"));
83     test(SV("1234567980"), A());
84 
85     test(SV("123456798012345679801234567980123456798012345679801234567980"));
86     test(SV("123456798012345679801234567980123456798012345679801234567980"), A());
87     }
88 #endif
89 }
90