]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/testsuite/27_io/istream_extractor_arith.cc
*.cc: Remove spaces, make sure testcases return zero.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / istream_extractor_arith.cc
CommitLineData
b2dad0e3
BK
1// 1999-04-12 bkoz
2
3// Copyright (C) 1999, 2000 Free Software Foundation, Inc.
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// 27.6.1.2.2 arithmetic extractors
22
4bc95009 23#include <cstdio> // for printf
b2dad0e3
BK
24#include <istream>
25#include <ostream>
26#include <sstream>
27#include <locale>
aa1b2f7d 28#include <debug_assert.h>
b2dad0e3
BK
29
30std::string str_01;
31std::string str_02("true false 0 1 110001");
32std::string str_03("-19999999 777777 -234234 233 -234 33 1 66300.25 .315 1.5");
f22ad9d0 33std::string str_04("0123");
b2dad0e3
BK
34
35std::stringbuf isbuf_01(std::ios_base::in);
36std::stringbuf isbuf_02(str_02, std::ios_base::in);
37std::stringbuf isbuf_03(str_03, std::ios_base::in);
f22ad9d0
BK
38std::stringbuf isbuf_04(str_04, std::ios_base::in);
39
b2dad0e3
BK
40std::istream is_01(NULL);
41std::istream is_02(&isbuf_02);
42std::istream is_03(&isbuf_03);
f22ad9d0 43std::istream is_04(&isbuf_04);
b2dad0e3
BK
44std::stringstream ss_01(str_01);
45
46// minimal sanity check
47bool test01() {
48
49 bool test = true;
50
51 // Integral Types:
52 bool b1 = false;
53 bool b2 = false;
54 short s1 = 0;
55 int i1 = 0;
56 long l1 = 0;
57 unsigned short us1 = 0;
58 unsigned int ui1 = 0;
59 unsigned long ul1 = 0;
60
61 // Floating-point Types:
62 float f1 = 0;
63 double d1 = 0;
64 long double ld1 = 0;
65
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);
70 fmt = is_02.flags();
71 testfmt = fmt & std::ios_base::boolalpha;
72 is_02 >> b1;
aa1b2f7d 73 VERIFY( b1 == 1 );
b2dad0e3 74 is_02 >> b1;
aa1b2f7d 75 VERIFY( b1 == 0 );
b2dad0e3
BK
76
77 // process numeric versions of of bool values
78 is_02.unsetf(std::ios_base::boolalpha);
79 fmt = is_02.flags();
80 testfmt = fmt & std::ios_base::boolalpha;
81 is_02 >> b1;
aa1b2f7d 82 VERIFY( b1 == 0 );
b2dad0e3 83 is_02 >> b1;
aa1b2f7d 84 VERIFY( b1 == 1 );
b2dad0e3
BK
85
86 // is_03 == "-19999999 777777 -234234 233 -234 33 1 66300.25 .315 1.5"
87 is_03 >> l1;
aa1b2f7d 88 VERIFY( l1 == -19999999 );
b2dad0e3 89 is_03 >> ul1;
aa1b2f7d 90 VERIFY( ul1 == 777777 );
b2dad0e3 91 is_03 >> i1;
aa1b2f7d 92 VERIFY( i1 == -234234 );
b2dad0e3 93 is_03 >> ui1;
aa1b2f7d 94 VERIFY( ui1 == 233 );
b2dad0e3 95 is_03 >> s1;
aa1b2f7d 96 VERIFY( s1 == -234 );
b2dad0e3 97 is_03 >> us1;
aa1b2f7d 98 VERIFY( us1 == 33 );
b2dad0e3 99 is_03 >> b1;
aa1b2f7d 100 VERIFY( b1 == 1 );
b2dad0e3 101 is_03 >> ld1;
aa1b2f7d 102 VERIFY( ld1 == 66300.25 );
b2dad0e3 103 is_03 >> d1;
aa1b2f7d 104 VERIFY( d1 == .315 );
b2dad0e3 105 is_03 >> f1;
aa1b2f7d 106 VERIFY( f1 == 1.5 );
b2dad0e3 107
f22ad9d0 108 is_04 >> std::hex >> i1;
4bc95009 109 std::printf ("%d %d %d\n", i1, i1 == 0x123, test);
aa1b2f7d 110 VERIFY( i1 == 0x123 );
4bc95009 111 std::printf ("%d %d %d\n", i1, i1 == 0x123, test);
f22ad9d0 112
b2dad0e3
BK
113 // test void pointers
114 int i = 55;
115 void* po = &i;
116 void* pi;
117
118 ss_01 << po;
119 ss_01 >> pi;
4bc95009 120 std::printf ("%x %x\n", pi, po);
aa1b2f7d 121 VERIFY( po == pi );
b2dad0e3
BK
122
123#ifdef DEBUG_ASSERT
124 assert(test);
125#endif
126
127 return test;
128}
129
130// elaborated test for ints
131bool test02() {
132
133 bool test = true;
134 const std::string str_01("20000AB");
135 std::stringbuf strb_01(str_01, std::ios_base::in);
136 std::istream is(&strb_01);
137
138 int n = 15;
139 is >> n;
aa1b2f7d 140 VERIFY( n == 20000 );
b2dad0e3 141 char c = is.peek();
aa1b2f7d 142 VERIFY( c == 65 );
b2dad0e3
BK
143
144#ifdef DEBUG_ASSERT
145 assert(test);
146#endif
147
148 return test;
149}
150
151bool test03()
152{
153 std::stringbuf sbuf;
154 std::istream istr(&sbuf);
155 std::ostream ostr(&sbuf);
156
157 bool test = true;
158 long l01;
159 ostr << "12220101";
160 istr >> l01; // _M_in_end set completely incorrectly here.
aa1b2f7d
BV
161 VERIFY( l01 == 12220101 );
162 VERIFY( istr.rdstate() == std::ios_base::eofbit );
b2dad0e3
BK
163
164#ifdef DEBUG_ASSERT
165 assert(test);
166#endif
167
168 return test;
169}
170
a9ab8db1 171// http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00081.html
b2dad0e3
BK
172// Jim Parsons
173void test06()
174{
175 // default locale, grouping is turned off
176 bool test = true;
177 unsigned int h4, h3, h2;
178 char c;
179 std::string s("205,199,144");
180 std::istringstream is(s);
181
182 is >> h4; // 205
aa1b2f7d 183 VERIFY( h4 == 205 );
b2dad0e3 184 is >> c; // ','
aa1b2f7d 185 VERIFY( c == ',' );
b2dad0e3
BK
186
187 is >> h4; // 199
aa1b2f7d 188 VERIFY( h4 == 199 );
b2dad0e3 189 is >> c; // ','
aa1b2f7d 190 VERIFY( c == ',' );
b2dad0e3
BK
191
192 is >> h4; // 144
aa1b2f7d
BV
193 VERIFY( is.rdstate() == std::ios_base::eofbit );
194 VERIFY( h4 == 144 );
b2dad0e3 195 is >> c; // EOF
aa1b2f7d
BV
196 VERIFY( c == ',' );
197 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) );
b2dad0e3
BK
198
199#ifdef DEBUG_ASSERT
200 assert(test);
201#endif
202}
203
204namespace std {
205 class test_numpunct1 : public numpunct<char>
206 {
207 protected:
208 string
209 do_grouping() const
210 { return string(1, '\003'); }
211 };
212} // namespace std
213
214void test07()
215{
216 // manufactured locale, grouping is turned on
217 bool test = true;
218 unsigned int h4 = 0, h3 = 0, h2 = 0;
219 float f1 = 0.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));
223
224 // Basic operation.
225 is >> h4;
aa1b2f7d
BV
226 VERIFY( h4 == 205199 );
227 VERIFY( is.good() );
b2dad0e3
BK
228
229 is.clear();
230 is >> f1;
aa1b2f7d
BV
231 VERIFY( f1 == 23445.25 );
232 VERIFY( is.good() );
b2dad0e3
BK
233
234 is.clear();
235 is >> h3;
aa1b2f7d
BV
236 VERIFY( h3 == 1024365 );
237 VERIFY( is.good() );
b2dad0e3
BK
238
239 is.clear();
240 is >> h2;
aa1b2f7d
BV
241 VERIFY( h2 == 0 );
242 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) );
243 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::eofbit) );
b2dad0e3
BK
244
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");
250 h3 = h4 = h2 = 0;
251 f1 = 0.0;
252 const char c_control = '?';
253 char c = c_control;
254 is.clear();
255 is.str(s2);
256
257 is >> h4;
aa1b2f7d
BV
258 VERIFY( h4 == 0 );
259 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) );
b2dad0e3
BK
260 is.clear();
261 is >> c;
aa1b2f7d
BV
262 VERIFY( c == ',' );
263 VERIFY( is.good() );
b2dad0e3
BK
264
265 is.ignore(3);
266 is >> f1;
aa1b2f7d
BV
267 VERIFY( f1 == 0.0 );
268 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) );
b2dad0e3
BK
269 is.clear();
270 is >> c;
aa1b2f7d 271 VERIFY( c == ',' );
b2dad0e3 272 is >> c;
aa1b2f7d
BV
273 VERIFY( c == '4' );
274 VERIFY( is.good() );
b2dad0e3
BK
275
276 is >> f1;
aa1b2f7d
BV
277 VERIFY( f1 == 0.25 );
278 VERIFY( is.good() );
b2dad0e3 279 is >> c;
aa1b2f7d 280 VERIFY( c == ',' );
b2dad0e3 281 is >> h2;
aa1b2f7d
BV
282 VERIFY( h2 == 345 );
283 VERIFY( is.good() );
b2dad0e3
BK
284 f1 = 0.0;
285 h2 = 0;
286
287 is >> f1;
aa1b2f7d
BV
288 VERIFY( f1 == 5.0 );
289 VERIFY( is.good() );
b2dad0e3 290 is >> f1;
aa1b2f7d
BV
291 VERIFY( f1 == .25 );
292 VERIFY( is.good() );
b2dad0e3
BK
293
294 is >> h3;
aa1b2f7d
BV
295 VERIFY( h3 == 0 );
296 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) );
b2dad0e3
BK
297 is.clear();
298 is >> c;
aa1b2f7d
BV
299 VERIFY( c == ',' ); // second one
300 VERIFY( is.good() );
b2dad0e3
BK
301
302 is >> h2;
aa1b2f7d
BV
303 VERIFY( h2 == 0 );
304 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) );
b2dad0e3
BK
305 is.clear();
306
307 is >> h2;
aa1b2f7d
BV
308 VERIFY( h2 == 1000000 );
309 VERIFY( is.good() );
b2dad0e3
BK
310 h2 = 0;
311
312 is >> h2;
aa1b2f7d
BV
313 VERIFY( h2 == 0 );
314 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) );
315 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::eofbit) );
b2dad0e3
BK
316 is.clear();
317
318#ifdef DEBUG_ASSERT
319 assert(test);
320#endif
321}
322
323namespace std {
324 class test_numpunct2 : public numpunct<char>
325 {
326 protected:
327 string
328 do_grouping() const
329 { return string("\002\003"); }
330 };
331} // namespace std
332
333void test08()
334{
335 // manufactured locale, grouping is turned on
336 bool test = true;
337 unsigned int h4 = 0, h3 = 0, h2 = 0;
338 float f1 = 0.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");
341
342 std::istringstream is(s1);
343 is.imbue(std::locale(std::locale(), new std::test_numpunct2));
344
345 // Basic operation.
346 is >> h4;
aa1b2f7d
BV
347 VERIFY( h4 == 122 );
348 VERIFY( is.good() );
b2dad0e3
BK
349
350 is.clear();
351 is >> h3;
aa1b2f7d
BV
352 VERIFY( h3 == 20519 );
353 VERIFY( is.good() );
b2dad0e3
BK
354
355 is.clear();
356 is >> h2;
aa1b2f7d
BV
357 VERIFY( h2 == 2212322 );
358 VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::eofbit) );
b2dad0e3
BK
359
360
361#ifdef DEBUG_ASSERT
362 assert(test);
363#endif
364}
365
64cdd351
BK
366
367bool test09()
368{
369 bool test = true;
370
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;
375 char c;
376 (is>>std::ws) >> f1;
377 (is>>std::ws) >> c;
378 (is>>std::ws) >> f2;
379 test = f1 == 2456;
aa1b2f7d
BV
380 VERIFY( f2 == 0.00567 );
381 VERIFY( c == '-' );
64cdd351
BK
382#ifdef DEBUG_ASSERT
383 assert(test);
384#endif
385
386 return test;
387}
388
7f1063f8
RD
389bool test10() {
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);
393
394 bool test = true;
395
396 int n = 365;
397 is_01 >> n;
aa1b2f7d 398 VERIFY( n == 0 );
7f1063f8
RD
399 n = 364;
400 is_01 >> n;
aa1b2f7d 401 VERIFY( n == 0 );
7f1063f8
RD
402 n = 363;
403 is_01 >> n;
aa1b2f7d 404 VERIFY( n == 0 );
7f1063f8
RD
405 n = 362;
406 is_01 >> n;
aa1b2f7d 407 VERIFY( n == 0 );
7f1063f8
RD
408 n = 361;
409 is_01 >> n;
aa1b2f7d 410 VERIFY( n == 0 );
7f1063f8
RD
411 n = 360;
412 is_01 >> n;
aa1b2f7d
BV
413 VERIFY( n == 0 );
414 VERIFY( is_01.rdstate() == std::ios_base::eofbit );
7f1063f8
RD
415
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);
420 is_02 >> n;
aa1b2f7d 421 VERIFY( n == 50 );
7f1063f8 422 is_02 >> n;
aa1b2f7d 423 VERIFY( n == 51 );
7f1063f8 424 is_02 >> n;
aa1b2f7d 425 VERIFY( n == 27 );
7f1063f8 426 is_02 >> n;
aa1b2f7d
BV
427 VERIFY( n == 33 );
428 VERIFY( is_02.rdstate() == std::ios_base::eofbit );
7f1063f8
RD
429
430 std::stringbuf isbuf_03(str_02);
431 std::istream is_03(&isbuf_03);
432 char c;
433 int m;
434
435 is_03 >> std::dec >> n >> c >> m;
aa1b2f7d
BV
436 VERIFY( n == 0 );
437 VERIFY( c == 'x' );
438 VERIFY( m == 32 );
7f1063f8
RD
439
440 is_03 >> std::oct >> m >> c >> n;
aa1b2f7d
BV
441 VERIFY( m == 0 );
442 VERIFY( c == 'X' );
443 VERIFY( n == 27 );
7f1063f8
RD
444
445 is_03 >> std::dec >> m >> n;
aa1b2f7d
BV
446 VERIFY( m == 33 );
447 VERIFY( n == 33 );
448 VERIFY( is_03.rdstate() == std::ios_base::eofbit );
7f1063f8
RD
449
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);
453
454 double f;
455 is_04 >> f;
aa1b2f7d 456 VERIFY( f == 3.0 );
7f1063f8 457 is_04 >> f;
aa1b2f7d 458 VERIFY( f == 450.0 );
7f1063f8
RD
459 is_04.ignore();
460 is_04 >> f;
aa1b2f7d 461 VERIFY( f == 0.005 );
7f1063f8 462 is_04 >> f;
aa1b2f7d
BV
463 VERIFY( f == 6 );
464 VERIFY( is_03.rdstate() == std::ios_base::eofbit );
7f1063f8
RD
465
466 std::string str_05("0E20 5Ea E16");
467 std::stringbuf isbuf_05(str_05);
468 std::istream is_05(&isbuf_05);
469
470 is_05 >> f;
aa1b2f7d 471 VERIFY( f == 0 );
7f1063f8 472 is_05 >> f;
aa1b2f7d
BV
473 VERIFY( f == 0 );
474 VERIFY( is_05.rdstate() == std::ios_base::failbit );
7f1063f8
RD
475 is_05.clear();
476 is_05 >> c;
aa1b2f7d 477 VERIFY( c == 'a' );
7f1063f8 478 is_05 >> f;
aa1b2f7d
BV
479 VERIFY( f == 0 );
480 VERIFY( is_05.rdstate() == std::ios_base::failbit );
7f1063f8
RD
481 is_05.clear();
482 is_05.ignore();
483 is_05 >> n;
aa1b2f7d 484 VERIFY( n == 16 );
7f1063f8
RD
485
486#ifdef DEBUG_ASSERT
487 assert(test);
488#endif
489
490 return test;
491}
492
8ce10512
BK
493// In the presence of no fmtflags, the input operator should behave
494// like strtol(x, y, 0)
495// libstdc++/90
496bool test11()
497{
498 bool test = true;
499 const char* cstrlit = "0x2a";
500
501 // sanity check via 'C' library call
502 char* err;
503 long l = strtol(cstrlit, &err, 0);
504
505 std::istringstream iss(cstrlit);
506 iss.setf(std::ios::fmtflags(0), std::ios::basefield);
507 int i;
508 iss >> i;
509
510 VERIFY (!iss.fail());
511 VERIFY (l == i);
512
513 return test;
514}
515
b2dad0e3
BK
516int main()
517{
518 test01();
519 test02();
520 test03();
521
522 test06();
523 test07();
524 test08();
64cdd351 525 test09();
7f1063f8 526 test10();
8ce10512
BK
527
528 test11();
b2dad0e3
BK
529 return 0;
530}
531
b2dad0e3 532// paul miller was right on with riddim warfare!