1 // 2001-09-12 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.1.1 money_get 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 istreambuf_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@euro");
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_get facet
79 const money_get<char>& mon_get = use_facet<money_get<char> >(iss.getloc());
82 iss.str("7.200.000.000,00 ");
83 iterator_type is_it01(iss);
85 ios_base::iostate err01 = ios_base::goodbit;
86 mon_get.get(is_it01, end, true, iss, err01, result1);
87 VERIFY( result1 == digits1 );
88 VERIFY( err01 == ios_base::eofbit );
90 iss.str("7.200.000.000,00 ");
91 iterator_type is_it02(iss);
93 ios_base::iostate err02 = ios_base::goodbit;
94 mon_get.get(is_it02, end, true, iss, err02, result2);
95 VERIFY( result2 == digits1 );
96 VERIFY( err02 == ios_base::eofbit );
98 iss.str("7.200.000.000,00 a");
99 iterator_type is_it03(iss);
101 ios_base::iostate err03 = ios_base::goodbit;
102 mon_get.get(is_it03, end, true, iss, err03, result3);
103 VERIFY( result3 == digits1 );
104 VERIFY( err03 == ios_base::goodbit );
107 iterator_type is_it04(iss);
109 ios_base::iostate err04 = ios_base::goodbit;
110 mon_get.get(is_it04, end, true, iss, err04, result4);
111 VERIFY( result4 == empty );
112 VERIFY( err04 == ios_base::failbit | ios_base::eofbit );
114 iss.str("working for enlightenment and peace in a mad world");
115 iterator_type is_it05(iss);
117 ios_base::iostate err05 = ios_base::goodbit;
118 mon_get.get(is_it05, end, true, iss, err05, result5);
119 VERIFY( result5 == empty );
120 VERIFY( err05 == ios_base::failbit );
122 // now try with showbase, to get currency symbol in format
123 iss.setf(ios_base::showbase);
125 iss.str("7.200.000.000,00 EUR ");
126 iterator_type is_it06(iss);
128 ios_base::iostate err06 = ios_base::goodbit;
129 mon_get.get(is_it06, end, true, iss, err06, result6);
130 VERIFY( result6 == digits1 );
131 VERIFY( err06 == ios_base::eofbit );
133 iss.str("7.200.000.000,00 EUR "); // Extra space.
134 iterator_type is_it07(iss);
136 ios_base::iostate err07 = ios_base::goodbit;
137 mon_get.get(is_it07, end, true, iss, err07, result7);
138 VERIFY( result7 == digits1 );
139 VERIFY( err07 == ios_base::goodbit );
141 iss.str("7.200.000.000,00 \244");
142 iterator_type is_it08(iss);
144 ios_base::iostate err08 = ios_base::goodbit;
145 mon_get.get(is_it08, end, false, iss, err08, result8);
146 VERIFY( result8 == digits1 );
147 VERIFY( err08 == ios_base::eofbit );
150 iss.str("HK$7,200,000,000.00");
151 iterator_type is_it09(iss);
153 ios_base::iostate err09 = ios_base::goodbit;
154 mon_get.get(is_it09, end, false, iss, err09, result9);
155 VERIFY( result9 == digits1 );
156 VERIFY( err09 == ios_base::eofbit );
158 iss.str("(HKD 100,000,000,000.00)");
159 iterator_type is_it10(iss);
161 ios_base::iostate err10 = ios_base::goodbit;
162 mon_get.get(is_it10, end, true, iss, err10, result10);
163 VERIFY( result10 == digits2 );
164 VERIFY( err10 == ios_base::goodbit );
166 iss.str("(HKD .01)");
167 iterator_type is_it11(iss);
169 ios_base::iostate err11 = ios_base::goodbit;
170 mon_get.get(is_it11, end, true, iss, err11, result11);
171 VERIFY( result11 == digits4 );
172 VERIFY( err11 == ios_base::goodbit );
174 // for the "en_HK" locale the parsing of the very same input streams must
175 // be successful without showbase too, since the symbol field appears in
176 // the first positions in the format and the symbol, when present, must be
178 iss.unsetf(ios_base::showbase);
180 iss.str("HK$7,200,000,000.00");
181 iterator_type is_it12(iss);
183 ios_base::iostate err12 = ios_base::goodbit;
184 mon_get.get(is_it12, end, false, iss, err12, result12);
185 VERIFY( result12 == digits1 );
186 VERIFY( err12 == ios_base::eofbit );
188 iss.str("(HKD 100,000,000,000.00)");
189 iterator_type is_it13(iss);
191 ios_base::iostate err13 = ios_base::goodbit;
192 mon_get.get(is_it13, end, true, iss, err13, result13);
193 VERIFY( result13 == digits2 );
194 VERIFY( err13 == ios_base::goodbit );
196 iss.str("(HKD .01)");
197 iterator_type is_it14(iss);
199 ios_base::iostate err14 = ios_base::goodbit;
200 mon_get.get(is_it14, end, true, iss, err14, result14);
201 VERIFY( result14 == digits4 );
202 VERIFY( err14 == ios_base::goodbit );
205 // test double version
209 typedef money_base::part part;
210 typedef money_base::pattern pattern;
211 typedef istreambuf_iterator<char> iterator_type;
215 // basic construction
216 locale loc_c = locale::classic();
217 locale loc_hk("en_HK");
218 locale loc_fr("fr_FR@euro");
219 locale loc_de("de_DE@euro");
220 VERIFY( loc_c != loc_de );
221 VERIFY( loc_hk != loc_fr );
222 VERIFY( loc_hk != loc_de );
223 VERIFY( loc_de != loc_fr );
225 // cache the moneypunct facets
226 typedef moneypunct<char, true> __money_true;
227 typedef moneypunct<char, false> __money_false;
228 const __money_true& monpunct_c_t = use_facet<__money_true>(loc_c);
229 const __money_true& monpunct_de_t = use_facet<__money_true>(loc_de);
230 const __money_false& monpunct_c_f = use_facet<__money_false>(loc_c);
231 const __money_false& monpunct_de_f = use_facet<__money_false>(loc_de);
232 const __money_true& monpunct_hk_t = use_facet<__money_true>(loc_hk);
233 const __money_false& monpunct_hk_f = use_facet<__money_false>(loc_hk);
235 // sanity check the data is correct.
238 // total EPA budget FY 2002
239 const long double digits1 = 720000000000.0;
241 // est. cost, national missile "defense", expressed as a loss in USD 2001
242 const long double digits2 = -10000000000000.0;
244 // input less than frac_digits
245 const long double digits4 = -1.0;
250 // cache the money_get facet
251 const money_get<char>& mon_get = use_facet<money_get<char> >(iss.getloc());
253 iss.str("7.200.000.000,00 ");
254 iterator_type is_it01(iss);
256 ios_base::iostate err01 = ios_base::goodbit;
257 mon_get.get(is_it01, end, true, iss, err01, result1);
258 VERIFY( result1 == digits1 );
259 VERIFY( err01 == ios_base::eofbit );
261 iss.str("7.200.000.000,00 ");
262 iterator_type is_it02(iss);
264 ios_base::iostate err02 = ios_base::goodbit;
265 mon_get.get(is_it02, end, false, iss, err02, result2);
266 VERIFY( result2 == digits1 );
267 VERIFY( err02 == ios_base::eofbit );
269 // now try with showbase, to get currency symbol in format
270 iss.setf(ios_base::showbase);
273 iss.str("(HKD .01)");
274 iterator_type is_it03(iss);
276 ios_base::iostate err03 = ios_base::goodbit;
277 mon_get.get(is_it03, end, true, iss, err03, result3);
278 VERIFY( result3 == digits4 );
279 VERIFY( err03 == ios_base::goodbit );
287 // Check money_get works with other iterators besides streambuf
289 typedef string::const_iterator iter_type;
290 typedef money_get<char, iter_type> mon_get_type;
291 const ios_base::iostate goodbit = ios_base::goodbit;
292 const ios_base::iostate eofbit = ios_base::eofbit;
293 ios_base::iostate err = goodbit;
294 const locale loc_c = locale::classic();
295 const string str = "0.01Eleanor Roosevelt";
298 iss.imbue(locale(loc_c, new mon_get_type));
300 // Iterator advanced, state, output.
301 const mon_get_type& mg = use_facet<mon_get_type>(iss.getloc());
305 iter_type end1 = mg.get(str.begin(), str.end(), false, iss, err, res1);
306 string rem1(end1, str.end());
307 VERIFY( err == goodbit );
308 VERIFY( res1 == "1" );
309 VERIFY( rem1 == "Eleanor Roosevelt" );
315 iter_type end2 = mg.get(str.begin(), str.end(), false, iss, err, res2);
316 string rem2(end2, str.end());
317 VERIFY( err == goodbit );
319 VERIFY( rem2 == "Eleanor Roosevelt" );
325 #ifdef _GLIBCPP_HAVE_SETENV
326 // Set the global locale to non-"C".
327 std::locale loc_de("de_DE@euro");
328 std::locale::global(loc_de);
330 // Set LANG environment variable to de_DE@euro.
331 const char* oldLANG = getenv("LANG");
332 if (!setenv("LANG", "de_DE@euro", 1))
337 setenv("LANG", oldLANG ? oldLANG : "", 1);
342 struct My_money_io : public std::moneypunct<char,false>
344 char_type do_decimal_point() const { return '.'; }
345 std::string do_grouping() const { return "\004"; }
347 std::string do_curr_symbol() const { return "$"; }
348 std::string do_positive_sign() const { return ""; }
349 std::string do_negative_sign() const { return "-"; }
351 int do_frac_digits() const { return 2; }
353 pattern do_pos_format() const
355 pattern pat = { { symbol, none, sign, value } };
359 pattern do_neg_format() const
361 pattern pat = { { symbol, none, sign, value } };
370 typedef istreambuf_iterator<char> InIt;
374 locale loc(locale::classic(), new My_money_io);
376 string bufferp("$1234.56");
377 string buffern("$-1234.56");
378 string bufferp_ns("1234.56");
379 string buffern_ns("-1234.56");
383 InIt iendp, iendn, iendp_ns, iendn_ns;
384 ios_base::iostate err;
385 string valp, valn, valp_ns, valn_ns;
387 const money_get<char,InIt>& mg =
388 use_facet<money_get<char, InIt> >(loc);
390 istringstream fmtp(bufferp);
393 mg.get(ibegp,iendp,intl,fmtp,err,valp);
394 VERIFY( valp == "123456" );
396 istringstream fmtn(buffern);
399 mg.get(ibegn,iendn,intl,fmtn,err,valn);
400 VERIFY( valn == "-123456" );
402 istringstream fmtp_ns(bufferp_ns);
404 InIt ibegp_ns(fmtp_ns);
405 mg.get(ibegp_ns,iendp_ns,intl,fmtp_ns,err,valp_ns);
406 VERIFY( valp_ns == "123456" );
408 istringstream fmtn_ns(buffern_ns);
410 InIt ibegn_ns(fmtn_ns);
411 mg.get(ibegn_ns,iendn_ns,intl,fmtn_ns,err,valn_ns);
412 VERIFY( valn_ns == "-123456" );
415 // We were appending to the string val passed by reference, instead
416 // of constructing a temporary candidate, eventually copied into
417 // val in case of successful parsing.
423 typedef istreambuf_iterator<char> InIt;
424 InIt iend1, iend2, iend3;
427 string buffer1("123");
428 string buffer2("456");
429 string buffer3("Golgafrincham"); // From Nathan's original idea.
433 ios_base::iostate err;
435 const money_get<char,InIt>& mg =
436 use_facet<money_get<char, InIt> >(loc);
438 istringstream fmt1(buffer1);
440 mg.get(ibeg1,iend1,false,fmt1,err,val);
441 VERIFY( val == buffer1 );
443 istringstream fmt2(buffer2);
445 mg.get(ibeg2,iend2,false,fmt2,err,val);
446 VERIFY( val == buffer2 );
449 istringstream fmt3(buffer3);
451 mg.get(ibeg3,iend3,false,fmt3,err,val);
452 VERIFY( val == buffer3 );
455 struct My_money_io_a : public std::moneypunct<char,false>
457 char_type do_decimal_point() const { return '.'; }
458 std::string do_grouping() const { return "\004"; }
460 std::string do_curr_symbol() const { return "$"; }
461 std::string do_positive_sign() const { return "()"; }
463 int do_frac_digits() const { return 2; }
465 pattern do_pos_format() const
467 pattern pat = { { sign, value, space, symbol } };
472 struct My_money_io_b : public std::moneypunct<char,false>
474 char_type do_decimal_point() const { return '.'; }
475 std::string do_grouping() const { return "\004"; }
477 std::string do_curr_symbol() const { return "$"; }
478 std::string do_positive_sign() const { return "()"; }
480 int do_frac_digits() const { return 2; }
482 pattern do_pos_format() const
484 pattern pat = { { sign, value, symbol, none } };
489 // This one exercises patterns of the type { X, Y, Z, symbol } and
490 // { X, Y, symbol, none } for a two character long sign. Therefore
491 // the optional symbol (showbase is false by default) must be consumed
492 // if present, since "rest of the sign" is left to read.
496 typedef istreambuf_iterator<char> InIt;
500 ios_base::iostate err;
502 locale loc_a(locale::classic(), new My_money_io_a);
504 string buffer_a("(1234.56 $)");
505 string buffer_a_ns("(1234.56 )");
507 InIt iend_a, iend_a_ns;
508 string val_a, val_a_ns;
510 const money_get<char,InIt>& mg_a =
511 use_facet<money_get<char, InIt> >(loc_a);
513 istringstream fmt_a(buffer_a);
516 mg_a.get(ibeg_a,iend_a,intl,fmt_a,err,val_a);
517 VERIFY( val_a == "123456" );
519 istringstream fmt_a_ns(buffer_a_ns);
520 fmt_a_ns.imbue(loc_a);
521 InIt ibeg_a_ns(fmt_a_ns);
522 mg_a.get(ibeg_a_ns,iend_a_ns,intl,fmt_a_ns,err,val_a_ns);
523 VERIFY( val_a_ns == "123456" );
525 locale loc_b(locale::classic(), new My_money_io_b);
527 string buffer_b("(1234.56$)");
528 string buffer_b_ns("(1234.56)");
530 InIt iend_b, iend_b_ns;
531 string val_b, val_b_ns;
533 const money_get<char,InIt>& mg_b =
534 use_facet<money_get<char, InIt> >(loc_b);
536 istringstream fmt_b(buffer_b);
539 mg_b.get(ibeg_b,iend_b,intl,fmt_b,err,val_b);
540 VERIFY( val_b == "123456" );
542 istringstream fmt_b_ns(buffer_b_ns);
543 fmt_b_ns.imbue(loc_b);
544 InIt ibeg_b_ns(fmt_b_ns);
545 mg_b.get(ibeg_b_ns,iend_b_ns,intl,fmt_b_ns,err,val_b_ns);
546 VERIFY( val_b_ns == "123456" );
549 // http://gcc.gnu.org/ml/libstdc++/2002-05/msg00038.html
554 const char* tentLANG = std::setlocale(LC_ALL, "ja_JP.eucjp");
555 if (tentLANG != NULL)
557 std::string preLANG = tentLANG;
564 std::string postLANG = std::setlocale(LC_ALL, NULL);
565 VERIFY( preLANG == postLANG );