1 //===----------------------------------------------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // <locale>
11
12 // class money_put<charT, OutputIterator>
13
14 // iter_type put(iter_type s, bool intl, ios_base& f, char_type fill,
15 // long double units) const;
16
17 #include <locale>
18 #include <ios>
19 #include <streambuf>
20 #include <cassert>
21 #include "test_iterators.h"
22
23 #include "platform_support.h" // locale name macros
24
25 typedef std::money_put<char, output_iterator<char*> > Fn;
26
27 class my_facet
28 : public Fn
29 {
30 public:
my_facet(std::size_t refs=0)31 explicit my_facet(std::size_t refs = 0)
32 : Fn(refs) {}
33 };
34
35 typedef std::money_put<wchar_t, output_iterator<wchar_t*> > Fw;
36
37 class my_facetw
38 : public Fw
39 {
40 public:
my_facetw(std::size_t refs=0)41 explicit my_facetw(std::size_t refs = 0)
42 : Fw(refs) {}
43 };
44
main()45 int main()
46 {
47 std::ios ios(0);
48 std::string loc_name(LOCALE_zh_CN_UTF_8);
49 ios.imbue(std::locale(ios.getloc(),
50 new std::moneypunct_byname<char, false>(loc_name)));
51 ios.imbue(std::locale(ios.getloc(),
52 new std::moneypunct_byname<char, true>(loc_name)));
53 ios.imbue(std::locale(ios.getloc(),
54 new std::moneypunct_byname<wchar_t, false>(loc_name)));
55 ios.imbue(std::locale(ios.getloc(),
56 new std::moneypunct_byname<wchar_t, true>(loc_name)));
57 {
58 const my_facet f(1);
59 // char, national
60 { // zero
61 long double v = 0;
62 char str[100];
63 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
64 false, ios, '*', v);
65 std::string ex(str, iter.base());
66 assert(ex == "0.00");
67 }
68 { // negative one
69 long double v = -1;
70 char str[100];
71 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
72 false, ios, '*', v);
73 std::string ex(str, iter.base());
74 assert(ex == "-0.01");
75 }
76 { // positive
77 long double v = 123456789;
78 char str[100];
79 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
80 false, ios, '*', v);
81 std::string ex(str, iter.base());
82 assert(ex == "1,234,567.89");
83 }
84 { // negative
85 long double v = -123456789;
86 char str[100];
87 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
88 false, ios, '*', v);
89 std::string ex(str, iter.base());
90 assert(ex == "-1,234,567.89");
91 }
92 { // zero, showbase
93 long double v = 0;
94 showbase(ios);
95 char str[100];
96 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
97 false, ios, '*', v);
98 std::string ex(str, iter.base());
99 assert(ex == "\xEF\xBF\xA5""0.00");
100 }
101 { // negative one, showbase
102 long double v = -1;
103 showbase(ios);
104 char str[100];
105 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
106 false, ios, '*', v);
107 std::string ex(str, iter.base());
108 assert(ex == "\xEF\xBF\xA5""-0.01");
109 }
110 { // positive, showbase
111 long double v = 123456789;
112 showbase(ios);
113 char str[100];
114 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
115 false, ios, '*', v);
116 std::string ex(str, iter.base());
117 assert(ex == "\xEF\xBF\xA5""1,234,567.89");
118 }
119 { // negative, showbase
120 long double v = -123456789;
121 showbase(ios);
122 char str[100];
123 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
124 false, ios, '*', v);
125 std::string ex(str, iter.base());
126 assert(ex == "\xEF\xBF\xA5""-1,234,567.89");
127 }
128 { // negative, showbase, left
129 long double v = -123456789;
130 showbase(ios);
131 ios.width(20);
132 left(ios);
133 char str[100];
134 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
135 false, ios, ' ', v);
136 std::string ex(str, iter.base());
137 assert(ex == "\xEF\xBF\xA5""-1,234,567.89 ");
138 assert(ios.width() == 0);
139 }
140 { // negative, showbase, internal
141 long double v = -123456789;
142 showbase(ios);
143 ios.width(20);
144 internal(ios);
145 char str[100];
146 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
147 false, ios, ' ', v);
148 std::string ex(str, iter.base());
149 assert(ex == "\xEF\xBF\xA5""- 1,234,567.89");
150 assert(ios.width() == 0);
151 }
152 { // negative, showbase, right
153 long double v = -123456789;
154 showbase(ios);
155 ios.width(20);
156 right(ios);
157 char str[100];
158 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
159 false, ios, ' ', v);
160 std::string ex(str, iter.base());
161 assert(ex == " \xEF\xBF\xA5""-1,234,567.89");
162 assert(ios.width() == 0);
163 }
164
165 // char, international
166 noshowbase(ios);
167 ios.unsetf(std::ios_base::adjustfield);
168 { // zero
169 long double v = 0;
170 char str[100];
171 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
172 true, ios, '*', v);
173 std::string ex(str, iter.base());
174 assert(ex == "0.00");
175 }
176 { // negative one
177 long double v = -1;
178 char str[100];
179 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
180 true, ios, '*', v);
181 std::string ex(str, iter.base());
182 assert(ex == "-0.01");
183 }
184 { // positive
185 long double v = 123456789;
186 char str[100];
187 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
188 true, ios, '*', v);
189 std::string ex(str, iter.base());
190 assert(ex == "1,234,567.89");
191 }
192 { // negative
193 long double v = -123456789;
194 char str[100];
195 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
196 true, ios, '*', v);
197 std::string ex(str, iter.base());
198 assert(ex == "-1,234,567.89");
199 }
200 { // zero, showbase
201 long double v = 0;
202 showbase(ios);
203 char str[100];
204 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
205 true, ios, '*', v);
206 std::string ex(str, iter.base());
207 assert(ex == "CNY 0.00");
208 }
209 { // negative one, showbase
210 long double v = -1;
211 showbase(ios);
212 char str[100];
213 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
214 true, ios, '*', v);
215 std::string ex(str, iter.base());
216 assert(ex == "CNY -0.01");
217 }
218 { // positive, showbase
219 long double v = 123456789;
220 showbase(ios);
221 char str[100];
222 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
223 true, ios, '*', v);
224 std::string ex(str, iter.base());
225 assert(ex == "CNY 1,234,567.89");
226 }
227 { // negative, showbase
228 long double v = -123456789;
229 showbase(ios);
230 char str[100];
231 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
232 true, ios, '*', v);
233 std::string ex(str, iter.base());
234 assert(ex == "CNY -1,234,567.89");
235 }
236 { // negative, showbase, left
237 long double v = -123456789;
238 showbase(ios);
239 ios.width(20);
240 left(ios);
241 char str[100];
242 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
243 true, ios, ' ', v);
244 std::string ex(str, iter.base());
245 assert(ex == "CNY -1,234,567.89 ");
246 assert(ios.width() == 0);
247 }
248 { // negative, showbase, internal
249 long double v = -123456789;
250 showbase(ios);
251 ios.width(20);
252 internal(ios);
253 char str[100];
254 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
255 true, ios, ' ', v);
256 std::string ex(str, iter.base());
257 assert(ex == "CNY - 1,234,567.89");
258 assert(ios.width() == 0);
259 }
260 { // negative, showbase, right
261 long double v = -123456789;
262 showbase(ios);
263 ios.width(20);
264 right(ios);
265 char str[100];
266 output_iterator<char*> iter = f.put(output_iterator<char*>(str),
267 true, ios, ' ', v);
268 std::string ex(str, iter.base());
269 assert(ex == " CNY -1,234,567.89");
270 assert(ios.width() == 0);
271 }
272 }
273 {
274 const my_facetw f(1);
275 // wchar_t, national
276 noshowbase(ios);
277 ios.unsetf(std::ios_base::adjustfield);
278 { // zero
279 long double v = 0;
280 wchar_t str[100];
281 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
282 false, ios, '*', v);
283 std::wstring ex(str, iter.base());
284 assert(ex == L"0.00");
285 }
286 { // negative one
287 long double v = -1;
288 wchar_t str[100];
289 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
290 false, ios, '*', v);
291 std::wstring ex(str, iter.base());
292 assert(ex == L"-0.01");
293 }
294 { // positive
295 long double v = 123456789;
296 wchar_t str[100];
297 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
298 false, ios, '*', v);
299 std::wstring ex(str, iter.base());
300 assert(ex == L"1,234,567.89");
301 }
302 { // negative
303 long double v = -123456789;
304 wchar_t str[100];
305 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
306 false, ios, '*', v);
307 std::wstring ex(str, iter.base());
308 assert(ex == L"-1,234,567.89");
309 }
310 { // zero, showbase
311 long double v = 0;
312 showbase(ios);
313 wchar_t str[100];
314 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
315 false, ios, '*', v);
316 std::wstring ex(str, iter.base());
317 assert(ex == L"\xFFE5""0.00");
318 }
319 { // negative one, showbase
320 long double v = -1;
321 showbase(ios);
322 wchar_t str[100];
323 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
324 false, ios, '*', v);
325 std::wstring ex(str, iter.base());
326 assert(ex == L"\xFFE5""-0.01");
327 }
328 { // positive, showbase
329 long double v = 123456789;
330 showbase(ios);
331 wchar_t str[100];
332 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
333 false, ios, '*', v);
334 std::wstring ex(str, iter.base());
335 assert(ex == L"\xFFE5""1,234,567.89");
336 }
337 { // negative, showbase
338 long double v = -123456789;
339 showbase(ios);
340 wchar_t str[100];
341 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
342 false, ios, '*', v);
343 std::wstring ex(str, iter.base());
344 assert(ex == L"\xFFE5""-1,234,567.89");
345 }
346 { // negative, showbase, left
347 long double v = -123456789;
348 showbase(ios);
349 ios.width(20);
350 left(ios);
351 wchar_t str[100];
352 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
353 false, ios, ' ', v);
354 std::wstring ex(str, iter.base());
355 assert(ex == L"\xFFE5""-1,234,567.89 ");
356 assert(ios.width() == 0);
357 }
358 { // negative, showbase, internal
359 long double v = -123456789;
360 showbase(ios);
361 ios.width(20);
362 internal(ios);
363 wchar_t str[100];
364 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
365 false, ios, ' ', v);
366 std::wstring ex(str, iter.base());
367 assert(ex == L"\xFFE5""- 1,234,567.89");
368 assert(ios.width() == 0);
369 }
370 { // negative, showbase, right
371 long double v = -123456789;
372 showbase(ios);
373 ios.width(20);
374 right(ios);
375 wchar_t str[100];
376 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
377 false, ios, ' ', v);
378 std::wstring ex(str, iter.base());
379 assert(ex == L" \xFFE5""-1,234,567.89");
380 assert(ios.width() == 0);
381 }
382
383 // wchar_t, international
384 noshowbase(ios);
385 ios.unsetf(std::ios_base::adjustfield);
386 { // zero
387 long double v = 0;
388 wchar_t str[100];
389 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
390 true, ios, '*', v);
391 std::wstring ex(str, iter.base());
392 assert(ex == L"0.00");
393 }
394 { // negative one
395 long double v = -1;
396 wchar_t str[100];
397 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
398 true, ios, '*', v);
399 std::wstring ex(str, iter.base());
400 assert(ex == L"-0.01");
401 }
402 { // positive
403 long double v = 123456789;
404 wchar_t str[100];
405 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
406 true, ios, '*', v);
407 std::wstring ex(str, iter.base());
408 assert(ex == L"1,234,567.89");
409 }
410 { // negative
411 long double v = -123456789;
412 wchar_t str[100];
413 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
414 true, ios, '*', v);
415 std::wstring ex(str, iter.base());
416 assert(ex == L"-1,234,567.89");
417 }
418 { // zero, showbase
419 long double v = 0;
420 showbase(ios);
421 wchar_t str[100];
422 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
423 true, ios, '*', v);
424 std::wstring ex(str, iter.base());
425 assert(ex == L"CNY 0.00");
426 }
427 { // negative one, showbase
428 long double v = -1;
429 showbase(ios);
430 wchar_t str[100];
431 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
432 true, ios, '*', v);
433 std::wstring ex(str, iter.base());
434 assert(ex == L"CNY -0.01");
435 }
436 { // positive, showbase
437 long double v = 123456789;
438 showbase(ios);
439 wchar_t str[100];
440 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
441 true, ios, '*', v);
442 std::wstring ex(str, iter.base());
443 assert(ex == L"CNY 1,234,567.89");
444 }
445 { // negative, showbase
446 long double v = -123456789;
447 showbase(ios);
448 wchar_t str[100];
449 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
450 true, ios, '*', v);
451 std::wstring ex(str, iter.base());
452 assert(ex == L"CNY -1,234,567.89");
453 }
454 { // negative, showbase, left
455 long double v = -123456789;
456 showbase(ios);
457 ios.width(20);
458 left(ios);
459 wchar_t str[100];
460 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
461 true, ios, ' ', v);
462 std::wstring ex(str, iter.base());
463 assert(ex == L"CNY -1,234,567.89 ");
464 assert(ios.width() == 0);
465 }
466 { // negative, showbase, internal
467 long double v = -123456789;
468 showbase(ios);
469 ios.width(20);
470 internal(ios);
471 wchar_t str[100];
472 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
473 true, ios, ' ', v);
474 std::wstring ex(str, iter.base());
475 assert(ex == L"CNY - 1,234,567.89");
476 assert(ios.width() == 0);
477 }
478 { // negative, showbase, right
479 long double v = -123456789;
480 showbase(ios);
481 ios.width(20);
482 right(ios);
483 wchar_t str[100];
484 output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
485 true, ios, ' ', v);
486 std::wstring ex(str, iter.base());
487 assert(ex == L" CNY -1,234,567.89");
488 assert(ios.width() == 0);
489 }
490 }
491 }
492