blob: 3d741c33ffbd0b2f316b58cc61909bb7155c256e [file] [log] [blame]
Marshall Clow053d81c2016-07-21 05:31:24 +00001//===----------------------------------------------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Marshall Clow053d81c2016-07-21 05:31:24 +00006//
7//===----------------------------------------------------------------------===//
8
Stephan T. Lavaveja730ed32017-01-18 20:10:25 +00009// NOTE: Older versions of clang have a bug where they fail to evaluate
Marshall Clow053d81c2016-07-21 05:31:24 +000010// string_view::at as a constant expression.
11// XFAIL: clang-3.4, clang-3.3
12
13
14// <string_view>
15
16// constexpr const _CharT& at(size_type _pos) const;
17
18#include <string_view>
19#include <stdexcept>
20#include <cassert>
21
22#include "test_macros.h"
23
24template <typename CharT>
25void test ( const CharT *s, size_t len ) {
26 std::basic_string_view<CharT> sv ( s, len );
27 assert ( sv.length() == len );
28 for ( size_t i = 0; i < len; ++i ) {
29 assert ( sv.at(i) == s[i] );
30 assert ( &sv.at(i) == s + i );
31 }
32
33#ifndef TEST_HAS_NO_EXCEPTIONS
Billy Robert O'Neal III83252762017-11-15 07:40:37 +000034 try { (void)sv.at(len); } catch ( const std::out_of_range & ) { return ; }
Marshall Clow053d81c2016-07-21 05:31:24 +000035 assert ( false );
36#endif
37}
38
JF Bastien2df59c52019-02-04 20:31:13 +000039int main(int, char**) {
Marshall Clow053d81c2016-07-21 05:31:24 +000040 test ( "ABCDE", 5 );
41 test ( "a", 1 );
42
43 test ( L"ABCDE", 5 );
44 test ( L"a", 1 );
45
46#if TEST_STD_VER >= 11
47 test ( u"ABCDE", 5 );
48 test ( u"a", 1 );
49
50 test ( U"ABCDE", 5 );
51 test ( U"a", 1 );
52#endif
53
54#if TEST_STD_VER >= 11
55 {
56 constexpr std::basic_string_view<char> sv ( "ABC", 2 );
57 static_assert ( sv.length() == 2, "" );
58 static_assert ( sv.at(0) == 'A', "" );
59 static_assert ( sv.at(1) == 'B', "" );
60 }
61#endif
JF Bastien2df59c52019-02-04 20:31:13 +000062
63 return 0;
Marshall Clow053d81c2016-07-21 05:31:24 +000064}