Home | History | Annotate | Download | only in string.view.access
      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 // NOTE: Older versions of clang have a bug where they fail to evaluate
     11 // string_view::at as a constant expression.
     12 // XFAIL: clang-3.4, clang-3.3
     13 
     14 
     15 // <string_view>
     16 
     17 // constexpr const _CharT& at(size_type _pos) const;
     18 
     19 #include <string_view>
     20 #include <stdexcept>
     21 #include <cassert>
     22 
     23 #include "test_macros.h"
     24 
     25 template <typename CharT>
     26 void test ( const CharT *s, size_t len ) {
     27     std::basic_string_view<CharT> sv ( s, len );
     28     assert ( sv.length() == len );
     29     for ( size_t i = 0; i < len; ++i ) {
     30         assert (  sv.at(i) == s[i] );
     31         assert ( &sv.at(i) == s + i );
     32     }
     33 
     34 #ifndef TEST_HAS_NO_EXCEPTIONS
     35     try { (void)sv.at(len); } catch ( const std::out_of_range & ) { return ; }
     36     assert ( false );
     37 #endif
     38 }
     39 
     40 int main () {
     41     test ( "ABCDE", 5 );
     42     test ( "a", 1 );
     43 
     44     test ( L"ABCDE", 5 );
     45     test ( L"a", 1 );
     46 
     47 #if TEST_STD_VER >= 11
     48     test ( u"ABCDE", 5 );
     49     test ( u"a", 1 );
     50 
     51     test ( U"ABCDE", 5 );
     52     test ( U"a", 1 );
     53 #endif
     54 
     55 #if TEST_STD_VER >= 11
     56     {
     57     constexpr std::basic_string_view<char> sv ( "ABC", 2 );
     58     static_assert ( sv.length() ==  2,  "" );
     59     static_assert ( sv.at(0) == 'A', "" );
     60     static_assert ( sv.at(1) == 'B', "" );
     61     }
     62 #endif
     63 }
     64