1 // 2001-11-21 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.2.1.1 num_get members
25 #include <testsuite_hooks.h>
27 // XXX This test is not working for non-glibc locale models.
28 // { dg-do run { xfail *-*-* } }
33 typedef istreambuf_iterator<char> iterator_type;
38 locale loc_c = locale::classic();
39 locale loc_hk("en_HK");
40 locale loc_fr("fr_FR@euro");
41 locale loc_de("de_DE");
42 VERIFY( loc_c != loc_de );
43 VERIFY( loc_hk != loc_fr );
44 VERIFY( loc_hk != loc_de );
45 VERIFY( loc_de != loc_fr );
47 // cache the numpunct facets
48 const numpunct<char>& numpunct_c = use_facet<numpunct<char> >(loc_c);
49 const numpunct<char>& numpunct_de = use_facet<numpunct<char> >(loc_de);
50 const numpunct<char>& numpunct_hk = use_facet<numpunct<char> >(loc_hk);
52 // sanity check the data is correct.
59 long l2 = -2147483647;
61 unsigned long ul1 = 1294967294;
62 unsigned long ul2 = 0;
64 double d1 = 1.02345e+308;
65 double d2 = 3.15e-308;
67 long double ld1 = 6.630025e+4;
68 long double ld2 = 0.0;
71 const void* cv = &ul2;
73 // cache the num_get facet
76 const num_get<char>& ng = use_facet<num_get<char> >(iss.getloc());
77 const ios_base::iostate goodbit = ios_base::goodbit;
78 const ios_base::iostate eofbit = ios_base::eofbit;
79 ios_base::iostate err = ios_base::goodbit;
83 iterator_type os_it00 = iss.rdbuf();
84 iterator_type os_it01 = ng.get(os_it00, 0, iss, err, b1);
86 VERIFY( err & ios_base::eofbit );
90 ng.get(iss.rdbuf(), 0, iss, err, b0);
91 VERIFY( b0 == false );
92 VERIFY( err & eofbit );
94 // bool, more twisted examples
98 iss.setf(ios_base::boolalpha);
100 ng.get(iss.rdbuf(), 0, iss, err, b0);
101 VERIFY( b0 == true );
102 VERIFY( err == goodbit );
106 iss.setf(ios_base::boolalpha);
108 ng.get(iss.rdbuf(), 0, iss, err, b1);
109 VERIFY( b1 == false );
110 VERIFY( err == goodbit );
112 // long, in a locale that expects grouping
114 iss.str("2,147,483,647 ");
117 ng.get(iss.rdbuf(), 0, iss, err, l);
119 VERIFY( err == goodbit );
121 iss.str("-2,147,483,647++++++");
124 ng.get(iss.rdbuf(), 0, iss, err, l);
126 VERIFY( err == goodbit );
128 // unsigned long, in a locale that does not group
130 iss.str("1294967294");
133 ng.get(iss.rdbuf(), 0, iss, err, ul);
135 VERIFY( err == eofbit );
137 iss.str("0+++++++++++++++++++");
140 ng.get(iss.rdbuf(), 0, iss, err, ul);
142 VERIFY( err == goodbit );
144 // ... and one that does
146 iss.str("1.294.967.294+++++++");
149 iss.setf(ios_base::left, ios_base::adjustfield);
151 ng.get(iss.rdbuf(), 0, iss, err, ul);
153 VERIFY( err == goodbit );
157 iss.str("1.02345e+308++++++++");
160 iss.setf(ios_base::left, ios_base::adjustfield);
162 ng.get(iss.rdbuf(), 0, iss, err, d);
164 VERIFY( err == goodbit );
166 iss.str("+3.15e-308");
169 iss.setf(ios_base::right, ios_base::adjustfield);
171 ng.get(iss.rdbuf(), 0, iss, err, d);
173 VERIFY( err == eofbit );
176 iss.str("+1,02345e+308");
179 iss.setf(ios_base::right, ios_base::adjustfield);
180 iss.setf(ios_base::scientific, ios_base::floatfield);
182 ng.get(iss.rdbuf(), 0, iss, err, d);
184 VERIFY( err == eofbit );
186 iss.str("3,15E-308 ");
190 iss.setf(ios_base::right, ios_base::adjustfield);
191 iss.setf(ios_base::scientific, ios_base::floatfield);
192 iss.setf(ios_base::uppercase);
194 ng.get(iss.rdbuf(), 0, iss, err, d);
196 VERIFY( err == goodbit );
199 iss.str("6,630025e+4");
202 ng.get(iss.rdbuf(), 0, iss, err, ld);
204 VERIFY( err == eofbit );
209 iss.setf(ios_base::fixed, ios_base::floatfield);
211 ng.get(iss.rdbuf(), 0, iss, err, ld);
213 VERIFY( err == goodbit );
216 iss.str("0xbffff74c.");
219 ng.get(iss.rdbuf(), 0, iss, err, v);
221 VERIFY( err == goodbit );
224 #ifdef _GLIBCPP_USE_LONG_LONG
225 long long ll1 = 9223372036854775807;
226 long long ll2 = -9223372036854775807;
229 iss.str("9.223.372.036.854.775.807");
232 ng.get(iss.rdbuf(), 0, iss, err, ll);
234 VERIFY( err == eofbit );
238 // 2002-01-10 David Seymour <seymour_dj@yahoo.com>
245 // Num_get works with other iterators besides streambuf output iterators
246 typedef string::const_iterator iter_type;
247 typedef num_get<char, iter_type> num_get_type;
248 const ios_base::iostate goodbit = ios_base::goodbit;
249 const ios_base::iostate eofbit = ios_base::eofbit;
250 ios_base::iostate err = ios_base::goodbit;
251 const locale loc_c = locale::classic();
254 const string str = "20000106 Elizabeth Durack";
255 istringstream iss; // need an ios, add my num_get facet
256 iss.imbue(locale(loc_c, new num_get_type));
258 // Iterator advanced, state, output.
259 const num_get_type& ng = use_facet<num_get_type>(iss.getloc());
260 iter_type end = ng.get(str.begin(), str.end(), iss, err, i);
261 string rem(end, str.end());
263 VERIFY( err == goodbit );
264 VERIFY( i == 20000106);
265 VERIFY( rem == " Elizabeth Durack" );
276 // Kathleen Hannah, humanitarian, woman, art-thief