]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/22_locale/ctype_is_char.cc
dwarf2out.c (fde_table_in_use): Mark GTY.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 22_locale / ctype_is_char.cc
1 // Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 2, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING. If not, write to the Free
16 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17 // USA.
18
19 // As a special exception, you may use this file as part of a free software
20 // library without restriction. Specifically, if other files instantiate
21 // templates or use macros or inline functions from this file, or you compile
22 // this file and link it with other files to produce an executable, this
23 // file does not by itself cause the resulting executable to be covered by
24 // the GNU General Public License. This exception does not however
25 // invalidate any other reasons why the executable file might be covered by
26 // the GNU General Public License.
27
28 // 22.2.1.3.2 ctype<char> members
29
30 #include <locale>
31 #include <vector>
32 #include <testsuite_hooks.h>
33
34 // XXX This test (test02) is not working for non-glibc locale models.
35 // { dg-do run { xfail *-*-* } }
36
37 typedef char char_type;
38 class gnu_ctype: public std::ctype<char_type> { };
39
40 void test01()
41 {
42 bool test = true;
43 const char_type strlit00[] = "manilla, cebu, tandag PHILIPPINES";
44 const char_type strlit01[] = "MANILLA, CEBU, TANDAG PHILIPPINES";
45 const char_type strlit02[] = "manilla, cebu, tandag philippines";
46 const char_type c00 = 'S';
47 const char_type c10 = 's';
48 const char_type c20 = '9';
49 const char_type c30 = ' ';
50 const char_type c40 = '!';
51 const char_type c50 = 'F';
52 const char_type c60 = 'f';
53 const char_type c70 = 'X';
54 const char_type c80 = 'x';
55
56 gnu_ctype gctype;
57 char_type c100;
58 int len = std::char_traits<char_type>::length(strlit00);
59 char_type c_array[len + 1];
60
61 // sanity check ctype_base::mask members
62 int i01 = std::ctype_base::space;
63 int i02 = std::ctype_base::upper;
64 int i03 = std::ctype_base::lower;
65 int i04 = std::ctype_base::digit;
66 int i05 = std::ctype_base::punct;
67 int i06 = std::ctype_base::alpha;
68 int i07 = std::ctype_base::xdigit;
69 int i08 = std::ctype_base::alnum;
70 int i09 = std::ctype_base::graph;
71 int i10 = std::ctype_base::print;
72 int i11 = std::ctype_base::cntrl;
73 int i12 = sizeof(std::ctype_base::mask);
74 VERIFY ( i01 != i02);
75 VERIFY ( i02 != i03);
76 VERIFY ( i03 != i04);
77 VERIFY ( i04 != i05);
78 VERIFY ( i05 != i06);
79 VERIFY ( i06 != i07);
80 VERIFY ( i07 != i08);
81 VERIFY ( i08 != i09);
82 VERIFY ( i09 != i10);
83 VERIFY ( i10 != i11);
84 VERIFY ( i11 != i01);
85
86 // bool is(mask m, char_type c) const;
87 VERIFY( gctype.is(std::ctype_base::space, c30) );
88 VERIFY( gctype.is(std::ctype_base::upper, c00) );
89 VERIFY( gctype.is(std::ctype_base::lower, c10) );
90 VERIFY( gctype.is(std::ctype_base::digit, c20) );
91 VERIFY( gctype.is(std::ctype_base::punct, c40) );
92 VERIFY( gctype.is(std::ctype_base::alpha, c50) );
93 VERIFY( gctype.is(std::ctype_base::alpha, c60) );
94 VERIFY( gctype.is(std::ctype_base::xdigit, c20) );
95 VERIFY( !gctype.is(std::ctype_base::xdigit, c80) );
96 VERIFY( gctype.is(std::ctype_base::alnum, c50) );
97 VERIFY( gctype.is(std::ctype_base::alnum, c20) );
98 VERIFY( gctype.is(std::ctype_base::graph, c40) );
99 VERIFY( gctype.is(std::ctype_base::graph, c20) );
100
101 // const char* is(const char* low, const char* high, mask* vec) const
102 std::ctype_base::mask m00 = static_cast<std::ctype_base::mask>(0);
103 std::ctype_base::mask m01[3];
104 std::ctype_base::mask m02[13];
105 const char_type* cc0 = strlit00;
106 const char_type* cc1 = NULL;
107 const char_type* cc2 = NULL;
108
109 cc0 = strlit00;
110 m01[0] = m00;
111 m01[1] = m00;
112 m01[2] = m00;
113 cc1 = gctype.is(cc0, cc0, m01);
114 VERIFY( cc1 == strlit00 );
115 VERIFY( m01[0] == m00 );
116 VERIFY( m01[1] == m00 );
117 VERIFY( m01[2] == m00 );
118
119 cc0 = strlit00;
120 m01[0] = m00;
121 m01[1] = m00;
122 m01[2] = m00;
123 cc2 = gctype.is(cc0, cc0 + 3, m01);
124 VERIFY( cc2 == strlit00 + 3);
125 VERIFY( m01[0] != m00 );
126 VERIFY( m01[1] != m00 );
127 VERIFY( m01[2] != m00 );
128 VERIFY( gctype.is(m01[0], cc0[0]) );
129 VERIFY( gctype.is(m01[1], cc0[1]) );
130 VERIFY( gctype.is(m01[2], cc0[2]) );
131
132 cc0 = strlit01;
133 cc1 = gctype.is(cc0, cc0 + 13, m02);
134 VERIFY( cc1 == strlit01 + 13);
135 VERIFY( m02[6] != m00 );
136 VERIFY( m02[7] != m00 );
137 VERIFY( m02[8] != m00 );
138 VERIFY( m02[8] != m02[6] );
139 VERIFY( m02[6] != m02[7] );
140 VERIFY( static_cast<bool>(m02[6] & std::ctype_base::alnum) );
141 VERIFY( static_cast<bool>(m02[6] & std::ctype_base::upper) );
142 VERIFY( static_cast<bool>(m02[6] & std::ctype_base::alpha) );
143 VERIFY( static_cast<bool>(m02[7] & std::ctype_base::punct) );
144 VERIFY( static_cast<bool>(m02[8] & std::ctype_base::space) );
145 VERIFY( gctype.is(m02[6], cc0[6]) );
146 VERIFY( gctype.is(m02[7], cc0[7]) );
147 VERIFY( gctype.is(m02[8], cc0[8]) );
148 }
149
150 // libstdc++/4456, libstdc++/4457, libstdc++/4458
151 void test02()
152 {
153 using namespace std;
154 typedef ctype_base::mask mask;
155 typedef vector<mask> vector_type;
156
157 bool test = true;
158
159 // const int max = numeric_limits<char>::max();
160 const int max = 255;
161 const int ctype_mask_max = 10;
162 vector_type v_c(max);
163 vector_type v_de(max);
164
165 // "C"
166 locale loc_c = locale::classic();
167 const ctype<char>& ctype_c = use_facet<ctype<char> >(loc_c);
168 for (int i = 0; i < max; ++i)
169 {
170 char_type c = static_cast<char>(i);
171 mask mask_test = static_cast<mask>(0);
172 mask mask_is = static_cast<mask>(0);
173 for (int j = 0; j <= ctype_mask_max; ++j)
174 {
175 mask_test = static_cast<mask>(1 << j);
176 if (ctype_c.is(mask_test, c))
177 mask_is |= mask_test;
178 }
179 v_c[i] = mask_is;
180 }
181
182 // "de_DE"
183 locale loc_de("de_DE");
184 const ctype<char>& ctype_de = use_facet<ctype<char> >(loc_de);
185 for (int i = 0; i < max; ++i)
186 {
187 char_type c = static_cast<char>(i);
188 mask mask_test = static_cast<mask>(0);
189 mask mask_is = static_cast<mask>(0);
190 for (int j = 0; j <= ctype_mask_max; ++j)
191 {
192 mask_test = static_cast<mask>(1 << j);
193 if (ctype_de.is(mask_test, c))
194 mask_is |= mask_test;
195 }
196 v_de[i] = mask_is;
197 }
198
199 #if QUANNUM_VERBOSE_LYRICALLY_ADEPT_BAY_AREA_MCS_MODE
200 for (int i = 0; i < max; ++i)
201 {
202 char_type mark = v_c[i] == v_de[i] ? ' ' : '-';
203 cout << i << ' ' << mark << ' ' << static_cast<char>(i) << '\t' ;
204 cout << "v_c: " << setw(4) << v_c[i] << '\t';
205 cout << "v_de: " << setw(4) << v_de[i] << endl;
206 }
207 cout << (v_c == v_de) << endl;
208 #endif
209
210 VERIFY( v_c != v_de );
211 }
212
213 // Per Liboriussen <liborius@stofanet.dk>
214 void test03()
215 {
216 bool test = true;
217 std::ctype_base::mask maskdata[256];
218 for (int i = 0; i < 256; ++i)
219 maskdata[i] = std::ctype_base::alpha;
220 std::ctype<char>* f = new std::ctype<char>(maskdata);
221 std::locale global;
222 std::locale loc(global, f);
223 for (int i = 0; i < 256; ++i)
224 {
225 char_type ch = i;
226 VERIFY( std::isalpha(ch, loc) );
227 }
228 }
229
230 // libstdc++/5280
231 void test04()
232 {
233 #ifdef _GLIBCPP_HAVE_SETENV
234 // Set the global locale to non-"C".
235 std::locale loc_de("de_DE");
236 std::locale::global(loc_de);
237
238 // Set LANG environment variable to de_DE.
239 const char* oldLANG = getenv("LANG");
240 if (!setenv("LANG", "de_DE", 1))
241 {
242 test01();
243 test02();
244 test03();
245 setenv("LANG", oldLANG ? oldLANG : "", 1);
246 }
247 #endif
248 }
249
250 // http://gcc.gnu.org/ml/libstdc++/2002-05/msg00038.html
251 void test05()
252 {
253 bool test = true;
254
255 const char* tentLANG = std::setlocale(LC_ALL, "ja_JP.eucjp");
256 if (tentLANG != NULL)
257 {
258 std::string preLANG = tentLANG;
259 test01();
260 test02();
261 test03();
262 std::string postLANG = std::setlocale(LC_ALL, NULL);
263 VERIFY( preLANG == postLANG );
264 }
265 }
266
267 int main()
268 {
269 test01();
270 test02();
271 test03();
272 test04();
273 test05();
274 return 0;
275 }