1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 // UNSUPPORTED: !stdlib=libc++ && (c++03 || c++11 || c++14)
10
11 // <string_view>
12
13 // constexpr int compare(basic_string_view str) const noexcept;
14
15 #include <string_view>
16 #include <cassert>
17
18 #include "test_macros.h"
19 #include "constexpr_char_traits.h"
20
sign(int x)21 int sign ( int x ) { return x > 0 ? 1 : ( x < 0 ? -1 : 0 ); }
22
23 template<typename CharT>
test1(std::basic_string_view<CharT> sv1,std::basic_string_view<CharT> sv2,int expected)24 void test1 ( std::basic_string_view<CharT> sv1,
25 std::basic_string_view<CharT> sv2, int expected ) {
26 assert ( sign( sv1.compare(sv2)) == sign(expected));
27 }
28
29
30 template<typename CharT>
test(const CharT * s1,const CharT * s2,int expected)31 void test ( const CharT *s1, const CharT *s2, int expected ) {
32 typedef std::basic_string_view<CharT> string_view_t;
33
34 string_view_t sv1 ( s1 );
35 string_view_t sv2 ( s2 );
36 test1(sv1, sv2, expected);
37 }
38
main(int,char **)39 int main(int, char**) {
40
41 test("", "", 0);
42 test("", "abcde", -5);
43 test("", "abcdefghij", -10);
44 test("", "abcdefghijklmnopqrst", -20);
45 test("abcde", "", 5);
46 test("abcde", "abcde", 0);
47 test("abcde", "abcdefghij", -5);
48 test("abcde", "abcdefghijklmnopqrst", -15);
49 test("abcdefghij", "", 10);
50 test("abcdefghij", "abcde", 5);
51 test("abcdefghij", "abcdefghij", 0);
52 test("abcdefghij", "abcdefghijklmnopqrst", -10);
53 test("abcdefghijklmnopqrst", "", 20);
54 test("abcdefghijklmnopqrst", "abcde", 15);
55 test("abcdefghijklmnopqrst", "abcdefghij", 10);
56 test("abcdefghijklmnopqrst", "abcdefghijklmnopqrst", 0);
57
58 test(L"", L"", 0);
59 test(L"", L"abcde", -5);
60 test(L"", L"abcdefghij", -10);
61 test(L"", L"abcdefghijklmnopqrst", -20);
62 test(L"abcde", L"", 5);
63 test(L"abcde", L"abcde", 0);
64 test(L"abcde", L"abcdefghij", -5);
65 test(L"abcde", L"abcdefghijklmnopqrst", -15);
66 test(L"abcdefghij", L"", 10);
67 test(L"abcdefghij", L"abcde", 5);
68 test(L"abcdefghij", L"abcdefghij", 0);
69 test(L"abcdefghij", L"abcdefghijklmnopqrst", -10);
70 test(L"abcdefghijklmnopqrst", L"", 20);
71 test(L"abcdefghijklmnopqrst", L"abcde", 15);
72 test(L"abcdefghijklmnopqrst", L"abcdefghij", 10);
73 test(L"abcdefghijklmnopqrst", L"abcdefghijklmnopqrst", 0);
74
75 #if TEST_STD_VER >= 11
76 test(u"", u"", 0);
77 test(u"", u"abcde", -5);
78 test(u"", u"abcdefghij", -10);
79 test(u"", u"abcdefghijklmnopqrst", -20);
80 test(u"abcde", u"", 5);
81 test(u"abcde", u"abcde", 0);
82 test(u"abcde", u"abcdefghij", -5);
83 test(u"abcde", u"abcdefghijklmnopqrst", -15);
84 test(u"abcdefghij", u"", 10);
85 test(u"abcdefghij", u"abcde", 5);
86 test(u"abcdefghij", u"abcdefghij", 0);
87 test(u"abcdefghij", u"abcdefghijklmnopqrst", -10);
88 test(u"abcdefghijklmnopqrst", u"", 20);
89 test(u"abcdefghijklmnopqrst", u"abcde", 15);
90 test(u"abcdefghijklmnopqrst", u"abcdefghij", 10);
91 test(u"abcdefghijklmnopqrst", u"abcdefghijklmnopqrst", 0);
92
93 test(U"", U"", 0);
94 test(U"", U"abcde", -5);
95 test(U"", U"abcdefghij", -10);
96 test(U"", U"abcdefghijklmnopqrst", -20);
97 test(U"abcde", U"", 5);
98 test(U"abcde", U"abcde", 0);
99 test(U"abcde", U"abcdefghij", -5);
100 test(U"abcde", U"abcdefghijklmnopqrst", -15);
101 test(U"abcdefghij", U"", 10);
102 test(U"abcdefghij", U"abcde", 5);
103 test(U"abcdefghij", U"abcdefghij", 0);
104 test(U"abcdefghij", U"abcdefghijklmnopqrst", -10);
105 test(U"abcdefghijklmnopqrst", U"", 20);
106 test(U"abcdefghijklmnopqrst", U"abcde", 15);
107 test(U"abcdefghijklmnopqrst", U"abcdefghij", 10);
108 test(U"abcdefghijklmnopqrst", U"abcdefghijklmnopqrst", 0);
109 #endif
110
111 #if TEST_STD_VER > 11
112 {
113 typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
114 constexpr SV sv1 { "abcde", 5 };
115 constexpr SV sv2 { "abcde", 5 };
116 constexpr SV sv3 { "edcba0", 6 };
117 static_assert ( sv1.compare(sv2) == 0, "" );
118 static_assert ( sv2.compare(sv1) == 0, "" );
119 static_assert ( sv3.compare(sv2) > 0, "" );
120 static_assert ( sv2.compare(sv3) < 0, "" );
121 }
122 #endif
123
124 return 0;
125 }
126