1 // 1999-11-15 Kevin Ediger <kediger@licor.com>
2 // test the floating point inserters (facet num_put)
4 // Copyright (C) 1999, 2002 Free Software Foundation, Inc.
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)
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,
22 #include <cstdio> // for sprintf
23 #include <cmath> // for abs
24 #include <cfloat> // for DBL_EPSILON
30 #include <testsuite_hooks.h>
35 # define TEST_NUMPUT_VERBOSE 1
57 #if _GLIBCPP_USE_WCHAR_T
58 const wchar_t* wresult
;
65 static _TestCase testcases
[] =
67 #if _GLIBCPP_USE_WCHAR_T
68 // standard output (no formatting applied) 1-4
69 { 1.2, 6,0,'.',' ', F
,F
,F
,F
,F
,F
,F
,F
, "1.2",L
"1.2" },
70 { 54, 6,0,'.',' ', F
,F
,F
,F
,F
,F
,F
,F
, "54",L
"54" },
71 { -.012, 6,0,'.',' ', F
,F
,F
,F
,F
,F
,F
,F
, "-0.012",L
"-0.012" },
72 { -.00000012, 6,0,'.',' ', F
,F
,F
,F
,F
,F
,F
,F
, "-1.2e-07",L
"-1.2e-07" },
74 // fixed formatting 5-11
75 { 10.2345, 0,0,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10",L
"10" },
76 { 10.2345, 0,0,'.',' ', T
,F
,F
,T
,F
,F
,F
,F
, "10.",L
"10." },
77 { 10.2345, 1,0,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2",L
"10.2" },
78 { 10.2345, 4,0,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2345",L
"10.2345" },
79 { 10.2345, 6,0,'.',' ', T
,F
,T
,F
,F
,F
,F
,F
, "+10.234500",L
"+10.234500" },
80 { -10.2345, 6,0,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "-10.234500",L
"-10.234500" },
81 { -10.2345, 6,0,',',' ', T
,F
,F
,F
,F
,F
,F
,F
, "-10,234500",L
"-10,234500" },
83 // fixed formatting with width 12-22
84 { 10.2345, 4,5,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2345",L
"10.2345" },
85 { 10.2345, 4,6,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2345",L
"10.2345" },
86 { 10.2345, 4,7,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2345",L
"10.2345" },
87 { 10.2345, 4,8,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, " 10.2345",L
" 10.2345" },
88 { 10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, " 10.2345",L
" 10.2345" },
89 { 10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,F
,T
,F
, "10.2345 ",L
"10.2345 " },
90 { 10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,F
,F
,T
, " 10.2345",L
" 10.2345" },
91 { 10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,T
,F
,F
, " 10.2345",L
" 10.2345" },
92 { -10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,T
,F
,F
, "- 10.2345",L
"- 10.2345" },
93 { -10.2345, 4,10,'.','A', T
,F
,F
,F
,F
,T
,F
,F
, "-AA10.2345",L
"-AA10.2345" },
94 { 10.2345, 4,10,'.','#', T
,F
,T
,F
,F
,T
,F
,F
, "+##10.2345",L
"+##10.2345" },
96 // scientific formatting 23-29
97 { 1.23e+12, 1,0,'.',' ', F
,T
,F
,F
,F
,F
,F
,F
, "1.2e+12",L
"1.2e+12" },
98 { 1.23e+12, 1,0,'.',' ', F
,T
,F
,F
,T
,F
,F
,F
, "1.2E+12",L
"1.2E+12" },
99 { 1.23e+12, 2,0,'.',' ', F
,T
,F
,F
,F
,F
,F
,F
, "1.23e+12",L
"1.23e+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
,T
,F
,F
,F
,F
,F
, "+1.230e+12",L
"+1.230e+12" },
102 { -1.23e-12, 3,0,'.',' ', F
,T
,F
,F
,F
,F
,F
,F
, "-1.230e-12",L
"-1.230e-12" },
103 { 1.23e+12, 3,0,',',' ', F
,T
,F
,F
,F
,F
,F
,F
, "1,230e+12",L
"1,230e+12" },
105 // standard output (no formatting applied)
106 { 1.2, 6,0,'.',' ', F
,F
,F
,F
,F
,F
,F
,F
, "1.2" },
107 { 54, 6,0,'.',' ', F
,F
,F
,F
,F
,F
,F
,F
, "54" },
108 { -.012, 6,0,'.',' ', F
,F
,F
,F
,F
,F
,F
,F
, "-0.012" },
109 { -.00000012, 6,0,'.',' ', F
,F
,F
,F
,F
,F
,F
,F
, "-1.2e-07" },
112 { 10.2345, 0,0,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10" },
113 { 10.2345, 0,0,'.',' ', T
,F
,F
,T
,F
,F
,F
,F
, "10." },
114 { 10.2345, 1,0,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2" },
115 { 10.2345, 4,0,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2345" },
116 { 10.2345, 6,0,'.',' ', T
,F
,T
,F
,F
,F
,F
,F
, "+10.234500" },
117 { -10.2345, 6,0,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "-10.234500" },
118 { -10.2345, 6,0,',',' ', T
,F
,F
,F
,F
,F
,F
,F
, "-10,234500" },
120 // fixed formatting with width
121 { 10.2345, 4,5,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2345" },
122 { 10.2345, 4,6,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2345" },
123 { 10.2345, 4,7,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, "10.2345" },
124 { 10.2345, 4,8,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, " 10.2345" },
125 { 10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,F
,F
,F
, " 10.2345" },
126 { 10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,F
,T
,F
, "10.2345 " },
127 { 10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,F
,F
,T
, " 10.2345" },
128 { 10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,T
,F
,F
, " 10.2345" },
129 { -10.2345, 4,10,'.',' ', T
,F
,F
,F
,F
,T
,F
,F
, "- 10.2345" },
130 { -10.2345, 4,10,'.','A', T
,F
,F
,F
,F
,T
,F
,F
, "-AA10.2345" },
131 { 10.2345, 4,10,'.','#', T
,F
,T
,F
,F
,T
,F
,F
, "+##10.2345" },
133 // scientific formatting
134 { 1.23e+12, 1,0,'.',' ', F
,T
,F
,F
,F
,F
,F
,F
, "1.2e+12" },
135 { 1.23e+12, 1,0,'.',' ', F
,T
,F
,F
,T
,F
,F
,F
, "1.2E+12" },
136 { 1.23e+12, 2,0,'.',' ', F
,T
,F
,F
,F
,F
,F
,F
, "1.23e+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
,T
,F
,F
,F
,F
,F
, "+1.230e+12" },
139 { -1.23e-12, 3,0,'.',' ', F
,T
,F
,F
,F
,F
,F
,F
, "-1.230e-12" },
140 { 1.23e+12, 3,0,',',' ', F
,T
,F
,F
,F
,F
,F
,F
, "1,230e+12" },
144 template<typename _CharT
>
145 class testpunct
: public numpunct
<_CharT
>
148 typedef _CharT char_type
;
149 const char_type dchar
;
152 testpunct(char_type decimal_char
) : numpunct
<_CharT
>(), dchar(decimal_char
)
157 do_decimal_point() const
161 do_thousands_sep() const
169 template<typename _CharT
>
170 void apply_formatting(const _TestCase
& tc
, basic_ostream
<_CharT
> & os
)
172 os
.precision(tc
.precision
);
174 os
.fill(static_cast<_CharT
>(tc
.fill
));
178 os
.setf(ios::scientific
);
180 os
.setf(ios::showpos
);
182 os
.setf(ios::showpoint
);
184 os
.setf(ios::uppercase
);
186 os
.setf(ios::internal
);
197 for (int j
=0; j
<sizeof(testcases
)/sizeof(testcases
[0]); j
++)
199 _TestCase
& tc
= testcases
[j
];
200 #ifdef TEST_NUMPUT_VERBOSE
201 cout
<< "expect: " << tc
.result
<< endl
;
203 // test double with char type
205 testpunct
<char>* __tp
= new testpunct
<char>(tc
.decimal
);
207 locale
__loc(os
.getloc(), __tp
);
209 apply_formatting(tc
, os
);
211 #ifdef TEST_NUMPUT_VERBOSE
212 cout
<< j
<< "result 1: " << os
.str() << endl
;
214 VERIFY( os
&& os
.str() == tc
.result
);
216 // test long double with char type
218 testpunct
<char>* __tp
= new testpunct
<char>(tc
.decimal
);
220 locale
__loc(os
.getloc(), __tp
);
222 apply_formatting(tc
, os
);
223 os
<< (long double)tc
.val
;
224 #ifdef TEST_NUMPUT_VERBOSE
225 cout
<< j
<< "result 2: " << os
.str() << endl
;
227 VERIFY( os
&& os
.str() == tc
.result
);
229 #if _GLIBCPP_USE_WCHAR_T
230 // test double with wchar_t type
232 testpunct
<wchar_t>* __tp
= new testpunct
<wchar_t>(tc
.decimal
);
234 locale
__loc(os
.getloc(), __tp
);
236 apply_formatting(tc
, os
);
238 VERIFY( os
&& os
.str() == tc
.wresult
);
240 // test long double with wchar_t type
242 testpunct
<wchar_t>* __tp
= new testpunct
<wchar_t>(tc
.decimal
);
244 locale
__loc(os
.getloc(), __tp
);
246 apply_formatting(tc
, os
);
247 os
<< (long double)tc
.val
;
248 VERIFY( os
&& os
.str() == tc
.wresult
);
260 // make sure we can output a very long float
261 long double val
= 1.2345678901234567890123456789e+1000L;
262 int prec
= numeric_limits
<long double>::digits10
;
266 os
.setf(ios::scientific
);
270 sprintf(largebuf
, "%.*Le", prec
, val
);
271 #ifdef TEST_NUMPUT_VERBOSE
272 cout
<< "expect: " << largebuf
<< endl
;
273 cout
<< "result: " << os
.str() << endl
;
275 VERIFY(os
&& os
.str() == largebuf
);
277 // Make sure we can output a long float in fixed format
278 // without seg-faulting (libstdc++/4402)
279 double val2
= 3.5e230
;
283 os2
.setf(ios::fixed
);
286 sprintf(largebuf
, "%.*f", 3, val2
);
287 #ifdef TEST_NUMPUT_VERBOSE
288 cout
<< "expect: " << largebuf
<< endl
;
289 cout
<< "result: " << os2
.str() << endl
;
291 VERIFY(os2
&& os2
.str() == largebuf
);
293 // Check it can be done in a locale with grouping on.
294 locale
loc2("de_DE");
296 os2
<< fixed
<< setprecision(3) << val2
<< endl
;
298 os2
<< fixed
<< setprecision(1) << val2
<< endl
;
312 if (numeric_limits
<T
>::digits
+ 1 == 16)
313 expect
= "177777 ffff";
314 else if (numeric_limits
<T
>::digits
+ 1 == 32)
315 expect
= "37777777777 ffffffff";
316 else if (numeric_limits
<T
>::digits
+ 1 == 64)
317 expect
= "1777777777777777777777 ffffffffffffffff";
319 expect
= "wow, you've got some big numbers here";
321 o
<< oct
<< n
<< ' ' << hex
<< n
;
322 VERIFY ( strbuf
.str() == expect
);
335 test
&= test03_check (s
);
336 test
&= test03_check (i
);
337 test
&= test03_check (l
);
346 stringbuf strbuf1
, strbuf2
;
347 ostream
o1(&strbuf1
), o2(&strbuf2
);
350 o1
<< hex
<< showbase
<< setw(6) << internal
<< 0xff;
351 VERIFY( strbuf1
.str() == "0x ff" );
353 // ... vs internal-adjusted const char*-type objects
354 o2
<< hex
<< showbase
<< setw(6) << internal
<< "0xff";
355 VERIFY( strbuf2
.str() == " 0xff" );
363 double pi
= 3.14159265358979323846;
367 string sval
= ostr
.str();
368 istringstream
istr (sval
);
371 VERIFY( abs(pi
-d
)/pi
< DBL_EPSILON
);
380 int prec
= numeric_limits
<double>::digits10
+ 2;
381 double oval
= numeric_limits
<double>::min();
384 ostr
.precision(prec
);
386 string sval
= ostr
.str();
387 istringstream
istr (sval
);
390 VERIFY( abs(oval
-ival
)/oval
< DBL_EPSILON
);
403 #ifdef TEST_NUMPUT_VERBOSE
404 cout
<< "Test passed!" << endl
;