]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/21_strings/basic_string/compare/char/1.cc
Licensing changes to GPLv3 resp. GPLv3 with GCC Runtime Exception.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / compare / char / 1.cc
1 // 980930 bkoz work with libstdc++v3
2
3 // Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2009
5 // Free Software Foundation, Inc.
6 //
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)
11 // any later version.
12
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.
17
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/>.
21
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;
30
31 // NB compare should be thought of as a lexographical compare, ie how
32 // things would be sorted in a dictionary.
33
34 #include <string>
35 #include <cstring>
36 #include <testsuite_hooks.h>
37
38 enum want_value {lt=0, z=1, gt=2};
39
40 int
41 test_value(int result, want_value expected);
42
43 int
44 test_value(int result, want_value expected)
45 {
46 bool test __attribute__((unused)) = true;
47 bool pass = false;
48
49 switch (expected) {
50 case lt:
51 if (result < 0)
52 pass = true;
53 break;
54 case z:
55 if (!result)
56 pass = true;
57 break;
58 case gt:
59 if (result > 0)
60 pass = true;
61 break;
62 default:
63 pass = false; //should not get here
64 }
65 VERIFY(pass);
66 return 0;
67 }
68
69
70 int
71 test01()
72 {
73 using namespace std;
74
75 string str_0("costa rica");
76 string str_1("costa marbella");
77 string str_2;
78
79 //sanity check
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);
89
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
93 str_2 = str_0;
94 test_value(str_2.compare(str_0), z);
95 str_2 = "cost";
96 test_value(str_2.compare(str_0), lt);
97 str_2 = "costa ricans";
98 test_value(str_2.compare(str_0), gt);
99
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);
102 str_2 = "cost";
103 test_value(str_1.compare(0, 4, str_2), z);
104 test_value(str_1.compare(0, 5, str_2), gt);
105
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);
111
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);
115 str_2 = str_0;
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);
119
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);
125
126 return 0;
127 }
128
129
130 int
131 main()
132 {
133 test01();
134 return 0;
135 }