xref: /llvm-project/libcxx/test/std/strings/string.view/string.view.ops/compare.pointer.pass.cpp (revision a40bada91aeda276a772acfbcae6e8de26755a11)
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(const charT* s) const;
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,const CharT * s,int expected)24 void test1(std::basic_string_view<CharT> sv1, const CharT* s, int expected) {
25   assert(sign(sv1.compare(s)) == sign(expected));
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   string_view_t sv1(s1);
32   test1(sv1, s2, expected);
33 }
34 
main(int,char **)35 int main(int, char**) {
36   {
37     test("", "", 0);
38     test("", "abcde", -5);
39     test("", "abcdefghij", -10);
40     test("", "abcdefghijklmnopqrst", -20);
41     test("abcde", "", 5);
42     test("abcde", "abcde", 0);
43     test("abcde", "abcdefghij", -5);
44     test("abcde", "abcdefghijklmnopqrst", -15);
45     test("abcdefghij", "", 10);
46     test("abcdefghij", "abcde", 5);
47     test("abcdefghij", "abcdefghij", 0);
48     test("abcdefghij", "abcdefghijklmnopqrst", -10);
49     test("abcdefghijklmnopqrst", "", 20);
50     test("abcdefghijklmnopqrst", "abcde", 15);
51     test("abcdefghijklmnopqrst", "abcdefghij", 10);
52     test("abcdefghijklmnopqrst", "abcdefghijklmnopqrst", 0);
53   }
54 
55 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
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 #endif
75 
76 #if TEST_STD_VER >= 11
77   {
78     test(U"", U"", 0);
79     test(U"", U"abcde", -5);
80     test(U"", U"abcdefghij", -10);
81     test(U"", U"abcdefghijklmnopqrst", -20);
82     test(U"abcde", U"", 5);
83     test(U"abcde", U"abcde", 0);
84     test(U"abcde", U"abcdefghij", -5);
85     test(U"abcde", U"abcdefghijklmnopqrst", -15);
86     test(U"abcdefghij", U"", 10);
87     test(U"abcdefghij", U"abcde", 5);
88     test(U"abcdefghij", U"abcdefghij", 0);
89     test(U"abcdefghij", U"abcdefghijklmnopqrst", -10);
90     test(U"abcdefghijklmnopqrst", U"", 20);
91     test(U"abcdefghijklmnopqrst", U"abcde", 15);
92     test(U"abcdefghijklmnopqrst", U"abcdefghij", 10);
93     test(U"abcdefghijklmnopqrst", U"abcdefghijklmnopqrst", 0);
94   }
95 
96   {
97     test(u"", u"", 0);
98     test(u"", u"abcde", -5);
99     test(u"", u"abcdefghij", -10);
100     test(u"", u"abcdefghijklmnopqrst", -20);
101     test(u"abcde", u"", 5);
102     test(u"abcde", u"abcde", 0);
103     test(u"abcde", u"abcdefghij", -5);
104     test(u"abcde", u"abcdefghijklmnopqrst", -15);
105     test(u"abcdefghij", u"", 10);
106     test(u"abcdefghij", u"abcde", 5);
107     test(u"abcdefghij", u"abcdefghij", 0);
108     test(u"abcdefghij", u"abcdefghijklmnopqrst", -10);
109     test(u"abcdefghijklmnopqrst", u"", 20);
110     test(u"abcdefghijklmnopqrst", u"abcde", 15);
111     test(u"abcdefghijklmnopqrst", u"abcdefghij", 10);
112     test(u"abcdefghijklmnopqrst", u"abcdefghijklmnopqrst", 0);
113   }
114 #endif
115 
116 #if TEST_STD_VER > 11
117   {
118     typedef std::basic_string_view<char, constexpr_char_traits<char>> SV;
119     constexpr SV sv1;
120     constexpr SV sv2{"abcde", 5};
121     static_assert(sv1.compare("") == 0, "");
122     static_assert(sv1.compare("abcde") < 0, "");
123     static_assert(sv2.compare("") > 0, "");
124     static_assert(sv2.compare("abcde") == 0, "");
125   }
126 #endif
127 
128   return 0;
129 }
130