]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/22_locale/num_put_members_char.cc
dwarf2out.c (fde_table_in_use): Mark GTY.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 22_locale / num_put_members_char.cc
1 // 2001-11-19 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.2.1 num_put 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 void test01()
31 {
32 using namespace std;
33 typedef ostreambuf_iterator<char> iterator_type;
34
35 bool test = true;
36
37 // basic construction
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 );
46
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);
51
52 // sanity check the data is correct.
53 const string empty;
54 string result1;
55 string result2;
56 char c;
57
58 bool b1 = true;
59 bool b0 = false;
60 long l1 = 2147483647;
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;
69
70 // cache the num_put facet
71 ostringstream oss;
72 oss.imbue(loc_de);
73 const num_put<char>& np = use_facet<num_put<char> >(oss.getloc());
74
75 // bool, simple
76 iterator_type os_it00 = oss.rdbuf();
77 iterator_type os_it01 = np.put(os_it00, oss, '+', b1);
78 result1 = oss.str();
79 VERIFY( result1 == "1" );
80 // VERIFY( os_it00 != os_it01 );
81
82 oss.str(empty);
83 np.put(oss.rdbuf(), oss, '+', b0);
84 result2 = oss.str();
85 VERIFY( result2 == "0" );
86
87 // bool, more twisted examples
88 oss.imbue(loc_c);
89 oss.str(empty);
90 oss.width(20);
91 oss.setf(ios_base::right, ios_base::adjustfield);
92 np.put(oss.rdbuf(), oss, '+', b0);
93 result1 = oss.str();
94 VERIFY( result1 == "+++++++++++++++++++0" );
95
96 oss.str(empty);
97 oss.width(20);
98 oss.setf(ios_base::left, ios_base::adjustfield);
99 oss.setf(ios_base::boolalpha);
100 np.put(oss.rdbuf(), oss, '+', b1);
101 result2 = oss.str();
102 VERIFY( result2 == "true++++++++++++++++" );
103
104 // long, in a locale that expects grouping
105 oss.imbue(loc_hk);
106 oss.str(empty);
107 oss.clear();
108 np.put(oss.rdbuf(), oss, '+', l1);
109 result1 = oss.str();
110 VERIFY( result1 == "2,147,483,647" );
111
112 oss.str(empty);
113 oss.clear();
114 oss.width(20);
115 oss.setf(ios_base::left, ios_base::adjustfield);
116 np.put(oss.rdbuf(), oss, '+', l2);
117 result1 = oss.str();
118 VERIFY( result1 == "-2,147,483,647++++++" );
119
120 // unsigned long, in a locale that does not group
121 oss.imbue(loc_c);
122 oss.str(empty);
123 oss.clear();
124 np.put(oss.rdbuf(), oss, '+', ul1);
125 result1 = oss.str();
126 VERIFY( result1 == "1294967294" );
127
128 oss.str(empty);
129 oss.clear();
130 oss.width(20);
131 oss.setf(ios_base::left, ios_base::adjustfield);
132 np.put(oss.rdbuf(), oss, '+', ul2);
133 result1 = oss.str();
134 VERIFY( result1 == "0+++++++++++++++++++" );
135
136 // ... and one that does
137 oss.imbue(loc_de);
138 oss.str(empty);
139 oss.clear();
140 oss.width(20);
141 oss.setf(ios_base::left, ios_base::adjustfield);
142 np.put(oss.rdbuf(), oss, '+', ul1);
143 result1 = oss.str();
144 VERIFY( result1 == "1.294.967.294+++++++" );
145
146 // double
147 oss.str(empty);
148 oss.clear();
149 oss.width(20);
150 oss.setf(ios_base::left, ios_base::adjustfield);
151 np.put(oss.rdbuf(), oss, '+', d1);
152 result1 = oss.str();
153 VERIFY( result1 == "1,79769e+308++++++++" );
154
155 oss.str(empty);
156 oss.clear();
157 oss.width(20);
158 oss.setf(ios_base::right, ios_base::adjustfield);
159 np.put(oss.rdbuf(), oss, '+', d2);
160 result1 = oss.str();
161 VERIFY( result1 == "++++++++2,22507e-308" );
162
163 oss.str(empty);
164 oss.clear();
165 oss.width(20);
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);
169 result2 = oss.str();
170 VERIFY( result2 == "+++++++2,225074e-308" );
171
172 oss.str(empty);
173 oss.clear();
174 oss.width(20);
175 oss.precision(10);
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);
180 result1 = oss.str();
181 VERIFY( result1 == "+++2,2250738585E-308" );
182
183 // long double
184 oss.str(empty);
185 oss.clear();
186 np.put(oss.rdbuf(), oss, '+', ld1);
187 result1 = oss.str();
188 VERIFY( result1 == "1,7976931349E+308" );
189
190 oss.str(empty);
191 oss.clear();
192 oss.precision(0);
193 oss.setf(ios_base::fixed, ios_base::floatfield);
194 np.put(oss.rdbuf(), oss, '+', ld2);
195 result1 = oss.str();
196 VERIFY( result1 == "0" );
197
198 // const void
199 oss.str(empty);
200 oss.clear();
201 np.put(oss.rdbuf(), oss, '+', cv);
202 result1 = oss.str();
203 // No grouping characters.
204 VERIFY( !char_traits<char>::find(result1.c_str(),
205 result1.size(),
206 numpunct_de.decimal_point()) );
207 // Should contain an 'x'.
208 VERIFY( result1.find('x') == 1 );
209
210 #ifdef _GLIBCPP_USE_LONG_LONG
211 long long ll1 = 9223372036854775807LL;
212 long long ll2 = -9223372036854775807LL;
213
214 oss.str(empty);
215 oss.clear();
216 np.put(oss.rdbuf(), oss, '+', ll1);
217 result1 = oss.str();
218 VERIFY( result1 == "9.223.372.036.854.775.807" );
219 #endif
220 }
221
222 void test02()
223 {
224 using namespace std;
225 bool test = true;
226
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!
238 string res;
239
240 ostringstream oss;
241 oss.imbue(locale(loc_c, new num_put_type));
242
243 // Iterator advanced, state, output.
244 const num_put_type& tp = use_facet<num_put_type>(oss.getloc());
245
246 // 01 put(long)
247 // 02 put(long double)
248 // 03 put(bool)
249 // 04 put(void*)
250
251 // 01 put(long)
252 const long l = 1798;
253 res = x;
254 iter_type ret1 = tp.put(res.begin(), oss, ' ', l);
255 string sanity1(res.begin(), ret1);
256 VERIFY( res == "1798xxxxxxxxxxx" );
257 VERIFY( sanity1 == "1798" );
258
259 // 02 put(long double)
260 const long double ld = 1798.0;
261 res = x;
262 iter_type ret2 = tp.put(res.begin(), oss, ' ', ld);
263 string sanity2(res.begin(), ret2);
264 VERIFY( res == "1798xxxxxxxxxxx" );
265 VERIFY( sanity2 == "1798" );
266
267 // 03 put(bool)
268 bool b = 1;
269 res = x;
270 iter_type ret3 = tp.put(res.begin(), oss, ' ', b);
271 string sanity3(res.begin(), ret3);
272 VERIFY( res == "1xxxxxxxxxxxxxx" );
273 VERIFY( sanity3 == "1" );
274
275 b = 0;
276 res = x;
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" );
282
283 // 04 put(void*)
284 oss.clear();
285 const void* cv = &ld;
286 res = x;
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' );
292 }
293
294 // libstdc++/5280
295 void test03()
296 {
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);
301
302 // Set LANG environment variable to de_DE.
303 const char* oldLANG = getenv("LANG");
304 if (!setenv("LANG", "de_DE", 1))
305 {
306 test01();
307 test02();
308 setenv("LANG", oldLANG ? oldLANG : "", 1);
309 }
310 #endif
311 }
312
313 // http://gcc.gnu.org/ml/libstdc++/2002-05/msg00038.html
314 void test04()
315 {
316 bool test = true;
317
318 const char* tentLANG = std::setlocale(LC_ALL, "ja_JP.eucjp");
319 if (tentLANG != NULL)
320 {
321 std::string preLANG = tentLANG;
322 test01();
323 test02();
324 std::string postLANG = std::setlocale(LC_ALL, NULL);
325 VERIFY( preLANG == postLANG );
326 }
327 }
328
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)
332 void test05()
333 {
334 using namespace std;
335 bool test = true;
336
337 // A locale that expects grouping.
338 locale loc_de("de_DE");
339
340 const string empty;
341 string result;
342
343 ostringstream oss;
344 oss.imbue(loc_de);
345 const num_put<char>& np = use_facet<num_put<char> >(oss.getloc());
346
347 long l = 0;
348
349 oss.str(empty);
350 oss.clear();
351 oss.setf(ios::showbase);
352 oss.setf(ios::hex, ios::basefield);
353 np.put(oss.rdbuf(), oss, '+', l);
354 result = oss.str();
355 VERIFY( result == "0" );
356
357 oss.str(empty);
358 oss.clear();
359 oss.setf(ios::showbase);
360 oss.setf(ios::oct, ios::basefield);
361 np.put(oss.rdbuf(), oss, '+', l);
362 result = oss.str();
363 VERIFY( result == "0" );
364 }
365
366 int main()
367 {
368 test01();
369 test02();
370 test03();
371 test04();
372 test05();
373 return 0;
374 }
375
376