]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/22_locale/num_get_members_char.cc
8786a2cb9b059efca4236defa8c86c132747eb63
1 // 2001-11-21 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2001, 2002 Free Software Foundation
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)
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.
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,
21 // 22.2.2.1.1 num_get members
25 #include <testsuite_hooks.h>
27 // XXX This test is not working for non-glibc locale models.
28 // { dg-do run { xfail *-*-* } }
33 typedef istreambuf_iterator
<char> iterator_type
;
38 locale loc_c
= locale::classic();
39 locale
loc_hk("en_HK");
40 locale
loc_fr("fr_FR@euro");
41 locale
loc_de("de_DE");
42 VERIFY( loc_c
!= loc_de
);
43 VERIFY( loc_hk
!= loc_fr
);
44 VERIFY( loc_hk
!= loc_de
);
45 VERIFY( loc_de
!= loc_fr
);
47 // cache the numpunct facets
48 const numpunct
<char>& numpunct_c
= use_facet
<numpunct
<char> >(loc_c
);
49 const numpunct
<char>& numpunct_de
= use_facet
<numpunct
<char> >(loc_de
);
50 const numpunct
<char>& numpunct_hk
= use_facet
<numpunct
<char> >(loc_hk
);
52 // sanity check the data is correct.
59 long l2
= -2147483647;
61 unsigned long ul1
= 1294967294;
62 unsigned long ul2
= 0;
64 double d1
= 1.02345e+308;
65 double d2
= 3.15e-308;
67 long double ld1
= 6.630025e+4;
68 long double ld2
= 0.0;
71 const void* cv
= &ul2
;
73 // cache the num_get facet
76 const num_get
<char>& ng
= use_facet
<num_get
<char> >(iss
.getloc());
77 const ios_base::iostate goodbit
= ios_base::goodbit
;
78 const ios_base::iostate eofbit
= ios_base::eofbit
;
79 ios_base::iostate err
= ios_base::goodbit
;
83 iterator_type os_it00
= iss
.rdbuf();
84 iterator_type os_it01
= ng
.get(os_it00
, 0, iss
, err
, b1
);
86 VERIFY( err
& ios_base::eofbit
);
90 ng
.get(iss
.rdbuf(), 0, iss
, err
, b0
);
91 VERIFY( b0
== false );
92 VERIFY( err
& eofbit
);
94 // bool, more twisted examples
98 iss
.setf(ios_base::boolalpha
);
100 ng
.get(iss
.rdbuf(), 0, iss
, err
, b0
);
101 VERIFY( b0
== true );
102 VERIFY( err
== goodbit
);
106 iss
.setf(ios_base::boolalpha
);
108 ng
.get(iss
.rdbuf(), 0, iss
, err
, b1
);
109 VERIFY( b1
== false );
110 VERIFY( err
== goodbit
);
112 // long, in a locale that expects grouping
114 iss
.str("2,147,483,647 ");
117 ng
.get(iss
.rdbuf(), 0, iss
, err
, l
);
119 VERIFY( err
== goodbit
);
121 iss
.str("-2,147,483,647++++++");
124 ng
.get(iss
.rdbuf(), 0, iss
, err
, l
);
126 VERIFY( err
== goodbit
);
128 // unsigned long, in a locale that does not group
130 iss
.str("1294967294");
133 ng
.get(iss
.rdbuf(), 0, iss
, err
, ul
);
135 VERIFY( err
== eofbit
);
137 iss
.str("0+++++++++++++++++++");
140 ng
.get(iss
.rdbuf(), 0, iss
, err
, ul
);
142 VERIFY( err
== goodbit
);
144 // ... and one that does
146 iss
.str("1.294.967.294+++++++");
149 iss
.setf(ios_base::left
, ios_base::adjustfield
);
151 ng
.get(iss
.rdbuf(), 0, iss
, err
, ul
);
153 VERIFY( err
== goodbit
);
157 iss
.str("1.02345e+308++++++++");
160 iss
.setf(ios_base::left
, ios_base::adjustfield
);
162 ng
.get(iss
.rdbuf(), 0, iss
, err
, d
);
164 VERIFY( err
== goodbit
);
166 iss
.str("+3.15e-308");
169 iss
.setf(ios_base::right
, ios_base::adjustfield
);
171 ng
.get(iss
.rdbuf(), 0, iss
, err
, d
);
173 VERIFY( err
== eofbit
);
176 iss
.str("+1,02345e+308");
179 iss
.setf(ios_base::right
, ios_base::adjustfield
);
180 iss
.setf(ios_base::scientific
, ios_base::floatfield
);
182 ng
.get(iss
.rdbuf(), 0, iss
, err
, d
);
184 VERIFY( err
== eofbit
);
186 iss
.str("3,15E-308 ");
190 iss
.setf(ios_base::right
, ios_base::adjustfield
);
191 iss
.setf(ios_base::scientific
, ios_base::floatfield
);
192 iss
.setf(ios_base::uppercase
);
194 ng
.get(iss
.rdbuf(), 0, iss
, err
, d
);
196 VERIFY( err
== goodbit
);
199 iss
.str("6,630025e+4");
202 ng
.get(iss
.rdbuf(), 0, iss
, err
, ld
);
204 VERIFY( err
== eofbit
);
209 iss
.setf(ios_base::fixed
, ios_base::floatfield
);
211 ng
.get(iss
.rdbuf(), 0, iss
, err
, ld
);
213 VERIFY( err
== goodbit
);
216 iss
.str("0xbffff74c,");
219 ng
.get(iss
.rdbuf(), 0, iss
, err
, v
);
221 VERIFY( err
== goodbit
);
224 #ifdef _GLIBCPP_USE_LONG_LONG
225 long long ll1
= 9223372036854775807LL;
226 long long ll2
= -9223372036854775807LL;
229 iss
.str("9.223.372.036.854.775.807");
232 ng
.get(iss
.rdbuf(), 0, iss
, err
, ll
);
234 VERIFY( err
== eofbit
);
238 // 2002-01-10 David Seymour <seymour_dj@yahoo.com>
245 // Check num_get works with other iterators besides streambuf
246 // output iterators. (As long as output_iterator requirements are met.)
247 typedef string::const_iterator iter_type
;
248 typedef num_get
<char, iter_type
> num_get_type
;
249 const ios_base::iostate goodbit
= ios_base::goodbit
;
250 const ios_base::iostate eofbit
= ios_base::eofbit
;
251 ios_base::iostate err
= ios_base::goodbit
;
252 const locale loc_c
= locale::classic();
253 const string
str("20000106 Elizabeth Durack");
254 const string
str2("0 true 0xbffff74c Durack");
256 istringstream iss
; // need an ios, add my num_get facet
257 iss
.imbue(locale(loc_c
, new num_get_type
));
259 // Iterator advanced, state, output.
260 const num_get_type
& ng
= use_facet
<num_get_type
>(iss
.getloc());
263 // 02 get(long double)
270 iter_type end1
= ng
.get(str
.begin(), str
.end(), iss
, err
, i
);
271 string
rem1(end1
, str
.end());
272 VERIFY( err
== goodbit
);
273 VERIFY( i
== 20000106);
274 VERIFY( rem1
== " Elizabeth Durack" );
276 // 02 get(long double)
277 long double ld
= 0.0;
279 iter_type end2
= ng
.get(str
.begin(), str
.end(), iss
, err
, ld
);
280 string
rem2(end2
, str
.end());
281 VERIFY( err
== goodbit
);
282 VERIFY( ld
== 20000106);
283 VERIFY( rem2
== " Elizabeth Durack" );
289 iter_type end3
= ng
.get(str2
.begin(), str2
.end(), iss
, err
, b
);
290 string
rem3(end3
, str2
.end());
291 VERIFY( err
== goodbit
);
293 VERIFY( rem3
== " true 0xbffff74c Durack" );
297 iss
.setf(ios_base::boolalpha
);
298 iter_type end4
= ng
.get(++end3
, str2
.end(), iss
, err
, b
);
299 string
rem4(end4
, str2
.end());
300 VERIFY( err
== goodbit
);
302 VERIFY( rem4
== " 0xbffff74c Durack" );
308 iss
.setf(ios_base::fixed
, ios_base::floatfield
);
309 iter_type end5
= ng
.get(++end4
, str2
.end(), iss
, err
, v
);
310 string
rem5(end5
, str2
.end());
311 VERIFY( err
== goodbit
);
313 VERIFY( rem5
== " Durack" );
319 #ifdef _GLIBCPP_HAVE_SETENV
320 // Set the global locale to non-"C".
321 std::locale
loc_de("de_DE");
322 std::locale::global(loc_de
);
324 // Set LANG environment variable to de_DE.
325 const char* oldLANG
= getenv("LANG");
326 if (!setenv("LANG", "de_DE", 1))
330 setenv("LANG", oldLANG
? oldLANG
: "", 1);
335 // Testing the correct parsing of grouped hexadecimals and octals.
346 // A locale that expects grouping
347 locale
loc_de("de_DE");
350 const num_get
<char>& ng
= use_facet
<num_get
<char> >(iss
.getloc());
351 const ios_base::iostate goodbit
= ios_base::goodbit
;
352 ios_base::iostate err
= ios_base::goodbit
;
354 iss
.setf(ios::hex
, ios::basefield
);
355 iss
.str("0xbf.fff.74c ");
357 ng
.get(iss
.rdbuf(), 0, iss
, err
, ul
);
358 VERIFY( err
== goodbit
);
359 VERIFY( ul
== 0xbffff74c );
363 ng
.get(iss
.rdbuf(), 0, iss
, err
, ul
);
364 VERIFY( err
== goodbit
);
365 VERIFY( ul
== 0xffff );
369 ng
.get(iss
.rdbuf(), 0, iss
, err
, ul
);
370 VERIFY( err
== goodbit
);
371 VERIFY( ul
== 0xffe );
373 iss
.setf(ios::oct
, ios::basefield
);
374 iss
.str("07.654.321 ");
376 ng
.get(iss
.rdbuf(), 0, iss
, err
, ul
);
377 VERIFY( err
== goodbit
);
378 VERIFY( ul
== 07654321 );
382 ng
.get(iss
.rdbuf(), 0, iss
, err
, ul
);
383 VERIFY( err
== goodbit
);
384 VERIFY( ul
== 07777 );
388 ng
.get(iss
.rdbuf(), 0, iss
, err
, ul
);
389 VERIFY( err
== goodbit
);
390 VERIFY( ul
== 0776 );
402 locale
loc_de("de_DE");
405 const num_get
<char>& ng
= use_facet
<num_get
<char> >(iss
.getloc());
406 const ios_base::iostate goodbit
= ios_base::goodbit
;
407 ios_base::iostate err
= ios_base::goodbit
;
411 ng
.get(iss
.rdbuf(), 0, iss
, err
, d
);
412 VERIFY( err
== goodbit
);
413 VERIFY( d
== 1234.5 );
416 // http://gcc.gnu.org/ml/libstdc++/2002-05/msg00038.html
421 const char* tentLANG
= setlocale(LC_ALL
, "ja_JP.eucjp");
422 if (tentLANG
!= NULL
)
424 std::string preLANG
= tentLANG
;
429 std::string postLANG
= setlocale(LC_ALL
, NULL
);
430 VERIFY( preLANG
== postLANG
);
446 // Kathleen Hannah, humanitarian, woman, art-thief