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