2001-02-05 Benjamin Kosnik <bkoz@redhat.com>
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / ostream_inserter_arith.cc
blobf503ad37f61474682202effdb2f6344d8e0fc952
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 <debug_assert.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)
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
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" },
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 << "result: " << 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 << "result: " << 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 return 0;
278 int
279 test03()
281 short s = -1;
282 int i = -1;
283 long l = -1;
284 bool test = true;
286 const string str_blank;
287 string str_tmp;
288 stringbuf strbuf;
289 ostream o(&strbuf);
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" );
306 VERIFY(test);
308 return 0;
311 int
312 main()
314 test01();
315 test02();
316 #ifdef TEST_NUMPUT_VERBOSE
317 cout << "Test passed!" << endl;
318 #endif
319 return 0;