OSDN Git Service

fc754dc59126bbe5d963730c934e8721d5696dac
[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 <istream>
24 #include <ostream>
25 #include <sstream>
26 #include <locale>
27 #ifdef DEBUG_ASSERT
28   #include <assert.h>
29 #endif
30
31 std::string str_01;
32 std::string str_02("true false 0 1 110001");
33 std::string str_03("-19999999 777777 -234234 233 -234 33 1 66300.25 .315 1.5");
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  
39 std::istream is_01(NULL);
40 std::istream is_02(&isbuf_02);
41 std::istream is_03(&isbuf_03);
42 std::stringstream ss_01(str_01);
43  
44 // minimal sanity check
45 bool test01() {
46
47   bool test = true;
48
49   // Integral Types:
50   bool                  b1  = false;
51   bool                  b2  = false;
52   short                 s1  = 0;
53   int                   i1  = 0;
54   long                  l1  = 0;
55   unsigned short        us1 = 0;
56   unsigned int          ui1 = 0;
57   unsigned long         ul1 = 0;
58
59   // Floating-point Types:
60   float                 f1  = 0;
61   double                d1  = 0;
62   long double           ld1 = 0;
63
64   // process alphanumeric versions of bool values
65   std::ios_base::fmtflags fmt = is_02.flags();
66   bool testfmt = fmt & std::ios_base::boolalpha;
67   is_02.setf(std::ios_base::boolalpha);
68   fmt = is_02.flags();
69   testfmt = fmt & std::ios_base::boolalpha;
70   is_02 >> b1;
71   test &= b1 == 1;
72   is_02 >> b1;
73   test &= b1 == 0;
74
75   // process numeric versions of of bool values
76   is_02.unsetf(std::ios_base::boolalpha);
77   fmt = is_02.flags();
78   testfmt = fmt & std::ios_base::boolalpha;
79   is_02 >> b1;
80   test &= b1 == 0;
81   is_02 >> b1;
82   test &= b1 == 1;
83
84   // is_03 == "-19999999 777777 -234234 233 -234 33 1 66300.25 .315 1.5"
85   is_03 >> l1;
86   test &= l1 == -19999999;
87   is_03 >> ul1;
88   test &= ul1 == 777777;
89   is_03 >> i1;
90   test &= i1 == -234234;
91   is_03 >> ui1;
92   test &= ui1 == 233;
93   is_03 >> s1;
94   test &= s1 == -234;
95   is_03 >> us1;
96   test &= us1 == 33;
97   is_03 >> b1;
98   test &= b1 == 1;
99   is_03 >> ld1;
100   test &= ld1 == 66300.25;
101   is_03 >> d1;
102   test &= d1 == .315;
103   is_03 >> f1;
104   test &= f1 == 1.5;
105
106   // test void pointers
107   int i = 55;
108   void* po = &i;
109   void* pi;
110
111   ss_01 << po;
112   ss_01 >> pi;
113   test &= po == pi;
114   
115 #ifdef DEBUG_ASSERT
116   assert(test);
117 #endif
118  
119   return test;
120 }
121
122 // elaborated test for ints
123 bool test02() {
124
125   bool test = true;
126   const std::string str_01("20000AB");
127   std::stringbuf strb_01(str_01, std::ios_base::in);
128   std::istream is(&strb_01);
129
130   int n = 15;
131   is >> n;
132   test &= n == 20000;
133   char c = is.peek();
134   test &= c == 65;
135
136 #ifdef DEBUG_ASSERT
137   assert(test);
138 #endif
139  
140   return test;
141 }
142
143 bool test03()
144 {
145   std::stringbuf sbuf;
146   std::istream istr(&sbuf);
147   std::ostream ostr(&sbuf);
148
149   bool test = true;
150   long l01;
151   ostr <<  "12220101";
152   istr >>  l01; // _M_in_end set completely incorrectly here.
153   test &= l01 == 12220101;
154   test &= istr.rdstate() == std::ios_base::eofbit;
155
156 #ifdef DEBUG_ASSERT
157   assert(test);
158 #endif
159
160   return test;
161 }
162
163 // http://sourceware.cygnus.com/ml/libstdc++/2000-q1/msg00081.html
164 // Jim Parsons
165 void test06()
166 {
167   // default locale, grouping is turned off
168   bool test = true;
169   unsigned int h4, h3, h2;
170   char c;
171   std::string s("205,199,144");
172   std::istringstream is(s);
173   
174   is >> h4; // 205
175   test &= h4 == 205;
176   is >> c; // ','
177   test &= c == ',';
178
179   is >> h4; // 199
180   test &= h4 == 199;
181   is >> c; // ','
182   test &= c == ',';
183
184   is >> h4; // 144
185   test &= is.rdstate() == std::ios_base::eofbit;
186   test &= h4 == 144;
187   is >> c; // EOF
188   test &= c == ',';
189   test &= static_cast<bool>(is.rdstate() & std::ios_base::failbit);
190
191 #ifdef DEBUG_ASSERT
192   assert(test);
193 #endif
194 }
195
196 namespace std {
197   class test_numpunct1 : public numpunct<char>
198   {
199   protected:
200     string
201     do_grouping() const 
202     { return string(1, '\003'); }
203   };
204 } // namespace std
205
206 void test07()
207 {
208   // manufactured locale, grouping is turned on
209   bool test = true;
210   unsigned int h4 = 0, h3 = 0, h2 = 0;
211   float f1 = 0.0;
212   const std::string s1("205,199 23,445.25 1,024,365 123,22,24");
213   std::istringstream is(s1);
214   is.imbue(std::locale(std::locale(), new std::test_numpunct1));  
215
216   // Basic operation.
217   is >> h4; 
218   test &= h4 == 205199;
219   test &= is.good();
220
221   is.clear();
222   is >> f1; 
223   test &= f1 == 23445.25;
224   test &= is.good();
225
226   is.clear();
227   is >> h3; 
228   test &= h3 == 1024365;
229   test &= is.good();
230
231   is.clear();
232   is >> h2; 
233   test &= h2 == 0;
234   test &= static_cast<bool>(is.rdstate() & std::ios_base::failbit);
235   test &= static_cast<bool>(is.rdstate() & std::ios_base::eofbit);
236
237   // Stress tests for explicit errors in grouping corner cases.  The
238   // validity of these tests and results have been hammered out in
239   // private email between bkoz and ncm between Jan 25 and Jan 27, 2000.
240   // Thanks nate -- benjamin
241   const std::string s2(",111 4,,4 0.25,345 5..25 156,, 1,000000 1000000 1234,567");
242   h3 = h4 = h2 = 0;
243   f1 = 0.0;
244   const char c_control = '?';
245   char c = c_control;
246   is.clear();
247   is.str(s2);
248
249   is >> h4; 
250   test &= h4 == 0;
251   test &= static_cast<bool>(is.rdstate() & std::ios_base::failbit);
252   is.clear();
253   is >> c;
254   test &= c == ',';
255   test &= is.good();
256
257   is.ignore(3);
258   is >> f1; 
259   test &= f1 == 0.0;
260   test &= static_cast<bool>(is.rdstate() & std::ios_base::failbit);
261   is.clear();
262   is >> c;
263   test &= c == ',';
264   is >> c;
265   test &= c == '4';
266   test &= is.good();
267
268   is >> f1; 
269   test &= f1 == 0.25;
270   test &= is.good();
271   is >> c;
272   test &= c == ',';
273   is >> h2;
274   test &= h2 == 345;
275   test &= is.good();
276   f1 = 0.0;
277   h2 = 0;
278
279   is >> f1; 
280   test &= f1 == 5.0;
281   test &= is.good();
282   is >> f1; 
283   test &= f1 == .25;
284   test &= is.good();
285
286   is >> h3; 
287   test &= h3 == 0;
288   test &= static_cast<bool>(is.rdstate() & std::ios_base::failbit);
289   is.clear();
290   is >> c;
291   test &= c == ','; // second one
292   test &= is.good();
293
294   is >> h2; 
295   test &= h2 == 0;
296   test &= static_cast<bool>(is.rdstate() & std::ios_base::failbit);
297   is.clear();
298
299   is >> h2; 
300   test &= h2 == 1000000;
301   test &= is.good();
302   h2 = 0;
303
304   is >> h2; 
305   test &= h2 == 0;
306   test &= static_cast<bool>(is.rdstate() & std::ios_base::failbit);
307   test &= static_cast<bool>(is.rdstate() & std::ios_base::eofbit);
308   is.clear();
309
310 #ifdef DEBUG_ASSERT
311   assert(test);
312 #endif
313 }
314
315 namespace std {
316   class test_numpunct2 : public numpunct<char>
317   {
318   protected:
319     string
320     do_grouping() const 
321     { return string("\002\003"); }
322   };
323 } // namespace std
324
325 void test08()
326 {
327   // manufactured locale, grouping is turned on
328   bool test = true;
329   unsigned int h4 = 0, h3 = 0, h2 = 0;
330   float f1 = 0.0;
331   const std::string s1("1,22 205,19 22,123,22");
332   const std::string s2("1,220 2050,19 202,123,22");
333
334   std::istringstream is(s1);
335   is.imbue(std::locale(std::locale(), new std::test_numpunct2));  
336
337   // Basic operation.
338   is >> h4; 
339   test &= h4 == 122;
340   test &= is.good();
341
342   is.clear();
343   is >> h3; 
344   test &= h3 == 20519;
345   test &= is.good();
346
347   is.clear();
348   is >> h2; 
349   test &= h2 == 2212322;
350   test &= static_cast<bool>(is.rdstate() & std::ios_base::eofbit);
351
352
353 #ifdef DEBUG_ASSERT
354   assert(test);
355 #endif
356 }
357
358
359 bool test09()
360 {
361    bool test = true;
362
363    std::string st("2.456e3-+0.567e-2");
364    std::stringbuf sb(st);
365    std::istream is(&sb);
366    double f1 = 0, f2 = 0;
367    char c;
368    (is>>std::ws) >> f1;
369    (is>>std::ws) >> c;
370    (is>>std::ws) >> f2;
371    test = f1 == 2456;
372    test &= f2 == 0.00567;
373    test &= c == '-';
374 #ifdef DEBUG_ASSERT
375   assert(test);
376 #endif
377  
378   return test;
379 }
380
381 bool test10() {
382   std::string str_01("0 00 000 +0 +  0 -   0");
383   std::stringbuf isbuf_01(str_01);
384   std::istream is_01(&isbuf_01);
385
386   bool test = true;
387
388   int n = 365;
389   is_01 >> n;
390   test &= n == 0;
391   n = 364;
392   is_01 >> n;
393   test &= n == 0;
394   n = 363;
395   is_01 >> n;
396   test &= n == 0;
397   n = 362;
398   is_01 >> n;
399   test &= n == 0;
400   n = 361;
401   is_01 >> n;
402   test &= n == 0;
403   n = 360;
404   is_01 >> n;
405   test &= n == 0;
406   test &= is_01.rdstate() == std::ios_base::eofbit;
407
408   std::string str_02("0x32 0X33 033 33");
409   std::stringbuf isbuf_02(str_02);
410   std::istream is_02(&isbuf_02);
411   is_02.unsetf(std::ios_base::basefield);
412   is_02 >> n;
413   test &= n == 50;
414   is_02 >> n;
415   test &= n == 51;
416   is_02 >> n;
417   test &= n == 27;
418   is_02 >> n;
419   test &= n == 33;
420   test &= is_02.rdstate() == std::ios_base::eofbit;
421
422   std::stringbuf isbuf_03(str_02);
423   std::istream is_03(&isbuf_03);
424   char c;
425   int m;
426
427   is_03 >> std::dec >> n >> c >> m;
428   test &= n == 0;
429   test &= c == 'x';
430   test &= m == 32;
431
432   is_03 >> std::oct >> m >> c >> n;
433   test &= m == 0;
434   test &= c == 'X';
435   test &= n == 27;
436
437   is_03 >> std::dec >> m >> n;
438   test &= m == 33;
439   test &= n == 33;
440   test &= is_03.rdstate() == std::ios_base::eofbit;
441
442   std::string str_04("3. 4.5E+  2a5E-3 .6E1");
443   std::stringbuf isbuf_04(str_04);
444   std::istream is_04(&isbuf_04);
445
446   double f;
447   is_04 >> f;
448   test &= f == 3.0;
449   is_04 >> f;
450   test &= f == 450.0;
451   is_04.ignore();
452   is_04 >> f;
453   test &= f == 0.005;
454   is_04 >> f;
455   test &= f == 6;
456   test &= is_03.rdstate() == std::ios_base::eofbit;
457
458   std::string str_05("0E20 5Ea E16");
459   std::stringbuf isbuf_05(str_05);
460   std::istream is_05(&isbuf_05);
461
462   is_05 >> f;
463   test &= f == 0;
464   is_05 >> f;
465   test &= f == 0;
466   test &= is_05.rdstate() == std::ios_base::failbit;
467   is_05.clear();
468   is_05 >> c;
469   test &= c == 'a';
470   is_05 >> f;
471   test &= f == 0;
472   test &= is_05.rdstate() == std::ios_base::failbit;
473   is_05.clear();
474   is_05.ignore();
475   is_05 >> n;
476   test &= n == 16;
477
478 #ifdef DEBUG_ASSERT
479   assert(test);
480 #endif
481
482   return test;
483 }
484
485 int main()
486 {
487   test01();
488   test02();
489   test03();
490
491   test06();
492   test07();
493   test08();
494   test09();
495   test10();
496   return 0;
497 }
498
499
500
501
502
503 // paul miller was right on with riddim warfare!
504
505
506
507
508
509
510
511
512
513
514
515
516