xref: /llvm-project/libcxx/test/std/strings/basic.string/string.ops/string_compare/string_view.pass.cpp (revision 425620ccdd47e56b59512913cdc71e116f951e4e)
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 // XFAIL: LIBCXX-AIX-FIXME
10 
11 // <string>
12 
13 // int compare(const basic_string_view sv) const // constexpr since C++20
14 
15 #include <string>
16 #include <cassert>
17 
18 #include "test_macros.h"
19 #include "min_allocator.h"
20 
21 TEST_CONSTEXPR_CXX20 int sign(int x)
22 {
23     if (x == 0)
24         return 0;
25     if (x < 0)
26         return -1;
27     return 1;
28 }
29 
30 template <class S, class SV>
31 TEST_CONSTEXPR_CXX20 void
32 test(const S& s, SV sv, int x)
33 {
34     LIBCPP_ASSERT_NOEXCEPT(s.compare(sv));
35     assert(sign(s.compare(sv)) == sign(x));
36 }
37 
38 TEST_CONSTEXPR_CXX20 bool test() {
39   {
40     typedef std::string S;
41     typedef std::string_view SV;
42     test(S(""), SV(""), 0);
43     test(S(""), SV("abcde"), -5);
44     test(S(""), SV("abcdefghij"), -10);
45     test(S(""), SV("abcdefghijklmnopqrst"), -20);
46     test(S("abcde"), SV(""), 5);
47     test(S("abcde"), SV("abcde"), 0);
48     test(S("abcde"), SV("abcdefghij"), -5);
49     test(S("abcde"), SV("abcdefghijklmnopqrst"), -15);
50     test(S("abcdefghij"), SV(""), 10);
51     test(S("abcdefghij"), SV("abcde"), 5);
52     test(S("abcdefghij"), SV("abcdefghij"), 0);
53     test(S("abcdefghij"), SV("abcdefghijklmnopqrst"), -10);
54     test(S("abcdefghijklmnopqrst"), SV(""), 20);
55     test(S("abcdefghijklmnopqrst"), SV("abcde"), 15);
56     test(S("abcdefghijklmnopqrst"), SV("abcdefghij"), 10);
57     test(S("abcdefghijklmnopqrst"), SV("abcdefghijklmnopqrst"), 0);
58   }
59 #if TEST_STD_VER >= 11
60   {
61     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
62     typedef std::string_view SV;
63     test(S(""), SV(""), 0);
64     test(S(""), SV("abcde"), -5);
65     test(S(""), SV("abcdefghij"), -10);
66     test(S(""), SV("abcdefghijklmnopqrst"), -20);
67     test(S("abcde"), SV(""), 5);
68     test(S("abcde"), SV("abcde"), 0);
69     test(S("abcde"), SV("abcdefghij"), -5);
70     test(S("abcde"), SV("abcdefghijklmnopqrst"), -15);
71     test(S("abcdefghij"), SV(""), 10);
72     test(S("abcdefghij"), SV("abcde"), 5);
73     test(S("abcdefghij"), SV("abcdefghij"), 0);
74     test(S("abcdefghij"), SV("abcdefghijklmnopqrst"), -10);
75     test(S("abcdefghijklmnopqrst"), SV(""), 20);
76     test(S("abcdefghijklmnopqrst"), SV("abcde"), 15);
77     test(S("abcdefghijklmnopqrst"), SV("abcdefghij"), 10);
78     test(S("abcdefghijklmnopqrst"), SV("abcdefghijklmnopqrst"), 0);
79   }
80 #endif
81 
82   return true;
83 }
84 
85 int main(int, char**)
86 {
87   test();
88 #if TEST_STD_VER > 17
89   static_assert(test());
90 #endif
91 
92   return 0;
93 }
94