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.
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
28 #include <debug_assert.h>
33 # define TEST_NUMPUT_VERBOSE 1
55 #if _GLIBCPP_USE_WCHAR_T
56 const wchar_t* wresult
;
63 static _TestCase testcases
[] =
65 #if _GLIBCPP_USE_WCHAR_T
66 // standard output (no formatting applied)
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" },
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
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
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" },
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" },
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" },
142 template<typename _CharT
>
143 class testpunct
: public numpunct
<_CharT
>
146 typedef _CharT char_type
;
147 const char_type dchar
;
150 testpunct(char_type decimal_char
) : numpunct
<_CharT
>(), dchar(decimal_char
)
155 do_decimal_point() const
159 do_thousands_sep() const
167 template<typename _CharT
>
168 void apply_formatting(const _TestCase
& tc
, basic_ostream
<_CharT
> & os
)
170 os
.precision(tc
.precision
);
172 os
.fill(static_cast<_CharT
>(tc
.fill
));
176 os
.setf(ios::scientific
);
178 os
.setf(ios::showpos
);
180 os
.setf(ios::showpoint
);
182 os
.setf(ios::uppercase
);
184 os
.setf(ios::internal
);
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
;
201 // test double with char type
203 testpunct
<char>* __tp
= new testpunct
<char>(tc
.decimal
);
205 locale
__loc(os
.getloc(), __tp
);
207 apply_formatting(tc
, os
);
209 #ifdef TEST_NUMPUT_VERBOSE
210 cout
<< "result: " << os
.str() << endl
;
212 VERIFY( os
&& os
.str() == tc
.result
);
214 // test long double with char type
216 testpunct
<char>* __tp
= new testpunct
<char>(tc
.decimal
);
218 locale
__loc(os
.getloc(), __tp
);
220 apply_formatting(tc
, os
);
221 os
<< (long double)tc
.val
;
222 #ifdef TEST_NUMPUT_VERBOSE
223 cout
<< "result: " << os
.str() << endl
;
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
);
232 locale
__loc(os
.getloc(), __tp
);
234 apply_formatting(tc
, os
);
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
);
242 locale
__loc(os
.getloc(), __tp
);
244 apply_formatting(tc
, os
);
245 os
<< (long double)tc
.val
;
246 VERIFY( os
&& os
.str() == tc
.wresult
);
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
;
264 os
.setf(ios::scientific
);
268 sprintf(largebuf
, "%.*Le", prec
, val
);
269 #ifdef TEST_NUMPUT_VERBOSE
270 cout
<< "expect: " << largebuf
<< endl
;
271 cout
<< "result: " << os
.str() << endl
;
273 VERIFY(os
&& os
.str() == largebuf
);
286 const string str_blank
;
291 o
<< oct
<< s
<< ' ' << hex
<< s
;
292 VERIFY( strbuf
.str() == "177777 ffff" ); // Assuming 2byte-shorts
293 strbuf
.str(str_blank
);
295 o
<< oct
<< i
<< ' ' << hex
<< i
;
296 VERIFY( strbuf
.str() == "37777777777 ffffffff" );
297 strbuf
.str(str_blank
);
299 o
<< oct
<< l
<< ' ' << hex
<< l
;
300 VERIFY( strbuf
.str() == "37777777777 ffffffff" );
301 strbuf
.str(str_blank
);
303 o
<< showpos
<< hex
<< showbase
<< 11;
304 VERIFY( strbuf
.str() == "0xb" );
316 #ifdef TEST_NUMPUT_VERBOSE
317 cout
<< "Test passed!" << endl
;