1*053d81ceSMarshall Clow //===----------------------------------------------------------------------===//
2*053d81ceSMarshall Clow //
3*053d81ceSMarshall Clow //                     The LLVM Compiler Infrastructure
4*053d81ceSMarshall Clow //
5*053d81ceSMarshall Clow // This file is dual licensed under the MIT and the University of Illinois Open
6*053d81ceSMarshall Clow // Source Licenses. See LICENSE.TXT for details.
7*053d81ceSMarshall Clow //
8*053d81ceSMarshall Clow //===----------------------------------------------------------------------===//
9*053d81ceSMarshall Clow 
10*053d81ceSMarshall Clow // <string_view>
11*053d81ceSMarshall Clow 
12*053d81ceSMarshall Clow // constexpr int compare(const charT* s) const;
13*053d81ceSMarshall Clow 
14*053d81ceSMarshall Clow #include <string_view>
15*053d81ceSMarshall Clow #include <cassert>
16*053d81ceSMarshall Clow 
17*053d81ceSMarshall Clow #include "constexpr_char_traits.hpp"
18*053d81ceSMarshall Clow 
19*053d81ceSMarshall Clow int sign ( int x ) { return x > 0 ? 1 : ( x < 0 ? -1 : 0 ); }
20*053d81ceSMarshall Clow 
21*053d81ceSMarshall Clow template<typename CharT>
22*053d81ceSMarshall Clow void test1 ( std::basic_string_view<CharT> sv1, const CharT *s, int expected ) {
23*053d81ceSMarshall Clow     assert ( sign( sv1.compare(s)) == sign(expected));
24*053d81ceSMarshall Clow }
25*053d81ceSMarshall Clow 
26*053d81ceSMarshall Clow template<typename CharT>
27*053d81ceSMarshall Clow void
28*053d81ceSMarshall Clow test( const CharT *s1, const CharT *s2, int expected)
29*053d81ceSMarshall Clow {
30*053d81ceSMarshall Clow     typedef std::basic_string_view<CharT> string_view_t;
31*053d81ceSMarshall Clow     string_view_t sv1 ( s1 );
32*053d81ceSMarshall Clow     test1 ( sv1, s2, expected );
33*053d81ceSMarshall Clow }
34*053d81ceSMarshall Clow 
35*053d81ceSMarshall Clow int main()
36*053d81ceSMarshall Clow {
37*053d81ceSMarshall Clow     {
38*053d81ceSMarshall Clow     test("", "", 0);
39*053d81ceSMarshall Clow     test("", "abcde", -5);
40*053d81ceSMarshall Clow     test("", "abcdefghij", -10);
41*053d81ceSMarshall Clow     test("", "abcdefghijklmnopqrst", -20);
42*053d81ceSMarshall Clow     test("abcde", "", 5);
43*053d81ceSMarshall Clow     test("abcde", "abcde", 0);
44*053d81ceSMarshall Clow     test("abcde", "abcdefghij", -5);
45*053d81ceSMarshall Clow     test("abcde", "abcdefghijklmnopqrst", -15);
46*053d81ceSMarshall Clow     test("abcdefghij", "", 10);
47*053d81ceSMarshall Clow     test("abcdefghij", "abcde", 5);
48*053d81ceSMarshall Clow     test("abcdefghij", "abcdefghij", 0);
49*053d81ceSMarshall Clow     test("abcdefghij", "abcdefghijklmnopqrst", -10);
50*053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "", 20);
51*053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "abcde", 15);
52*053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "abcdefghij", 10);
53*053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "abcdefghijklmnopqrst", 0);
54*053d81ceSMarshall Clow     }
55*053d81ceSMarshall Clow 
56*053d81ceSMarshall Clow     {
57*053d81ceSMarshall Clow     test(L"", L"", 0);
58*053d81ceSMarshall Clow     test(L"", L"abcde", -5);
59*053d81ceSMarshall Clow     test(L"", L"abcdefghij", -10);
60*053d81ceSMarshall Clow     test(L"", L"abcdefghijklmnopqrst", -20);
61*053d81ceSMarshall Clow     test(L"abcde", L"", 5);
62*053d81ceSMarshall Clow     test(L"abcde", L"abcde", 0);
63*053d81ceSMarshall Clow     test(L"abcde", L"abcdefghij", -5);
64*053d81ceSMarshall Clow     test(L"abcde", L"abcdefghijklmnopqrst", -15);
65*053d81ceSMarshall Clow     test(L"abcdefghij", L"", 10);
66*053d81ceSMarshall Clow     test(L"abcdefghij", L"abcde", 5);
67*053d81ceSMarshall Clow     test(L"abcdefghij", L"abcdefghij", 0);
68*053d81ceSMarshall Clow     test(L"abcdefghij", L"abcdefghijklmnopqrst", -10);
69*053d81ceSMarshall Clow     test(L"abcdefghijklmnopqrst", L"", 20);
70*053d81ceSMarshall Clow     test(L"abcdefghijklmnopqrst", L"abcde", 15);
71*053d81ceSMarshall Clow     test(L"abcdefghijklmnopqrst", L"abcdefghij", 10);
72*053d81ceSMarshall Clow     test(L"abcdefghijklmnopqrst", L"abcdefghijklmnopqrst", 0);
73*053d81ceSMarshall Clow     }
74*053d81ceSMarshall Clow 
75*053d81ceSMarshall Clow #if TEST_STD_VER >= 11
76*053d81ceSMarshall Clow     {
77*053d81ceSMarshall Clow     test(U"", U"", 0);
78*053d81ceSMarshall Clow     test(U"", U"abcde", -5);
79*053d81ceSMarshall Clow     test(U"", U"abcdefghij", -10);
80*053d81ceSMarshall Clow     test(U"", U"abcdefghijklmnopqrst", -20);
81*053d81ceSMarshall Clow     test(U"abcde", U"", 5);
82*053d81ceSMarshall Clow     test(U"abcde", U"abcde", 0);
83*053d81ceSMarshall Clow     test(U"abcde", U"abcdefghij", -5);
84*053d81ceSMarshall Clow     test(U"abcde", U"abcdefghijklmnopqrst", -15);
85*053d81ceSMarshall Clow     test(U"abcdefghij", U"", 10);
86*053d81ceSMarshall Clow     test(U"abcdefghij", U"abcde", 5);
87*053d81ceSMarshall Clow     test(U"abcdefghij", U"abcdefghij", 0);
88*053d81ceSMarshall Clow     test(U"abcdefghij", U"abcdefghijklmnopqrst", -10);
89*053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"", 20);
90*053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"abcde", 15);
91*053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"abcdefghij", 10);
92*053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"abcdefghijklmnopqrst", 0);
93*053d81ceSMarshall Clow     }
94*053d81ceSMarshall Clow 
95*053d81ceSMarshall Clow     {
96*053d81ceSMarshall Clow     test(u"", u"", 0);
97*053d81ceSMarshall Clow     test(u"", u"abcde", -5);
98*053d81ceSMarshall Clow     test(u"", u"abcdefghij", -10);
99*053d81ceSMarshall Clow     test(u"", u"abcdefghijklmnopqrst", -20);
100*053d81ceSMarshall Clow     test(u"abcde", u"", 5);
101*053d81ceSMarshall Clow     test(u"abcde", u"abcde", 0);
102*053d81ceSMarshall Clow     test(u"abcde", u"abcdefghij", -5);
103*053d81ceSMarshall Clow     test(u"abcde", u"abcdefghijklmnopqrst", -15);
104*053d81ceSMarshall Clow     test(u"abcdefghij", u"", 10);
105*053d81ceSMarshall Clow     test(u"abcdefghij", u"abcde", 5);
106*053d81ceSMarshall Clow     test(u"abcdefghij", u"abcdefghij", 0);
107*053d81ceSMarshall Clow     test(u"abcdefghij", u"abcdefghijklmnopqrst", -10);
108*053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"", 20);
109*053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"abcde", 15);
110*053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"abcdefghij", 10);
111*053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"abcdefghijklmnopqrst", 0);
112*053d81ceSMarshall Clow     }
113*053d81ceSMarshall Clow #endif
114*053d81ceSMarshall Clow 
115*053d81ceSMarshall Clow #if _LIBCPP_STD_VER > 11
116*053d81ceSMarshall Clow     {
117*053d81ceSMarshall Clow     typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
118*053d81ceSMarshall Clow     constexpr SV  sv1;
119*053d81ceSMarshall Clow     constexpr SV  sv2 { "abcde", 5 };
120*053d81ceSMarshall Clow     static_assert ( sv1.compare("") == 0, "" );
121*053d81ceSMarshall Clow     static_assert ( sv1.compare("abcde") == -1, "" );
122*053d81ceSMarshall Clow     static_assert ( sv2.compare("") == 1, "" );
123*053d81ceSMarshall Clow     static_assert ( sv2.compare("abcde") == 0, "" );
124*053d81ceSMarshall Clow     }
125*053d81ceSMarshall Clow #endif
126*053d81ceSMarshall Clow }
127