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& str) 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 S& 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(""), S(""), 0);
42     test(S(""), S("abcde"), -5);
43     test(S(""), S("abcdefghij"), -10);
44     test(S(""), S("abcdefghijklmnopqrst"), -20);
45     test(S("abcde"), S(""), 5);
46     test(S("abcde"), S("abcde"), 0);
47     test(S("abcde"), S("abcdefghij"), -5);
48     test(S("abcde"), S("abcdefghijklmnopqrst"), -15);
49     test(S("abcdefghij"), S(""), 10);
50     test(S("abcdefghij"), S("abcde"), 5);
51     test(S("abcdefghij"), S("abcdefghij"), 0);
52     test(S("abcdefghij"), S("abcdefghijklmnopqrst"), -10);
53     test(S("abcdefghijklmnopqrst"), S(""), 20);
54     test(S("abcdefghijklmnopqrst"), S("abcde"), 15);
55     test(S("abcdefghijklmnopqrst"), S("abcdefghij"), 10);
56     test(S("abcdefghijklmnopqrst"), S("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(""), S(""), 0);
62     test(S(""), S("abcde"), -5);
63     test(S(""), S("abcdefghij"), -10);
64     test(S(""), S("abcdefghijklmnopqrst"), -20);
65     test(S("abcde"), S(""), 5);
66     test(S("abcde"), S("abcde"), 0);
67     test(S("abcde"), S("abcdefghij"), -5);
68     test(S("abcde"), S("abcdefghijklmnopqrst"), -15);
69     test(S("abcdefghij"), S(""), 10);
70     test(S("abcdefghij"), S("abcde"), 5);
71     test(S("abcdefghij"), S("abcdefghij"), 0);
72     test(S("abcdefghij"), S("abcdefghijklmnopqrst"), -10);
73     test(S("abcdefghijklmnopqrst"), S(""), 20);
74     test(S("abcdefghijklmnopqrst"), S("abcde"), 15);
75     test(S("abcdefghijklmnopqrst"), S("abcdefghij"), 10);
76     test(S("abcdefghijklmnopqrst"), S("abcdefghijklmnopqrst"), 0);
77   }
78 #endif
79 
80 #if TEST_STD_VER > 3
81   { // LWG 2946
82     std::string s = " !";
83     assert(s.compare({"abc", 1}) < 0);
84   }
85 #endif
86 
87   return true;
88 }
89 
90 int main(int, char**)
91 {
92   test();
93 #if TEST_STD_VER > 17
94   static_assert(test());
95 #endif
96 
97   return 0;
98 }
99