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 
10 // <string_view>
11 
12 //  constexpr basic_string_view(const _CharT* _s, size_type _len)
13 //      : __data (_s), __size(_len) {}
14 
15 
16 #include <string_view>
17 #include <string>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 
22 template<typename CharT>
test(const CharT * s,size_t sz)23 void test ( const CharT *s, size_t sz ) {
24     {
25     typedef std::basic_string_view<CharT> SV;
26     LIBCPP_ASSERT_NOEXCEPT(SV(s, sz));
27 
28     SV sv1 ( s, sz );
29     assert ( sv1.size() == sz );
30     assert ( sv1.data() == s );
31     }
32 }
33 
main(int,char **)34 int main(int, char**) {
35 
36     test ( "QBCDE", 5 );
37     test ( "QBCDE", 2 );
38     test ( "", 0 );
39 #if TEST_STD_VER > 11
40     {
41     constexpr const char *s = "QBCDE";
42     constexpr std::basic_string_view<char> sv1 ( s, 2 );
43     static_assert ( sv1.size() == 2, "" );
44     static_assert ( sv1.data() == s, "" );
45     }
46 #endif
47 
48     test ( L"QBCDE", 5 );
49     test ( L"QBCDE", 2 );
50     test ( L"", 0 );
51 #if TEST_STD_VER > 11
52     {
53     constexpr const wchar_t *s = L"QBCDE";
54     constexpr std::basic_string_view<wchar_t> sv1 ( s, 2 );
55     static_assert ( sv1.size() == 2, "" );
56     static_assert ( sv1.data() == s, "" );
57     }
58 #endif
59 
60 #if TEST_STD_VER >= 11
61     test ( u"QBCDE", 5 );
62     test ( u"QBCDE", 2 );
63     test ( u"", 0 );
64 #if TEST_STD_VER > 11
65     {
66     constexpr const char16_t *s = u"QBCDE";
67     constexpr std::basic_string_view<char16_t> sv1 ( s, 2 );
68     static_assert ( sv1.size() == 2, "" );
69     static_assert ( sv1.data() == s, "" );
70     }
71 #endif
72 
73     test ( U"QBCDE", 5 );
74     test ( U"QBCDE", 2 );
75     test ( U"", 0 );
76 #if TEST_STD_VER > 11
77     {
78     constexpr const char32_t *s = U"QBCDE";
79     constexpr std::basic_string_view<char32_t> sv1 ( s, 2 );
80     static_assert ( sv1.size() == 2, "" );
81     static_assert ( sv1.data() == s, "" );
82     }
83 #endif
84 #endif
85 
86   return 0;
87 }
88