blob: 3003ea83547077bc5b3c353d6f42653bf4c3397d [file] [log] [blame]
Marshall Clow164b2972014-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 Fiselier427f1a32014-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 Fiselier40310ac2015-02-24 10:52:07 +000012// XFAIL: clang-3.4, clang-3.3
Marshall Clow164b2972014-06-11 16:44:55 +000013
14// <string_view>
15
16// constexpr const _CharT& at(size_type _pos) const;
17
18#include <experimental/string_view>
19#include <cassert>
20
21template <typename CharT>
22void test ( const CharT *s, size_t len ) {
23 std::experimental::basic_string_view<CharT> sv ( s, len );
24 assert ( sv.length() == len );
25 for ( size_t i = 0; i < len; ++i ) {
26 assert ( sv.at(i) == s[i] );
27 assert ( &sv.at(i) == s + i );
28 }
29
30 try { sv.at(len); } catch ( const std::out_of_range & ) { return ; }
31 assert ( false );
32 }
33
34int main () {
35 test ( "ABCDE", 5 );
36 test ( "a", 1 );
37
38 test ( L"ABCDE", 5 );
39 test ( L"a", 1 );
40
41#if __cplusplus >= 201103L
42 test ( u"ABCDE", 5 );
43 test ( u"a", 1 );
44
45 test ( U"ABCDE", 5 );
46 test ( U"a", 1 );
47#endif
48
Marshall Clow48472872014-07-08 22:38:11 +000049#if __cplusplus >= 201103L
Marshall Clow164b2972014-06-11 16:44:55 +000050 {
51 constexpr std::experimental::basic_string_view<char> sv ( "ABC", 2 );
52 static_assert ( sv.length() == 2, "" );
53 static_assert ( sv.at(0) == 'A', "" );
54 static_assert ( sv.at(1) == 'B', "" );
55 }
56#endif
57}