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