OSDN Git Service

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