xref: /llvm-project/libcxx/test/std/strings/basic.string/string.ops/string_compare/string_view.pass.cpp (revision dea74b2820612ecca967528f7f2bbcc6d05801fe)
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 basic_string_view sv) const
12 
13 #include <string>
14 #include <cassert>
15 
16 #include "test_macros.h"
17 #include "min_allocator.h"
18 
19 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, class SV>
29 void
30 test(const S& s, SV sv, int x)
31 {
32     LIBCPP_ASSERT_NOEXCEPT(s.compare(sv));
33     assert(sign(s.compare(sv)) == sign(x));
34 }
35 
36 int main(int, char**)
37 {
38     {
39     typedef std::string S;
40     typedef std::string_view SV;
41     test(S(""), SV(""), 0);
42     test(S(""), SV("abcde"), -5);
43     test(S(""), SV("abcdefghij"), -10);
44     test(S(""), SV("abcdefghijklmnopqrst"), -20);
45     test(S("abcde"), SV(""), 5);
46     test(S("abcde"), SV("abcde"), 0);
47     test(S("abcde"), SV("abcdefghij"), -5);
48     test(S("abcde"), SV("abcdefghijklmnopqrst"), -15);
49     test(S("abcdefghij"), SV(""), 10);
50     test(S("abcdefghij"), SV("abcde"), 5);
51     test(S("abcdefghij"), SV("abcdefghij"), 0);
52     test(S("abcdefghij"), SV("abcdefghijklmnopqrst"), -10);
53     test(S("abcdefghijklmnopqrst"), SV(""), 20);
54     test(S("abcdefghijklmnopqrst"), SV("abcde"), 15);
55     test(S("abcdefghijklmnopqrst"), SV("abcdefghij"), 10);
56     test(S("abcdefghijklmnopqrst"), SV("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     typedef std::string_view SV;
62     test(S(""), SV(""), 0);
63     test(S(""), SV("abcde"), -5);
64     test(S(""), SV("abcdefghij"), -10);
65     test(S(""), SV("abcdefghijklmnopqrst"), -20);
66     test(S("abcde"), SV(""), 5);
67     test(S("abcde"), SV("abcde"), 0);
68     test(S("abcde"), SV("abcdefghij"), -5);
69     test(S("abcde"), SV("abcdefghijklmnopqrst"), -15);
70     test(S("abcdefghij"), SV(""), 10);
71     test(S("abcdefghij"), SV("abcde"), 5);
72     test(S("abcdefghij"), SV("abcdefghij"), 0);
73     test(S("abcdefghij"), SV("abcdefghijklmnopqrst"), -10);
74     test(S("abcdefghijklmnopqrst"), SV(""), 20);
75     test(S("abcdefghijklmnopqrst"), SV("abcde"), 15);
76     test(S("abcdefghijklmnopqrst"), SV("abcdefghij"), 10);
77     test(S("abcdefghijklmnopqrst"), SV("abcdefghijklmnopqrst"), 0);
78     }
79 #endif
80 
81   return 0;
82 }
83