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 void
test(std::basic_string_view<charT> sv)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 void
test(std::basic_string_view<charT> sv,const A & a)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 
main(int,char **)74 int main(int, char**)
75 {
76     {
77     typedef test_allocator<char> A;
78     typedef std::basic_string_view<char, std::char_traits<char> > SV;
79 
80     test(SV(""));
81     test(SV(""), A(2));
82 
83     test(SV("1"));
84     test(SV("1") ,A(2));
85 
86     test(SV("1234567980"));
87     test(SV("1234567980"), A(2));
88 
89     test(SV("123456798012345679801234567980123456798012345679801234567980"));
90     test(SV("123456798012345679801234567980123456798012345679801234567980"), A(2));
91     }
92 #if TEST_STD_VER >= 11
93     {
94     typedef min_allocator<char> A;
95     typedef std::basic_string_view<char, std::char_traits<char> > SV;
96 
97     test(SV(""));
98     test(SV(""), A());
99 
100     test(SV("1"));
101     test(SV("1") ,A());
102 
103     test(SV("1234567980"));
104     test(SV("1234567980"), A());
105 
106     test(SV("123456798012345679801234567980123456798012345679801234567980"));
107     test(SV("123456798012345679801234567980123456798012345679801234567980"), A());
108     }
109 #endif
110 
111   return 0;
112 }
113