]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/22_locale/num_put_members_char.cc
1 // 2001-11-19 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.2.1 num_put 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 ostreambuf_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.
61 long l2
= -2147483647;
62 unsigned long ul1
= 1294967294;
63 unsigned long ul2
= 0;
64 double d1
= 1.7976931348623157e+308;
65 double d2
= 2.2250738585072014e-308;
66 long double ld1
= 1.7976931348623157e+308;
67 long double ld2
= 2.2250738585072014e-308;
68 const void* cv
= &ld1
;
70 // cache the num_put facet
73 const num_put
<char>& np
= use_facet
<num_put
<char> >(oss
.getloc());
76 iterator_type os_it00
= oss
.rdbuf();
77 iterator_type os_it01
= np
.put(os_it00
, oss
, '+', b1
);
79 VERIFY( result1
== "1" );
80 // VERIFY( os_it00 != os_it01 );
83 np
.put(oss
.rdbuf(), oss
, '+', b0
);
85 VERIFY( result2
== "0" );
87 // bool, more twisted examples
91 oss
.setf(ios_base::right
, ios_base::adjustfield
);
92 np
.put(oss
.rdbuf(), oss
, '+', b0
);
94 VERIFY( result1
== "+++++++++++++++++++0" );
98 oss
.setf(ios_base::left
, ios_base::adjustfield
);
99 oss
.setf(ios_base::boolalpha
);
100 np
.put(oss
.rdbuf(), oss
, '+', b1
);
102 VERIFY( result2
== "true++++++++++++++++" );
104 // long, in a locale that expects grouping
108 np
.put(oss
.rdbuf(), oss
, '+', l1
);
110 VERIFY( result1
== "2,147,483,647" );
115 oss
.setf(ios_base::left
, ios_base::adjustfield
);
116 np
.put(oss
.rdbuf(), oss
, '+', l2
);
118 VERIFY( result1
== "-2,147,483,647++++++" );
120 // unsigned long, in a locale that does not group
124 np
.put(oss
.rdbuf(), oss
, '+', ul1
);
126 VERIFY( result1
== "1294967294" );
131 oss
.setf(ios_base::left
, ios_base::adjustfield
);
132 np
.put(oss
.rdbuf(), oss
, '+', ul2
);
134 VERIFY( result1
== "0+++++++++++++++++++" );
136 // ... and one that does
141 oss
.setf(ios_base::left
, ios_base::adjustfield
);
142 np
.put(oss
.rdbuf(), oss
, '+', ul1
);
144 VERIFY( result1
== "1.294.967.294+++++++" );
150 oss
.setf(ios_base::left
, ios_base::adjustfield
);
151 np
.put(oss
.rdbuf(), oss
, '+', d1
);
153 VERIFY( result1
== "1,79769e+308++++++++" );
158 oss
.setf(ios_base::right
, ios_base::adjustfield
);
159 np
.put(oss
.rdbuf(), oss
, '+', d2
);
161 VERIFY( result1
== "++++++++2,22507e-308" );
166 oss
.setf(ios_base::right
, ios_base::adjustfield
);
167 oss
.setf(ios_base::scientific
, ios_base::floatfield
);
168 np
.put(oss
.rdbuf(), oss
, '+', d2
);
170 VERIFY( result2
== "+++++++2,225074e-308" );
176 oss
.setf(ios_base::right
, ios_base::adjustfield
);
177 oss
.setf(ios_base::scientific
, ios_base::floatfield
);
178 oss
.setf(ios_base::uppercase
);
179 np
.put(oss
.rdbuf(), oss
, '+', d2
);
181 VERIFY( result1
== "+++2,2250738585E-308" );
186 np
.put(oss
.rdbuf(), oss
, '+', ld1
);
188 VERIFY( result1
== "1,7976931349E+308" );
193 oss
.setf(ios_base::fixed
, ios_base::floatfield
);
194 np
.put(oss
.rdbuf(), oss
, '+', ld2
);
196 VERIFY( result1
== "0" );
201 np
.put(oss
.rdbuf(), oss
, '+', cv
);
203 // No grouping characters.
204 VERIFY( !char_traits
<char>::find(result1
.c_str(),
206 numpunct_de
.decimal_point()) );
207 // Should contain an 'x'.
208 VERIFY( result1
.find('x') == 1 );
210 #ifdef _GLIBCPP_USE_LONG_LONG
211 long long ll1
= 9223372036854775807LL;
212 long long ll2
= -9223372036854775807LL;
216 np
.put(oss
.rdbuf(), oss
, '+', ll1
);
218 VERIFY( result1
== "9.223.372.036.854.775.807" );
227 // Check num_put works with other iterators besides streambuf
228 // output iterators. (As long as output_iterator requirements are met.)
229 typedef string::iterator iter_type
;
230 typedef char_traits
<char> traits
;
231 typedef num_put
<char, iter_type
> num_put_type
;
232 const ios_base::iostate goodbit
= ios_base::goodbit
;
233 const ios_base::iostate eofbit
= ios_base::eofbit
;
234 const locale loc_c
= locale::classic();
235 const string
str("1798 Lady Elgin");
236 const string
str2("0 true 0xbffff74c Mary Nisbet");
237 const string
x(15, 'x'); // have to have allocated string!
241 oss
.imbue(locale(loc_c
, new num_put_type
));
243 // Iterator advanced, state, output.
244 const num_put_type
& tp
= use_facet
<num_put_type
>(oss
.getloc());
247 // 02 put(long double)
254 iter_type ret1
= tp
.put(res
.begin(), oss
, ' ', l
);
255 string
sanity1(res
.begin(), ret1
);
256 VERIFY( res
== "1798xxxxxxxxxxx" );
257 VERIFY( sanity1
== "1798" );
259 // 02 put(long double)
260 const long double ld
= 1798.0;
262 iter_type ret2
= tp
.put(res
.begin(), oss
, ' ', ld
);
263 string
sanity2(res
.begin(), ret2
);
264 VERIFY( res
== "1798xxxxxxxxxxx" );
265 VERIFY( sanity2
== "1798" );
270 iter_type ret3
= tp
.put(res
.begin(), oss
, ' ', b
);
271 string
sanity3(res
.begin(), ret3
);
272 VERIFY( res
== "1xxxxxxxxxxxxxx" );
273 VERIFY( sanity3
== "1" );
277 oss
.setf(ios_base::boolalpha
);
278 iter_type ret4
= tp
.put(res
.begin(), oss
, ' ', b
);
279 string
sanity4(res
.begin(), ret4
);
280 VERIFY( res
== "falsexxxxxxxxxx" );
281 VERIFY( sanity4
== "false" );
285 const void* cv
= &ld
;
287 oss
.setf(ios_base::fixed
, ios_base::floatfield
);
288 iter_type ret5
= tp
.put(res
.begin(), oss
, ' ', cv
);
289 string
sanity5(res
.begin(), ret5
);
290 VERIFY( sanity5
.size() );
291 VERIFY( sanity5
[1] == 'x' );
297 #ifdef _GLIBCPP_HAVE_SETENV
298 // Set the global locale to non-"C".
299 std::locale
loc_de("de_DE");
300 std::locale::global(loc_de
);
302 // Set LANG environment variable to de_DE.
303 const char* oldLANG
= getenv("LANG");
304 if (!setenv("LANG", "de_DE", 1))
308 setenv("LANG", oldLANG
? oldLANG
: "", 1);
313 // http://gcc.gnu.org/ml/libstdc++/2002-05/msg00038.html
318 const char* tentLANG
= std::setlocale(LC_ALL
, "ja_JP.eucjp");
319 if (tentLANG
!= NULL
)
321 std::string preLANG
= tentLANG
;
324 std::string postLANG
= std::setlocale(LC_ALL
, NULL
);
325 VERIFY( preLANG
== postLANG
);
329 // Make sure that, in a locale that expects grouping, when showbase
330 // is true, an hexadecimal or octal zero is correctly output (the case
331 // of zero is special since there is no 0x, 0 respectively, prefix)
337 // A locale that expects grouping.
338 locale
loc_de("de_DE");
345 const num_put
<char>& np
= use_facet
<num_put
<char> >(oss
.getloc());
351 oss
.setf(ios::showbase
);
352 oss
.setf(ios::hex
, ios::basefield
);
353 np
.put(oss
.rdbuf(), oss
, '+', l
);
355 VERIFY( result
== "0" );
359 oss
.setf(ios::showbase
);
360 oss
.setf(ios::oct
, ios::basefield
);
361 np
.put(oss
.rdbuf(), oss
, '+', l
);
363 VERIFY( result
== "0" );