]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/21_strings/basic_string/compare/wchar_t/1.cc
natString.cc (init(gnu.gcj.runtime.StringBuffer)): New method.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 21_strings / basic_string / compare / wchar_t / 1.cc
CommitLineData
b2dad0e3
BK
1// 980930 bkoz work with libstdc++v3
2
61f1ed59 3// Copyright (C) 1998, 1999, 2003 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
8// Free Software Foundation; either version 2, 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 COPYING. If not, write to the Free
18// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19// USA.
20
21// 21.3.6.8 basic_string::compare
22// int compare(const basic_string& str) const;
23// int compare(size_type pos1, size_type n1, const basic_string& str) const;
24// int compare(size_type pos1, size_type n1, const basic_string& 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>
fe413112 34#include <testsuite_hooks.h>
b2dad0e3
BK
35
36enum want_value {lt=0, z=1, gt=2};
37
aa1b2f7d
BV
38int
39test_value(int result, want_value expected);
40
41int
42test_value(int result, want_value expected)
43{
b2dad0e3
BK
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
63#ifdef DEBUG_ASSERT
64 assert(pass);
65#endif
aa1b2f7d
BV
66
67 return 0;
b2dad0e3
BK
68}
69
70
aa1b2f7d
BV
71int
72test01()
73{
9aa1d5ac 74 using namespace std;
b2dad0e3 75
61f1ed59
PC
76 wstring str_0(L"costa rica");
77 wstring str_1(L"costa marbella");
78 wstring str_2;
dcc41852 79
b2dad0e3 80 //sanity check
61f1ed59
PC
81 test_value(wcscmp(L"costa marbella", L"costa rica"), lt);
82 test_value(wcscmp(L"costa rica", L"costa rica"), z);
83 test_value(wcscmp(str_1.data(), str_0.data()), lt);
84 test_value(wcscmp(str_0.data(), str_1.data()), gt);
85 test_value(wcsncmp(str_1.data(), str_0.data(), 6), z);
86 test_value(wcsncmp(str_1.data(), str_0.data(), 14), lt);
87 test_value(wmemcmp(str_1.data(), str_0.data(), 6), z);
88 test_value(wmemcmp(str_1.data(), str_0.data(), 14), lt);
89 test_value(wmemcmp(L"costa marbella", L"costa rica", 14), lt);
b2dad0e3
BK
90
91 // int compare(const basic_string& str) const;
92 test_value(str_0.compare(str_1), gt); //because r>m
93 test_value(str_1.compare(str_0), lt); //because m<r
94 str_2 = str_0;
95 test_value(str_2.compare(str_0), z);
61f1ed59 96 str_2 = L"cost";
b2dad0e3 97 test_value(str_2.compare(str_0), lt);
61f1ed59 98 str_2 = L"costa ricans";
b2dad0e3
BK
99 test_value(str_2.compare(str_0), gt);
100
101 // int compare(size_type pos1, size_type n1, const basic_string& str) const;
102 test_value(str_1.compare(0, 6, str_0), lt);
61f1ed59 103 str_2 = L"cost";
b2dad0e3
BK
104 test_value(str_1.compare(0, 4, str_2), z);
105 test_value(str_1.compare(0, 5, str_2), gt);
106
107 // int compare(size_type pos1, size_type n1, const basic_string& str,
108 // size_type pos2, size_type n2) const;
109 test_value(str_1.compare(0, 6, str_0, 0, 6), z);
110 test_value(str_1.compare(0, 7, str_0, 0, 7), lt);
111 test_value(str_0.compare(0, 7, str_1, 0, 7), gt);
112
113 // int compare(const charT* s) const;
61f1ed59
PC
114 test_value(str_0.compare(L"costa marbella"), gt);
115 test_value(str_1.compare(L"costa rica"), lt);
b2dad0e3 116 str_2 = str_0;
61f1ed59
PC
117 test_value(str_2.compare(L"costa rica"), z);
118 test_value(str_2.compare(L"cost"), gt);
119 test_value(str_2.compare(L"costa ricans"), lt);
b2dad0e3
BK
120
121 // int compare(size_type pos, size_type n1, const charT* str,
122 // size_type n2 = npos) const;
61f1ed59
PC
123 test_value(str_1.compare(0, 6, L"costa rica", 0, 6), z);
124 test_value(str_1.compare(0, 7, L"costa rica", 0, 7), lt);
125 test_value(str_0.compare(0, 7, L"costa marbella", 0, 7), gt);
b2dad0e3
BK
126
127 return 0;
128}
129
130
aa1b2f7d
BV
131int
132main()
133{
b2dad0e3 134 test01();
aa1b2f7d 135 return 0;
b2dad0e3 136}