| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// |
| 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 |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
|
| 9 |
// <string_view> |
| 10 |
|
| 11 |
// constexpr const _CharT& at(size_type _pos) const; |
| 12 |
|
| 13 |
#include <string_view> |
| 14 |
#include <stdexcept> |
| 15 |
#include <cassert> |
| 16 |
|
| 17 |
#include "test_macros.h" |
| 18 |
|
| 19 |
template <typename CharT> |
| 20 |
void test(const CharT* s, std::size_t len) { |
| 21 |
std::basic_string_view<CharT> sv(s, len); |
| 22 |
assert(sv.length() == len); |
| 23 |
for (std::size_t i = 0; i < len; ++i) { |
| 24 |
assert(sv.at(i) == s[i]); |
| 25 |
assert(&sv.at(i) == s + i); |
| 26 |
} |
| 27 |
|
| 28 |
#ifndef TEST_HAS_NO_EXCEPTIONS |
| 29 |
try { |
| 30 |
(void)sv.at(len); |
| 31 |
} catch (const std::out_of_range&) { |
| 32 |
return; |
| 33 |
} |
| 34 |
assert(false); |
| 35 |
#endif |
| 36 |
} |
| 37 |
|
| 38 |
int main(int, char**) { |
| 39 |
test("ABCDE", 5); |
| 40 |
test("a", 1); |
| 41 |
|
| 42 |
#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 43 |
test(L"ABCDE", 5); |
| 44 |
test(L"a", 1); |
| 45 |
#endif |
| 46 |
|
| 47 |
#if TEST_STD_VER >= 11 |
| 48 |
test(u"ABCDE", 5); |
| 49 |
test(u"a", 1); |
| 50 |
|
| 51 |
test(U"ABCDE", 5); |
| 52 |
test(U"a", 1); |
| 53 |
#endif |
| 54 |
|
| 55 |
#if TEST_STD_VER >= 11 |
| 56 |
{ |
| 57 |
constexpr std::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 |
|
| 64 |
return 0; |
| 65 |
} |