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 // Note: libc++ supports string_view before C++17, but literals were introduced in C++14
10 // UNSUPPORTED: c++03, c++11
11 // UNSUPPORTED: !stdlib=libc++ && c++14
13 #include <string_view>
16 #include "test_macros.h"
18 #ifndef TEST_HAS_NO_CHAR8_T
19 typedef std::u8string_view u8string_view
;
21 typedef std::string_view u8string_view
;
24 int main(int, char**) {
26 using namespace std::literals::string_view_literals
;
28 ASSERT_SAME_TYPE(decltype("Hi"sv
), std::string_view
);
29 ASSERT_SAME_TYPE(decltype(u8
"Hi"sv
), u8string_view
);
30 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
31 ASSERT_SAME_TYPE(decltype(L
"Hi"sv
), std::wstring_view
);
33 ASSERT_SAME_TYPE(decltype(u
"Hi"sv
), std::u16string_view
);
34 ASSERT_SAME_TYPE(decltype(U
"Hi"sv
), std::u32string_view
);
37 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
38 std::wstring_view Lfoo
;
41 std::u16string_view ufoo
;
42 std::u32string_view Ufoo
;
45 assert(foo
.size() == 0);
47 assert(u8foo
.size() == 0);
48 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
50 assert(Lfoo
.size() == 0);
53 assert(ufoo
.size() == 0);
55 assert(Ufoo
.size() == 0);
58 assert(foo
.size() == 1);
60 assert(u8foo
.size() == 1);
61 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
63 assert(Lfoo
.size() == 1);
66 assert(ufoo
.size() == 1);
68 assert(Ufoo
.size() == 1);
72 assert(foo
== std::string_view("ABC"));
74 assert(u8foo
== u8
"ABC");
75 assert(u8foo
== u8string_view(u8
"ABC"));
76 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
78 assert(Lfoo
== L
"ABC");
79 assert(Lfoo
== std::wstring_view(L
"ABC"));
82 assert(ufoo
== u
"ABC");
83 assert(ufoo
== std::u16string_view(u
"ABC"));
85 assert(Ufoo
== U
"ABC");
86 assert(Ufoo
== std::u32string_view(U
"ABC"));
88 static_assert("ABC"sv
.size() == 3, "");
89 static_assert(u8
"ABC"sv
.size() == 3, "");
90 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
91 static_assert(L
"ABC"sv
.size() == 3, "");
93 static_assert(u
"ABC"sv
.size() == 3, "");
94 static_assert(U
"ABC"sv
.size() == 3, "");
96 ASSERT_NOEXCEPT("ABC"sv
);
97 ASSERT_NOEXCEPT(u8
"ABC"sv
);
98 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
99 ASSERT_NOEXCEPT(L
"ABC"sv
);
101 ASSERT_NOEXCEPT(u
"ABC"sv
);
102 ASSERT_NOEXCEPT(U
"ABC"sv
);
105 using namespace std::literals
;
106 std::string_view foo
= ""sv
;
107 assert(foo
.length() == 0);
111 std::string_view foo
= ""sv
;
112 assert(foo
.length() == 0);