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