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)
13 //    : __data (_s), __size(_Traits::length(_s)) {}
14 
15 
16 #include <string_view>
17 #include <string>
18 #include <cassert>
19 
20 #include "test_macros.h"
21 #include "constexpr_char_traits.h"
22 
23 template<typename CharT>
StrLen(const CharT * s)24 size_t StrLen ( const CharT *s ) {
25     size_t retVal = 0;
26     while ( *s != 0 ) { ++retVal; ++s; }
27     return retVal;
28     }
29 
30 template<typename CharT>
test(const CharT * s)31 void test ( const CharT *s ) {
32     typedef std::basic_string_view<CharT> SV;
33 //  I'd love to do this, but it would require traits::length() to be noexcept
34 //  LIBCPP_ASSERT_NOEXCEPT(SV(s));
35 
36     SV sv1 ( s );
37     assert ( sv1.size() == StrLen( s ));
38     assert ( sv1.data() == s );
39     }
40 
41 
main(int,char **)42 int main(int, char**) {
43 
44     test ( "QBCDE" );
45     test ( "A" );
46     test ( "" );
47 
48     test ( L"QBCDE" );
49     test ( L"A" );
50     test ( L"" );
51 
52 #if TEST_STD_VER >= 11
53     test ( u"QBCDE" );
54     test ( u"A" );
55     test ( u"" );
56 
57     test ( U"QBCDE" );
58     test ( U"A" );
59     test ( U"" );
60 #endif
61 
62 #if TEST_STD_VER > 11
63     {
64     constexpr std::basic_string_view<char, constexpr_char_traits<char>> sv1 ( "ABCDE" );
65     static_assert ( sv1.size() == 5, "");
66     }
67 #endif
68 
69   return 0;
70 }
71