]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 980930 bkoz work with libstdc++v3 |
2 | ||
99dee823 | 3 | // Copyright (C) 1998-2021 Free Software Foundation, Inc. |
b2dad0e3 BK |
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 | |
748086b7 | 8 | // Free Software Foundation; either version 3, or (at your option) |
b2dad0e3 BK |
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 | |
748086b7 JJ |
17 | // with this library; see the file COPYING3. If not see |
18 | // <http://www.gnu.org/licenses/>. | |
b2dad0e3 BK |
19 | |
20 | // 21.3.6.8 basic_string::compare | |
21 | // int compare(const basic_string& str) const; | |
22 | // int compare(size_type pos1, size_type n1, const basic_string& str) const; | |
23 | // int compare(size_type pos1, size_type n1, const basic_string& 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> | |
fe413112 | 33 | #include <testsuite_hooks.h> |
b2dad0e3 BK |
34 | |
35 | enum want_value {lt=0, z=1, gt=2}; | |
36 | ||
aa1b2f7d BV |
37 | int |
38 | test_value(int result, want_value expected); | |
39 | ||
40 | int | |
41 | test_value(int result, want_value expected) | |
42 | { | |
b2dad0e3 BK |
43 | bool pass = false; |
44 | ||
45 | switch (expected) { | |
46 | case lt: | |
47 | if (result < 0) | |
48 | pass = true; | |
49 | break; | |
50 | case z: | |
51 | if (!result) | |
52 | pass = true; | |
53 | break; | |
54 | case gt: | |
55 | if (result > 0) | |
56 | pass = true; | |
57 | break; | |
58 | default: | |
59 | pass = false; //should not get here | |
60 | } | |
61 | ||
cb584bcf | 62 | VERIFY(pass); |
aa1b2f7d | 63 | return 0; |
b2dad0e3 BK |
64 | } |
65 | ||
66 | ||
aa1b2f7d BV |
67 | int |
68 | test01() | |
69 | { | |
9aa1d5ac | 70 | using namespace std; |
b2dad0e3 | 71 | |
61f1ed59 PC |
72 | wstring str_0(L"costa rica"); |
73 | wstring str_1(L"costa marbella"); | |
74 | wstring str_2; | |
dcc41852 | 75 | |
b2dad0e3 | 76 | //sanity check |
61f1ed59 PC |
77 | test_value(wcscmp(L"costa marbella", L"costa rica"), lt); |
78 | test_value(wcscmp(L"costa rica", L"costa rica"), z); | |
79 | test_value(wcscmp(str_1.data(), str_0.data()), lt); | |
80 | test_value(wcscmp(str_0.data(), str_1.data()), gt); | |
81 | test_value(wcsncmp(str_1.data(), str_0.data(), 6), z); | |
82 | test_value(wcsncmp(str_1.data(), str_0.data(), 14), lt); | |
83 | test_value(wmemcmp(str_1.data(), str_0.data(), 6), z); | |
84 | test_value(wmemcmp(str_1.data(), str_0.data(), 14), lt); | |
85 | test_value(wmemcmp(L"costa marbella", L"costa rica", 14), lt); | |
b2dad0e3 BK |
86 | |
87 | // int compare(const basic_string& 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); | |
61f1ed59 | 92 | str_2 = L"cost"; |
b2dad0e3 | 93 | test_value(str_2.compare(str_0), lt); |
61f1ed59 | 94 | str_2 = L"costa ricans"; |
b2dad0e3 BK |
95 | test_value(str_2.compare(str_0), gt); |
96 | ||
97 | // int compare(size_type pos1, size_type n1, const basic_string& str) const; | |
98 | test_value(str_1.compare(0, 6, str_0), lt); | |
61f1ed59 | 99 | str_2 = L"cost"; |
b2dad0e3 BK |
100 | test_value(str_1.compare(0, 4, str_2), z); |
101 | test_value(str_1.compare(0, 5, str_2), gt); | |
102 | ||
103 | // int compare(size_type pos1, size_type n1, const basic_string& str, | |
104 | // size_type pos2, size_type n2) const; | |
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; | |
61f1ed59 PC |
110 | test_value(str_0.compare(L"costa marbella"), gt); |
111 | test_value(str_1.compare(L"costa rica"), lt); | |
b2dad0e3 | 112 | str_2 = str_0; |
61f1ed59 PC |
113 | test_value(str_2.compare(L"costa rica"), z); |
114 | test_value(str_2.compare(L"cost"), gt); | |
115 | test_value(str_2.compare(L"costa ricans"), lt); | |
b2dad0e3 BK |
116 | |
117 | // int compare(size_type pos, size_type n1, const charT* str, | |
118 | // size_type n2 = npos) const; | |
61f1ed59 PC |
119 | test_value(str_1.compare(0, 6, L"costa rica", 0, 6), z); |
120 | test_value(str_1.compare(0, 7, L"costa rica", 0, 7), lt); | |
121 | test_value(str_0.compare(0, 7, L"costa marbella", 0, 7), gt); | |
b2dad0e3 BK |
122 | |
123 | return 0; | |
124 | } | |
125 | ||
126 | ||
aa1b2f7d BV |
127 | int |
128 | main() | |
129 | { | |
b2dad0e3 | 130 | test01(); |
aa1b2f7d | 131 | return 0; |
b2dad0e3 | 132 | } |