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