OSDN Git Service

2002-09-27 Paolo Carlini <pcarlini@unitus.it>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 22_locale / money_get_members_char.cc
1 // 2001-09-12 Benjamin Kosnik  <bkoz@redhat.com>
2
3 // Copyright (C) 2001, 2002 Free Software Foundation
4 //
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)
9 // any later version.
10
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.
15
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,
19 // USA.
20
21 // 22.2.6.1.1 money_get members
22
23 #include <locale>
24 #include <sstream>
25 #include <testsuite_hooks.h>
26
27 // XXX This test is not working for non-glibc locale models.
28 // { dg-do run { xfail *-*-* } }
29
30 // test string version
31 void test01()
32 {
33   using namespace std;
34   typedef money_base::part part;
35   typedef money_base::pattern pattern;
36   typedef istreambuf_iterator<char> iterator_type;
37
38   bool test = true;
39
40   // basic construction
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 );
49
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); 
59
60   // sanity check the data is correct.
61   const string empty;
62
63   // total EPA budget FY 2002
64   const string digits1("720000000000");
65
66   // est. cost, national missile "defense", expressed as a loss in USD 2001
67   const string digits2("-10000000000000");  
68
69   // not valid input
70   const string digits3("-A"); 
71
72   // input less than frac_digits
73   const string digits4("-1");
74   
75   iterator_type end;
76   istringstream iss;
77   iss.imbue(loc_de);
78   // cache the money_get facet
79   const money_get<char>& mon_get = use_facet<money_get<char> >(iss.getloc()); 
80
81
82   iss.str("7.200.000.000,00 ");
83   iterator_type is_it01(iss);
84   string result1;
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 );
89
90   iss.str("7.200.000.000,00  ");
91   iterator_type is_it02(iss);
92   string result2;
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 );
97
98   iss.str("7.200.000.000,00  a");
99   iterator_type is_it03(iss);
100   string result3;
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 );
105
106   iss.str("");
107   iterator_type is_it04(iss);
108   string result4;
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 );
113
114   iss.str("working for enlightenment and peace in a mad world");
115   iterator_type is_it05(iss);
116   string result5;
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 );
121
122   // now try with showbase, to get currency symbol in format
123   iss.setf(ios_base::showbase);
124
125   iss.str("7.200.000.000,00 EUR ");
126   iterator_type is_it06(iss);
127   string result6;
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 );
132
133   iss.str("7.200.000.000,00 EUR  "); // Extra space.
134   iterator_type is_it07(iss);
135   string result7;
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 );
140
141   iss.str("7.200.000.000,00 \244"); 
142   iterator_type is_it08(iss);
143   string result8;
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 );
148
149   iss.imbue(loc_hk);
150   iss.str("HK$7,200,000,000.00"); 
151   iterator_type is_it09(iss);
152   string result9;
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 );
157
158   iss.str("(HKD 100,000,000,000.00)"); 
159   iterator_type is_it10(iss);
160   string result10;
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 );
165
166   iss.str("(HKD .01)"); 
167   iterator_type is_it11(iss);
168   string result11;
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 );
173
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
177   // consumed.
178   iss.unsetf(ios_base::showbase);
179
180   iss.str("HK$7,200,000,000.00"); 
181   iterator_type is_it12(iss);
182   string result12;
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 );
187
188   iss.str("(HKD 100,000,000,000.00)"); 
189   iterator_type is_it13(iss);
190   string result13;
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 );
195
196   iss.str("(HKD .01)"); 
197   iterator_type is_it14(iss);
198   string result14;
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 );
203 }
204
205 // test double version
206 void test02()
207 {
208   using namespace std;
209   typedef money_base::part part;
210   typedef money_base::pattern pattern;
211   typedef istreambuf_iterator<char> iterator_type;
212
213   bool test = true;
214
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 );
224
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); 
234
235   // sanity check the data is correct.
236   const string empty;
237
238   // total EPA budget FY 2002
239   const long double  digits1 = 720000000000.0;
240
241   // est. cost, national missile "defense", expressed as a loss in USD 2001
242   const long double digits2 = -10000000000000.0;  
243
244   // input less than frac_digits
245   const long double digits4 = -1.0;
246   
247   iterator_type end;
248   istringstream iss;
249   iss.imbue(loc_de);
250   // cache the money_get facet
251   const money_get<char>& mon_get = use_facet<money_get<char> >(iss.getloc()); 
252
253   iss.str("7.200.000.000,00 ");
254   iterator_type is_it01(iss);
255   long double result1;
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 );
260
261   iss.str("7.200.000.000,00 ");
262   iterator_type is_it02(iss);
263   long double result2;
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 );
268
269   // now try with showbase, to get currency symbol in format
270   iss.setf(ios_base::showbase);
271
272   iss.imbue(loc_hk);
273   iss.str("(HKD .01)"); 
274   iterator_type is_it03(iss);
275   long double result3;
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 );
280 }
281
282 void test03()
283 {
284   using namespace std;
285   bool test = true;
286
287   // Check money_get works with other iterators besides streambuf
288   // input iterators.
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";
296
297   istringstream iss; 
298   iss.imbue(locale(loc_c, new mon_get_type));
299
300   // Iterator advanced, state, output.
301   const mon_get_type& mg = use_facet<mon_get_type>(iss.getloc());
302
303   // 01 string
304   string res1;
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" );
310
311   // 02 long double
312   iss.clear();
313   err = goodbit;
314   long double res2;
315   iter_type end2 = mg.get(str.begin(), str.end(), false, iss, err, res2);
316   string rem2(end2, str.end());
317   VERIFY( err == goodbit );
318   VERIFY( res2 == 1 );
319   VERIFY( rem2 == "Eleanor Roosevelt" );
320 }
321
322 // libstdc++/5280
323 void test04()
324 {
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);
329
330   // Set LANG environment variable to de_DE@euro.
331   const char* oldLANG = getenv("LANG");
332   if (!setenv("LANG", "de_DE@euro", 1))
333     {
334       test01();
335       test02();
336       test03();
337       setenv("LANG", oldLANG ? oldLANG : "", 1);
338     }
339 #endif
340 }
341
342 struct My_money_io : public std::moneypunct<char,false>
343 {
344   char_type do_decimal_point() const { return '.'; }
345   std::string do_grouping() const { return "\004"; }
346   
347   std::string do_curr_symbol() const { return "$"; }
348   std::string do_positive_sign() const { return ""; }
349   std::string do_negative_sign() const { return "-"; }
350   
351   int do_frac_digits() const { return 2; }
352
353   pattern do_pos_format() const
354   {
355     pattern pat = { { symbol, none, sign, value } };
356     return pat;
357   }
358
359   pattern do_neg_format() const
360   {
361     pattern pat = { { symbol, none, sign, value } };
362     return pat;
363   }
364 };
365
366 // libstdc++/5579
367 void test05()
368 {
369   using namespace std;
370   typedef istreambuf_iterator<char> InIt;
371
372   bool test = true;
373
374   locale loc(locale::classic(), new My_money_io);
375
376   string bufferp("$1234.56");
377   string buffern("$-1234.56");
378   string bufferp_ns("1234.56");
379   string buffern_ns("-1234.56");
380
381   bool intl = false;
382
383   InIt iendp, iendn, iendp_ns, iendn_ns;
384   ios_base::iostate err;
385   string valp, valn, valp_ns, valn_ns;
386
387   const money_get<char,InIt>& mg  =
388     use_facet<money_get<char, InIt> >(loc);
389
390   istringstream fmtp(bufferp);
391   fmtp.imbue(loc);
392   InIt ibegp(fmtp);
393   mg.get(ibegp,iendp,intl,fmtp,err,valp);
394   VERIFY( valp == "123456" );
395
396   istringstream fmtn(buffern);
397   fmtn.imbue(loc);
398   InIt ibegn(fmtn);
399   mg.get(ibegn,iendn,intl,fmtn,err,valn);
400   VERIFY( valn == "-123456" );
401
402   istringstream fmtp_ns(bufferp_ns);
403   fmtp_ns.imbue(loc);
404   InIt ibegp_ns(fmtp_ns);
405   mg.get(ibegp_ns,iendp_ns,intl,fmtp_ns,err,valp_ns);
406   VERIFY( valp_ns == "123456" );
407
408   istringstream fmtn_ns(buffern_ns);
409   fmtn_ns.imbue(loc);
410   InIt ibegn_ns(fmtn_ns);
411   mg.get(ibegn_ns,iendn_ns,intl,fmtn_ns,err,valn_ns);
412   VERIFY( valn_ns == "-123456" );
413 }
414
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.
418 void test06()
419 {
420   using namespace std;
421   bool test = true;
422
423   typedef istreambuf_iterator<char> InIt;
424   InIt iend1, iend2, iend3;
425
426   locale loc;
427   string buffer1("123");
428   string buffer2("456");
429   string buffer3("Golgafrincham"); // From Nathan's original idea.
430
431   string val;
432
433   ios_base::iostate err;
434
435   const money_get<char,InIt>& mg =
436     use_facet<money_get<char, InIt> >(loc);
437
438   istringstream fmt1(buffer1);
439   InIt ibeg1(fmt1);
440   mg.get(ibeg1,iend1,false,fmt1,err,val);
441   VERIFY( val == buffer1 );
442
443   istringstream fmt2(buffer2);
444   InIt ibeg2(fmt2);
445   mg.get(ibeg2,iend2,false,fmt2,err,val);
446   VERIFY( val == buffer2 );
447
448   val = buffer3;
449   istringstream fmt3(buffer3);
450   InIt ibeg3(fmt3);
451   mg.get(ibeg3,iend3,false,fmt3,err,val);
452   VERIFY( val == buffer3 );
453 }
454
455 struct My_money_io_a : public std::moneypunct<char,false>
456 {
457   char_type do_decimal_point() const { return '.'; }
458   std::string do_grouping() const { return "\004"; }
459   
460   std::string do_curr_symbol() const { return "$"; }
461   std::string do_positive_sign() const { return "()"; }
462   
463   int do_frac_digits() const { return 2; }
464
465   pattern do_pos_format() const
466   {
467     pattern pat = { { sign, value, space, symbol } };
468     return pat;
469   }
470 };
471
472 struct My_money_io_b : public std::moneypunct<char,false>
473 {
474   char_type do_decimal_point() const { return '.'; }
475   std::string do_grouping() const { return "\004"; }
476   
477   std::string do_curr_symbol() const { return "$"; }
478   std::string do_positive_sign() const { return "()"; }
479   
480   int do_frac_digits() const { return 2; }
481
482   pattern do_pos_format() const
483   {
484     pattern pat = { { sign, value, symbol, none } };
485     return pat;
486   }
487 };
488
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.
493 void test07()
494 {
495   using namespace std;
496   typedef istreambuf_iterator<char> InIt;
497
498   bool intl = false;
499   bool test = true;
500   ios_base::iostate err;
501
502   locale loc_a(locale::classic(), new My_money_io_a);
503
504   string buffer_a("(1234.56 $)");
505   string buffer_a_ns("(1234.56 )");
506
507   InIt iend_a, iend_a_ns;
508   string val_a, val_a_ns;
509
510   const money_get<char,InIt>& mg_a  =
511     use_facet<money_get<char, InIt> >(loc_a);
512
513   istringstream fmt_a(buffer_a);
514   fmt_a.imbue(loc_a);
515   InIt ibeg_a(fmt_a);
516   mg_a.get(ibeg_a,iend_a,intl,fmt_a,err,val_a);
517   VERIFY( val_a == "123456" );
518
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" );
524
525   locale loc_b(locale::classic(), new My_money_io_b);
526
527   string buffer_b("(1234.56$)");
528   string buffer_b_ns("(1234.56)");
529
530   InIt iend_b, iend_b_ns;
531   string val_b, val_b_ns;
532
533   const money_get<char,InIt>& mg_b  =
534     use_facet<money_get<char, InIt> >(loc_b);
535
536   istringstream fmt_b(buffer_b);
537   fmt_b.imbue(loc_b);
538   InIt ibeg_b(fmt_b);
539   mg_b.get(ibeg_b,iend_b,intl,fmt_b,err,val_b);
540   VERIFY( val_b == "123456" );
541
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" );
547 }
548
549 // http://gcc.gnu.org/ml/libstdc++/2002-05/msg00038.html
550 void test08()
551 {
552   bool test = true;
553
554   const char* tentLANG = std::setlocale(LC_ALL, "ja_JP.eucjp");
555   if (tentLANG != NULL)
556     {
557       std::string preLANG = tentLANG;
558       test01();
559       test02();
560       test03();
561       test05();
562       test06();
563       test07();
564       std::string postLANG = std::setlocale(LC_ALL, NULL);
565       VERIFY( preLANG == postLANG );
566     }
567 }
568
569 int main()
570 {
571   test01();
572   test02();
573   test03();
574   test04();
575   test05();
576   test06();
577   test07();
578   test08();
579   return 0;
580 }