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