66 lines · cpp
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-exception6//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 19template <typename CharT>20void 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_EXCEPTIONS29 try {30 (void)sv.at(len);31 } catch (const std::out_of_range&) {32 return;33 }34 assert(false);35#endif36}37 38int main(int, char**) {39 test("ABCDE", 5);40 test("a", 1);41 42#ifndef TEST_HAS_NO_WIDE_CHARACTERS43 test(L"ABCDE", 5);44 test(L"a", 1);45#endif46 47#if TEST_STD_VER >= 1148 test(u"ABCDE", 5);49 test(u"a", 1);50 51 test(U"ABCDE", 5);52 test(U"a", 1);53#endif54 55#if TEST_STD_VER >= 1156 {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#endif63 64 return 0;65}66