Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / strings / string.view / string.view.capacity / capacity.pass.cpp
blob05dad28b717864d17d43866c043b14e4f96e57bf
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 // [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>
20 #include <cassert>
21 #include <iterator>
22 #include <limits>
24 #include "test_macros.h"
26 template <typename SV>
27 void test1() {
28 #if TEST_STD_VER > 11
30 constexpr SV sv1;
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(), "");
36 #endif
39 SV sv1;
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);
45 assert(sv1.empty());
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;
55 SV sv;
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());
70 #if TEST_STD_VER > 14
71 // make sure we pick up std::size, too!
72 assert(sv1.size() == std::size(sv1));
73 assert(sv1.empty() == std::empty(sv1));
74 #endif
78 int main(int, char**) {
79 test1<std::string_view>();
80 #ifndef TEST_HAS_NO_CHAR8_T
81 test1<std::u8string_view>();
82 #endif
83 test1<std::u16string_view>();
84 test1<std::u32string_view>();
85 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
86 test1<std::wstring_view>();
87 #endif
89 test2("ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",
90 105);
91 test2("ABCDE", 5);
92 test2("a", 1);
93 test2("", 0);
95 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
96 test2(L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",
97 105);
98 test2(L"ABCDE", 5);
99 test2(L"a", 1);
100 test2(L"", 0);
101 #endif
103 #ifndef TEST_HAS_NO_CHAR8_T
104 test2(u8"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",
105 105);
106 test2(u8"ABCDE", 5);
107 test2(u8"a", 1);
108 test2(u8"", 0);
109 #endif
111 #if TEST_STD_VER >= 11
112 test2(u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",
113 105);
114 test2(u"ABCDE", 5);
115 test2(u"a", 1);
116 test2(u"", 0);
118 test2(U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE",
119 105);
120 test2(U"ABCDE", 5);
121 test2(U"a", 1);
122 test2(U"", 0);
123 #endif
125 return 0;