put_string_en_US.pass.cpp revision c0d0cbad9ed434267a7af9531bdeeae52eb6d706
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//               const string_type& units) const;
16
17#include <locale>
18#include <ios>
19#include <streambuf>
20#include <cassert>
21#include "iterators.h"
22
23#include "../../../../../platform_support.h" // locale name macros
24
25typedef std::money_put<char, output_iterator<char*> > Fn;
26
27class my_facet
28    : public Fn
29{
30public:
31    explicit my_facet(std::size_t refs = 0)
32        : Fn(refs) {}
33};
34
35typedef std::money_put<wchar_t, output_iterator<wchar_t*> > Fw;
36
37class my_facetw
38    : public Fw
39{
40public:
41    explicit my_facetw(std::size_t refs = 0)
42        : Fw(refs) {}
43};
44
45int main()
46{
47    std::ios ios(0);
48    std::string loc_name(LOCALE_en_US_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        std::string 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        std::string 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        std::string 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        std::string 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        std::string 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 == "$0.00");
100    }
101    {   // negative one, showbase
102        std::string 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 == "-$0.01");
109    }
110    {   // positive, showbase
111        std::string 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 == "$1,234,567.89");
118    }
119    {   // negative, showbase
120        std::string 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 == "-$1,234,567.89");
127    }
128    {   // negative, showbase, left
129        std::string 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 == "-$1,234,567.89      ");
138        assert(ios.width() == 0);
139    }
140    {   // negative, showbase, internal
141        std::string 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 == "-$      1,234,567.89");
150        assert(ios.width() == 0);
151    }
152    {   // negative, showbase, right
153        std::string 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 == "      -$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        std::string 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        std::string 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        std::string 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        std::string 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        std::string 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 == "USD 0.00");
208    }
209    {   // negative one, showbase
210        std::string 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 == "-USD 0.01");
217    }
218    {   // positive, showbase
219        std::string 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 == "USD 1,234,567.89");
226    }
227    {   // negative, showbase
228        std::string 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 == "-USD 1,234,567.89");
235    }
236    {   // negative, showbase, left
237        std::string 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 == "-USD 1,234,567.89   ");
246        assert(ios.width() == 0);
247    }
248    {   // negative, showbase, internal
249        std::string 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 == "-USD    1,234,567.89");
258        assert(ios.width() == 0);
259    }
260    {   // negative, showbase, right
261        std::string 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 == "   -USD 1,234,567.89");
270        assert(ios.width() == 0);
271    }
272}
273{
274
275    const my_facetw f(1);
276    // wchar_t, national
277    noshowbase(ios);
278    ios.unsetf(std::ios_base::adjustfield);
279    {   // zero
280        std::wstring v = L"0";
281        wchar_t str[100];
282        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
283                                            false, ios, '*', v);
284        std::wstring ex(str, iter.base());
285        assert(ex == L"0.00");
286    }
287    {   // negative one
288        std::wstring v = L"-1";
289        wchar_t str[100];
290        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
291                                            false, ios, '*', v);
292        std::wstring ex(str, iter.base());
293        assert(ex == L"-0.01");
294    }
295    {   // positive
296        std::wstring v = L"123456789";
297        wchar_t str[100];
298        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
299                                            false, ios, '*', v);
300        std::wstring ex(str, iter.base());
301        assert(ex == L"1,234,567.89");
302    }
303    {   // negative
304        std::wstring v = L"-123456789";
305        wchar_t str[100];
306        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
307                                            false, ios, '*', v);
308        std::wstring ex(str, iter.base());
309        assert(ex == L"-1,234,567.89");
310    }
311    {   // zero, showbase
312        std::wstring v = L"0";
313        showbase(ios);
314        wchar_t str[100];
315        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
316                                            false, ios, '*', v);
317        std::wstring ex(str, iter.base());
318        assert(ex == L"$0.00");
319    }
320    {   // negative one, showbase
321        std::wstring v = L"-1";
322        showbase(ios);
323        wchar_t str[100];
324        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
325                                            false, ios, '*', v);
326        std::wstring ex(str, iter.base());
327        assert(ex == L"-$0.01");
328    }
329    {   // positive, showbase
330        std::wstring v = L"123456789";
331        showbase(ios);
332        wchar_t str[100];
333        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
334                                            false, ios, '*', v);
335        std::wstring ex(str, iter.base());
336        assert(ex == L"$1,234,567.89");
337    }
338    {   // negative, showbase
339        std::wstring v = L"-123456789";
340        showbase(ios);
341        wchar_t str[100];
342        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
343                                            false, ios, '*', v);
344        std::wstring ex(str, iter.base());
345        assert(ex == L"-$1,234,567.89");
346    }
347    {   // negative, showbase, left
348        std::wstring v = L"-123456789";
349        showbase(ios);
350        ios.width(20);
351        left(ios);
352        wchar_t str[100];
353        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
354                                            false, ios, ' ', v);
355        std::wstring ex(str, iter.base());
356        assert(ex == L"-$1,234,567.89      ");
357        assert(ios.width() == 0);
358    }
359    {   // negative, showbase, internal
360        std::wstring v = L"-123456789";
361        showbase(ios);
362        ios.width(20);
363        internal(ios);
364        wchar_t str[100];
365        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
366                                            false, ios, ' ', v);
367        std::wstring ex(str, iter.base());
368        assert(ex == L"-$      1,234,567.89");
369        assert(ios.width() == 0);
370    }
371    {   // negative, showbase, right
372        std::wstring v = L"-123456789";
373        showbase(ios);
374        ios.width(20);
375        right(ios);
376        wchar_t str[100];
377        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
378                                            false, ios, ' ', v);
379        std::wstring ex(str, iter.base());
380        assert(ex == L"      -$1,234,567.89");
381        assert(ios.width() == 0);
382    }
383
384    // wchar_t, international
385    noshowbase(ios);
386    ios.unsetf(std::ios_base::adjustfield);
387    {   // zero
388        std::wstring v = L"0";
389        wchar_t str[100];
390        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
391                                            true, ios, '*', v);
392        std::wstring ex(str, iter.base());
393        assert(ex == L"0.00");
394    }
395    {   // negative one
396        std::wstring v = L"-1";
397        wchar_t str[100];
398        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
399                                            true, ios, '*', v);
400        std::wstring ex(str, iter.base());
401        assert(ex == L"-0.01");
402    }
403    {   // positive
404        std::wstring v = L"123456789";
405        wchar_t str[100];
406        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
407                                            true, ios, '*', v);
408        std::wstring ex(str, iter.base());
409        assert(ex == L"1,234,567.89");
410    }
411    {   // negative
412        std::wstring v = L"-123456789";
413        wchar_t str[100];
414        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
415                                            true, ios, '*', v);
416        std::wstring ex(str, iter.base());
417        assert(ex == L"-1,234,567.89");
418    }
419    {   // zero, showbase
420        std::wstring v = L"0";
421        showbase(ios);
422        wchar_t str[100];
423        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
424                                            true, ios, '*', v);
425        std::wstring ex(str, iter.base());
426        assert(ex == L"USD 0.00");
427    }
428    {   // negative one, showbase
429        std::wstring v = L"-1";
430        showbase(ios);
431        wchar_t str[100];
432        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
433                                            true, ios, '*', v);
434        std::wstring ex(str, iter.base());
435        assert(ex == L"-USD 0.01");
436    }
437    {   // positive, showbase
438        std::wstring v = L"123456789";
439        showbase(ios);
440        wchar_t str[100];
441        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
442                                            true, ios, '*', v);
443        std::wstring ex(str, iter.base());
444        assert(ex == L"USD 1,234,567.89");
445    }
446    {   // negative, showbase
447        std::wstring v = L"-123456789";
448        showbase(ios);
449        wchar_t str[100];
450        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
451                                            true, ios, '*', v);
452        std::wstring ex(str, iter.base());
453        assert(ex == L"-USD 1,234,567.89");
454    }
455    {   // negative, showbase, left
456        std::wstring v = L"-123456789";
457        showbase(ios);
458        ios.width(20);
459        left(ios);
460        wchar_t str[100];
461        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
462                                            true, ios, ' ', v);
463        std::wstring ex(str, iter.base());
464        assert(ex == L"-USD 1,234,567.89   ");
465        assert(ios.width() == 0);
466    }
467    {   // negative, showbase, internal
468        std::wstring v = L"-123456789";
469        showbase(ios);
470        ios.width(20);
471        internal(ios);
472        wchar_t str[100];
473        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
474                                            true, ios, ' ', v);
475        std::wstring ex(str, iter.base());
476        assert(ex == L"-USD    1,234,567.89");
477        assert(ios.width() == 0);
478    }
479    {   // negative, showbase, right
480        std::wstring v = L"-123456789";
481        showbase(ios);
482        ios.width(20);
483        right(ios);
484        wchar_t str[100];
485        output_iterator<wchar_t*> iter = f.put(output_iterator<wchar_t*>(str),
486                                            true, ios, ' ', v);
487        std::wstring ex(str, iter.base());
488        assert(ex == L"   -USD 1,234,567.89");
489        assert(ios.width() == 0);
490    }
491}
492}
493