]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string/compare/char/1.cc
1 // 980930 bkoz work with libstdc++v3
3 // Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING3. If not see
20 // <http://www.gnu.org/licenses/>.
22 // 21.3.6.8 basic_string::compare
23 // int compare(const basic_string& str) const;
24 // int compare(size_type pos1, size_type n1, const basic_string& str) const;
25 // int compare(size_type pos1, size_type n1, const basic_string& str,
26 // size_type pos2, size_type n2) const;
27 // int compare(const charT* s) const;
28 // int compare(size_type pos1, size_type n1,
29 // const charT* s, size_type n2 = npos) const;
31 // NB compare should be thought of as a lexographical compare, ie how
32 // things would be sorted in a dictionary.
36 #include <testsuite_hooks.h>
38 enum want_value
{lt
=0, z
=1, gt
=2};
41 test_value(int result
, want_value expected
);
44 test_value(int result
, want_value expected
)
46 bool test
__attribute__((unused
)) = true;
63 pass
= false; //should not get here
75 string
str_0("costa rica");
76 string
str_1("costa marbella");
80 test_value(strcmp("costa marbella", "costa rica"), lt
);
81 test_value(strcmp("costa rica", "costa rica"), z
);
82 test_value(strcmp(str_1
.data(), str_0
.data()), lt
);
83 test_value(strcmp(str_0
.data(), str_1
.data()), gt
);
84 test_value(strncmp(str_1
.data(), str_0
.data(), 6), z
);
85 test_value(strncmp(str_1
.data(), str_0
.data(), 14), lt
);
86 test_value(memcmp(str_1
.data(), str_0
.data(), 6), z
);
87 test_value(memcmp(str_1
.data(), str_0
.data(), 14), lt
);
88 test_value(memcmp("costa marbella", "costa rica", 14), lt
);
90 // int compare(const basic_string& str) const;
91 test_value(str_0
.compare(str_1
), gt
); //because r>m
92 test_value(str_1
.compare(str_0
), lt
); //because m<r
94 test_value(str_2
.compare(str_0
), z
);
96 test_value(str_2
.compare(str_0
), lt
);
97 str_2
= "costa ricans";
98 test_value(str_2
.compare(str_0
), gt
);
100 // int compare(size_type pos1, size_type n1, const basic_string& str) const;
101 test_value(str_1
.compare(0, 6, str_0
), lt
);
103 test_value(str_1
.compare(0, 4, str_2
), z
);
104 test_value(str_1
.compare(0, 5, str_2
), gt
);
106 // int compare(size_type pos1, size_type n1, const basic_string& str,
107 // size_type pos2, size_type n2) const;
108 test_value(str_1
.compare(0, 6, str_0
, 0, 6), z
);
109 test_value(str_1
.compare(0, 7, str_0
, 0, 7), lt
);
110 test_value(str_0
.compare(0, 7, str_1
, 0, 7), gt
);
112 // int compare(const charT* s) const;
113 test_value(str_0
.compare("costa marbella"), gt
);
114 test_value(str_1
.compare("costa rica"), lt
);
116 test_value(str_2
.compare("costa rica"), z
);
117 test_value(str_2
.compare("cost"), gt
);
118 test_value(str_2
.compare("costa ricans"), lt
);
120 // int compare(size_type pos, size_type n1, const charT* str,
121 // size_type n2 = npos) const;
122 test_value(str_1
.compare(0, 6, "costa rica", 0, 6), z
);
123 test_value(str_1
.compare(0, 7, "costa rica", 0, 7), lt
);
124 test_value(str_0
.compare(0, 7, "costa marbella", 0, 7), gt
);