blob: eaea062987fb36664e61cb1003705a6a50d45d92 [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
Stephan T. Lavaveja730ed32017-01-18 20:10:25 +000010// NOTE: Older versions of clang have a bug where they fail to evaluate
Eric Fiselier0dc119b2014-12-07 05:31:17 +000011// string_view::at as a constant expression.
Eric Fiselierf7d36ac2015-02-24 10:52:07 +000012// XFAIL: clang-3.4, clang-3.3
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000013
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
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000023#include "test_macros.h"
24
Marshall Clow5aa8fa22014-06-11 16:44:55 +000025template <typename CharT>
26void test ( const CharT *s, size_t len ) {
27 std::experimental::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 );
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000032 }
Marshall Clow5aa8fa22014-06-11 16:44:55 +000033
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000034#ifndef TEST_HAS_NO_EXCEPTIONS
Marshall Clow5aa8fa22014-06-11 16:44:55 +000035 try { sv.at(len); } catch ( const std::out_of_range & ) { return ; }
36 assert ( false );
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000037#endif
38}
Eric Fiselierd04c6852016-06-01 21:35:39 +000039
Marshall Clow5aa8fa22014-06-11 16:44:55 +000040int main () {
41 test ( "ABCDE", 5 );
42 test ( "a", 1 );
43
44 test ( L"ABCDE", 5 );
45 test ( L"a", 1 );
46
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000047#if TEST_STD_VER >= 11
Marshall Clow5aa8fa22014-06-11 16:44:55 +000048 test ( u"ABCDE", 5 );
49 test ( u"a", 1 );
50
51 test ( U"ABCDE", 5 );
52 test ( U"a", 1 );
53#endif
54
Eric Fiselieraf4a5a72016-05-30 23:53:19 +000055#if TEST_STD_VER >= 11
Marshall Clow5aa8fa22014-06-11 16:44:55 +000056 {
57 constexpr std::experimental::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}