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