]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string_view/operations/compare/char/1.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string_view / operations / compare / char / 1.cc
1 // { dg-do run { target c++17 } }
2
3 // Copyright (C) 2013-2024 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 #include <string_view>
33 #include <cstring>
34 #include <testsuite_hooks.h>
35
36 enum want_value {lt=0, z=1, gt=2};
37
38 int
39 test_value(int result, want_value expected);
40
41 int
42 test_value(int result, want_value expected)
43 {
44 bool pass = false;
45
46 switch (expected) {
47 case lt:
48 if (result < 0)
49 pass = true;
50 break;
51 case z:
52 if (!result)
53 pass = true;
54 break;
55 case gt:
56 if (result > 0)
57 pass = true;
58 break;
59 default:
60 pass = false; //should not get here
61 }
62 VERIFY(pass);
63 return 0;
64 }
65
66 int
67 test01()
68 {
69 using std::string_view;
70
71 string_view str_0("costa rica");
72 string_view str_1("costa marbella");
73 string_view str_2;
74
75 //sanity check
76 test_value(strcmp("costa marbella", "costa rica"), lt);
77 test_value(strcmp("costa rica", "costa rica"), z);
78 test_value(strcmp(str_1.data(), str_0.data()), lt);
79 test_value(strcmp(str_0.data(), str_1.data()), gt);
80 test_value(strncmp(str_1.data(), str_0.data(), 6), z);
81 test_value(strncmp(str_1.data(), str_0.data(), 14), lt);
82 test_value(memcmp(str_1.data(), str_0.data(), 6), z);
83 test_value(memcmp(str_1.data(), str_0.data(), 10), lt);
84 test_value(memcmp("costa marbella", "costa rica", 10), lt);
85
86 // int compare(const basic_string_view& str) const;
87 test_value(str_0.compare(str_1), gt); //because r>m
88 test_value(str_1.compare(str_0), lt); //because m<r
89 str_2 = str_0;
90 test_value(str_2.compare(str_0), z);
91 str_2 = "cost";
92 test_value(str_2.compare(str_0), lt);
93 str_2 = "costa ricans";
94 test_value(str_2.compare(str_0), gt);
95
96 // int compare(size_type pos1, size_type n1, const basic_string_view& str) const;
97 test_value(str_1.compare(0, 6, str_0), lt);
98 str_2 = "cost";
99 test_value(str_1.compare(0, 4, str_2), z);
100 test_value(str_1.compare(0, 5, str_2), gt);
101
102 // int compare(size_type pos1, size_type n1, const basic_string_view& str,
103 // size_type pos2, size_type n2) const;
104 test_value(str_1.compare(0, 6, str_0, 0, 6), z);
105 test_value(str_1.compare(0, 7, str_0, 0, 7), lt);
106 test_value(str_0.compare(0, 7, str_1, 0, 7), gt);
107
108 // int compare(const charT* s) const;
109 test_value(str_0.compare("costa marbella"), gt);
110 test_value(str_1.compare("costa rica"), lt);
111 str_2 = str_0;
112 test_value(str_2.compare("costa rica"), z);
113 test_value(str_2.compare("cost"), gt);
114 test_value(str_2.compare("costa ricans"), lt);
115
116 // int compare(size_type pos, size_type n1, const charT* str,
117 // size_type n2 = npos) const;
118 test_value(str_1.compare(0, 6, "costa rica", 0, 6), z);
119 test_value(str_1.compare(0, 7, "costa rica", 0, 7), lt);
120 test_value(str_0.compare(0, 7, "costa marbella", 0, 7), gt);
121
122 return 0;
123 }
124
125
126 int
127 main()
128 {
129 test01();
130
131 return 0;
132 }