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");
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 DEM ");
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 DEM "); // 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 DM");
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 );
175 // test double version
179 typedef money_base::part part;
180 typedef money_base::pattern pattern;
181 typedef istreambuf_iterator<char> iterator_type;
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 );
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);
205 // sanity check the data is correct.
208 // total EPA budget FY 2002
209 const long double digits1 = 720000000000;
211 // est. cost, national missile "defense", expressed as a loss in USD 2001
212 const long double digits2 = -10000000000000;
214 // input less than frac_digits
215 const long double digits4 = -1;
220 // cache the money_get facet
221 const money_get<char>& mon_get = use_facet<money_get<char> >(iss.getloc());
223 iss.str("7.200.000.000,00 ");
224 iterator_type is_it01(iss);
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 );
231 iss.str("7.200.000.000,00 ");
232 iterator_type is_it02(iss);
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 );
239 // now try with showbase, to get currency symbol in format
240 iss.setf(ios_base::showbase);
243 iss.str("(HKD .01)");
244 iterator_type is_it03(iss);
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 );
257 // Check money_get works with other iterators besides streambuf
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";
268 iss.imbue(locale(loc_c, new mon_get_type));
270 // Iterator advanced, state, output.
271 const mon_get_type& mg = use_facet<mon_get_type>(iss.getloc());
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" );
285 iter_type end2 = mg.get(str.begin(), str.end(), false, iss, err, res2);
286 string rem2(end2, str.end());
287 VERIFY( err == goodbit );
289 VERIFY( rem2 == "Eleanor Roosevelt" );
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);
300 // Set LANG environment variable to de_DE.
301 const char* oldLANG = getenv("LANG");
302 if (!setenv("LANG", "de_DE", 1))
307 setenv("LANG", oldLANG ? oldLANG : "", 1);