• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
10 // <string_view>
11 // constexpr int compare(basic_string_view str) const noexcept;
12 
13 #include <string_view>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 #include "constexpr_char_traits.h"
18 
sign(int x)19 int sign ( int x ) { return x > 0 ? 1 : ( x < 0 ? -1 : 0 ); }
20 
21 template<typename CharT>
test1(std::basic_string_view<CharT> sv1,std::basic_string_view<CharT> sv2,int expected)22 void test1 ( std::basic_string_view<CharT> sv1,
23             std::basic_string_view<CharT> sv2, int expected ) {
24     assert ( sign( sv1.compare(sv2)) == sign(expected));
25 }
26 
27 
28 template<typename CharT>
test(const CharT * s1,const CharT * s2,int expected)29 void test ( const CharT *s1, const CharT  *s2, int expected ) {
30     typedef std::basic_string_view<CharT> string_view_t;
31 
32     string_view_t sv1 ( s1 );
33     string_view_t sv2 ( s2 );
34     test1(sv1, sv2, expected);
35 }
36 
main(int,char **)37 int main(int, char**) {
38 
39     test("",                     "", 0);
40     test("",                     "abcde", -5);
41     test("",                     "abcdefghij", -10);
42     test("",                     "abcdefghijklmnopqrst", -20);
43     test("abcde",                "", 5);
44     test("abcde",                "abcde", 0);
45     test("abcde",                "abcdefghij", -5);
46     test("abcde",                "abcdefghijklmnopqrst", -15);
47     test("abcdefghij",           "", 10);
48     test("abcdefghij",           "abcde", 5);
49     test("abcdefghij",           "abcdefghij", 0);
50     test("abcdefghij",           "abcdefghijklmnopqrst", -10);
51     test("abcdefghijklmnopqrst", "", 20);
52     test("abcdefghijklmnopqrst", "abcde", 15);
53     test("abcdefghijklmnopqrst", "abcdefghij", 10);
54     test("abcdefghijklmnopqrst", "abcdefghijklmnopqrst", 0);
55 
56     test(L"",                     L"", 0);
57     test(L"",                     L"abcde", -5);
58     test(L"",                     L"abcdefghij", -10);
59     test(L"",                     L"abcdefghijklmnopqrst", -20);
60     test(L"abcde",                L"", 5);
61     test(L"abcde",                L"abcde", 0);
62     test(L"abcde",                L"abcdefghij", -5);
63     test(L"abcde",                L"abcdefghijklmnopqrst", -15);
64     test(L"abcdefghij",           L"", 10);
65     test(L"abcdefghij",           L"abcde", 5);
66     test(L"abcdefghij",           L"abcdefghij", 0);
67     test(L"abcdefghij",           L"abcdefghijklmnopqrst", -10);
68     test(L"abcdefghijklmnopqrst", L"", 20);
69     test(L"abcdefghijklmnopqrst", L"abcde", 15);
70     test(L"abcdefghijklmnopqrst", L"abcdefghij", 10);
71     test(L"abcdefghijklmnopqrst", L"abcdefghijklmnopqrst", 0);
72 
73 #if TEST_STD_VER >= 11
74     test(u"",                     u"", 0);
75     test(u"",                     u"abcde", -5);
76     test(u"",                     u"abcdefghij", -10);
77     test(u"",                     u"abcdefghijklmnopqrst", -20);
78     test(u"abcde",                u"", 5);
79     test(u"abcde",                u"abcde", 0);
80     test(u"abcde",                u"abcdefghij", -5);
81     test(u"abcde",                u"abcdefghijklmnopqrst", -15);
82     test(u"abcdefghij",           u"", 10);
83     test(u"abcdefghij",           u"abcde", 5);
84     test(u"abcdefghij",           u"abcdefghij", 0);
85     test(u"abcdefghij",           u"abcdefghijklmnopqrst", -10);
86     test(u"abcdefghijklmnopqrst", u"", 20);
87     test(u"abcdefghijklmnopqrst", u"abcde", 15);
88     test(u"abcdefghijklmnopqrst", u"abcdefghij", 10);
89     test(u"abcdefghijklmnopqrst", u"abcdefghijklmnopqrst", 0);
90 
91     test(U"",                     U"", 0);
92     test(U"",                     U"abcde", -5);
93     test(U"",                     U"abcdefghij", -10);
94     test(U"",                     U"abcdefghijklmnopqrst", -20);
95     test(U"abcde",                U"", 5);
96     test(U"abcde",                U"abcde", 0);
97     test(U"abcde",                U"abcdefghij", -5);
98     test(U"abcde",                U"abcdefghijklmnopqrst", -15);
99     test(U"abcdefghij",           U"", 10);
100     test(U"abcdefghij",           U"abcde", 5);
101     test(U"abcdefghij",           U"abcdefghij", 0);
102     test(U"abcdefghij",           U"abcdefghijklmnopqrst", -10);
103     test(U"abcdefghijklmnopqrst", U"", 20);
104     test(U"abcdefghijklmnopqrst", U"abcde", 15);
105     test(U"abcdefghijklmnopqrst", U"abcdefghij", 10);
106     test(U"abcdefghijklmnopqrst", U"abcdefghijklmnopqrst", 0);
107 #endif
108 
109 #if TEST_STD_VER > 11
110     {
111     typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
112     constexpr SV  sv1 { "abcde", 5 };
113     constexpr SV  sv2 { "abcde", 5 };
114     constexpr SV  sv3 { "edcba0", 6 };
115     static_assert ( sv1.compare(sv2) == 0, "" );
116     static_assert ( sv2.compare(sv1) == 0, "" );
117     static_assert ( sv3.compare(sv2)  > 0, "" );
118     static_assert ( sv2.compare(sv3)  < 0, "" );
119     }
120 #endif
121 
122   return 0;
123 }
124