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 "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, class SV>
28 void
29 test(const S& s, SV sv, int x)
30 {
31     assert(sign(s.compare(sv)) == sign(x));
32 }
33 
34 int main()
35 {
36     {
37     typedef std::string S;
38     typedef std::string_view SV;
39     test(S(""), SV(""), 0);
40     test(S(""), SV("abcde"), -5);
41     test(S(""), SV("abcdefghij"), -10);
42     test(S(""), SV("abcdefghijklmnopqrst"), -20);
43     test(S("abcde"), SV(""), 5);
44     test(S("abcde"), SV("abcde"), 0);
45     test(S("abcde"), SV("abcdefghij"), -5);
46     test(S("abcde"), SV("abcdefghijklmnopqrst"), -15);
47     test(S("abcdefghij"), SV(""), 10);
48     test(S("abcdefghij"), SV("abcde"), 5);
49     test(S("abcdefghij"), SV("abcdefghij"), 0);
50     test(S("abcdefghij"), SV("abcdefghijklmnopqrst"), -10);
51     test(S("abcdefghijklmnopqrst"), SV(""), 20);
52     test(S("abcdefghijklmnopqrst"), SV("abcde"), 15);
53     test(S("abcdefghijklmnopqrst"), SV("abcdefghij"), 10);
54     test(S("abcdefghijklmnopqrst"), SV("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     typedef std::string_view SV;
60     test(S(""), SV(""), 0);
61     test(S(""), SV("abcde"), -5);
62     test(S(""), SV("abcdefghij"), -10);
63     test(S(""), SV("abcdefghijklmnopqrst"), -20);
64     test(S("abcde"), SV(""), 5);
65     test(S("abcde"), SV("abcde"), 0);
66     test(S("abcde"), SV("abcdefghij"), -5);
67     test(S("abcde"), SV("abcdefghijklmnopqrst"), -15);
68     test(S("abcdefghij"), SV(""), 10);
69     test(S("abcdefghij"), SV("abcde"), 5);
70     test(S("abcdefghij"), SV("abcdefghij"), 0);
71     test(S("abcdefghij"), SV("abcdefghijklmnopqrst"), -10);
72     test(S("abcdefghijklmnopqrst"), SV(""), 20);
73     test(S("abcdefghijklmnopqrst"), SV("abcde"), 15);
74     test(S("abcdefghijklmnopqrst"), SV("abcdefghij"), 10);
75     test(S("abcdefghijklmnopqrst"), SV("abcdefghijklmnopqrst"), 0);
76     }
77 #endif
78 }
79