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 int compare(const charT* s) const;
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,const CharT * s,int expected)24053d81ceSMarshall Clow void test1 ( std::basic_string_view<CharT> sv1, const CharT *s, int expected ) {
25053d81ceSMarshall Clow     assert ( sign( sv1.compare(s)) == sign(expected));
26053d81ceSMarshall Clow }
27053d81ceSMarshall Clow 
28053d81ceSMarshall Clow template<typename CharT>
29053d81ceSMarshall Clow void
test(const CharT * s1,const CharT * s2,int expected)30053d81ceSMarshall Clow test( const CharT *s1, const CharT *s2, int expected)
31053d81ceSMarshall Clow {
32053d81ceSMarshall Clow     typedef std::basic_string_view<CharT> string_view_t;
33053d81ceSMarshall Clow     string_view_t sv1 ( s1 );
34053d81ceSMarshall Clow     test1 ( sv1, s2, expected );
35053d81ceSMarshall Clow }
36053d81ceSMarshall Clow 
main(int,char **)372df59c50SJF Bastien int main(int, char**)
38053d81ceSMarshall Clow {
39053d81ceSMarshall Clow     {
40053d81ceSMarshall Clow     test("", "", 0);
41053d81ceSMarshall Clow     test("", "abcde", -5);
42053d81ceSMarshall Clow     test("", "abcdefghij", -10);
43053d81ceSMarshall Clow     test("", "abcdefghijklmnopqrst", -20);
44053d81ceSMarshall Clow     test("abcde", "", 5);
45053d81ceSMarshall Clow     test("abcde", "abcde", 0);
46053d81ceSMarshall Clow     test("abcde", "abcdefghij", -5);
47053d81ceSMarshall Clow     test("abcde", "abcdefghijklmnopqrst", -15);
48053d81ceSMarshall Clow     test("abcdefghij", "", 10);
49053d81ceSMarshall Clow     test("abcdefghij", "abcde", 5);
50053d81ceSMarshall Clow     test("abcdefghij", "abcdefghij", 0);
51053d81ceSMarshall Clow     test("abcdefghij", "abcdefghijklmnopqrst", -10);
52053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "", 20);
53053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "abcde", 15);
54053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "abcdefghij", 10);
55053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "abcdefghijklmnopqrst", 0);
56053d81ceSMarshall Clow     }
57053d81ceSMarshall Clow 
58053d81ceSMarshall Clow     {
59053d81ceSMarshall Clow     test(L"", L"", 0);
60053d81ceSMarshall Clow     test(L"", L"abcde", -5);
61053d81ceSMarshall Clow     test(L"", L"abcdefghij", -10);
62053d81ceSMarshall Clow     test(L"", L"abcdefghijklmnopqrst", -20);
63053d81ceSMarshall Clow     test(L"abcde", L"", 5);
64053d81ceSMarshall Clow     test(L"abcde", L"abcde", 0);
65053d81ceSMarshall Clow     test(L"abcde", L"abcdefghij", -5);
66053d81ceSMarshall Clow     test(L"abcde", L"abcdefghijklmnopqrst", -15);
67053d81ceSMarshall Clow     test(L"abcdefghij", L"", 10);
68053d81ceSMarshall Clow     test(L"abcdefghij", L"abcde", 5);
69053d81ceSMarshall Clow     test(L"abcdefghij", L"abcdefghij", 0);
70053d81ceSMarshall Clow     test(L"abcdefghij", L"abcdefghijklmnopqrst", -10);
71053d81ceSMarshall Clow     test(L"abcdefghijklmnopqrst", L"", 20);
72053d81ceSMarshall Clow     test(L"abcdefghijklmnopqrst", L"abcde", 15);
73053d81ceSMarshall Clow     test(L"abcdefghijklmnopqrst", L"abcdefghij", 10);
74053d81ceSMarshall Clow     test(L"abcdefghijklmnopqrst", L"abcdefghijklmnopqrst", 0);
75053d81ceSMarshall Clow     }
76053d81ceSMarshall Clow 
77053d81ceSMarshall Clow #if TEST_STD_VER >= 11
78053d81ceSMarshall Clow     {
79053d81ceSMarshall Clow     test(U"", U"", 0);
80053d81ceSMarshall Clow     test(U"", U"abcde", -5);
81053d81ceSMarshall Clow     test(U"", U"abcdefghij", -10);
82053d81ceSMarshall Clow     test(U"", U"abcdefghijklmnopqrst", -20);
83053d81ceSMarshall Clow     test(U"abcde", U"", 5);
84053d81ceSMarshall Clow     test(U"abcde", U"abcde", 0);
85053d81ceSMarshall Clow     test(U"abcde", U"abcdefghij", -5);
86053d81ceSMarshall Clow     test(U"abcde", U"abcdefghijklmnopqrst", -15);
87053d81ceSMarshall Clow     test(U"abcdefghij", U"", 10);
88053d81ceSMarshall Clow     test(U"abcdefghij", U"abcde", 5);
89053d81ceSMarshall Clow     test(U"abcdefghij", U"abcdefghij", 0);
90053d81ceSMarshall Clow     test(U"abcdefghij", U"abcdefghijklmnopqrst", -10);
91053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"", 20);
92053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"abcde", 15);
93053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"abcdefghij", 10);
94053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"abcdefghijklmnopqrst", 0);
95053d81ceSMarshall Clow     }
96053d81ceSMarshall Clow 
97053d81ceSMarshall Clow     {
98053d81ceSMarshall Clow     test(u"", u"", 0);
99053d81ceSMarshall Clow     test(u"", u"abcde", -5);
100053d81ceSMarshall Clow     test(u"", u"abcdefghij", -10);
101053d81ceSMarshall Clow     test(u"", u"abcdefghijklmnopqrst", -20);
102053d81ceSMarshall Clow     test(u"abcde", u"", 5);
103053d81ceSMarshall Clow     test(u"abcde", u"abcde", 0);
104053d81ceSMarshall Clow     test(u"abcde", u"abcdefghij", -5);
105053d81ceSMarshall Clow     test(u"abcde", u"abcdefghijklmnopqrst", -15);
106053d81ceSMarshall Clow     test(u"abcdefghij", u"", 10);
107053d81ceSMarshall Clow     test(u"abcdefghij", u"abcde", 5);
108053d81ceSMarshall Clow     test(u"abcdefghij", u"abcdefghij", 0);
109053d81ceSMarshall Clow     test(u"abcdefghij", u"abcdefghijklmnopqrst", -10);
110053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"", 20);
111053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"abcde", 15);
112053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"abcdefghij", 10);
113053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"abcdefghijklmnopqrst", 0);
114053d81ceSMarshall Clow     }
115053d81ceSMarshall Clow #endif
116053d81ceSMarshall Clow 
1170f901c7eSStephan T. Lavavej #if TEST_STD_VER > 11
118053d81ceSMarshall Clow     {
119053d81ceSMarshall Clow     typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
120053d81ceSMarshall Clow     constexpr SV  sv1;
121053d81ceSMarshall Clow     constexpr SV  sv2 { "abcde", 5 };
122053d81ceSMarshall Clow     static_assert ( sv1.compare("") == 0, "" );
12398b15320SMarshall Clow     static_assert ( sv1.compare("abcde") < 0, "" );
12498b15320SMarshall Clow     static_assert ( sv2.compare("") > 0, "" );
125053d81ceSMarshall Clow     static_assert ( sv2.compare("abcde") == 0, "" );
126053d81ceSMarshall Clow     }
127053d81ceSMarshall Clow #endif
1282df59c50SJF Bastien 
1292df59c50SJF Bastien   return 0;
130053d81ceSMarshall Clow }
131