657608399cc0c5b8f1593c11e4379ad2fb754360
[deliverable/binutils-gdb.git] / gdb / unittests / basic_string_view / operations / compare / char / 1.cc
1 // { dg-options "-std=gnu++17" }
2
3 // Copyright (C) 2013-2021 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 // basic_string_view::compare
21 // int compare(const basic_string_view& str) const;
22 // int compare(size_type pos1, size_type n1, const basic_string_view& str) const;
23 // int compare(size_type pos1, size_type n1, const basic_string_view& str,
24 // size_type pos2, size_type n2) const;
25 // int compare(const charT* s) const;
26 // int compare(size_type pos1, size_type n1,
27 // const charT* s, size_type n2 = npos) const;
28
29 // NB compare should be thought of as a lexographical compare, ie how
30 // things would be sorted in a dictionary.
31
32 namespace operations_compare_1 {
33
34 enum want_value {lt=0, z=1, gt=2};
35
36 int
37 test_value(int result, want_value expected);
38
39 int
40 test_value(int result, want_value expected)
41 {
42 bool pass = false;
43
44 switch (expected) {
45 case lt:
46 if (result < 0)
47 pass = true;
48 break;
49 case z:
50 if (!result)
51 pass = true;
52 break;
53 case gt:
54 if (result > 0)
55 pass = true;
56 break;
57 default:
58 pass = false; //should not get here
59 }
60 VERIFY(pass);
61 return 0;
62 }
63
64 static int
65 test01 ()
66 {
67 using gdb::string_view;
68
69 string_view str_0("costa rica");
70 string_view str_1("costa marbella");
71 string_view str_2;
72
73 //sanity check
74 test_value(strcmp("costa marbella", "costa rica"), lt);
75 test_value(strcmp("costa rica", "costa rica"), z);
76 test_value(strcmp(str_1.data(), str_0.data()), lt);
77 test_value(strcmp(str_0.data(), str_1.data()), gt);
78 test_value(strncmp(str_1.data(), str_0.data(), 6), z);
79 test_value(strncmp(str_1.data(), str_0.data(), 14), lt);
80 test_value(memcmp(str_1.data(), str_0.data(), 6), z);
81 test_value(memcmp(str_1.data(), str_0.data(), 10), lt);
82 test_value(memcmp("costa marbella", "costa rica", 10), lt);
83
84 // int compare(const basic_string_view& str) const;
85 test_value(str_0.compare(str_1), gt); //because r>m
86 test_value(str_1.compare(str_0), lt); //because m<r
87 str_2 = str_0;
88 test_value(str_2.compare(str_0), z);
89 str_2 = "cost";
90 test_value(str_2.compare(str_0), lt);
91 str_2 = "costa ricans";
92 test_value(str_2.compare(str_0), gt);
93
94 // int compare(size_type pos1, size_type n1, const basic_string_view& str) const;
95 test_value(str_1.compare(0, 6, str_0), lt);
96 str_2 = "cost";
97 test_value(str_1.compare(0, 4, str_2), z);
98 test_value(str_1.compare(0, 5, str_2), gt);
99
100 // int compare(size_type pos1, size_type n1, const basic_string_view& str,
101 // size_type pos2, size_type n2) const;
102 test_value(str_1.compare(0, 6, str_0, 0, 6), z);
103 test_value(str_1.compare(0, 7, str_0, 0, 7), lt);
104 test_value(str_0.compare(0, 7, str_1, 0, 7), gt);
105
106 // int compare(const charT* s) const;
107 test_value(str_0.compare("costa marbella"), gt);
108 test_value(str_1.compare("costa rica"), lt);
109 str_2 = str_0;
110 test_value(str_2.compare("costa rica"), z);
111 test_value(str_2.compare("cost"), gt);
112 test_value(str_2.compare("costa ricans"), lt);
113
114 // int compare(size_type pos, size_type n1, const charT* str,
115 // size_type n2 = npos) const;
116 test_value(str_1.compare(0, 6, "costa rica", 0, 6), z);
117 test_value(str_1.compare(0, 7, "costa rica", 0, 7), lt);
118 test_value(str_0.compare(0, 7, "costa marbella", 0, 7), gt);
119
120 return 0;
121 }
122
123
124 static int
125 main ()
126 {
127 test01();
128
129 return 0;
130 }
131
132 } // namespace operations_compare_1
This page took 0.033599 seconds and 3 git commands to generate.