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>
1202d11757SLouis Dionne 
13053d81ceSMarshall Clow // constexpr int compare(basic_string_view str) const noexcept;
14053d81ceSMarshall Clow 
15053d81ceSMarshall Clow #include <string_view>
16053d81ceSMarshall Clow #include <cassert>
17053d81ceSMarshall Clow 
180f901c7eSStephan T. Lavavej #include "test_macros.h"
19cc89063bSNico Weber #include "constexpr_char_traits.h"
20053d81ceSMarshall Clow 
sign(int x)21053d81ceSMarshall Clow int sign ( int x ) { return x > 0 ? 1 : ( x < 0 ? -1 : 0 ); }
22053d81ceSMarshall Clow 
23053d81ceSMarshall Clow template<typename CharT>
test1(std::basic_string_view<CharT> sv1,std::basic_string_view<CharT> sv2,int expected)24053d81ceSMarshall Clow void test1 ( std::basic_string_view<CharT> sv1,
25053d81ceSMarshall Clow             std::basic_string_view<CharT> sv2, int expected ) {
26053d81ceSMarshall Clow     assert ( sign( sv1.compare(sv2)) == sign(expected));
27053d81ceSMarshall Clow }
28053d81ceSMarshall Clow 
29053d81ceSMarshall Clow 
30053d81ceSMarshall Clow template<typename CharT>
test(const CharT * s1,const CharT * s2,int expected)31053d81ceSMarshall Clow void test ( const CharT *s1, const CharT  *s2, int expected ) {
32053d81ceSMarshall Clow     typedef std::basic_string_view<CharT> string_view_t;
33053d81ceSMarshall Clow 
34053d81ceSMarshall Clow     string_view_t sv1 ( s1 );
35053d81ceSMarshall Clow     string_view_t sv2 ( s2 );
36053d81ceSMarshall Clow     test1(sv1, sv2, expected);
37053d81ceSMarshall Clow }
38053d81ceSMarshall Clow 
main(int,char **)392df59c50SJF Bastien int main(int, char**) {
40053d81ceSMarshall Clow 
41053d81ceSMarshall Clow     test("",                     "", 0);
42053d81ceSMarshall Clow     test("",                     "abcde", -5);
43053d81ceSMarshall Clow     test("",                     "abcdefghij", -10);
44053d81ceSMarshall Clow     test("",                     "abcdefghijklmnopqrst", -20);
45053d81ceSMarshall Clow     test("abcde",                "", 5);
46053d81ceSMarshall Clow     test("abcde",                "abcde", 0);
47053d81ceSMarshall Clow     test("abcde",                "abcdefghij", -5);
48053d81ceSMarshall Clow     test("abcde",                "abcdefghijklmnopqrst", -15);
49053d81ceSMarshall Clow     test("abcdefghij",           "", 10);
50053d81ceSMarshall Clow     test("abcdefghij",           "abcde", 5);
51053d81ceSMarshall Clow     test("abcdefghij",           "abcdefghij", 0);
52053d81ceSMarshall Clow     test("abcdefghij",           "abcdefghijklmnopqrst", -10);
53053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "", 20);
54053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "abcde", 15);
55053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "abcdefghij", 10);
56053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "abcdefghijklmnopqrst", 0);
57053d81ceSMarshall Clow 
58053d81ceSMarshall Clow     test(L"",                     L"", 0);
59053d81ceSMarshall Clow     test(L"",                     L"abcde", -5);
60053d81ceSMarshall Clow     test(L"",                     L"abcdefghij", -10);
61053d81ceSMarshall Clow     test(L"",                     L"abcdefghijklmnopqrst", -20);
62053d81ceSMarshall Clow     test(L"abcde",                L"", 5);
63053d81ceSMarshall Clow     test(L"abcde",                L"abcde", 0);
64053d81ceSMarshall Clow     test(L"abcde",                L"abcdefghij", -5);
65053d81ceSMarshall Clow     test(L"abcde",                L"abcdefghijklmnopqrst", -15);
66053d81ceSMarshall Clow     test(L"abcdefghij",           L"", 10);
67053d81ceSMarshall Clow     test(L"abcdefghij",           L"abcde", 5);
68053d81ceSMarshall Clow     test(L"abcdefghij",           L"abcdefghij", 0);
69053d81ceSMarshall Clow     test(L"abcdefghij",           L"abcdefghijklmnopqrst", -10);
70053d81ceSMarshall Clow     test(L"abcdefghijklmnopqrst", L"", 20);
71053d81ceSMarshall Clow     test(L"abcdefghijklmnopqrst", L"abcde", 15);
72053d81ceSMarshall Clow     test(L"abcdefghijklmnopqrst", L"abcdefghij", 10);
73053d81ceSMarshall Clow     test(L"abcdefghijklmnopqrst", L"abcdefghijklmnopqrst", 0);
74053d81ceSMarshall Clow 
75053d81ceSMarshall Clow #if TEST_STD_VER >= 11
76053d81ceSMarshall Clow     test(u"",                     u"", 0);
77053d81ceSMarshall Clow     test(u"",                     u"abcde", -5);
78053d81ceSMarshall Clow     test(u"",                     u"abcdefghij", -10);
79053d81ceSMarshall Clow     test(u"",                     u"abcdefghijklmnopqrst", -20);
80053d81ceSMarshall Clow     test(u"abcde",                u"", 5);
81053d81ceSMarshall Clow     test(u"abcde",                u"abcde", 0);
82053d81ceSMarshall Clow     test(u"abcde",                u"abcdefghij", -5);
83053d81ceSMarshall Clow     test(u"abcde",                u"abcdefghijklmnopqrst", -15);
84053d81ceSMarshall Clow     test(u"abcdefghij",           u"", 10);
85053d81ceSMarshall Clow     test(u"abcdefghij",           u"abcde", 5);
86053d81ceSMarshall Clow     test(u"abcdefghij",           u"abcdefghij", 0);
87053d81ceSMarshall Clow     test(u"abcdefghij",           u"abcdefghijklmnopqrst", -10);
88053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"", 20);
89053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"abcde", 15);
90053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"abcdefghij", 10);
91053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"abcdefghijklmnopqrst", 0);
92053d81ceSMarshall Clow 
93053d81ceSMarshall Clow     test(U"",                     U"", 0);
94053d81ceSMarshall Clow     test(U"",                     U"abcde", -5);
95053d81ceSMarshall Clow     test(U"",                     U"abcdefghij", -10);
96053d81ceSMarshall Clow     test(U"",                     U"abcdefghijklmnopqrst", -20);
97053d81ceSMarshall Clow     test(U"abcde",                U"", 5);
98053d81ceSMarshall Clow     test(U"abcde",                U"abcde", 0);
99053d81ceSMarshall Clow     test(U"abcde",                U"abcdefghij", -5);
100053d81ceSMarshall Clow     test(U"abcde",                U"abcdefghijklmnopqrst", -15);
101053d81ceSMarshall Clow     test(U"abcdefghij",           U"", 10);
102053d81ceSMarshall Clow     test(U"abcdefghij",           U"abcde", 5);
103053d81ceSMarshall Clow     test(U"abcdefghij",           U"abcdefghij", 0);
104053d81ceSMarshall Clow     test(U"abcdefghij",           U"abcdefghijklmnopqrst", -10);
105053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"", 20);
106053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"abcde", 15);
107053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"abcdefghij", 10);
108053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"abcdefghijklmnopqrst", 0);
109053d81ceSMarshall Clow #endif
110053d81ceSMarshall Clow 
1110f901c7eSStephan T. Lavavej #if TEST_STD_VER > 11
112053d81ceSMarshall Clow     {
113053d81ceSMarshall Clow     typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
114053d81ceSMarshall Clow     constexpr SV  sv1 { "abcde", 5 };
115053d81ceSMarshall Clow     constexpr SV  sv2 { "abcde", 5 };
116053d81ceSMarshall Clow     constexpr SV  sv3 { "edcba0", 6 };
117053d81ceSMarshall Clow     static_assert ( sv1.compare(sv2) == 0, "" );
118053d81ceSMarshall Clow     static_assert ( sv2.compare(sv1) == 0, "" );
119053d81ceSMarshall Clow     static_assert ( sv3.compare(sv2)  > 0, "" );
120053d81ceSMarshall Clow     static_assert ( sv2.compare(sv3)  < 0, "" );
121053d81ceSMarshall Clow     }
122053d81ceSMarshall Clow #endif
1232df59c50SJF Bastien 
1242df59c50SJF Bastien   return 0;
125053d81ceSMarshall Clow }
126