3 // Copyright (C) 1999, 2000 Free Software Foundation, Inc.
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 // 27.6.1.2.2 arithmetic extractors
23 #include <cstdio> // for printf
28 #include <debug_assert.h>
31 std::string
str_02("true false 0 1 110001");
32 std::string
str_03("-19999999 777777 -234234 233 -234 33 1 66300.25 .315 1.5");
33 std::string
str_04("0123");
35 std::stringbuf
isbuf_01(std::ios_base::in
);
36 std::stringbuf
isbuf_02(str_02
, std::ios_base::in
);
37 std::stringbuf
isbuf_03(str_03
, std::ios_base::in
);
38 std::stringbuf
isbuf_04(str_04
, std::ios_base::in
);
40 std::istream
is_01(NULL
);
41 std::istream
is_02(&isbuf_02
);
42 std::istream
is_03(&isbuf_03
);
43 std::istream
is_04(&isbuf_04
);
44 std::stringstream
ss_01(str_01
);
46 // minimal sanity check
57 unsigned short us1
= 0;
59 unsigned long ul1
= 0;
61 // Floating-point Types:
66 // process alphanumeric versions of bool values
67 std::ios_base::fmtflags fmt
= is_02
.flags();
68 bool testfmt
= fmt
& std::ios_base::boolalpha
;
69 is_02
.setf(std::ios_base::boolalpha
);
71 testfmt
= fmt
& std::ios_base::boolalpha
;
77 // process numeric versions of of bool values
78 is_02
.unsetf(std::ios_base::boolalpha
);
80 testfmt
= fmt
& std::ios_base::boolalpha
;
86 // is_03 == "-19999999 777777 -234234 233 -234 33 1 66300.25 .315 1.5"
88 VERIFY( l1
== -19999999 );
90 VERIFY( ul1
== 777777 );
92 VERIFY( i1
== -234234 );
102 VERIFY( ld1
== 66300.25 );
104 VERIFY( d1
== .315 );
108 is_04
>> std::hex
>> i1
;
109 std::printf ("%d %d %d\n", i1
, i1
== 0x123, test
);
110 VERIFY( i1
== 0x123 );
111 std::printf ("%d %d %d\n", i1
, i1
== 0x123, test
);
113 // test void pointers
120 std::printf ("%x %x\n", pi
, po
);
130 // elaborated test for ints
134 const std::string
str_01("20000AB");
135 std::stringbuf
strb_01(str_01
, std::ios_base::in
);
136 std::istream
is(&strb_01
);
140 VERIFY( n
== 20000 );
154 std::istream
istr(&sbuf
);
155 std::ostream
ostr(&sbuf
);
160 istr
>> l01
; // _M_in_end set completely incorrectly here.
161 VERIFY( l01
== 12220101 );
162 VERIFY( istr
.rdstate() == std::ios_base::eofbit
);
171 // http://sourceware.cygnus.com/ml/libstdc++/2000-q1/msg00081.html
175 // default locale, grouping is turned off
177 unsigned int h4
, h3
, h2
;
179 std::string
s("205,199,144");
180 std::istringstream
is(s
);
193 VERIFY( is
.rdstate() == std::ios_base::eofbit
);
197 VERIFY( static_cast<bool>(is
.rdstate() & std::ios_base::failbit
) );
205 class test_numpunct1
: public numpunct
<char>
210 { return string(1, '\003'); }
216 // manufactured locale, grouping is turned on
218 unsigned int h4
= 0, h3
= 0, h2
= 0;
220 const std::string
s1("205,199 23,445.25 1,024,365 123,22,24");
221 std::istringstream
is(s1
);
222 is
.imbue(std::locale(std::locale(), new std::test_numpunct1
));
226 VERIFY( h4
== 205199 );
231 VERIFY( f1
== 23445.25 );
236 VERIFY( h3
== 1024365 );
242 VERIFY( static_cast<bool>(is
.rdstate() & std::ios_base::failbit
) );
243 VERIFY( static_cast<bool>(is
.rdstate() & std::ios_base::eofbit
) );
245 // Stress tests for explicit errors in grouping corner cases. The
246 // validity of these tests and results have been hammered out in
247 // private email between bkoz and ncm between Jan 25 and Jan 27, 2000.
248 // Thanks nate -- benjamin
249 const std::string
s2(",111 4,,4 0.25,345 5..25 156,, 1,000000 1000000 1234,567");
252 const char c_control
= '?';
259 VERIFY( static_cast<bool>(is
.rdstate() & std::ios_base::failbit
) );
268 VERIFY( static_cast<bool>(is
.rdstate() & std::ios_base::failbit
) );
277 VERIFY( f1
== 0.25 );
296 VERIFY( static_cast<bool>(is
.rdstate() & std::ios_base::failbit
) );
299 VERIFY( c
== ',' ); // second one
304 VERIFY( static_cast<bool>(is
.rdstate() & std::ios_base::failbit
) );
308 VERIFY( h2
== 1000000 );
314 VERIFY( static_cast<bool>(is
.rdstate() & std::ios_base::failbit
) );
315 VERIFY( static_cast<bool>(is
.rdstate() & std::ios_base::eofbit
) );
324 class test_numpunct2
: public numpunct
<char>
329 { return string("\002\003"); }
335 // manufactured locale, grouping is turned on
337 unsigned int h4
= 0, h3
= 0, h2
= 0;
339 const std::string
s1("1,22 205,19 22,123,22");
340 const std::string
s2("1,220 2050,19 202,123,22");
342 std::istringstream
is(s1
);
343 is
.imbue(std::locale(std::locale(), new std::test_numpunct2
));
352 VERIFY( h3
== 20519 );
357 VERIFY( h2
== 2212322 );
358 VERIFY( static_cast<bool>(is
.rdstate() & std::ios_base::eofbit
) );
371 std::string
st("2.456e3-+0.567e-2");
372 std::stringbuf
sb(st
);
373 std::istream
is(&sb
);
374 double f1
= 0, f2
= 0;
380 VERIFY( f2
== 0.00567 );
390 std::string
str_01("0 00 000 +0 + 0 - 0");
391 std::stringbuf
isbuf_01(str_01
);
392 std::istream
is_01(&isbuf_01
);
414 VERIFY( is_01
.rdstate() == std::ios_base::eofbit
);
416 std::string
str_02("0x32 0X33 033 33");
417 std::stringbuf
isbuf_02(str_02
);
418 std::istream
is_02(&isbuf_02
);
419 is_02
.unsetf(std::ios_base::basefield
);
428 VERIFY( is_02
.rdstate() == std::ios_base::eofbit
);
430 std::stringbuf
isbuf_03(str_02
);
431 std::istream
is_03(&isbuf_03
);
435 is_03
>> std::dec
>> n
>> c
>> m
;
440 is_03
>> std::oct
>> m
>> c
>> n
;
445 is_03
>> std::dec
>> m
>> n
;
448 VERIFY( is_03
.rdstate() == std::ios_base::eofbit
);
450 std::string
str_04("3. 4.5E+ 2a5E-3 .6E1");
451 std::stringbuf
isbuf_04(str_04
);
452 std::istream
is_04(&isbuf_04
);
458 VERIFY( f
== 450.0 );
461 VERIFY( f
== 0.005 );
464 VERIFY( is_03
.rdstate() == std::ios_base::eofbit
);
466 std::string
str_05("0E20 5Ea E16");
467 std::stringbuf
isbuf_05(str_05
);
468 std::istream
is_05(&isbuf_05
);
474 VERIFY( is_05
.rdstate() == std::ios_base::failbit
);
480 VERIFY( is_05
.rdstate() == std::ios_base::failbit
);
511 // paul miller was right on with riddim warfare!