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