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