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 const _CharT& operator[](size_type _pos) const;
12 
13 #include <string_view>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 
18 template <typename CharT>
19 void test ( const CharT *s, size_t len ) {
20     typedef std::basic_string_view<CharT> SV;
21     SV sv ( s, len );
22     ASSERT_SAME_TYPE(decltype(sv[0]), typename SV::const_reference);
23     LIBCPP_ASSERT_NOEXCEPT(   sv[0]);
24     assert ( sv.length() == len );
25     for ( size_t i = 0; i < len; ++i ) {
26         assert ( sv[i] == s[i] );
27         assert ( &sv[i] == s + i );
28         }
29     }
30 
31 int main(int, char**) {
32     test ( "ABCDE", 5 );
33     test ( "a", 1 );
34 
35     test ( L"ABCDE", 5 );
36     test ( L"a", 1 );
37 
38 #if TEST_STD_VER >= 11
39     test ( u"ABCDE", 5 );
40     test ( u"a", 1 );
41 
42     test ( U"ABCDE", 5 );
43     test ( U"a", 1 );
44 #endif
45 
46 #if TEST_STD_VER > 11
47     {
48     constexpr std::basic_string_view<char> sv ( "ABC", 2 );
49     static_assert ( sv.length() ==  2,  "" );
50     static_assert ( sv[0]  == 'A', "" );
51     static_assert ( sv[1]  == 'B', "" );
52     }
53 #endif
54 
55   return 0;
56 }
57