xref: /minix3/external/bsd/libc++/dist/libcxx/test/experimental/string.view/string.view.access/at.pass.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc //                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc //
5*0a6a1f1dSLionel Sambuc // This file is dual licensed under the MIT and the University of Illinois Open
6*0a6a1f1dSLionel Sambuc // Source Licenses. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc //
8*0a6a1f1dSLionel Sambuc //===----------------------------------------------------------------------===//
9*0a6a1f1dSLionel Sambuc 
10*0a6a1f1dSLionel Sambuc 
11*0a6a1f1dSLionel Sambuc // <string_view>
12*0a6a1f1dSLionel Sambuc 
13*0a6a1f1dSLionel Sambuc // constexpr const _CharT& at(size_type _pos) const;
14*0a6a1f1dSLionel Sambuc 
15*0a6a1f1dSLionel Sambuc #include <experimental/string_view>
16*0a6a1f1dSLionel Sambuc #include <cassert>
17*0a6a1f1dSLionel Sambuc 
18*0a6a1f1dSLionel Sambuc template <typename CharT>
test(const CharT * s,size_t len)19*0a6a1f1dSLionel Sambuc void test ( const CharT *s, size_t len ) {
20*0a6a1f1dSLionel Sambuc     std::experimental::basic_string_view<CharT> sv ( s, len );
21*0a6a1f1dSLionel Sambuc     assert ( sv.length() == len );
22*0a6a1f1dSLionel Sambuc     for ( size_t i = 0; i < len; ++i ) {
23*0a6a1f1dSLionel Sambuc         assert (  sv.at(i) == s[i] );
24*0a6a1f1dSLionel Sambuc         assert ( &sv.at(i) == s + i );
25*0a6a1f1dSLionel Sambuc         }
26*0a6a1f1dSLionel Sambuc 
27*0a6a1f1dSLionel Sambuc     try { sv.at(len); } catch ( const std::out_of_range & ) { return ; }
28*0a6a1f1dSLionel Sambuc     assert ( false );
29*0a6a1f1dSLionel Sambuc     }
30*0a6a1f1dSLionel Sambuc 
main()31*0a6a1f1dSLionel Sambuc int main () {
32*0a6a1f1dSLionel Sambuc     test ( "ABCDE", 5 );
33*0a6a1f1dSLionel Sambuc     test ( "a", 1 );
34*0a6a1f1dSLionel Sambuc 
35*0a6a1f1dSLionel Sambuc     test ( L"ABCDE", 5 );
36*0a6a1f1dSLionel Sambuc     test ( L"a", 1 );
37*0a6a1f1dSLionel Sambuc 
38*0a6a1f1dSLionel Sambuc #if __cplusplus >= 201103L
39*0a6a1f1dSLionel Sambuc     test ( u"ABCDE", 5 );
40*0a6a1f1dSLionel Sambuc     test ( u"a", 1 );
41*0a6a1f1dSLionel Sambuc 
42*0a6a1f1dSLionel Sambuc     test ( U"ABCDE", 5 );
43*0a6a1f1dSLionel Sambuc     test ( U"a", 1 );
44*0a6a1f1dSLionel Sambuc #endif
45*0a6a1f1dSLionel Sambuc 
46*0a6a1f1dSLionel Sambuc #if __cplusplus >= 201103L
47*0a6a1f1dSLionel Sambuc     {
48*0a6a1f1dSLionel Sambuc     constexpr std::experimental::basic_string_view<char> sv ( "ABC", 2 );
49*0a6a1f1dSLionel Sambuc     static_assert ( sv.length() ==  2,  "" );
50*0a6a1f1dSLionel Sambuc     static_assert ( sv.at(0) == 'A', "" );
51*0a6a1f1dSLionel Sambuc     static_assert ( sv.at(1) == 'B', "" );
52*0a6a1f1dSLionel Sambuc     }
53*0a6a1f1dSLionel Sambuc #endif
54*0a6a1f1dSLionel Sambuc }
55