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 // 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
12
13 #include <string_view>
14 #include <cassert>
15
16 #include "test_macros.h"
17
18 #ifndef TEST_HAS_NO_CHAR8_T
19 typedef std::u8string_view u8string_view;
20 #else
21 typedef std::string_view u8string_view;
22 #endif
23
main(int,char **)24 int main(int, char**) {
25 {
26 using namespace std::literals::string_view_literals;
27
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);
32 #endif
33 ASSERT_SAME_TYPE(decltype(u"Hi"sv), std::u16string_view);
34 ASSERT_SAME_TYPE(decltype(U"Hi"sv), std::u32string_view);
35
36 std::string_view foo;
37 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
38 std::wstring_view Lfoo;
39 #endif
40 u8string_view u8foo;
41 std::u16string_view ufoo;
42 std::u32string_view Ufoo;
43
44 foo = ""sv;
45 assert(foo.size() == 0);
46 u8foo = u8""sv;
47 assert(u8foo.size() == 0);
48 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
49 Lfoo = L""sv;
50 assert(Lfoo.size() == 0);
51 #endif
52 ufoo = u""sv;
53 assert(ufoo.size() == 0);
54 Ufoo = U""sv;
55 assert(Ufoo.size() == 0);
56
57 foo = " "sv;
58 assert(foo.size() == 1);
59 u8foo = u8" "sv;
60 assert(u8foo.size() == 1);
61 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
62 Lfoo = L" "sv;
63 assert(Lfoo.size() == 1);
64 #endif
65 ufoo = u" "sv;
66 assert(ufoo.size() == 1);
67 Ufoo = U" "sv;
68 assert(Ufoo.size() == 1);
69
70 foo = "ABC"sv;
71 assert(foo == "ABC");
72 assert(foo == std::string_view("ABC"));
73 u8foo = u8"ABC"sv;
74 assert(u8foo == u8"ABC");
75 assert(u8foo == u8string_view(u8"ABC"));
76 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
77 Lfoo = L"ABC"sv;
78 assert(Lfoo == L"ABC");
79 assert(Lfoo == std::wstring_view(L"ABC"));
80 #endif
81 ufoo = u"ABC"sv;
82 assert(ufoo == u"ABC");
83 assert(ufoo == std::u16string_view(u"ABC"));
84 Ufoo = U"ABC"sv;
85 assert(Ufoo == U"ABC");
86 assert(Ufoo == std::u32string_view(U"ABC"));
87
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, "");
92 #endif
93 static_assert(u"ABC"sv.size() == 3, "");
94 static_assert(U"ABC"sv.size() == 3, "");
95
96 ASSERT_NOEXCEPT("ABC"sv);
97 ASSERT_NOEXCEPT(u8"ABC"sv);
98 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
99 ASSERT_NOEXCEPT(L"ABC"sv);
100 #endif
101 ASSERT_NOEXCEPT(u"ABC"sv);
102 ASSERT_NOEXCEPT(U"ABC"sv);
103 }
104 {
105 using namespace std::literals;
106 std::string_view foo = ""sv;
107 assert(foo.length() == 0);
108 }
109 {
110 using namespace std;
111 std::string_view foo = ""sv;
112 assert(foo.length() == 0);
113 }
114
115 return 0;
116 }
117