]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/22_locale/num_get_members_wchar_t.cc
dwarf2out.c (fde_table_in_use): Mark GTY.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 22_locale / num_get_members_wchar_t.cc
1 // 2001-11-26 Benjamin Kosnik <bkoz@redhat.com>
2
3 // Copyright (C) 2001, 2002 Free Software Foundation
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 // 22.2.2.1.1 num_get members
22
23 #include <locale>
24 #include <sstream>
25 #include <testsuite_hooks.h>
26
27 // XXX This test is not working for non-glibc locale models.
28 // { dg-do run { xfail *-*-* } }
29
30 #ifdef _GLIBCPP_USE_WCHAR_T
31 void test01()
32 {
33 using namespace std;
34 typedef istreambuf_iterator<wchar_t> iterator_type;
35
36 bool test = true;
37
38 // basic construction
39 locale loc_c = locale::classic();
40 locale loc_hk("en_HK");
41 locale loc_fr("fr_FR@euro");
42 locale loc_de("de_DE");
43 VERIFY( loc_c != loc_de );
44 VERIFY( loc_hk != loc_fr );
45 VERIFY( loc_hk != loc_de );
46 VERIFY( loc_de != loc_fr );
47
48 // cache the numpunct facets
49 const numpunct<wchar_t>& numpunct_c = use_facet<numpunct<wchar_t> >(loc_c);
50 const numpunct<wchar_t>& numpunct_de = use_facet<numpunct<wchar_t> >(loc_de);
51 const numpunct<wchar_t>& numpunct_hk = use_facet<numpunct<wchar_t> >(loc_hk);
52
53 // sanity check the data is correct.
54 const string empty;
55 char c;
56
57 bool b1 = true;
58 bool b0 = false;
59 long l1 = 2147483647;
60 long l2 = -2147483647;
61 long l;
62 unsigned long ul1 = 1294967294;
63 unsigned long ul2 = 0;
64 unsigned long ul;
65 double d1 = 1.02345e+308;
66 double d2 = 3.15e-308;
67 double d;
68 long double ld1 = 6.630025e+4;
69 long double ld2 = 0.0;
70 long double ld;
71 void* v;
72 const void* cv = &ul2;
73
74 // cache the num_get facet
75 wistringstream iss;
76 iss.imbue(loc_de);
77 const num_get<wchar_t>& ng = use_facet<num_get<wchar_t> >(iss.getloc());
78 const ios_base::iostate goodbit = ios_base::goodbit;
79 const ios_base::iostate eofbit = ios_base::eofbit;
80 ios_base::iostate err = ios_base::goodbit;
81
82 // bool, simple
83 iss.str(L"1");
84 iterator_type os_it00 = iss.rdbuf();
85 iterator_type os_it01 = ng.get(os_it00, 0, iss, err, b1);
86 VERIFY( b1 == true );
87 VERIFY( err & ios_base::eofbit );
88
89 iss.str(L"0");
90 err = goodbit;
91 ng.get(iss.rdbuf(), 0, iss, err, b0);
92 VERIFY( b0 == false );
93 VERIFY( err & eofbit );
94
95 // bool, more twisted examples
96 iss.imbue(loc_c);
97 iss.str(L"true ");
98 iss.clear();
99 iss.setf(ios_base::boolalpha);
100 err = goodbit;
101 ng.get(iss.rdbuf(), 0, iss, err, b0);
102 VERIFY( b0 == true );
103 VERIFY( err == goodbit );
104
105 iss.str(L"false ");
106 iss.clear();
107 iss.setf(ios_base::boolalpha);
108 err = goodbit;
109 ng.get(iss.rdbuf(), 0, iss, err, b1);
110 VERIFY( b1 == false );
111 VERIFY( err == goodbit );
112
113 // long, in a locale that expects grouping
114 iss.imbue(loc_hk);
115 iss.str(L"2,147,483,647 ");
116 iss.clear();
117 err = goodbit;
118 ng.get(iss.rdbuf(), 0, iss, err, l);
119 VERIFY( l == l1 );
120 VERIFY( err == goodbit );
121
122 iss.str(L"-2,147,483,647++++++");
123 iss.clear();
124 err = goodbit;
125 ng.get(iss.rdbuf(), 0, iss, err, l);
126 VERIFY( l == l2 );
127 VERIFY( err == goodbit );
128
129 // unsigned long, in a locale that does not group
130 iss.imbue(loc_c);
131 iss.str(L"1294967294");
132 iss.clear();
133 err = goodbit;
134 ng.get(iss.rdbuf(), 0, iss, err, ul);
135 VERIFY( ul == ul1);
136 VERIFY( err == eofbit );
137
138 iss.str(L"0+++++++++++++++++++");
139 iss.clear();
140 err = goodbit;
141 ng.get(iss.rdbuf(), 0, iss, err, ul);
142 VERIFY( ul == ul2);
143 VERIFY( err == goodbit );
144
145 // ... and one that does
146 iss.imbue(loc_de);
147 iss.str(L"1.294.967.294+++++++");
148 iss.clear();
149 iss.width(20);
150 iss.setf(ios_base::left, ios_base::adjustfield);
151 err = goodbit;
152 ng.get(iss.rdbuf(), 0, iss, err, ul);
153 VERIFY( ul == ul1 );
154 VERIFY( err == goodbit );
155
156 // double
157 iss.imbue(loc_c);
158 iss.str(L"1.02345e+308++++++++");
159 iss.clear();
160 iss.width(20);
161 iss.setf(ios_base::left, ios_base::adjustfield);
162 err = goodbit;
163 ng.get(iss.rdbuf(), 0, iss, err, d);
164 VERIFY( d == d1 );
165 VERIFY( err == goodbit );
166
167 iss.str(L"+3.15e-308");
168 iss.clear();
169 iss.width(20);
170 iss.setf(ios_base::right, ios_base::adjustfield);
171 err = goodbit;
172 ng.get(iss.rdbuf(), 0, iss, err, d);
173 VERIFY( d == d2 );
174 VERIFY( err == eofbit );
175
176 iss.imbue(loc_de);
177 iss.str(L"+1,02345e+308");
178 iss.clear();
179 iss.width(20);
180 iss.setf(ios_base::right, ios_base::adjustfield);
181 iss.setf(ios_base::scientific, ios_base::floatfield);
182 err = goodbit;
183 ng.get(iss.rdbuf(), 0, iss, err, d);
184 VERIFY( d == d1 );
185 VERIFY( err == eofbit );
186
187 iss.str(L"3,15E-308 ");
188 iss.clear();
189 iss.width(20);
190 iss.precision(10);
191 iss.setf(ios_base::right, ios_base::adjustfield);
192 iss.setf(ios_base::scientific, ios_base::floatfield);
193 iss.setf(ios_base::uppercase);
194 err = goodbit;
195 ng.get(iss.rdbuf(), 0, iss, err, d);
196 VERIFY( d == d2 );
197 VERIFY( err == goodbit );
198
199 // long double
200 iss.str(L"6,630025e+4");
201 iss.clear();
202 err = goodbit;
203 ng.get(iss.rdbuf(), 0, iss, err, ld);
204 VERIFY( ld == ld1 );
205 VERIFY( err == eofbit );
206
207 iss.str(L"0 ");
208 iss.clear();
209 iss.precision(0);
210 iss.setf(ios_base::fixed, ios_base::floatfield);
211 err = goodbit;
212 ng.get(iss.rdbuf(), 0, iss, err, ld);
213 VERIFY( ld == 0 );
214 VERIFY( err == goodbit );
215
216 // const void
217 iss.str(L"0xbffff74c,");
218 iss.clear();
219 err = goodbit;
220 ng.get(iss.rdbuf(), 0, iss, err, v);
221 VERIFY( &v != &cv );
222 VERIFY( err == goodbit );
223
224
225 #ifdef _GLIBCPP_USE_LONG_LONG
226 long long ll1 = 9223372036854775807LL;
227 long long ll2 = -9223372036854775807LL;
228 long long ll;
229
230 iss.str(L"9.223.372.036.854.775.807");
231 iss.clear();
232 err = goodbit;
233 ng.get(iss.rdbuf(), 0, iss, err, ll);
234 VERIFY( ll == ll1 );
235 VERIFY( err == eofbit );
236 #endif
237 }
238
239 // 2002-01-10 David Seymour <seymour_dj@yahoo.com>
240 // libstdc++/5331
241 void test02()
242 {
243 using namespace std;
244 bool test = true;
245
246 // Check num_get works with other iterators besides streambuf
247 // output iterators. (As long as output_iterator requirements are met.)
248 typedef wstring::const_iterator iter_type;
249 typedef num_get<wchar_t, iter_type> num_get_type;
250 const ios_base::iostate goodbit = ios_base::goodbit;
251 const ios_base::iostate eofbit = ios_base::eofbit;
252 ios_base::iostate err = ios_base::goodbit;
253 const locale loc_c = locale::classic();
254 const wstring str(L"20000106 Elizabeth Durack");
255 const wstring str2(L"0 true 0xbffff74c Durack");
256
257 istringstream iss; // need an ios, add my num_get facet
258 iss.imbue(locale(loc_c, new num_get_type));
259
260 // Iterator advanced, state, output.
261 const num_get_type& ng = use_facet<num_get_type>(iss.getloc());
262
263 // 01 get(long)
264 // 02 get(long double)
265 // 03 get(bool)
266 // 04 get(void*)
267
268 // 01 get(long)
269 long i = 0;
270 err = goodbit;
271 iter_type end1 = ng.get(str.begin(), str.end(), iss, err, i);
272 wstring rem1(end1, str.end());
273 VERIFY( err == goodbit );
274 VERIFY( i == 20000106);
275 VERIFY( rem1 == L" Elizabeth Durack" );
276
277 // 02 get(long double)
278 long double ld = 0.0;
279 err = goodbit;
280 iter_type end2 = ng.get(str.begin(), str.end(), iss, err, ld);
281 wstring rem2(end2, str.end());
282 VERIFY( err == goodbit );
283 VERIFY( ld == 20000106);
284 VERIFY( rem2 == L" Elizabeth Durack" );
285
286 // 03 get(bool)
287 // const string str2("0 true 0xbffff74c Durack");
288 bool b = 1;
289 iss.clear();
290 err = goodbit;
291 iter_type end3 = ng.get(str2.begin(), str2.end(), iss, err, b);
292 wstring rem3(end3, str2.end());
293 VERIFY( err == goodbit );
294 VERIFY( b == 0 );
295 VERIFY( rem3 == L" true 0xbffff74c Durack" );
296
297 iss.clear();
298 err = goodbit;
299 iss.setf(ios_base::boolalpha);
300 iter_type end4 = ng.get(++end3, str2.end(), iss, err, b);
301 wstring rem4(end4, str2.end());
302 VERIFY( err == goodbit );
303 VERIFY( b == true );
304 VERIFY( rem4 == L" 0xbffff74c Durack" );
305
306 // 04 get(void*)
307 void* v;
308 iss.clear();
309 err = goodbit;
310 iss.setf(ios_base::fixed, ios_base::floatfield);
311 iter_type end5 = ng.get(++end4, str2.end(), iss, err, v);
312 wstring rem5(end5, str2.end());
313 VERIFY( err == goodbit );
314 VERIFY( b == true );
315 VERIFY( rem5 == L" Durack" );
316 }
317
318 // libstdc++/5280
319 void test03()
320 {
321 #ifdef _GLIBCPP_HAVE_SETENV
322 // Set the global locale to non-"C".
323 std::locale loc_de("de_DE");
324 std::locale::global(loc_de);
325
326 // Set LANG environment variable to de_DE.
327 const char* oldLANG = getenv("LANG");
328 if (!setenv("LANG", "de_DE", 1))
329 {
330 test01();
331 test02();
332 setenv("LANG", oldLANG ? oldLANG : "", 1);
333 }
334 #endif
335 }
336
337 // Testing the correct parsing of grouped hexadecimals and octals.
338 void test04()
339 {
340 using namespace std;
341
342 bool test = true;
343
344 unsigned long ul;
345
346 wistringstream iss;
347
348 // A locale that expects grouping
349 locale loc_de("de_DE");
350 iss.imbue(loc_de);
351
352 const num_get<wchar_t>& ng = use_facet<num_get<wchar_t> >(iss.getloc());
353 const ios_base::iostate goodbit = ios_base::goodbit;
354 ios_base::iostate err = ios_base::goodbit;
355
356 iss.setf(ios::hex, ios::basefield);
357 iss.str(L"0xbf.fff.74c ");
358 err = goodbit;
359 ng.get(iss.rdbuf(), 0, iss, err, ul);
360 VERIFY( err == goodbit );
361 VERIFY( ul == 0xbffff74c );
362
363 iss.str(L"0Xf.fff ");
364 err = goodbit;
365 ng.get(iss.rdbuf(), 0, iss, err, ul);
366 VERIFY( err == goodbit );
367 VERIFY( ul == 0xffff );
368
369 iss.str(L"ffe ");
370 err = goodbit;
371 ng.get(iss.rdbuf(), 0, iss, err, ul);
372 VERIFY( err == goodbit );
373 VERIFY( ul == 0xffe );
374
375 iss.setf(ios::oct, ios::basefield);
376 iss.str(L"07.654.321 ");
377 err = goodbit;
378 ng.get(iss.rdbuf(), 0, iss, err, ul);
379 VERIFY( err == goodbit );
380 VERIFY( ul == 07654321 );
381
382 iss.str(L"07.777 ");
383 err = goodbit;
384 ng.get(iss.rdbuf(), 0, iss, err, ul);
385 VERIFY( err == goodbit );
386 VERIFY( ul == 07777 );
387
388 iss.str(L"776 ");
389 err = goodbit;
390 ng.get(iss.rdbuf(), 0, iss, err, ul);
391 VERIFY( err == goodbit );
392 VERIFY( ul == 0776 );
393 }
394
395 // libstdc++/5816
396 void test05()
397 {
398 using namespace std;
399 bool test = true;
400
401 double d = 0.0;
402
403 wistringstream iss;
404 locale loc_de("de_DE");
405 iss.imbue(loc_de);
406
407 const num_get<wchar_t>& ng = use_facet<num_get<wchar_t> >(iss.getloc());
408 const ios_base::iostate goodbit = ios_base::goodbit;
409 ios_base::iostate err = ios_base::goodbit;
410
411 iss.str(L"1234,5 ");
412 err = goodbit;
413 ng.get(iss.rdbuf(), 0, iss, err, d);
414 VERIFY( err == goodbit );
415 VERIFY( d == 1234.5 );
416 }
417
418 // http://gcc.gnu.org/ml/libstdc++/2002-05/msg00038.html
419 void test06()
420 {
421 bool test = true;
422
423 const char* tentLANG = std::setlocale(LC_ALL, "ja_JP.eucjp");
424 if (tentLANG != NULL)
425 {
426 std::string preLANG = tentLANG;
427 test01();
428 test02();
429 test04();
430 test05();
431 std::string postLANG = std::setlocale(LC_ALL, NULL);
432 VERIFY( preLANG == postLANG );
433 }
434 }
435 #endif
436
437 int main()
438 {
439 #ifdef _GLIBCPP_USE_WCHAR_T
440 test01();
441 test02();
442 test03();
443 test04();
444 test05();
445 test06();
446 #endif
447 return 0;
448 }
449
450
451 // Kathleen Hannah, humanitarian, woman, art-thief
452
453
454
455
456