blob: a3208c1645a297e7d3387965e2c2abac7a3cc974 [file] [log] [blame]
Marshall Clow5aa8fa22014-06-11 16:44:55 +00001//===----------------------------------------------------------------------===//
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
Eric Fiselier0dc119b2014-12-07 05:31:17 +000010// NOTE: Older versions of clang have a bug where they fail to evalute
11// string_view::at as a constant expression.
Eric Fiselierf7d36ac2015-02-24 10:52:07 +000012// XFAIL: clang-3.4, clang-3.3
Asiri Rathnayakef520c142015-11-10 11:41:22 +000013// XFAIL: libcpp-no-exceptions
Marshall Clow5aa8fa22014-06-11 16:44:55 +000014
15// <string_view>
16
17// constexpr const _CharT& at(size_type _pos) const;
18
19#include <experimental/string_view>
20#include <cassert>
21
22template <typename CharT>
23void test ( const CharT *s, size_t len ) {
24 std::experimental::basic_string_view<CharT> sv ( s, len );
25 assert ( sv.length() == len );
26 for ( size_t i = 0; i < len; ++i ) {
27 assert ( sv.at(i) == s[i] );
28 assert ( &sv.at(i) == s + i );
29 }
30
31 try { sv.at(len); } catch ( const std::out_of_range & ) { return ; }
32 assert ( false );
33 }
34
35int main () {
36 test ( "ABCDE", 5 );
37 test ( "a", 1 );
38
39 test ( L"ABCDE", 5 );
40 test ( L"a", 1 );
41
42#if __cplusplus >= 201103L
43 test ( u"ABCDE", 5 );
44 test ( u"a", 1 );
45
46 test ( U"ABCDE", 5 );
47 test ( U"a", 1 );
48#endif
49
Marshall Clow926731b2014-07-08 22:38:11 +000050#if __cplusplus >= 201103L
Marshall Clow5aa8fa22014-06-11 16:44:55 +000051 {
52 constexpr std::experimental::basic_string_view<char> sv ( "ABC", 2 );
53 static_assert ( sv.length() == 2, "" );
54 static_assert ( sv.at(0) == 'A', "" );
55 static_assert ( sv.at(1) == 'B', "" );
56 }
57#endif
58}