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