]>
Commit | Line | Data |
---|---|---|
ca8f2cb1 | 1 | // { dg-options "-std=gnu++17" } |
6458742a | 2 | // { dg-do run { target c++17 } } |
ca8f2cb1 | 3 | |
99dee823 | 4 | // Copyright (C) 2016-2021 Free Software Foundation, Inc. |
ca8f2cb1 VV |
5 | // |
6 | // This file is part of the GNU ISO C++ Library. This library is free | |
7 | // software; you can redistribute it and/or modify it under the | |
8 | // terms of the GNU General Public License as published by the | |
9 | // Free Software Foundation; either version 3, or (at your option) | |
10 | // any later version. | |
11 | ||
12 | // This library is distributed in the hope that it will be useful, | |
13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | // GNU General Public License for more details. | |
16 | ||
17 | // You should have received a copy of the GNU General Public License along | |
18 | // with this library; see the file COPYING3. If not see | |
19 | // <http://www.gnu.org/licenses/>. | |
20 | ||
21 | // [string::compare] | |
22 | ||
23 | #include <string> | |
24 | #include <testsuite_hooks.h> | |
25 | ||
26 | void | |
27 | test03() | |
28 | { | |
ca8f2cb1 VV |
29 | std::string_view str1("foobar"); |
30 | std::string str2("foobar"); | |
31 | ||
32 | auto x = str2.compare(str1); | |
33 | VERIFY (x == 0); | |
34 | ||
35 | str1 = "bar"; | |
36 | x = str2.compare(str1); | |
37 | VERIFY (x > 0); | |
38 | ||
39 | str1 = "foobar"; | |
40 | str2 = "bar"; | |
41 | x = str2.compare(str1); | |
42 | VERIFY (x < 0); | |
43 | x = str2.compare(0, 3, str1, 3, 3); | |
44 | VERIFY (x == 0); | |
45 | ||
46 | str1 = "bar"; | |
47 | str2 = "foobar"; | |
48 | x = str2.compare(3, 3, str1); | |
49 | VERIFY (x == 0); | |
50 | } | |
51 | ||
657213f7 JW |
52 | // PR libstdc++/77264 |
53 | void | |
54 | test04() | |
55 | { | |
657213f7 JW |
56 | const std::string str("a"); |
57 | char c = 'a'; | |
58 | int res = str.compare(0, 1, &c, 1); | |
59 | VERIFY ( !res ); | |
60 | ||
61 | char arr[] = "a"; | |
62 | res = str.compare(0, 1, arr, 1); | |
63 | VERIFY ( !res ); | |
64 | ||
65 | const char carr[] = "a"; | |
66 | res = str.compare(0, 1, carr, 1); | |
67 | VERIFY ( !res ); | |
68 | ||
69 | struct S { | |
70 | operator char*() { return &c; } | |
71 | operator std::string_view() { return "!"; } | |
72 | char c = 'a'; | |
73 | }; | |
74 | ||
75 | res = str.compare(0, 1, S{}, 1); | |
76 | VERIFY ( !res ); | |
77 | } | |
78 | ||
ca8f2cb1 VV |
79 | int main() |
80 | { | |
81 | test03(); | |
657213f7 | 82 | test04(); |
ca8f2cb1 VV |
83 | return 0; |
84 | } |