1 // 2001-08-27 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2001, 2002 Free Software Foundation
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // 22.2.6.2.1 money_put members
25 #include <testsuite_hooks.h>
27 // XXX This test is not working for non-glibc locale models.
28 // { dg-do run { xfail *-*-* } }
30 // test string version
34 typedef money_base::part part
;
35 typedef money_base::pattern pattern
;
36 typedef ostreambuf_iterator
<char> iterator_type
;
41 locale loc_c
= locale::classic();
42 locale
loc_hk("en_HK");
43 locale
loc_fr("fr_FR@euro");
44 locale
loc_de("de_DE");
45 VERIFY( loc_c
!= loc_de
);
46 VERIFY( loc_hk
!= loc_fr
);
47 VERIFY( loc_hk
!= loc_de
);
48 VERIFY( loc_de
!= loc_fr
);
50 // cache the moneypunct facets
51 typedef moneypunct
<char, true> __money_true
;
52 typedef moneypunct
<char, false> __money_false
;
53 const __money_true
& monpunct_c_t
= use_facet
<__money_true
>(loc_c
);
54 const __money_true
& monpunct_de_t
= use_facet
<__money_true
>(loc_de
);
55 const __money_false
& monpunct_c_f
= use_facet
<__money_false
>(loc_c
);
56 const __money_false
& monpunct_de_f
= use_facet
<__money_false
>(loc_de
);
57 const __money_true
& monpunct_hk_t
= use_facet
<__money_true
>(loc_hk
);
58 const __money_false
& monpunct_hk_f
= use_facet
<__money_false
>(loc_hk
);
60 // sanity check the data is correct.
63 // total EPA budget FY 2002
64 const string
digits1("720000000000");
66 // est. cost, national missile "defense", expressed as a loss in USD 2001
67 const string
digits2("-10000000000000");
70 const string
digits3("-A");
72 // input less than frac_digits
73 const string
digits4("-1");
78 // cache the money_put facet
79 const money_put
<char>& mon_put
= use_facet
<money_put
<char> >(oss
.getloc());
82 iterator_type os_it01
= mon_put
.put(oss
.rdbuf(), true, oss
, ' ', digits1
);
83 string result1
= oss
.str();
84 VERIFY( result1
== "7.200.000.000,00 ");
87 iterator_type os_it02
= mon_put
.put(oss
.rdbuf(), false, oss
, ' ', digits1
);
88 string result2
= oss
.str();
89 VERIFY( result2
== "7.200.000.000,00 ");
91 // intl and non-intl versions should be the same.
92 VERIFY( result1
== result2
);
94 // now try with showbase, to get currency symbol in format
95 oss
.setf(ios_base::showbase
);
98 iterator_type os_it03
= mon_put
.put(oss
.rdbuf(), true, oss
, ' ', digits1
);
99 string result3
= oss
.str();
100 VERIFY( result3
== "7.200.000.000,00 DEM ");
103 iterator_type os_it04
= mon_put
.put(oss
.rdbuf(), false, oss
, ' ', digits1
);
104 string result4
= oss
.str();
105 VERIFY( result4
== "7.200.000.000,00 DM");
107 // intl and non-intl versions should be different.
108 VERIFY( result3
!= result4
);
109 VERIFY( result3
!= result1
);
110 VERIFY( result4
!= result2
);
112 // test sign of more than one digit, say hong kong.
115 iterator_type os_it05
= mon_put
.put(oss
.rdbuf(), false, oss
, ' ', digits1
);
116 string result5
= oss
.str();
117 VERIFY( result5
== "HK$7,200,000,000.00");
120 iterator_type os_it06
= mon_put
.put(oss
.rdbuf(), true, oss
, ' ', digits2
);
121 string result6
= oss
.str();
122 VERIFY( result6
== "(HKD 100,000,000,000.00)");
124 // test one-digit formats without zero padding
127 iterator_type os_it07
= mon_put
.put(oss
.rdbuf(), true, oss
, ' ', digits4
);
128 string result7
= oss
.str();
129 VERIFY( result7
== "1");
131 // test one-digit formats with zero padding, zero frac widths
134 iterator_type os_it08
= mon_put
.put(oss
.rdbuf(), true, oss
, ' ', digits4
);
135 string result8
= oss
.str();
136 VERIFY( result8
== "(HKD .01)");
138 oss
.unsetf(ios_base::showbase
);
142 iterator_type os_it09
= mon_put
.put(oss
.rdbuf(), true, oss
, ' ', digits3
);
143 string result9
= oss
.str();
144 VERIFY( result9
== "");
146 // test io.width() > length
147 // test various fill strategies
152 iterator_type os_it10
= mon_put
.put(oss
.rdbuf(), true, oss
, '*', digits4
);
153 string result10
= oss
.str();
154 VERIFY( result10
== "***************-,01*");
158 oss
.setf(ios_base::internal
);
159 iterator_type os_it11
= mon_put
.put(oss
.rdbuf(), true, oss
, '*', digits4
);
160 string result11
= oss
.str();
161 VERIFY( result11
== "-,01****************");
164 // test double version
168 typedef money_base::part part
;
169 typedef money_base::pattern pattern
;
170 typedef ostreambuf_iterator
<char> iterator_type
;
174 // basic construction
175 locale loc_c
= locale::classic();
176 locale
loc_hk("en_HK");
177 locale
loc_fr("fr_FR@euro");
178 locale
loc_de("de_DE");
179 VERIFY( loc_c
!= loc_de
);
180 VERIFY( loc_hk
!= loc_fr
);
181 VERIFY( loc_hk
!= loc_de
);
182 VERIFY( loc_de
!= loc_fr
);
184 // cache the moneypunct facets
185 typedef moneypunct
<char, true> __money_true
;
186 typedef moneypunct
<char, false> __money_false
;
187 const __money_true
& monpunct_c_t
= use_facet
<__money_true
>(loc_c
);
188 const __money_true
& monpunct_de_t
= use_facet
<__money_true
>(loc_de
);
189 const __money_false
& monpunct_c_f
= use_facet
<__money_false
>(loc_c
);
190 const __money_false
& monpunct_de_f
= use_facet
<__money_false
>(loc_de
);
191 const __money_true
& monpunct_hk_t
= use_facet
<__money_true
>(loc_hk
);
192 const __money_false
& monpunct_hk_f
= use_facet
<__money_false
>(loc_hk
);
194 // sanity check the data is correct.
197 // total EPA budget FY 2002
198 const long double digits1
= 720000000000;
200 // est. cost, national missile "defense", expressed as a loss in USD 2001
201 const long double digits2
= -10000000000000;
203 // input less than frac_digits
204 const long double digits4
= -1;
209 // cache the money_put facet
210 const money_put
<char>& mon_put
= use_facet
<money_put
<char> >(oss
.getloc());
213 iterator_type os_it01
= mon_put
.put(oss
.rdbuf(), true, oss
, ' ', digits1
);
214 string result1
= oss
.str();
215 VERIFY( result1
== "7.200.000.000,00 ");
218 iterator_type os_it02
= mon_put
.put(oss
.rdbuf(), false, oss
, ' ', digits1
);
219 string result2
= oss
.str();
220 VERIFY( result2
== "7.200.000.000,00 ");
222 // intl and non-intl versions should be the same.
223 VERIFY( result1
== result2
);
225 // now try with showbase, to get currency symbol in format
226 oss
.setf(ios_base::showbase
);
229 iterator_type os_it03
= mon_put
.put(oss
.rdbuf(), true, oss
, ' ', digits1
);
230 string result3
= oss
.str();
231 VERIFY( result3
== "7.200.000.000,00 DEM ");
234 iterator_type os_it04
= mon_put
.put(oss
.rdbuf(), false, oss
, ' ', digits1
);
235 string result4
= oss
.str();
236 VERIFY( result4
== "7.200.000.000,00 DM");
238 // intl and non-intl versions should be different.
239 VERIFY( result3
!= result4
);
240 VERIFY( result3
!= result1
);
241 VERIFY( result4
!= result2
);
249 // Check money_put works with other iterators besides streambuf
250 // output iterators. (As long as output_iterator requirements are met.)
251 typedef string::iterator iter_type
;
252 typedef money_put
<char, iter_type
> mon_put_type
;
253 const ios_base::iostate goodbit
= ios_base::goodbit
;
254 const ios_base::iostate eofbit
= ios_base::eofbit
;
255 ios_base::iostate err
= goodbit
;
256 const locale loc_c
= locale::classic();
257 // woman, art, thief (stole the blues)
258 const string
str("1943 Janis Joplin");
259 const long double ld
= 1943;
260 const string
x(str
.size(), 'x'); // have to have allocated string!
264 oss
.imbue(locale(loc_c
, new mon_put_type
));
266 // Iterator advanced, state, output.
267 const mon_put_type
& mp
= use_facet
<mon_put_type
>(oss
.getloc());
271 iter_type ret1
= mp
.put(res
.begin(), false, oss
, ' ', str
);
272 string
sanity1(res
.begin(), ret1
);
273 VERIFY( err
== goodbit
);
274 VERIFY( res
== "1943xxxxxxxxxxxxx" );
275 VERIFY( sanity1
== "1943" );
279 iter_type ret2
= mp
.put(res
.begin(), false, oss
, ' ', ld
);
280 string
sanity2(res
.begin(), ret2
);
281 VERIFY( err
== goodbit
);
282 VERIFY( res
== "1943xxxxxxxxxxxxx" );
283 VERIFY( sanity2
== "1943" );
289 #ifdef _GLIBCPP_HAVE_SETENV
290 // Set the global locale to non-"C".
291 std::locale
loc_de("de_DE");
292 std::locale::global(loc_de
);
294 // Set LANG environment variable to de_DE.
295 const char* oldLANG
= getenv("LANG");
296 if (!setenv("LANG", "de_DE", 1))
301 setenv("LANG", oldLANG
? oldLANG
: "", 1);
306 struct My_money_io
: public std::moneypunct
<char,false>
308 char_type
do_decimal_point() const { return '.'; }
309 char_type
do_thousands_sep() const { return ','; }
310 std::string
do_grouping() const { return "\003"; }
312 std::string
do_negative_sign() const { return "()"; }
314 int do_frac_digits() const { return 2; }
316 pattern
do_neg_format() const
318 pattern pat
= { { symbol
, space
, sign
, value
} };
327 typedef ostreambuf_iterator
<char> OutIt
;
329 locale
loc(locale::classic(), new My_money_io
);
333 string
val("-123456");
334 const money_put
<char,OutIt
>& mp
=
335 use_facet
<money_put
<char, OutIt
> >(loc
);
340 mp
.put(out
,intl
,fmt
,'*',val
);
341 VERIFY( fmt
.str() == "*(1,234.56)" );
344 struct My_money_io_2
: public std::moneypunct
<char,false>
346 char_type
do_thousands_sep() const { return ','; }
347 std::string
do_grouping() const { return "\001"; }
350 // Make sure we can output a very big amount of money (with grouping too).
354 typedef ostreambuf_iterator
<char> OutIt
;
356 locale
loc(locale::classic(), new My_money_io_2
);
360 long double val
= 1e50L
;
361 const money_put
<char,OutIt
>& mp
=
362 use_facet
<money_put
<char, OutIt
> >(loc
);
367 mp
.put(out
,intl
,fmt
,'*',val
);
371 // http://gcc.gnu.org/ml/libstdc++/2002-05/msg00038.html
376 std::string loc1
= setlocale(LC_ALL
, "ja_JP.eucjp");
382 std::string loc2
= setlocale(LC_ALL
, NULL
);
383 VERIFY( loc1
== loc2
);