Marshall Clow | 164b297 | 2014-06-11 16:44:55 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 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 Fiselier | 427f1a3 | 2014-12-07 05:31:17 +0000 | [diff] [blame] | 10 | // NOTE: Older versions of clang have a bug where they fail to evalute |
| 11 | // string_view::at as a constant expression. |
Eric Fiselier | 40310ac | 2015-02-24 10:52:07 +0000 | [diff] [blame] | 12 | // XFAIL: clang-3.4, clang-3.3 |
Marshall Clow | 164b297 | 2014-06-11 16:44:55 +0000 | [diff] [blame] | 13 | |
| 14 | // <string_view> |
| 15 | |
| 16 | // constexpr const _CharT& at(size_type _pos) const; |
| 17 | |
| 18 | #include <experimental/string_view> |
| 19 | #include <cassert> |
| 20 | |
| 21 | template <typename CharT> |
| 22 | void 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 | |
| 34 | int 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 Clow | 4847287 | 2014-07-08 22:38:11 +0000 | [diff] [blame] | 49 | #if __cplusplus >= 201103L |
Marshall Clow | 164b297 | 2014-06-11 16:44:55 +0000 | [diff] [blame] | 50 | { |
| 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 | } |