Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / string.view / string.view.access / index.pass.cpp
blobbe6bbd78bd1e4b1f98d3a32f65fcc73ecebb8891
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 // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)
11 // <string_view>
13 // constexpr const _CharT& operator[](size_type _pos) const;
15 #include <string_view>
16 #include <cassert>
18 #include "test_macros.h"
20 template <typename CharT>
21 void test(const CharT* s, std::size_t len) {
22 typedef std::basic_string_view<CharT> SV;
23 SV sv(s, len);
24 ASSERT_SAME_TYPE(decltype(sv[0]), typename SV::const_reference);
25 LIBCPP_ASSERT_NOEXCEPT(sv[0]);
26 assert(sv.length() == len);
27 for (std::size_t i = 0; i < len; ++i) {
28 assert(sv[i] == s[i]);
29 assert(&sv[i] == s + i);
33 int main(int, char**) {
34 test("ABCDE", 5);
35 test("a", 1);
37 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
38 test(L"ABCDE", 5);
39 test(L"a", 1);
40 #endif
42 #if TEST_STD_VER >= 11
43 test(u"ABCDE", 5);
44 test(u"a", 1);
46 test(U"ABCDE", 5);
47 test(U"a", 1);
48 #endif
50 #if TEST_STD_VER > 11
52 constexpr std::basic_string_view<char> sv("ABC", 2);
53 static_assert(sv.length() == 2, "");
54 static_assert(sv[0] == 'A', "");
55 static_assert(sv[1] == 'B', "");
57 #endif
59 return 0;