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 *-*-* } }
30 #ifdef _GLIBCPP_USE_WCHAR_T
34 typedef ostreambuf_iterator<wchar_t> iterator_type;
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 );
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);
53 // sanity check the data is correct.
62 long l2 = -2147483647;
63 unsigned long ul1 = 1294967294;
64 unsigned long ul2 = 0;
65 double d1 = 1.7976931348623157e+308;
66 double d2 = 2.2250738585072014e-308;
67 long double ld1 = 1.7976931348623157e+308;
68 long double ld2 = 2.2250738585072014e-308;
69 const void* cv = &ld1;
71 // cache the num_put facet
74 const num_put<wchar_t>& np = use_facet<num_put<wchar_t> >(oss.getloc());
77 iterator_type os_it00 = oss.rdbuf();
78 iterator_type os_it01 = np.put(os_it00, oss, '+', b1);
80 VERIFY( result1 == L"1" );
81 // VERIFY( os_it00 != os_it01 );
84 np.put(oss.rdbuf(), oss, L'+', b0);
86 VERIFY( result2 == L"0" );
88 // bool, more twisted examples
92 oss.setf(ios_base::right, ios_base::adjustfield);
93 np.put(oss.rdbuf(), oss, L'+', b0);
95 VERIFY( result1 == L"+++++++++++++++++++0" );
99 oss.setf(ios_base::left, ios_base::adjustfield);
100 oss.setf(ios_base::boolalpha);
101 np.put(oss.rdbuf(), oss, L'+', b1);
103 VERIFY( result2 == L"true++++++++++++++++" );
105 // long, in a locale that expects grouping
109 np.put(oss.rdbuf(), oss, L'+', l1);
111 VERIFY( result1 == L"2,147,483,647" );
116 oss.setf(ios_base::left, ios_base::adjustfield);
117 np.put(oss.rdbuf(), oss, L'+', l2);
119 VERIFY( result1 == L"-2,147,483,647++++++" );
121 // unsigned long, in a locale that does not group
125 np.put(oss.rdbuf(), oss, L'+', ul1);
127 VERIFY( result1 == L"1294967294" );
132 oss.setf(ios_base::left, ios_base::adjustfield);
133 np.put(oss.rdbuf(), oss, L'+', ul2);
135 VERIFY( result1 == L"0+++++++++++++++++++" );
137 // ... and one that does
142 oss.setf(ios_base::left, ios_base::adjustfield);
143 np.put(oss.rdbuf(), oss, L'+', ul1);
145 VERIFY( result1 == L"1.294.967.294+++++++" );
151 oss.setf(ios_base::left, ios_base::adjustfield);
152 np.put(oss.rdbuf(), oss, L'+', d1);
154 VERIFY( result1 == L"1,79769e+308++++++++" );
159 oss.setf(ios_base::right, ios_base::adjustfield);
160 np.put(oss.rdbuf(), oss, L'+', d2);
162 VERIFY( result1 == L"++++++++2,22507e-308" );
167 oss.setf(ios_base::right, ios_base::adjustfield);
168 oss.setf(ios_base::scientific, ios_base::floatfield);
169 np.put(oss.rdbuf(), oss, L'+', d2);
171 VERIFY( result2 == L"+++++++2,225074e-308" );
177 oss.setf(ios_base::right, ios_base::adjustfield);
178 oss.setf(ios_base::scientific, ios_base::floatfield);
179 oss.setf(ios_base::uppercase);
180 np.put(oss.rdbuf(), oss, L'+', d2);
182 VERIFY( result1 == L"+++2,2250738585E-308" );
187 np.put(oss.rdbuf(), oss, L'+', ld1);
189 VERIFY( result1 == L"1,7976931349E+308" );
194 oss.setf(ios_base::fixed, ios_base::floatfield);
195 np.put(oss.rdbuf(), oss, L'+', ld2);
197 VERIFY( result1 == L"0" );
202 np.put(oss.rdbuf(), oss, L'+', cv);
204 // No grouping characters.
205 VERIFY( !char_traits<wchar_t>::find(result1.c_str(),
206 numpunct_de.decimal_point(),
208 // Should contain an 'x'.
209 VERIFY( result1.find(L'x') == 1 );
211 #ifdef _GLIBCPP_USE_LONG_LONG
212 long long ll1 = 9223372036854775807;
213 long long ll2 = -9223372036854775807;
217 np.put(oss.rdbuf(), oss, '+', ll1);
219 VERIFY( result1 == L"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 wstring::iterator iter_type;
230 typedef char_traits<wchar_t> traits;
231 typedef num_put<wchar_t, 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 wstring str(L"1798 Lady Elgin");
236 const wstring str2(L"0 true 0xbffff74c Mary Nisbet");
237 const wstring x(15, L'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' ', l);
255 wstring sanity1(res.begin(), ret1);
256 VERIFY( res == L"1798xxxxxxxxxxx" );
257 VERIFY( sanity1 == L"1798" );
259 // 02 put(long double)
260 const long double ld = 1798;
262 iter_type ret2 = tp.put(res.begin(), oss, L' ', ld);
263 wstring sanity2(res.begin(), ret2);
264 VERIFY( res == L"1798xxxxxxxxxxx" );
265 VERIFY( sanity2 == L"1798" );
270 iter_type ret3 = tp.put(res.begin(), oss, L' ', b);
271 wstring sanity3(res.begin(), ret3);
272 VERIFY( res == L"1xxxxxxxxxxxxxx" );
273 VERIFY( sanity3 == L"1" );
277 oss.setf(ios_base::boolalpha);
278 iter_type ret4 = tp.put(res.begin(), oss, L' ', b);
279 wstring sanity4(res.begin(), ret4);
280 VERIFY( res == L"falsexxxxxxxxxx" );
281 VERIFY( sanity4 == L"false" );
285 const void* cv = &ld;
287 oss.setf(ios_base::fixed, ios_base::floatfield);
288 iter_type ret5 = tp.put(res.begin(), oss, L' ', cv);
289 wstring sanity5(res.begin(), ret5);
290 VERIFY( sanity5.size() );
291 VERIFY( sanity5[1] == L'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, 1);
316 #ifdef _GLIBCPP_USE_WCHAR_T
325 // Diana D. Brooks, former chief executive of Sotheby's
326 // art-thief extraordinaire