2002-01-28 Paolo Carlini <pcarlini@unitus.it>
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / ostream_inserter_arith.cc
blobbf93950c8c4095e5a56663f956b0312f5105fc61
1 // 1999-11-15 Kevin Ediger <kediger@licor.com>
2 // test the floating point inserters (facet num_put)
4 // Copyright (C) 1999 Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
22 #include <cstdio> // for sprintf
23 #include <iostream>
24 #include <iomanip>
25 #include <locale>
26 #include <sstream>
27 #include <limits>
28 #include <testsuite_hooks.h>
30 using namespace std;
32 #ifndef DEBUG_ASSERT
33 # define TEST_NUMPUT_VERBOSE 1
34 #endif
36 struct _TestCase
38 double val;
40 int precision;
41 int width;
42 char decimal;
43 char fill;
45 bool fixed;
46 bool scientific;
47 bool showpos;
48 bool showpoint;
49 bool uppercase;
50 bool internal;
51 bool left;
52 bool right;
54 const char* result;
55 #if _GLIBCPP_USE_WCHAR_T
56 const wchar_t* wresult;
57 #endif
60 static bool T=true;
61 static bool F=false;
63 static _TestCase testcases[] =
65 #if _GLIBCPP_USE_WCHAR_T
66 // standard output (no formatting applied) 1-4
67 { 1.2, 6,0,'.',' ', F,F,F,F,F,F,F,F, "1.2",L"1.2" },
68 { 54, 6,0,'.',' ', F,F,F,F,F,F,F,F, "54",L"54" },
69 { -.012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-0.012",L"-0.012" },
70 { -.00000012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-1.2e-07",L"-1.2e-07" },
72 // fixed formatting 5-11
73 { 10.2345, 0,0,'.',' ', T,F,F,F,F,F,F,F, "10",L"10" },
74 { 10.2345, 0,0,'.',' ', T,F,F,T,F,F,F,F, "10.",L"10." },
75 { 10.2345, 1,0,'.',' ', T,F,F,F,F,F,F,F, "10.2",L"10.2" },
76 { 10.2345, 4,0,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
77 { 10.2345, 6,0,'.',' ', T,F,T,F,F,F,F,F, "+10.234500",L"+10.234500" },
78 { -10.2345, 6,0,'.',' ', T,F,F,F,F,F,F,F, "-10.234500",L"-10.234500" },
79 { -10.2345, 6,0,',',' ', T,F,F,F,F,F,F,F, "-10,234500",L"-10,234500" },
81 // fixed formatting with width 12-22
82 { 10.2345, 4,5,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
83 { 10.2345, 4,6,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
84 { 10.2345, 4,7,'.',' ', T,F,F,F,F,F,F,F, "10.2345",L"10.2345" },
85 { 10.2345, 4,8,'.',' ', T,F,F,F,F,F,F,F, " 10.2345",L" 10.2345" },
86 { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,F, " 10.2345",L" 10.2345" },
87 { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,T,F, "10.2345 ",L"10.2345 " },
88 { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,T, " 10.2345",L" 10.2345" },
89 { 10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, " 10.2345",L" 10.2345" },
90 { -10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, "- 10.2345",L"- 10.2345" },
91 { -10.2345, 4,10,'.','A', T,F,F,F,F,T,F,F, "-AA10.2345",L"-AA10.2345" },
92 { 10.2345, 4,10,'.','#', T,F,T,F,F,T,F,F, "+##10.2345",L"+##10.2345" },
94 // scientific formatting 23-29
95 { 1.23e+12, 1,0,'.',' ', F,T,F,F,F,F,F,F, "1.2e+12",L"1.2e+12" },
96 { 1.23e+12, 1,0,'.',' ', F,T,F,F,T,F,F,F, "1.2E+12",L"1.2E+12" },
97 { 1.23e+12, 2,0,'.',' ', F,T,F,F,F,F,F,F, "1.23e+12",L"1.23e+12" },
98 { 1.23e+12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "1.230e+12",L"1.230e+12" },
99 { 1.23e+12, 3,0,'.',' ', F,T,T,F,F,F,F,F, "+1.230e+12",L"+1.230e+12" },
100 { -1.23e-12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "-1.230e-12",L"-1.230e-12" },
101 { 1.23e+12, 3,0,',',' ', F,T,F,F,F,F,F,F, "1,230e+12",L"1,230e+12" },
102 #else
103 // standard output (no formatting applied)
104 { 1.2, 6,0,'.',' ', F,F,F,F,F,F,F,F, "1.2" },
105 { 54, 6,0,'.',' ', F,F,F,F,F,F,F,F, "54" },
106 { -.012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-0.012" },
107 { -.00000012, 6,0,'.',' ', F,F,F,F,F,F,F,F, "-1.2e-07" },
109 // fixed formatting
110 { 10.2345, 0,0,'.',' ', T,F,F,F,F,F,F,F, "10" },
111 { 10.2345, 0,0,'.',' ', T,F,F,T,F,F,F,F, "10." },
112 { 10.2345, 1,0,'.',' ', T,F,F,F,F,F,F,F, "10.2" },
113 { 10.2345, 4,0,'.',' ', T,F,F,F,F,F,F,F, "10.2345" },
114 { 10.2345, 6,0,'.',' ', T,F,T,F,F,F,F,F, "+10.234500" },
115 { -10.2345, 6,0,'.',' ', T,F,F,F,F,F,F,F, "-10.234500" },
116 { -10.2345, 6,0,',',' ', T,F,F,F,F,F,F,F, "-10,234500" },
118 // fixed formatting with width
119 { 10.2345, 4,5,'.',' ', T,F,F,F,F,F,F,F, "10.2345" },
120 { 10.2345, 4,6,'.',' ', T,F,F,F,F,F,F,F, "10.2345" },
121 { 10.2345, 4,7,'.',' ', T,F,F,F,F,F,F,F, "10.2345" },
122 { 10.2345, 4,8,'.',' ', T,F,F,F,F,F,F,F, " 10.2345" },
123 { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,F, " 10.2345" },
124 { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,T,F, "10.2345 " },
125 { 10.2345, 4,10,'.',' ', T,F,F,F,F,F,F,T, " 10.2345" },
126 { 10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, " 10.2345" },
127 { -10.2345, 4,10,'.',' ', T,F,F,F,F,T,F,F, "- 10.2345" },
128 { -10.2345, 4,10,'.','A', T,F,F,F,F,T,F,F, "-AA10.2345" },
129 { 10.2345, 4,10,'.','#', T,F,T,F,F,T,F,F, "+##10.2345" },
131 // scientific formatting
132 { 1.23e+12, 1,0,'.',' ', F,T,F,F,F,F,F,F, "1.2e+12" },
133 { 1.23e+12, 1,0,'.',' ', F,T,F,F,T,F,F,F, "1.2E+12" },
134 { 1.23e+12, 2,0,'.',' ', F,T,F,F,F,F,F,F, "1.23e+12" },
135 { 1.23e+12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "1.230e+12" },
136 { 1.23e+12, 3,0,'.',' ', F,T,T,F,F,F,F,F, "+1.230e+12" },
137 { -1.23e-12, 3,0,'.',' ', F,T,F,F,F,F,F,F, "-1.230e-12" },
138 { 1.23e+12, 3,0,',',' ', F,T,F,F,F,F,F,F, "1,230e+12" },
139 #endif
142 template<typename _CharT>
143 class testpunct : public numpunct<_CharT>
145 public:
146 typedef _CharT char_type;
147 const char_type dchar;
149 explicit
150 testpunct(char_type decimal_char) : numpunct<_CharT>(), dchar(decimal_char)
153 protected:
154 char_type
155 do_decimal_point() const
156 { return dchar; }
158 char_type
159 do_thousands_sep() const
160 { return ','; }
162 string
163 do_grouping() const
164 { return string(); }
167 template<typename _CharT>
168 void apply_formatting(const _TestCase & tc, basic_ostream<_CharT> & os)
170 os.precision(tc.precision);
171 os.width(tc.width);
172 os.fill(static_cast<_CharT>(tc.fill));
173 if (tc.fixed)
174 os.setf(ios::fixed);
175 if (tc.scientific)
176 os.setf(ios::scientific);
177 if (tc.showpos)
178 os.setf(ios::showpos);
179 if (tc.showpoint)
180 os.setf(ios::showpoint);
181 if (tc.uppercase)
182 os.setf(ios::uppercase);
183 if (tc.internal)
184 os.setf(ios::internal);
185 if (tc.left)
186 os.setf(ios::left);
187 if (tc.right)
188 os.setf(ios::right);
192 test01()
194 bool test = true;
195 for (int j=0; j<sizeof(testcases)/sizeof(testcases[0]); j++)
197 _TestCase & tc = testcases[j];
198 #ifdef TEST_NUMPUT_VERBOSE
199 cout << "expect: " << tc.result << endl;
200 #endif
201 // test double with char type
203 testpunct<char>* __tp = new testpunct<char>(tc.decimal);
204 ostringstream os;
205 locale __loc(os.getloc(), __tp);
206 os.imbue(__loc);
207 apply_formatting(tc, os);
208 os << tc.val;
209 #ifdef TEST_NUMPUT_VERBOSE
210 cout << j << "result 1: " << os.str() << endl;
211 #endif
212 VERIFY( os && os.str() == tc.result );
214 // test long double with char type
216 testpunct<char>* __tp = new testpunct<char>(tc.decimal);
217 ostringstream os;
218 locale __loc(os.getloc(), __tp);
219 os.imbue(__loc);
220 apply_formatting(tc, os);
221 os << (long double)tc.val;
222 #ifdef TEST_NUMPUT_VERBOSE
223 cout << j << "result 2: " << os.str() << endl;
224 #endif
225 VERIFY( os && os.str() == tc.result );
227 #if _GLIBCPP_USE_WCHAR_T
228 // test double with wchar_t type
230 testpunct<wchar_t>* __tp = new testpunct<wchar_t>(tc.decimal);
231 wostringstream os;
232 locale __loc(os.getloc(), __tp);
233 os.imbue(__loc);
234 apply_formatting(tc, os);
235 os << tc.val;
236 VERIFY( os && os.str() == tc.wresult );
238 // test long double with wchar_t type
240 testpunct<wchar_t>* __tp = new testpunct<wchar_t>(tc.decimal);
241 wostringstream os;
242 locale __loc(os.getloc(), __tp);
243 os.imbue(__loc);
244 apply_formatting(tc, os);
245 os << (long double)tc.val;
246 VERIFY( os && os.str() == tc.wresult );
248 #endif
251 return 0;
255 test02()
257 bool test = true;
258 // make sure we can output a very long float
259 long double val = 1.2345678901234567890123456789e+1000L;
260 int prec = numeric_limits<long double>::digits10;
262 ostringstream os;
263 os.precision(prec);
264 os.setf(ios::scientific);
265 os << val;
267 char largebuf[512];
268 sprintf(largebuf, "%.*Le", prec, val);
269 #ifdef TEST_NUMPUT_VERBOSE
270 cout << "expect: " << largebuf << endl;
271 cout << "result: " << os.str() << endl;
272 #endif
273 VERIFY(os && os.str() == largebuf);
275 // Make sure we can output a long float in fixed format
276 // without seg-faulting (libstdc++/4402)
277 double val2 = 3.5e230;
279 ostringstream os2;
280 os2.precision(3);
281 os2.setf(ios::fixed);
282 os2 << val2;
284 sprintf(largebuf, "%.*f", 3, val2);
285 #ifdef TEST_NUMPUT_VERBOSE
286 cout << "expect: " << largebuf << endl;
287 cout << "result: " << os2.str() << endl;
288 #endif
289 VERIFY(os2 && os2.str() == largebuf);
291 // Check it can be done in a locale with grouping on.
292 locale loc2("de_DE");
293 os2.imbue(loc2);
294 os2 << fixed << setprecision(3) << val2 << endl;
295 os2 << endl;
296 os2 << fixed << setprecision(1) << val2 << endl;
298 return 0;
301 int
302 test03()
304 short s = -1;
305 int i = -1;
306 long l = -1;
307 bool test = true;
309 const string str_blank;
310 string str_tmp;
311 stringbuf strbuf;
312 ostream o(&strbuf);
314 o << oct << s << ' ' << hex << s;
315 if (sizeof(short) == 2)
316 VERIFY( strbuf.str() == "177777 ffff" );
317 else // sizeof(short) == 4
318 VERIFY( strbuf.str() == "37777777777 ffffffff" );
319 strbuf.str(str_blank);
321 o << oct << i << ' ' << hex << i;
322 if (sizeof(int) == 2)
323 VERIFY( strbuf.str() == "177777 ffff" );
324 else if (sizeof(int) == 4)
325 VERIFY( strbuf.str() == "37777777777 ffffffff" );
326 else // sizeof(int) == 8
327 VERIFY( strbuf.str() == "1777777777777777777777 "
328 "ffffffffffffffff" );
329 strbuf.str(str_blank);
331 o << oct << l << ' ' << hex << l;
332 if (sizeof(long) == 4)
333 VERIFY( strbuf.str() == "37777777777 ffffffff" );
334 else // sizeof(long) == 8
335 VERIFY( strbuf.str() == "1777777777777777777777 "
336 "ffffffffffffffff" );
337 strbuf.str(str_blank);
339 o << showpos << hex << showbase << 11;
340 VERIFY( strbuf.str() == "0xb" );
342 VERIFY(test);
344 return 0;
347 // libstdc++/3655
349 test04()
351 stringbuf strbuf1, strbuf2;
352 ostream o1(&strbuf1), o2(&strbuf2);
353 o1 << hex << showbase << setw(6) << internal << 0xff;
354 VERIFY( strbuf1.str() == "0x ff" );
356 // ... vs internal-adjusted const char*-type objects
357 o2 << hex << showbase << setw(6) << internal << "0xff";
358 VERIFY( strbuf2.str() == " 0xff" );
359 return 0;
362 int
363 main()
365 test01();
366 test02();
367 test03();
368 test04();
369 #ifdef TEST_NUMPUT_VERBOSE
370 cout << "Test passed!" << endl;
371 #endif
372 return 0;