Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 3 | // 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 Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | |
| 10 | // <string_view> |
| 11 | |
| 12 | // constexpr const _CharT& operator[](size_type _pos) const; |
| 13 | |
| 14 | #include <string_view> |
| 15 | #include <cassert> |
| 16 | |
| 17 | #include "test_macros.h" |
| 18 | |
| 19 | template <typename CharT> |
| 20 | void test ( const CharT *s, size_t len ) { |
Marshall Clow | 9ea0e47 | 2019-03-19 03:30:07 +0000 | [diff] [blame] | 21 | typedef std::basic_string_view<CharT> SV; |
| 22 | SV sv ( s, len ); |
| 23 | ASSERT_SAME_TYPE(decltype(sv[0]), typename SV::const_reference); |
| 24 | LIBCPP_ASSERT_NOEXCEPT( sv[0]); |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 25 | assert ( sv.length() == len ); |
| 26 | for ( size_t i = 0; i < len; ++i ) { |
| 27 | assert ( sv[i] == s[i] ); |
| 28 | assert ( &sv[i] == s + i ); |
| 29 | } |
| 30 | } |
| 31 | |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 32 | int main(int, char**) { |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 33 | test ( "ABCDE", 5 ); |
| 34 | test ( "a", 1 ); |
| 35 | |
| 36 | test ( L"ABCDE", 5 ); |
| 37 | test ( L"a", 1 ); |
| 38 | |
| 39 | #if TEST_STD_VER >= 11 |
| 40 | test ( u"ABCDE", 5 ); |
| 41 | test ( u"a", 1 ); |
| 42 | |
| 43 | test ( U"ABCDE", 5 ); |
| 44 | test ( U"a", 1 ); |
| 45 | #endif |
| 46 | |
Stephan T. Lavavej | 0f901c7 | 2016-11-04 20:26:59 +0000 | [diff] [blame] | 47 | #if TEST_STD_VER > 11 |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 48 | { |
| 49 | constexpr std::basic_string_view<char> sv ( "ABC", 2 ); |
| 50 | static_assert ( sv.length() == 2, "" ); |
| 51 | static_assert ( sv[0] == 'A', "" ); |
| 52 | static_assert ( sv[1] == 'B', "" ); |
| 53 | } |
| 54 | #endif |
JF Bastien | 2df59c5 | 2019-02-04 20:31:13 +0000 | [diff] [blame] | 55 | |
| 56 | return 0; |
Marshall Clow | 053d81c | 2016-07-21 05:31:24 +0000 | [diff] [blame] | 57 | } |