OSDN Git Service

* testsuite/22_locale/codecvt_members_char_char.cc: Do not
[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");
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 DEM ");
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 DEM  "); // 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 DM"); 
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
175 // test double version
176 void test02()
177 {
178   using namespace std;
179   typedef money_base::part part;
180   typedef money_base::pattern pattern;
181   typedef istreambuf_iterator<char> iterator_type;
182
183   bool test = true;
184
185   // basic construction
186   locale loc_c = locale::classic();
187   locale loc_hk("en_HK");
188   locale loc_fr("fr_FR@euro");
189   locale loc_de("de_DE");
190   VERIFY( loc_c != loc_de );
191   VERIFY( loc_hk != loc_fr );
192   VERIFY( loc_hk != loc_de );
193   VERIFY( loc_de != loc_fr );
194
195   // cache the moneypunct facets
196   typedef moneypunct<char, true> __money_true;
197   typedef moneypunct<char, false> __money_false;
198   const __money_true& monpunct_c_t = use_facet<__money_true>(loc_c); 
199   const __money_true& monpunct_de_t = use_facet<__money_true>(loc_de); 
200   const __money_false& monpunct_c_f = use_facet<__money_false>(loc_c); 
201   const __money_false& monpunct_de_f = use_facet<__money_false>(loc_de); 
202   const __money_true& monpunct_hk_t = use_facet<__money_true>(loc_hk); 
203   const __money_false& monpunct_hk_f = use_facet<__money_false>(loc_hk); 
204
205   // sanity check the data is correct.
206   const string empty;
207
208   // total EPA budget FY 2002
209   const long double  digits1 = 720000000000;
210
211   // est. cost, national missile "defense", expressed as a loss in USD 2001
212   const long double digits2 = -10000000000000;  
213
214   // input less than frac_digits
215   const long double digits4 = -1;
216   
217   iterator_type end;
218   istringstream iss;
219   iss.imbue(loc_de);
220   // cache the money_get facet
221   const money_get<char>& mon_get = use_facet<money_get<char> >(iss.getloc()); 
222
223   iss.str("7.200.000.000,00 ");
224   iterator_type is_it01(iss);
225   long double result1;
226   ios_base::iostate err01 = ios_base::goodbit;
227   mon_get.get(is_it01, end, true, iss, err01, result1);
228   VERIFY( result1 == digits1 );
229   VERIFY( err01 == ios_base::eofbit );
230
231   iss.str("7.200.000.000,00 ");
232   iterator_type is_it02(iss);
233   long double result2;
234   ios_base::iostate err02 = ios_base::goodbit;
235   mon_get.get(is_it02, end, false, iss, err02, result2);
236   VERIFY( result2 == digits1 );
237   VERIFY( err02 == ios_base::eofbit );
238
239   // now try with showbase, to get currency symbol in format
240   iss.setf(ios_base::showbase);
241
242   iss.imbue(loc_hk);
243   iss.str("(HKD .01)"); 
244   iterator_type is_it03(iss);
245   long double result3;
246   ios_base::iostate err03 = ios_base::goodbit;
247   mon_get.get(is_it03, end, true, iss, err03, result3);
248   VERIFY( result3 == digits4 );
249   VERIFY( err03 == ios_base::goodbit );
250 }
251
252 void test03()
253 {
254   using namespace std;
255   bool test = true;
256
257   // Check money_get works with other iterators besides streambuf
258   // input iterators.
259   typedef string::const_iterator iter_type;
260   typedef money_get<char, iter_type> mon_get_type;
261   const ios_base::iostate goodbit = ios_base::goodbit;
262   const ios_base::iostate eofbit = ios_base::eofbit;
263   ios_base::iostate err = goodbit;
264   const locale loc_c = locale::classic();
265   const string str = "0.01Eleanor Roosevelt";
266
267   istringstream iss; 
268   iss.imbue(locale(loc_c, new mon_get_type));
269
270   // Iterator advanced, state, output.
271   const mon_get_type& mg = use_facet<mon_get_type>(iss.getloc());
272
273   // 01 string
274   string res1;
275   iter_type end1 = mg.get(str.begin(), str.end(), false, iss, err, res1);
276   string rem1(end1, str.end());
277   VERIFY( err == goodbit );
278   VERIFY( res1 == "1" );
279   VERIFY( rem1 == "Eleanor Roosevelt" );
280
281   // 02 long double
282   iss.clear();
283   err = goodbit;
284   long double res2;
285   iter_type end2 = mg.get(str.begin(), str.end(), false, iss, err, res2);
286   string rem2(end2, str.end());
287   VERIFY( err == goodbit );
288   VERIFY( res2 == 1 );
289   VERIFY( rem2 == "Eleanor Roosevelt" );
290 }
291
292 // libstdc++/5280
293 void test04()
294 {
295 #ifdef _GLIBCPP_HAVE_SETENV 
296   // Set the global locale to non-"C".
297   std::locale loc_de("de_DE");
298   std::locale::global(loc_de);
299
300   // Set LANG environment variable to de_DE.
301   const char* oldLANG = getenv("LANG");
302   if (!setenv("LANG", "de_DE", 1))
303     {
304       test01();
305       test02();
306       test03();
307       setenv("LANG", oldLANG ? oldLANG : "", 1);
308     }
309 #endif
310 }
311
312 int main()
313 {
314   test01();
315   test02();
316   test03();
317   test04();
318   return 0;
319 }