blob: 46804d4df3af8e09ba3212cd404ddaf12eb86e04 [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>
Marshall Clow8d113d42016-01-12 14:51:04 +000020#include <stdexcept>
Marshall Clow5aa8fa22014-06-11 16:44:55 +000021#include <cassert>
22
23template <typename CharT>
24void test ( const CharT *s, size_t len ) {
25 std::experimental::basic_string_view<CharT> sv ( s, len );
26 assert ( sv.length() == len );
27 for ( size_t i = 0; i < len; ++i ) {
28 assert ( sv.at(i) == s[i] );
29 assert ( &sv.at(i) == s + i );
30 }
31
32 try { sv.at(len); } catch ( const std::out_of_range & ) { return ; }
33 assert ( false );
34 }
35
36int main () {
37 test ( "ABCDE", 5 );
38 test ( "a", 1 );
39
40 test ( L"ABCDE", 5 );
41 test ( L"a", 1 );
42
43#if __cplusplus >= 201103L
44 test ( u"ABCDE", 5 );
45 test ( u"a", 1 );
46
47 test ( U"ABCDE", 5 );
48 test ( U"a", 1 );
49#endif
50
Marshall Clow926731b2014-07-08 22:38:11 +000051#if __cplusplus >= 201103L
Marshall Clow5aa8fa22014-06-11 16:44:55 +000052 {
53 constexpr std::experimental::basic_string_view<char> sv ( "ABC", 2 );
54 static_assert ( sv.length() == 2, "" );
55 static_assert ( sv.at(0) == 'A', "" );
56 static_assert ( sv.at(1) == 'B', "" );
57 }
58#endif
59}