OSDN Git Service

e6bb415d2ef5a3bc92e2fb1d7d2cebbee1f5b14f
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 27_io / istream_extractor_arith.cc
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
23 #include <cstdio> // for printf
24 #include <istream>
25 #include <ostream>
26 #include <sstream>
27 #include <locale>
28 #include <testsuite_hooks.h>
29
30 std::string str_01;
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");
34
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);
39
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);
45  
46 // minimal sanity check
47 bool 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;
73   VERIFY( b1 == 1 );
74   is_02 >> b1;
75   VERIFY( b1 == 0 );
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;
82   VERIFY( b1 == 0 );
83   is_02 >> b1;
84   VERIFY( b1 == 1 );
85
86   // is_03 == "-19999999 777777 -234234 233 -234 33 1 66300.25 .315 1.5"
87   is_03 >> l1;
88   VERIFY( l1 == -19999999 );
89   is_03 >> ul1;
90   VERIFY( ul1 == 777777 );
91   is_03 >> i1;
92   VERIFY( i1 == -234234 );
93   is_03 >> ui1;
94   VERIFY( ui1 == 233 );
95   is_03 >> s1;
96   VERIFY( s1 == -234 );
97   is_03 >> us1;
98   VERIFY( us1 == 33 );
99   is_03 >> b1;
100   VERIFY( b1 == 1 );
101   is_03 >> ld1;
102   VERIFY( ld1 == 66300.25 );
103   is_03 >> d1;
104   VERIFY( d1 == .315 );
105   is_03 >> f1;
106   VERIFY( f1 == 1.5 );
107
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);
112
113   // test void pointers
114   int i = 55;
115   void* po = &i;
116   void* pi;
117
118   ss_01 << po;
119   ss_01 >> pi;
120   std::printf ("%x %x\n", pi, po);
121   VERIFY( po == pi );
122   
123 #ifdef DEBUG_ASSERT
124   assert(test);
125 #endif
126  
127   return test;
128 }
129
130 // elaborated test for ints
131 bool 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;
140   VERIFY( n == 20000 );
141   char c = is.peek();
142   VERIFY( c == 65 );
143
144 #ifdef DEBUG_ASSERT
145   assert(test);
146 #endif
147  
148   return test;
149 }
150
151 bool 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.
161   VERIFY( l01 == 12220101 );
162   VERIFY( istr.rdstate() == std::ios_base::eofbit );
163
164 #ifdef DEBUG_ASSERT
165   assert(test);
166 #endif
167
168   return test;
169 }
170
171 // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00081.html
172 // Jim Parsons
173 void 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
183   VERIFY( h4 == 205 );
184   is >> c; // ','
185   VERIFY( c == ',' );
186
187   is >> h4; // 199
188   VERIFY( h4 == 199 );
189   is >> c; // ','
190   VERIFY( c == ',' );
191
192   is >> h4; // 144
193   VERIFY( is.rdstate() == std::ios_base::eofbit );
194   VERIFY( h4 == 144 );
195   is >> c; // EOF
196   VERIFY( c == ',' );
197   VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) );
198
199 #ifdef DEBUG_ASSERT
200   assert(test);
201 #endif
202 }
203
204 namespace 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
214 void 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; 
226   VERIFY( h4 == 205199 );
227   VERIFY( is.good() );
228
229   is.clear();
230   is >> f1; 
231   VERIFY( f1 == 23445.25 );
232   VERIFY( is.good() );
233
234   is.clear();
235   is >> h3; 
236   VERIFY( h3 == 1024365 );
237   VERIFY( is.good() );
238
239   is.clear();
240   is >> h2; 
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) );
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; 
258   VERIFY( h4 == 0 );
259   VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) );
260   is.clear();
261   is >> c;
262   VERIFY( c == ',' );
263   VERIFY( is.good() );
264
265   is.ignore(3);
266   is >> f1; 
267   VERIFY( f1 == 0.0 );
268   VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) );
269   is.clear();
270   is >> c;
271   VERIFY( c == ',' );
272   is >> c;
273   VERIFY( c == '4' );
274   VERIFY( is.good() );
275
276   is >> f1; 
277   VERIFY( f1 == 0.25 );
278   VERIFY( is.good() );
279   is >> c;
280   VERIFY( c == ',' );
281   is >> h2;
282   VERIFY( h2 == 345 );
283   VERIFY( is.good() );
284   f1 = 0.0;
285   h2 = 0;
286
287   is >> f1; 
288   VERIFY( f1 == 5.0 );
289   VERIFY( is.good() );
290   is >> f1; 
291   VERIFY( f1 == .25 );
292   VERIFY( is.good() );
293
294   is >> h3; 
295   VERIFY( h3 == 0 );
296   VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) );
297   is.clear();
298   is >> c;
299   VERIFY( c == ',' ); // second one
300   VERIFY( is.good() );
301
302   is >> h2; 
303   VERIFY( h2 == 0 );
304   VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::failbit) );
305   is.clear();
306
307   is >> h2; 
308   VERIFY( h2 == 1000000 );
309   VERIFY( is.good() );
310   h2 = 0;
311
312   is >> h2; 
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) );
316   is.clear();
317
318 #ifdef DEBUG_ASSERT
319   assert(test);
320 #endif
321 }
322
323 namespace 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
333 void 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; 
347   VERIFY( h4 == 122 );
348   VERIFY( is.good() );
349
350   is.clear();
351   is >> h3; 
352   VERIFY( h3 == 20519 );
353   VERIFY( is.good() );
354
355   is.clear();
356   is >> h2; 
357   VERIFY( h2 == 2212322 );
358   VERIFY( static_cast<bool>(is.rdstate() & std::ios_base::eofbit) );
359
360
361 #ifdef DEBUG_ASSERT
362   assert(test);
363 #endif
364 }
365
366
367 bool 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;
380    VERIFY( f2 == 0.00567 );
381    VERIFY( c == '-' );
382 #ifdef DEBUG_ASSERT
383   assert(test);
384 #endif
385  
386   return test;
387 }
388
389 bool 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;
398   VERIFY( n == 0 );
399   n = 364;
400   is_01 >> n;
401   VERIFY( n == 0 );
402   n = 363;
403   is_01 >> n;
404   VERIFY( n == 0 );
405   n = 362;
406   is_01 >> n;
407   VERIFY( n == 0 );
408   n = 361;
409   is_01 >> n;
410   VERIFY( n == 0 );
411   n = 360;
412   is_01 >> n;
413   VERIFY( n == 0 );
414   VERIFY( is_01.rdstate() == std::ios_base::eofbit );
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;
421   VERIFY( n == 50 );
422   is_02 >> n;
423   VERIFY( n == 51 );
424   is_02 >> n;
425   VERIFY( n == 27 );
426   is_02 >> n;
427   VERIFY( n == 33 );
428   VERIFY( is_02.rdstate() == std::ios_base::eofbit );
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;
436   VERIFY( n == 0 );
437   VERIFY( c == 'x' );
438   VERIFY( m == 32 );
439
440   is_03 >> std::oct >> m >> c >> n;
441   VERIFY( m == 0 );
442   VERIFY( c == 'X' );
443   VERIFY( n == 27 );
444
445   is_03 >> std::dec >> m >> n;
446   VERIFY( m == 33 );
447   VERIFY( n == 33 );
448   VERIFY( is_03.rdstate() == std::ios_base::eofbit );
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;
456   VERIFY( f == 3.0 );
457   is_04 >> f;
458   VERIFY( f == 450.0 );
459   is_04.ignore();
460   is_04 >> f;
461   VERIFY( f == 0.005 );
462   is_04 >> f;
463   VERIFY( f == 6 );
464   VERIFY( is_03.rdstate() == std::ios_base::eofbit );
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;
471   VERIFY( f == 0 );
472   is_05 >> f;
473   VERIFY( f == 0 );
474   VERIFY( is_05.rdstate() == std::ios_base::failbit );
475   is_05.clear();
476   is_05 >> c;
477   VERIFY( c == 'a' );
478   is_05 >> f;
479   VERIFY( f == 0 );
480   VERIFY( is_05.rdstate() == std::ios_base::failbit );
481   is_05.clear();
482   is_05.ignore();
483   is_05 >> n;
484   VERIFY( n == 16 );
485
486 #ifdef DEBUG_ASSERT
487   assert(test);
488 #endif
489
490   return test;
491 }
492
493 // In the presence of no fmtflags, the input operator should behave
494 // like strtol(x, y, 0)
495 // libstdc++/90
496 bool 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
516 int main()
517 {
518   test01();
519   test02();
520   test03();
521
522   test06();
523   test07();
524   test08();
525   test09();
526   test10();
527   
528   test11();
529   return 0;
530 }
531
532 // paul miller was right on with riddim warfare!