]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string/operations/compare/char/2.cc
08b3f0c378b25d7b0d2e5707c544bd16ecd9af67
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / operations / compare / char / 2.cc
1 // { dg-options "-std=gnu++17" }
2
3 // Copyright (C) 2016 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 3, or (at your option)
9 // any later version.
10
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
19
20 // [string::compare]
21
22 #include <string>
23 #include <testsuite_hooks.h>
24
25 void
26 test03()
27 {
28 bool test __attribute__((unused)) = true;
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
52 // PR libstdc++/77264
53 void
54 test04()
55 {
56 bool test __attribute__((unused)) = true;
57
58 const std::string str("a");
59 char c = 'a';
60 int res = str.compare(0, 1, &c, 1);
61 VERIFY ( !res );
62
63 char arr[] = "a";
64 res = str.compare(0, 1, arr, 1);
65 VERIFY ( !res );
66
67 const char carr[] = "a";
68 res = str.compare(0, 1, carr, 1);
69 VERIFY ( !res );
70
71 struct S {
72 operator char*() { return &c; }
73 operator std::string_view() { return "!"; }
74 char c = 'a';
75 };
76
77 res = str.compare(0, 1, S{}, 1);
78 VERIFY ( !res );
79 }
80
81 int main()
82 {
83 test03();
84 test04();
85 return 0;
86 }