1053d81ceSMarshall Clow //===----------------------------------------------------------------------===//
2053d81ceSMarshall Clow //
3053d81ceSMarshall Clow //                     The LLVM Compiler Infrastructure
4053d81ceSMarshall Clow //
5053d81ceSMarshall Clow // This file is dual licensed under the MIT and the University of Illinois Open
6053d81ceSMarshall Clow // Source Licenses. See LICENSE.TXT for details.
7053d81ceSMarshall Clow //
8053d81ceSMarshall Clow //===----------------------------------------------------------------------===//
9053d81ceSMarshall Clow 
10053d81ceSMarshall Clow // <string_view>
11053d81ceSMarshall Clow 
12053d81ceSMarshall Clow // constexpr int compare(const charT* s) const;
13053d81ceSMarshall Clow 
14053d81ceSMarshall Clow #include <string_view>
15053d81ceSMarshall Clow #include <cassert>
16053d81ceSMarshall Clow 
17*0f901c7eSStephan T. Lavavej #include "test_macros.h"
18053d81ceSMarshall Clow #include "constexpr_char_traits.hpp"
19053d81ceSMarshall Clow 
20053d81ceSMarshall Clow int sign ( int x ) { return x > 0 ? 1 : ( x < 0 ? -1 : 0 ); }
21053d81ceSMarshall Clow 
22053d81ceSMarshall Clow template<typename CharT>
23053d81ceSMarshall Clow void test1 ( std::basic_string_view<CharT> sv1, const CharT *s, int expected ) {
24053d81ceSMarshall Clow     assert ( sign( sv1.compare(s)) == sign(expected));
25053d81ceSMarshall Clow }
26053d81ceSMarshall Clow 
27053d81ceSMarshall Clow template<typename CharT>
28053d81ceSMarshall Clow void
29053d81ceSMarshall Clow test( const CharT *s1, const CharT *s2, int expected)
30053d81ceSMarshall Clow {
31053d81ceSMarshall Clow     typedef std::basic_string_view<CharT> string_view_t;
32053d81ceSMarshall Clow     string_view_t sv1 ( s1 );
33053d81ceSMarshall Clow     test1 ( sv1, s2, expected );
34053d81ceSMarshall Clow }
35053d81ceSMarshall Clow 
36053d81ceSMarshall Clow int main()
37053d81ceSMarshall Clow {
38053d81ceSMarshall Clow     {
39053d81ceSMarshall Clow     test("", "", 0);
40053d81ceSMarshall Clow     test("", "abcde", -5);
41053d81ceSMarshall Clow     test("", "abcdefghij", -10);
42053d81ceSMarshall Clow     test("", "abcdefghijklmnopqrst", -20);
43053d81ceSMarshall Clow     test("abcde", "", 5);
44053d81ceSMarshall Clow     test("abcde", "abcde", 0);
45053d81ceSMarshall Clow     test("abcde", "abcdefghij", -5);
46053d81ceSMarshall Clow     test("abcde", "abcdefghijklmnopqrst", -15);
47053d81ceSMarshall Clow     test("abcdefghij", "", 10);
48053d81ceSMarshall Clow     test("abcdefghij", "abcde", 5);
49053d81ceSMarshall Clow     test("abcdefghij", "abcdefghij", 0);
50053d81ceSMarshall Clow     test("abcdefghij", "abcdefghijklmnopqrst", -10);
51053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "", 20);
52053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "abcde", 15);
53053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "abcdefghij", 10);
54053d81ceSMarshall Clow     test("abcdefghijklmnopqrst", "abcdefghijklmnopqrst", 0);
55053d81ceSMarshall Clow     }
56053d81ceSMarshall Clow 
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 
76053d81ceSMarshall Clow #if TEST_STD_VER >= 11
77053d81ceSMarshall Clow     {
78053d81ceSMarshall Clow     test(U"", U"", 0);
79053d81ceSMarshall Clow     test(U"", U"abcde", -5);
80053d81ceSMarshall Clow     test(U"", U"abcdefghij", -10);
81053d81ceSMarshall Clow     test(U"", U"abcdefghijklmnopqrst", -20);
82053d81ceSMarshall Clow     test(U"abcde", U"", 5);
83053d81ceSMarshall Clow     test(U"abcde", U"abcde", 0);
84053d81ceSMarshall Clow     test(U"abcde", U"abcdefghij", -5);
85053d81ceSMarshall Clow     test(U"abcde", U"abcdefghijklmnopqrst", -15);
86053d81ceSMarshall Clow     test(U"abcdefghij", U"", 10);
87053d81ceSMarshall Clow     test(U"abcdefghij", U"abcde", 5);
88053d81ceSMarshall Clow     test(U"abcdefghij", U"abcdefghij", 0);
89053d81ceSMarshall Clow     test(U"abcdefghij", U"abcdefghijklmnopqrst", -10);
90053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"", 20);
91053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"abcde", 15);
92053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"abcdefghij", 10);
93053d81ceSMarshall Clow     test(U"abcdefghijklmnopqrst", U"abcdefghijklmnopqrst", 0);
94053d81ceSMarshall Clow     }
95053d81ceSMarshall Clow 
96053d81ceSMarshall Clow     {
97053d81ceSMarshall Clow     test(u"", u"", 0);
98053d81ceSMarshall Clow     test(u"", u"abcde", -5);
99053d81ceSMarshall Clow     test(u"", u"abcdefghij", -10);
100053d81ceSMarshall Clow     test(u"", u"abcdefghijklmnopqrst", -20);
101053d81ceSMarshall Clow     test(u"abcde", u"", 5);
102053d81ceSMarshall Clow     test(u"abcde", u"abcde", 0);
103053d81ceSMarshall Clow     test(u"abcde", u"abcdefghij", -5);
104053d81ceSMarshall Clow     test(u"abcde", u"abcdefghijklmnopqrst", -15);
105053d81ceSMarshall Clow     test(u"abcdefghij", u"", 10);
106053d81ceSMarshall Clow     test(u"abcdefghij", u"abcde", 5);
107053d81ceSMarshall Clow     test(u"abcdefghij", u"abcdefghij", 0);
108053d81ceSMarshall Clow     test(u"abcdefghij", u"abcdefghijklmnopqrst", -10);
109053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"", 20);
110053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"abcde", 15);
111053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"abcdefghij", 10);
112053d81ceSMarshall Clow     test(u"abcdefghijklmnopqrst", u"abcdefghijklmnopqrst", 0);
113053d81ceSMarshall Clow     }
114053d81ceSMarshall Clow #endif
115053d81ceSMarshall Clow 
116*0f901c7eSStephan T. Lavavej #if TEST_STD_VER > 11
117053d81ceSMarshall Clow     {
118053d81ceSMarshall Clow     typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
119053d81ceSMarshall Clow     constexpr SV  sv1;
120053d81ceSMarshall Clow     constexpr SV  sv2 { "abcde", 5 };
121053d81ceSMarshall Clow     static_assert ( sv1.compare("") == 0, "" );
122053d81ceSMarshall Clow     static_assert ( sv1.compare("abcde") == -1, "" );
123053d81ceSMarshall Clow     static_assert ( sv2.compare("") == 1, "" );
124053d81ceSMarshall Clow     static_assert ( sv2.compare("abcde") == 0, "" );
125053d81ceSMarshall Clow     }
126053d81ceSMarshall Clow #endif
127053d81ceSMarshall Clow }
128