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