OSDN Git Service

2002-01-24 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 22_locale / money_get_members_wchar_t.cc
1 // 2001-09-14 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 #ifdef _GLIBCPP_USE_WCHAR_T
31 // test string version
32 void test01()
33 {
34   using namespace std;
35   typedef money_base::part part;
36   typedef money_base::pattern pattern;
37   typedef istreambuf_iterator<wchar_t> iterator_type;
38
39   bool test = true;
40
41   // basic construction
42   locale loc_c = locale::classic();
43   locale loc_hk("en_HK");
44   locale loc_fr("fr_FR@euro");
45   locale loc_de("de_DE");
46   VERIFY( loc_c != loc_de );
47   VERIFY( loc_hk != loc_fr );
48   VERIFY( loc_hk != loc_de );
49   VERIFY( loc_de != loc_fr );
50
51   // cache the moneypunct facets
52   typedef moneypunct<wchar_t, true> __money_true;
53   typedef moneypunct<wchar_t, false> __money_false;
54   const __money_true& monpunct_c_t = use_facet<__money_true>(loc_c); 
55   const __money_true& monpunct_de_t = use_facet<__money_true>(loc_de); 
56   const __money_false& monpunct_c_f = use_facet<__money_false>(loc_c); 
57   const __money_false& monpunct_de_f = use_facet<__money_false>(loc_de); 
58   const __money_true& monpunct_hk_t = use_facet<__money_true>(loc_hk); 
59   const __money_false& monpunct_hk_f = use_facet<__money_false>(loc_hk); 
60
61   // sanity check the data is correct.
62   const wstring empty;
63
64   // total EPA budget FY 2002
65   const wstring digits1(L"720000000000");
66
67   // est. cost, national missile "defense", expressed as a loss in USD 2001
68   const wstring digits2(L"-10000000000000");  
69
70   // not valid input
71   const wstring digits3(L"-A"); 
72
73   // input less than frac_digits
74   const wstring digits4(L"-1");
75   
76   iterator_type end;
77   wistringstream iss;
78   iss.imbue(loc_de);
79   // cache the money_get facet
80   const money_get<wchar_t>& mon_get = use_facet<money_get<wchar_t> >(iss.getloc()); 
81
82
83   iss.str(L"7.200.000.000,00 ");
84   iterator_type is_it01(iss);
85   wstring result1;
86   ios_base::iostate err01 = ios_base::goodbit;
87   mon_get.get(is_it01, end, true, iss, err01, result1);
88   VERIFY( result1 == digits1 );
89   VERIFY( err01 == ios_base::eofbit );
90
91   iss.str(L"7.200.000.000,00  ");
92   iterator_type is_it02(iss);
93   wstring result2;
94   ios_base::iostate err02 = ios_base::goodbit;
95   mon_get.get(is_it02, end, true, iss, err02, result2);
96   VERIFY( result2 == digits1 );
97   VERIFY( err02 == ios_base::eofbit );
98
99   iss.str(L"7.200.000.000,00  a");
100   iterator_type is_it03(iss);
101   wstring result3;
102   ios_base::iostate err03 = ios_base::goodbit;
103   mon_get.get(is_it03, end, true, iss, err03, result3);
104   VERIFY( result3 == digits1 );
105   VERIFY( err03 == ios_base::goodbit );
106
107   iss.str(L"");
108   iterator_type is_it04(iss);
109   wstring result4;
110   ios_base::iostate err04 = ios_base::goodbit;
111   mon_get.get(is_it04, end, true, iss, err04, result4);
112   VERIFY( result4 == empty );
113   VERIFY( err04 == ios_base::failbit | ios_base::eofbit );
114
115   iss.str(L"working for enlightenment and peace in a mad world");
116   iterator_type is_it05(iss);
117   wstring result5;
118   ios_base::iostate err05 = ios_base::goodbit;
119   mon_get.get(is_it05, end, true, iss, err05, result5);
120   VERIFY( result5 == empty );
121   VERIFY( err05 == ios_base::failbit );
122
123   // now try with showbase, to get currency symbol in format
124   iss.setf(ios_base::showbase);
125
126   iss.str(L"7.200.000.000,00 DEM ");
127   iterator_type is_it06(iss);
128   wstring result6;
129   ios_base::iostate err06 = ios_base::goodbit;
130   mon_get.get(is_it06, end, true, iss, err06, result6);
131   VERIFY( result6 == digits1 );
132   VERIFY( err06 == ios_base::eofbit );
133
134   iss.str(L"7.200.000.000,00 DEM  "); // Extra space.
135   iterator_type is_it07(iss);
136   wstring result7;
137   ios_base::iostate err07 = ios_base::goodbit;
138   mon_get.get(is_it07, end, true, iss, err07, result7);
139   VERIFY( result7 == digits1 );
140   VERIFY( err07 == ios_base::goodbit );
141
142   iss.str(L"7.200.000.000,00 DM"); 
143   iterator_type is_it08(iss);
144   wstring result8;
145   ios_base::iostate err08 = ios_base::goodbit;
146   mon_get.get(is_it08, end, false, iss, err08, result8);
147   VERIFY( result8 == digits1 );
148   VERIFY( err08 == ios_base::eofbit );
149
150   iss.imbue(loc_hk);
151   iss.str(L"HK$7,200,000,000.00"); 
152   iterator_type is_it09(iss);
153   wstring result9;
154   ios_base::iostate err09 = ios_base::goodbit;
155   mon_get.get(is_it09, end, false, iss, err09, result9);
156   VERIFY( result9 == digits1 );
157   VERIFY( err09 == ios_base::eofbit );
158
159   iss.str(L"(HKD 100,000,000,000.00)"); 
160   iterator_type is_it10(iss);
161   wstring result10;
162   ios_base::iostate err10 = ios_base::goodbit;
163   mon_get.get(is_it10, end, true, iss, err10, result10);
164   VERIFY( result10 == digits2 );
165   VERIFY( err10 == ios_base::goodbit );
166
167   iss.str(L"(HKD .01)"); 
168   iterator_type is_it11(iss);
169   wstring result11;
170   ios_base::iostate err11 = ios_base::goodbit;
171   mon_get.get(is_it11, end, true, iss, err11, result11);
172   VERIFY( result11 == digits4 );
173   VERIFY( err11 == ios_base::goodbit );
174 }
175
176
177 // test double/wstring versions
178 void test02()
179 {
180   using namespace std;
181   typedef money_base::part part;
182   typedef money_base::pattern pattern;
183   typedef istreambuf_iterator<wchar_t> iterator_type;
184
185   bool test = true;
186
187   // basic construction
188   locale loc_c = locale::classic();
189   locale loc_hk("en_HK");
190   locale loc_fr("fr_FR@euro");
191   locale loc_de("de_DE");
192   VERIFY( loc_c != loc_de );
193   VERIFY( loc_hk != loc_fr );
194   VERIFY( loc_hk != loc_de );
195   VERIFY( loc_de != loc_fr );
196
197   // cache the moneypunct facets
198   typedef moneypunct<wchar_t, true> __money_true;
199   typedef moneypunct<wchar_t, false> __money_false;
200   const __money_true& monpunct_c_t = use_facet<__money_true>(loc_c); 
201   const __money_true& monpunct_de_t = use_facet<__money_true>(loc_de); 
202   const __money_false& monpunct_c_f = use_facet<__money_false>(loc_c); 
203   const __money_false& monpunct_de_f = use_facet<__money_false>(loc_de); 
204   const __money_true& monpunct_hk_t = use_facet<__money_true>(loc_hk); 
205   const __money_false& monpunct_hk_f = use_facet<__money_false>(loc_hk); 
206
207   // sanity check the data is correct.
208   const wstring empty;
209
210   // total EPA budget FY 2002
211   const long double  digits1 = 720000000000;
212
213   // est. cost, national missile "defense", expressed as a loss in USD 2001
214   const long double digits2 = -10000000000000;  
215
216   // input less than frac_digits
217   const long double digits4 = -1;
218   
219   iterator_type end;
220   wistringstream iss;
221   iss.imbue(loc_de);
222   // cache the money_get facet
223   const money_get<wchar_t>& mon_get = use_facet<money_get<wchar_t> >(iss.getloc()); 
224
225   iss.str(L"7.200.000.000,00 ");
226   iterator_type is_it01(iss);
227   long double result1;
228   ios_base::iostate err01 = ios_base::goodbit;
229   mon_get.get(is_it01, end, true, iss, err01, result1);
230   VERIFY( result1 == digits1 );
231   VERIFY( err01 == ios_base::eofbit );
232
233   iss.str(L"7.200.000.000,00 ");
234   iterator_type is_it02(iss);
235   long double result2;
236   ios_base::iostate err02 = ios_base::goodbit;
237   mon_get.get(is_it02, end, false, iss, err02, result2);
238   VERIFY( result2 == digits1 );
239   VERIFY( err02 == ios_base::eofbit );
240
241   // now try with showbase, to get currency symbol in format
242   iss.setf(ios_base::showbase);
243
244   iss.imbue(loc_hk);
245   iss.str(L"(HKD .01)"); 
246   iterator_type is_it03(iss);
247   long double result3;
248   ios_base::iostate err03 = ios_base::goodbit;
249   mon_get.get(is_it03, end, true, iss, err03, result3);
250   VERIFY( result3 == digits4 );
251   VERIFY( err03 == ios_base::goodbit );
252 }
253
254 void test03()
255 {
256   using namespace std;
257   bool test = true;
258
259   // Check money_get works with other iterators besides streambuf
260   // output iterators.
261   typedef wstring::const_iterator iter_type;
262   typedef money_get<wchar_t, iter_type> mon_get_type;
263   const ios_base::iostate goodbit = ios_base::goodbit;
264   const ios_base::iostate eofbit = ios_base::eofbit;
265   ios_base::iostate err = goodbit;
266   const locale loc_c = locale::classic();
267   const wstring str = L"0.01Eleanor Roosevelt";
268
269   wistringstream iss; 
270   iss.imbue(locale(loc_c, new mon_get_type));
271
272   // Iterator advanced, state, output.
273   const mon_get_type& mg = use_facet<mon_get_type>(iss.getloc());
274
275   // 01 string
276   wstring res1;
277   iter_type end1 = mg.get(str.begin(), str.end(), false, iss, err, res1);
278   wstring rem1(end1, str.end());
279   VERIFY( err == goodbit );
280   VERIFY( res1 == L"1" );
281   VERIFY( rem1 == L"Eleanor Roosevelt" );
282
283   // 02 long double
284   iss.clear();
285   err = goodbit;
286   long double res2;
287   iter_type end2 = mg.get(str.begin(), str.end(), false, iss, err, res2);
288   wstring rem2(end2, str.end());
289   VERIFY( err == goodbit );
290   VERIFY( res2 == 1 );
291   VERIFY( rem2 == L"Eleanor Roosevelt" );
292 }
293
294 // libstdc++/5280
295 void test04()
296 {
297 #ifdef _GLIBCPP_HAVE_SETENV 
298   // Set the global locale to non-"C".
299   std::locale loc_de("de_DE");
300   std::locale::global(loc_de);
301
302   // Set LANG environment variable to de_DE.
303   const char* oldLANG = getenv("LANG");
304   if (!setenv("LANG", "de_DE", 1))
305     {
306       test01();
307       test02();
308       test03();
309       setenv("LANG", oldLANG, 1);
310     }
311 #endif
312 }
313 #endif
314
315
316 int main()
317 {
318 #ifdef _GLIBCPP_USE_WCHAR_T
319   test01();
320   test02();
321   test03();
322   test04();
323 #endif
324   return 0;
325 }