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