1053d81ceSMarshall Clow //===----------------------------------------------------------------------===//
2053d81ceSMarshall Clow //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6053d81ceSMarshall Clow //
7053d81ceSMarshall Clow //===----------------------------------------------------------------------===//
8053d81ceSMarshall Clow
9*4fc50236SJoe Loser // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)
10*4fc50236SJoe Loser
11053d81ceSMarshall Clow // <string_view>
12053d81ceSMarshall Clow
13053d81ceSMarshall Clow // constexpr const_iterator begin() const;
14053d81ceSMarshall Clow
15053d81ceSMarshall Clow #include <string_view>
16053d81ceSMarshall Clow #include <cassert>
17053d81ceSMarshall Clow
18053d81ceSMarshall Clow #include "test_macros.h"
19053d81ceSMarshall Clow
20053d81ceSMarshall Clow template <class S>
21053d81ceSMarshall Clow void
test(S s)22053d81ceSMarshall Clow test(S s)
23053d81ceSMarshall Clow {
24053d81ceSMarshall Clow const S& cs = s;
25053d81ceSMarshall Clow typename S::iterator b = s.begin();
26053d81ceSMarshall Clow typename S::const_iterator cb1 = cs.begin();
27053d81ceSMarshall Clow typename S::const_iterator cb2 = s.cbegin();
28053d81ceSMarshall Clow if (!s.empty())
29053d81ceSMarshall Clow {
30053d81ceSMarshall Clow assert( *b == s[0]);
31053d81ceSMarshall Clow assert( &*b == &s[0]);
32053d81ceSMarshall Clow assert( *cb1 == s[0]);
33053d81ceSMarshall Clow assert(&*cb1 == &s[0]);
34053d81ceSMarshall Clow assert( *cb2 == s[0]);
35053d81ceSMarshall Clow assert(&*cb2 == &s[0]);
36053d81ceSMarshall Clow
37053d81ceSMarshall Clow }
38053d81ceSMarshall Clow assert( b == cb1);
39053d81ceSMarshall Clow assert( b == cb2);
40053d81ceSMarshall Clow assert(cb1 == cb2);
41053d81ceSMarshall Clow }
42053d81ceSMarshall Clow
43053d81ceSMarshall Clow
main(int,char **)442df59c50SJF Bastien int main(int, char**)
45053d81ceSMarshall Clow {
46053d81ceSMarshall Clow typedef std::string_view string_view;
477dad0bd6SMarshall Clow #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
487dad0bd6SMarshall Clow typedef std::u8string_view u8string_view;
497dad0bd6SMarshall Clow #endif
50053d81ceSMarshall Clow typedef std::u16string_view u16string_view;
51053d81ceSMarshall Clow typedef std::u32string_view u32string_view;
52053d81ceSMarshall Clow
53053d81ceSMarshall Clow test(string_view ());
54053d81ceSMarshall Clow test(u16string_view());
55053d81ceSMarshall Clow test(u32string_view());
56053d81ceSMarshall Clow test(string_view ( "123"));
577dad0bd6SMarshall Clow #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
587dad0bd6SMarshall Clow test(u8string_view{u8"123"});
597dad0bd6SMarshall Clow #endif
60053d81ceSMarshall Clow #if TEST_STD_VER >= 11
61053d81ceSMarshall Clow test(u16string_view{u"123"});
62053d81ceSMarshall Clow test(u32string_view{U"123"});
63053d81ceSMarshall Clow #endif
64053d81ceSMarshall Clow
65f4c1258dSLouis Dionne #ifndef TEST_HAS_NO_WIDE_CHARACTERS
66f4c1258dSLouis Dionne typedef std::wstring_view wstring_view;
67f4c1258dSLouis Dionne test(wstring_view ());
68f4c1258dSLouis Dionne test(wstring_view (L"123"));
69f4c1258dSLouis Dionne #endif
70f4c1258dSLouis Dionne
710f901c7eSStephan T. Lavavej #if TEST_STD_VER > 11
72053d81ceSMarshall Clow {
73053d81ceSMarshall Clow constexpr string_view sv { "123", 3 };
747dad0bd6SMarshall Clow #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
757dad0bd6SMarshall Clow constexpr u8string_view u8sv {u8"123", 3 };
767dad0bd6SMarshall Clow #endif
77053d81ceSMarshall Clow constexpr u16string_view u16sv {u"123", 3 };
78053d81ceSMarshall Clow constexpr u32string_view u32sv {U"123", 3 };
79053d81ceSMarshall Clow
80053d81ceSMarshall Clow static_assert ( *sv.begin() == sv[0], "" );
817dad0bd6SMarshall Clow #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
827dad0bd6SMarshall Clow static_assert ( *u8sv.begin() == u8sv[0], "" );
837dad0bd6SMarshall Clow #endif
84053d81ceSMarshall Clow static_assert ( *u16sv.begin() == u16sv[0], "" );
85053d81ceSMarshall Clow static_assert ( *u32sv.begin() == u32sv[0], "" );
86053d81ceSMarshall Clow
87053d81ceSMarshall Clow static_assert ( *sv.cbegin() == sv[0], "" );
887dad0bd6SMarshall Clow #if defined(__cpp_lib_char8_t) && __cpp_lib_char8_t >= 201811L
897dad0bd6SMarshall Clow static_assert ( *u8sv.cbegin() == u8sv[0], "" );
907dad0bd6SMarshall Clow #endif
91053d81ceSMarshall Clow static_assert ( *u16sv.cbegin() == u16sv[0], "" );
92053d81ceSMarshall Clow static_assert ( *u32sv.cbegin() == u32sv[0], "" );
93f4c1258dSLouis Dionne
94f4c1258dSLouis Dionne #ifndef TEST_HAS_NO_WIDE_CHARACTERS
95f4c1258dSLouis Dionne {
96f4c1258dSLouis Dionne constexpr wstring_view wsv {L"123", 3 };
97f4c1258dSLouis Dionne static_assert ( *wsv.begin() == wsv[0], "" );
98053d81ceSMarshall Clow static_assert ( *wsv.cbegin() == wsv[0], "" );
99053d81ceSMarshall Clow }
100053d81ceSMarshall Clow #endif
101f4c1258dSLouis Dionne }
102f4c1258dSLouis Dionne #endif // TEST_STD_VER > 11
1032df59c50SJF Bastien
1042df59c50SJF Bastien return 0;
105053d81ceSMarshall Clow }
106