xref: /llvm-project/libcxx/test/std/strings/basic.string/string.ops/string_compare/pointer.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 charT *s) 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>
31 TEST_CONSTEXPR_CXX20 void
32 test(const S& s, const typename S::value_type* str, int x)
33 {
34     LIBCPP_ASSERT_NOEXCEPT(s.compare(str));
35     assert(sign(s.compare(str)) == sign(x));
36 }
37 
38 TEST_CONSTEXPR_CXX20 bool test() {
39   {
40     typedef std::string S;
41     test(S(""), "", 0);
42     test(S(""), "abcde", -5);
43     test(S(""), "abcdefghij", -10);
44     test(S(""), "abcdefghijklmnopqrst", -20);
45     test(S("abcde"), "", 5);
46     test(S("abcde"), "abcde", 0);
47     test(S("abcde"), "abcdefghij", -5);
48     test(S("abcde"), "abcdefghijklmnopqrst", -15);
49     test(S("abcdefghij"), "", 10);
50     test(S("abcdefghij"), "abcde", 5);
51     test(S("abcdefghij"), "abcdefghij", 0);
52     test(S("abcdefghij"), "abcdefghijklmnopqrst", -10);
53     test(S("abcdefghijklmnopqrst"), "", 20);
54     test(S("abcdefghijklmnopqrst"), "abcde", 15);
55     test(S("abcdefghijklmnopqrst"), "abcdefghij", 10);
56     test(S("abcdefghijklmnopqrst"), "abcdefghijklmnopqrst", 0);
57   }
58 #if TEST_STD_VER >= 11
59   {
60     typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
61     test(S(""), "", 0);
62     test(S(""), "abcde", -5);
63     test(S(""), "abcdefghij", -10);
64     test(S(""), "abcdefghijklmnopqrst", -20);
65     test(S("abcde"), "", 5);
66     test(S("abcde"), "abcde", 0);
67     test(S("abcde"), "abcdefghij", -5);
68     test(S("abcde"), "abcdefghijklmnopqrst", -15);
69     test(S("abcdefghij"), "", 10);
70     test(S("abcdefghij"), "abcde", 5);
71     test(S("abcdefghij"), "abcdefghij", 0);
72     test(S("abcdefghij"), "abcdefghijklmnopqrst", -10);
73     test(S("abcdefghijklmnopqrst"), "", 20);
74     test(S("abcdefghijklmnopqrst"), "abcde", 15);
75     test(S("abcdefghijklmnopqrst"), "abcdefghij", 10);
76     test(S("abcdefghijklmnopqrst"), "abcdefghijklmnopqrst", 0);
77   }
78 #endif
79 
80   return true;
81 }
82 
83 int main(int, char**)
84 {
85   test();
86 #if TEST_STD_VER > 17
87   static_assert(test());
88 #endif
89 
90   return 0;
91 }
92