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 //===----------------------------------------------------------------------===//
8
9
10 // <string_view>
11
12 // [string.view.capacity], capacity
13 // constexpr size_type size() const noexcept;
14 // constexpr size_type length() const noexcept;
15 // constexpr size_type max_size() const noexcept;
16 // constexpr bool empty() const noexcept;
17
18 #include <string_view>
19 #include <cassert>
20
21 #include "test_macros.h"
22
23 template<typename SV>
test1()24 void test1 () {
25 #if TEST_STD_VER > 11
26 {
27 constexpr SV sv1;
28 static_assert ( sv1.size() == 0, "" );
29 static_assert ( sv1.empty(), "");
30 static_assert ( sv1.size() == sv1.length(), "" );
31 static_assert ( sv1.max_size() > sv1.size(), "");
32 }
33 #endif
34
35 {
36 SV sv1;
37 ASSERT_NOEXCEPT(sv1.size());
38 ASSERT_NOEXCEPT(sv1.empty());
39 ASSERT_NOEXCEPT(sv1.max_size());
40 ASSERT_NOEXCEPT(sv1.length());
41 assert ( sv1.size() == 0 );
42 assert ( sv1.empty());
43 assert ( sv1.size() == sv1.length());
44 assert ( sv1.max_size() > sv1.size());
45 }
46 }
47
48 template<typename CharT>
test2(const CharT * s,size_t len)49 void test2 ( const CharT *s, size_t len ) {
50 {
51 std::basic_string_view<CharT> sv1 ( s );
52 assert ( sv1.size() == len );
53 assert ( sv1.data() == s );
54 assert ( sv1.empty() == (len == 0));
55 assert ( sv1.size() == sv1.length());
56 assert ( sv1.max_size() > sv1.size());
57 #if TEST_STD_VER > 14
58 // make sure we pick up std::size, too!
59 assert ( sv1.size() == std::size(sv1));
60 assert ( sv1.empty() == std::empty(sv1));
61 #endif
62 }
63 }
64
main(int,char **)65 int main(int, char**) {
66 test1<std::string_view> ();
67 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
68 test1<std::u8string_view> ();
69 #endif
70 test1<std::u16string_view> ();
71 test1<std::u32string_view> ();
72 test1<std::wstring_view> ();
73
74 test2 ( "ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE", 105 );
75 test2 ( "ABCDE", 5 );
76 test2 ( "a", 1 );
77 test2 ( "", 0 );
78
79 test2 ( L"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE", 105 );
80 test2 ( L"ABCDE", 5 );
81 test2 ( L"a", 1 );
82 test2 ( L"", 0 );
83
84 #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
85 test2 ( u8"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE", 105 );
86 test2 ( u8"ABCDE", 5 );
87 test2 ( u8"a", 1 );
88 test2 ( u8"", 0 );
89 #endif
90
91 #if TEST_STD_VER >= 11
92 test2 ( u"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE", 105 );
93 test2 ( u"ABCDE", 5 );
94 test2 ( u"a", 1 );
95 test2 ( u"", 0 );
96
97 test2 ( U"ABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDEABCDE", 105 );
98 test2 ( U"ABCDE", 5 );
99 test2 ( U"a", 1 );
100 test2 ( U"", 0 );
101 #endif
102
103 return 0;
104 }
105