1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
11 // const_reference operator[](size_type pos) const; // constexpr since C++20
12 // reference operator[](size_type pos); // constexpr since C++20
17 #include "test_macros.h"
18 #include "min_allocator.h"
21 TEST_CONSTEXPR_CXX20
void test_string() {
24 ASSERT_SAME_TYPE(decltype(s
[0]), typename
S::reference
);
25 ASSERT_SAME_TYPE(decltype(cs
[0]), typename
S::const_reference
);
26 LIBCPP_ASSERT_NOEXCEPT(s
[0]);
27 LIBCPP_ASSERT_NOEXCEPT(cs
[0]);
28 for (typename
S::size_type i
= 0; i
< cs
.size(); ++i
) {
29 assert(s
[i
] == static_cast<char>('0' + i
));
30 assert(cs
[i
] == s
[i
]);
32 assert(cs
[cs
.size()] == '\0');
34 assert(s2
[0] == '\0');
37 // Same, but for the string that doesn't fit into SSO.
39 TEST_CONSTEXPR_CXX20
void test_string_long() {
40 S
s("0123456789012345678901234567890123456789");
42 ASSERT_SAME_TYPE(decltype(s
[0]), typename
S::reference
);
43 ASSERT_SAME_TYPE(decltype(cs
[0]), typename
S::const_reference
);
44 LIBCPP_ASSERT_NOEXCEPT(s
[0]);
45 LIBCPP_ASSERT_NOEXCEPT(cs
[0]);
46 for (typename
S::size_type i
= 0; i
< cs
.size(); ++i
) {
47 assert(s
[i
] == static_cast<char>('0' + (i
% 10)));
48 assert(cs
[i
] == s
[i
]);
50 assert(s
[33] == static_cast<char>('0' + (33 % 10)));
51 assert(cs
[34] == s
[34]);
52 assert(cs
[cs
.size()] == '\0');
54 assert(s2
[0] == '\0');
57 TEST_CONSTEXPR_CXX20
bool test() {
58 test_string
<std::string
>();
59 #if TEST_STD_VER >= 11
60 test_string
<std::basic_string
<char, std::char_traits
<char>, min_allocator
<char>>>();
61 test_string_long
<std::basic_string
<char, std::char_traits
<char>, min_allocator
<char>>>();
67 int main(int, char**) {
70 static_assert(test());