Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / string.view / string.view.access / at.pass.cpp
blob95d37932f4060c3c42c9a4944cdc6dc28e236bea
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 //===----------------------------------------------------------------------===//
9 // <string_view>
11 // constexpr const _CharT& at(size_type _pos) const;
13 #include <string_view>
14 #include <stdexcept>
15 #include <cassert>
17 #include "test_macros.h"
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);
28 #ifndef TEST_HAS_NO_EXCEPTIONS
29 try {
30 (void)sv.at(len);
31 } catch (const std::out_of_range&) {
32 return;
34 assert(false);
35 #endif
38 int main(int, char**) {
39 test("ABCDE", 5);
40 test("a", 1);
42 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
43 test(L"ABCDE", 5);
44 test(L"a", 1);
45 #endif
47 #if TEST_STD_VER >= 11
48 test(u"ABCDE", 5);
49 test(u"a", 1);
51 test(U"ABCDE", 5);
52 test(U"a", 1);
53 #endif
55 #if TEST_STD_VER >= 11
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', "");
62 #endif
64 return 0;