]>
Commit | Line | Data |
---|---|---|
b2dad0e3 BK |
1 | // 1999-11-15 Kevin Ediger <kediger@licor.com> |
2 | // test the floating point inserters (facet num_put) | |
3 | ||
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. | |
11 | ||
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. | |
16 | ||
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. | |
21 | ||
4bc95009 | 22 | #include <cstdio> // for sprintf |
b2dad0e3 BK |
23 | #include <iostream> |
24 | #include <iomanip> | |
25 | #include <locale> | |
26 | #include <sstream> | |
27 | #include <limits> | |
fe413112 | 28 | #include <testsuite_hooks.h> |
b2dad0e3 BK |
29 | |
30 | using namespace std; | |
31 | ||
644638bc BK |
32 | #ifndef DEBUG_ASSERT |
33 | # define TEST_NUMPUT_VERBOSE 1 | |
34 | #endif | |
b2dad0e3 BK |
35 | |
36 | struct _TestCase | |
37 | { | |
38 | double val; | |
39 | ||
40 | int precision; | |
41 | int width; | |
42 | char decimal; | |
43 | char fill; | |
44 | ||
45 | bool fixed; | |
46 | bool scientific; | |
47 | bool showpos; | |
48 | bool showpoint; | |
49 | bool uppercase; | |
50 | bool internal; | |
51 | bool left; | |
52 | bool right; | |
53 | ||
54 | const char* result; | |
55 | #if _GLIBCPP_USE_WCHAR_T | |
56 | const wchar_t* wresult; | |
57 | #endif | |
58 | }; | |
59 | ||
60 | static bool T=true; | |
61 | static bool F=false; | |
62 | ||
63 | static _TestCase testcases[] = | |
64 | { | |
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" }, | |
71 | ||
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" }, | |
80 | ||
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" }, | |
93 | ||
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" }, | |
108 | ||
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" }, | |
117 | ||
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" }, | |
130 | ||
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 | |
140 | }; | |
141 | ||
142 | template<typename _CharT> | |
143 | class testpunct : public numpunct<_CharT> | |
144 | { | |
145 | public: | |
146 | typedef _CharT char_type; | |
97e0a05a | 147 | const char_type dchar; |
b2dad0e3 BK |
148 | |
149 | explicit | |
97e0a05a AW |
150 | testpunct(char_type decimal_char) : numpunct<_CharT>(), dchar(decimal_char) |
151 | { } | |
152 | ||
153 | protected: | |
154 | char_type | |
155 | do_decimal_point() const | |
156 | { return dchar; } | |
157 | ||
158 | char_type | |
159 | do_thousands_sep() const | |
160 | { return ','; } | |
161 | ||
162 | string | |
163 | do_grouping() const | |
164 | { return string(); } | |
b2dad0e3 BK |
165 | }; |
166 | ||
167 | template<typename _CharT> | |
168 | void apply_formatting(const _TestCase & tc, basic_ostream<_CharT> & os) | |
169 | { | |
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); | |
189 | } | |
190 | ||
aa1b2f7d BV |
191 | int |
192 | test01() | |
b2dad0e3 | 193 | { |
aa1b2f7d | 194 | bool test = true; |
b2dad0e3 BK |
195 | for (int j=0; j<sizeof(testcases)/sizeof(testcases[0]); j++) |
196 | { | |
197 | _TestCase & tc = testcases[j]; | |
644638bc | 198 | #ifdef TEST_NUMPUT_VERBOSE |
b2dad0e3 BK |
199 | cout << "expect: " << tc.result << endl; |
200 | #endif | |
201 | // test double with char type | |
202 | { | |
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; | |
644638bc | 209 | #ifdef TEST_NUMPUT_VERBOSE |
b2dad0e3 BK |
210 | cout << "result: " << os.str() << endl; |
211 | #endif | |
aa1b2f7d | 212 | VERIFY( os && os.str() == tc.result ); |
b2dad0e3 BK |
213 | } |
214 | // test long double with char type | |
215 | { | |
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; | |
644638bc | 222 | #ifdef TEST_NUMPUT_VERBOSE |
b2dad0e3 BK |
223 | cout << "result: " << os.str() << endl; |
224 | #endif | |
aa1b2f7d | 225 | VERIFY( os && os.str() == tc.result ); |
b2dad0e3 BK |
226 | } |
227 | #if _GLIBCPP_USE_WCHAR_T | |
228 | // test double with wchar_t type | |
229 | { | |
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; | |
aa1b2f7d | 236 | VERIFY( os && os.str() == tc.wresult ); |
b2dad0e3 BK |
237 | } |
238 | // test long double with wchar_t type | |
239 | { | |
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; | |
aa1b2f7d | 246 | VERIFY( os && os.str() == tc.wresult ); |
b2dad0e3 BK |
247 | } |
248 | #endif | |
249 | } | |
aa1b2f7d BV |
250 | |
251 | return 0; | |
b2dad0e3 BK |
252 | } |
253 | ||
aa1b2f7d BV |
254 | int |
255 | test02() | |
b2dad0e3 | 256 | { |
aa1b2f7d | 257 | bool test = true; |
b2dad0e3 BK |
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; | |
261 | ||
262 | ostringstream os; | |
263 | os.precision(prec); | |
264 | os.setf(ios::scientific); | |
265 | os << val; | |
266 | ||
267 | char largebuf[512]; | |
268 | sprintf(largebuf, "%.*Le", prec, val); | |
644638bc | 269 | #ifdef TEST_NUMPUT_VERBOSE |
b2dad0e3 BK |
270 | cout << "expect: " << largebuf << endl; |
271 | cout << "result: " << os.str() << endl; | |
272 | #endif | |
aa1b2f7d BV |
273 | VERIFY(os && os.str() == largebuf); |
274 | ||
275 | return 0; | |
b2dad0e3 BK |
276 | } |
277 | ||
aa1b2f7d BV |
278 | int |
279 | test03() | |
b2dad0e3 BK |
280 | { |
281 | short s = -1; | |
282 | int i = -1; | |
283 | long l = -1; | |
284 | bool test = true; | |
285 | ||
286 | const string str_blank; | |
287 | string str_tmp; | |
288 | stringbuf strbuf; | |
289 | ostream o(&strbuf); | |
290 | ||
291 | o << oct << s << ' ' << hex << s; | |
aa1b2f7d | 292 | VERIFY( strbuf.str() == "177777 ffff" ); // Assuming 2byte-shorts |
b2dad0e3 BK |
293 | strbuf.str(str_blank); |
294 | ||
295 | o << oct << i << ' ' << hex << i; | |
aa1b2f7d | 296 | VERIFY( strbuf.str() == "37777777777 ffffffff" ); |
b2dad0e3 BK |
297 | strbuf.str(str_blank); |
298 | ||
299 | o << oct << l << ' ' << hex << l; | |
aa1b2f7d | 300 | VERIFY( strbuf.str() == "37777777777 ffffffff" ); |
b2dad0e3 BK |
301 | strbuf.str(str_blank); |
302 | ||
303 | o << showpos << hex << showbase << 11; | |
aa1b2f7d | 304 | VERIFY( strbuf.str() == "0xb" ); |
b2dad0e3 | 305 | |
aa1b2f7d BV |
306 | VERIFY(test); |
307 | ||
308 | return 0; | |
b2dad0e3 BK |
309 | } |
310 | ||
aa1b2f7d BV |
311 | int |
312 | main() | |
b2dad0e3 BK |
313 | { |
314 | test01(); | |
315 | test02(); | |
644638bc | 316 | #ifdef TEST_NUMPUT_VERBOSE |
b2dad0e3 BK |
317 | cout << "Test passed!" << endl; |
318 | #endif | |
319 | return 0; | |
320 | } |