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 const _CharT& at(size_type _pos) const;
14
15 #include <experimental/string_view>
16 #include <cassert>
17
18 template <typename CharT>
test(const CharT * s,size_t len)19 void test ( const CharT *s, size_t len ) {
20 std::experimental::basic_string_view<CharT> sv ( s, len );
21 assert ( sv.length() == len );
22 for ( size_t i = 0; i < len; ++i ) {
23 assert ( sv.at(i) == s[i] );
24 assert ( &sv.at(i) == s + i );
25 }
26
27 try { sv.at(len); } catch ( const std::out_of_range & ) { return ; }
28 assert ( false );
29 }
30
main()31 int main () {
32 test ( "ABCDE", 5 );
33 test ( "a", 1 );
34
35 test ( L"ABCDE", 5 );
36 test ( L"a", 1 );
37
38 #if __cplusplus >= 201103L
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 __cplusplus >= 201103L
47 {
48 constexpr std::experimental::basic_string_view<char> sv ( "ABC", 2 );
49 static_assert ( sv.length() == 2, "" );
50 static_assert ( sv.at(0) == 'A', "" );
51 static_assert ( sv.at(1) == 'B', "" );
52 }
53 #endif
54 }
55