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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)
13 // [string.view.capacity], capacity
14 // constexpr size_type size() const noexcept;
15 // constexpr size_type length() const noexcept;
16 // constexpr size_type max_size() const noexcept;
17 // constexpr bool empty() const noexcept;
19 #include <string_view>
24 #include "test_macros.h"
26 template <typename SV
>
31 static_assert(sv1
.size() == 0, "");
32 static_assert(sv1
.empty(), "");
33 static_assert(sv1
.size() == sv1
.length(), "");
34 static_assert(sv1
.max_size() > sv1
.size(), "");
40 ASSERT_NOEXCEPT(sv1
.size());
41 ASSERT_NOEXCEPT(sv1
.empty());
42 ASSERT_NOEXCEPT(sv1
.max_size());
43 ASSERT_NOEXCEPT(sv1
.length());
44 assert(sv1
.size() == 0);
46 assert(sv1
.size() == sv1
.length());
47 assert(sv1
.max_size() > sv1
.size());
50 // Sanity check max_size() -- a string_view can't store more bytes than a single object
51 // can contain. Any implementation that fails this check is certainly lying.
53 typedef typename
SV::value_type CharT
;
54 typedef typename
SV::size_type Size
;
56 assert(sv
.max_size() <= std::numeric_limits
<Size
>::max() / sizeof(CharT
));
57 LIBCPP_ASSERT(sv
.max_size() == std::numeric_limits
<Size
>::max() / sizeof(CharT
));
61 template <typename CharT
>
62 void test2(const CharT
* s
, std::size_t len
) {
64 std::basic_string_view
<CharT
> sv1(s
);
65 assert(sv1
.size() == len
);
66 assert(sv1
.data() == s
);
67 assert(sv1
.empty() == (len
== 0));
68 assert(sv1
.size() == sv1
.length());
69 assert(sv1
.max_size() > sv1
.size());
71 // make sure we pick up std::size, too!
72 assert(sv1
.size() == std::size(sv1
));
73 assert(sv1
.empty() == std::empty(sv1
));
78 int main(int, char**) {
79 test1
<std::string_view
>();
80 #ifndef TEST_HAS_NO_CHAR8_T
81 test1
<std::u8string_view
>();
83 test1
<std::u16string_view
>();
84 test1
<std::u32string_view
>();
85 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
86 test1
<std::wstring_view
>();
89 test2("ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",
95 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
96 test2(L
"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",
103 #ifndef TEST_HAS_NO_CHAR8_T
104 test2(u8
"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",
111 #if TEST_STD_VER >= 11
112 test2(u
"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",
118 test2(U
"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",