• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
test(std::basic_string_view<charT> sv)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   {
32     S s2(sv);
33     LIBCPP_ASSERT(s2.__invariants());
34     assert(s2.size() == sv.size());
35     assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
36     assert(s2.get_allocator() == A());
37     assert(s2.capacity() >= s2.size());
38   }
39   {
40     S s2;
41     s2 = sv;
42     LIBCPP_ASSERT(s2.__invariants());
43     assert(s2.size() == sv.size());
44     assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
45     assert(s2.get_allocator() == A());
46     assert(s2.capacity() >= s2.size());
47   }
48 }
49 
50 template <class charT, class A>
51 void
test(std::basic_string_view<charT> sv,const A & a)52 test(std::basic_string_view<charT> sv, const A& a)
53 {
54     typedef std::basic_string<charT, std::char_traits<charT>, A> S;
55     typedef typename S::traits_type T;
56   {
57     S s2(sv, a);
58     LIBCPP_ASSERT(s2.__invariants());
59     assert(s2.size() == sv.size());
60     assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
61     assert(s2.get_allocator() == a);
62     assert(s2.capacity() >= s2.size());
63   }
64   {
65     S s2(a);
66     s2 = sv;
67     LIBCPP_ASSERT(s2.__invariants());
68     assert(s2.size() == sv.size());
69     assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
70     assert(s2.get_allocator() == a);
71     assert(s2.capacity() >= s2.size());
72   }
73 }
74 
main()75 int main()
76 {
77     {
78     typedef test_allocator<char> A;
79     typedef std::basic_string_view<char, std::char_traits<char> > SV;
80 
81     test(SV(""));
82     test(SV(""), A(2));
83 
84     test(SV("1"));
85     test(SV("1") ,A(2));
86 
87     test(SV("1234567980"));
88     test(SV("1234567980"), A(2));
89 
90     test(SV("123456798012345679801234567980123456798012345679801234567980"));
91     test(SV("123456798012345679801234567980123456798012345679801234567980"), A(2));
92     }
93 #if TEST_STD_VER >= 11
94     {
95     typedef min_allocator<char> A;
96     typedef std::basic_string_view<char, std::char_traits<char> > SV;
97 
98     test(SV(""));
99     test(SV(""), A());
100 
101     test(SV("1"));
102     test(SV("1") ,A());
103 
104     test(SV("1234567980"));
105     test(SV("1234567980"), A());
106 
107     test(SV("123456798012345679801234567980123456798012345679801234567980"));
108     test(SV("123456798012345679801234567980123456798012345679801234567980"), A());
109     }
110 #endif
111 }
112