OSDN Git Service

2001-05-15 Zack Weinberg <zackw@stanford.edu>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 21_strings / inserters_extractors.cc
1 // 1999-07-01 bkoz
2
3 // Copyright (C) 1999, 2000, 2001 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 // 21.3.7.9 inserters and extractors
22
23 // NB: This file is predicated on sstreams, istreams, and ostreams
24 // working, not to mention other major details like char_traits, and
25 // all of the string class.
26
27 #include <string>
28 #include <stdexcept>
29 #include <sstream>
30 #include <fstream>
31 #include <iostream>
32 #include <iomanip>
33 #include <debug_assert.h>
34
35 bool test01(void)
36 {
37   bool test = true;
38   typedef std::string::size_type csize_type;
39   typedef std::string::const_reference cref;
40   typedef std::string::reference ref;
41   csize_type npos = std::string::npos;
42   csize_type csz01, csz02;
43
44   const std::string str01("sailing grand traverse bay\n"
45                "\t\t\t    from Elk Rapids to the point reminds me of miles");
46   const std::string str02("sailing");
47   const std::string str03("grand");
48   const std::string str04("traverse");
49   const std::string str05;
50   std::string str10;
51   
52   // istream& operator>>(istream&, string&)
53   std::istringstream istrs01(str01);
54   istrs01 >> str10;
55   VERIFY( str10 == str02 );
56   try {
57     std::istringstream::int_type i01 = istrs01.peek(); //a-boo
58     VERIFY( std::istringstream::traits_type::to_char_type(i01) == ' ' );
59   }
60   catch(std::exception& fail) {
61     VERIFY( false ); // shouldn't throw
62   }
63
64   istrs01 >> str10; 
65   VERIFY( str10 == str03 ); 
66   istrs01 >> str10; 
67   VERIFY( str10 == str04 ); // sentry picks out the white spaces. . 
68
69   std::istringstream istrs02(str05); // empty
70   istrs02 >> str10;
71   VERIFY( str10 == str04 );
72  
73   // istream& getline(istream&, string&, char)
74   // istream& getline(istream&, string&)
75   try {
76     getline(istrs01, str10);
77     VERIFY( !istrs01.fail() );
78     VERIFY( !istrs01.eof() );
79     VERIFY( istrs01.good() );
80     VERIFY( str10 == " bay" );
81   }
82   catch(std::exception& fail) {
83     VERIFY( false ); // shouldn't throw
84   }
85
86   try {
87     istrs01.clear();
88     getline(istrs01, str10,'\t');
89     VERIFY( !istrs01.fail() );
90     VERIFY( !istrs01.eof() );
91     VERIFY( istrs01.good() );
92     VERIFY( str10 == str05 );
93   }
94   catch(std::exception& fail) {
95     VERIFY( false ); // shouldn't throw
96   }
97
98   try {
99     istrs01.clear();
100     getline(istrs01, str10,'\t');
101     VERIFY( !istrs01.fail() );
102     VERIFY( !istrs01.eof() );
103     VERIFY( istrs01.good() );
104     VERIFY( str10 == str05 );
105   }
106   catch(std::exception& fail) {
107     VERIFY( false ); // shouldn't throw
108   }
109
110   try {
111     istrs01.clear();
112     getline(istrs01, str10, '.');
113     VERIFY( !istrs01.fail() );
114     VERIFY( istrs01.eof() );
115     VERIFY( !istrs01.good() );
116     VERIFY( str10 == "\t    from Elk Rapids to the point reminds me of miles" );
117   }
118   catch(std::exception& fail) {
119     VERIFY( false ); // shouldn't throw
120   }
121
122   try {
123     getline(istrs02, str10);
124     VERIFY( istrs02.fail() );
125     VERIFY( istrs02.eof() );
126     VERIFY( str10 =="\t    from Elk Rapids to the point reminds me of miles" );
127   }
128   catch(std::exception& fail) {
129     VERIFY( false ); // shouldn't throw
130   }
131
132   // ostream& operator<<(ostream&, const basic_string&)
133   std::ostringstream ostrs01;
134   try {
135     ostrs01 << str01;
136     VERIFY( ostrs01.str() == str01 );
137   }
138   catch(std::exception& fail) {
139     VERIFY( false );
140   }
141
142   std::string hello_world;
143   std::cout << hello_world;
144   
145 #ifdef DEBUG_ASSERT
146   assert(test);
147 #endif
148   return test;
149 }
150
151
152 // testing basic_stringbuf::xsputn via stress testing with large strings
153 // based on a bug report libstdc++ 9
154 void test04(int size)
155 {
156   bool test = true;
157   std::string str(size, 's');
158   int expected_size = (2 * (size + sizeof(char)));
159   std::ostringstream oss(str);
160   
161   // sanity checks
162   VERIFY( str.size() == size );
163   VERIFY( oss.good() );
164
165   // stress test
166   oss << str << std::endl;
167   if (!oss.good()) 
168     test = false;
169
170   oss << str << std::endl;
171   if (!oss.good()) 
172     test = false;
173
174   VERIFY( str.size() == size );
175   VERIFY( oss.good() );
176   std::string str_tmp = oss.str();
177   VERIFY( str_tmp.size() == expected_size );
178
179 #ifdef DEBUG_ASSERT
180   assert(test);
181 #endif
182 }
183
184
185 // testing basic_filebuf::xsputn via stress testing with large strings
186 // based on a bug report libstdc++ 9
187 // mode == out
188 void test05(int size)
189 {
190   bool test = true;
191   const char filename[] = "inserters_extractors-1.txt";
192   const char fillc = 'f';
193   std::ofstream ofs(filename);
194   std::string str(size, fillc);
195
196   // sanity checks
197   VERIFY( str.size() == size );
198   VERIFY( ofs.good() );
199
200   // stress test
201   ofs << str << std::endl;
202   if (!ofs.good()) 
203     test = false;
204
205   ofs << str << std::endl;
206   if (!ofs.good()) 
207     test = false;
208
209   VERIFY( str.size() == size );
210   VERIFY( ofs.good() );
211
212   ofs.close();
213
214   // sanity check on the written file
215   std::ifstream ifs(filename);
216   int count = 0;
217   char c;
218   while (count <= (2 * size) + 4)
219     {
220       ifs >> c;
221       if (ifs.good() && c == fillc)
222         {
223           ++count;
224           c = '0';
225         }
226       else 
227         break;
228     }
229
230   VERIFY( count == 2 * size );
231
232 #ifdef DEBUG_ASSERT
233   assert(test);
234 #endif
235 }
236
237
238 // istringstream/stringbuf extractor properly size buffer based on
239 // actual, not allocated contents (string.size() vs. string.capacity()).
240 // http://gcc.gnu.org/ml/libstdc++/1999-q4/msg00049.html
241 void test06(void)
242 {
243   bool test = true;
244
245   typedef std::string::size_type size_type;
246   std::string str01("@silent");
247   size_type i01 = str01.size();
248   size_type i02 = str01.capacity();
249   str01.erase(0, 1);
250   size_type i03 = str01.size();
251   size_type i04 = str01.capacity();
252   VERIFY( i01 - 1 == i03 );
253   VERIFY( i02 >= i04 );
254
255   std::istringstream is(str01);
256   std::string str02;
257   is >> str02 >> std::ws;
258   size_type i05 = str02.size();
259   size_type i06 = str02.capacity();
260   VERIFY( i05 == i03 );
261   VERIFY( i06 <= i04 );
262
263 #ifdef DEBUG_ASSERT
264   assert(test);
265 #endif
266 }
267
268 // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00085.html
269 // istream::operator>>(string)
270 // sets failbit
271 // NB: this is a defect in the standard.
272 void test07(void)
273 {
274   bool test = true;
275   const std::string name("z6.cc");
276   std::istringstream iss (name);
277   int i = 0;
278   std::string s;
279   while (iss >> s) 
280     ++i;
281
282   VERIFY( i < 3 );
283   VERIFY( static_cast<bool>(iss.rdstate() & std::ios_base::failbit) );
284
285 #ifdef DEBUG_ASSERT
286   assert(test);
287 #endif
288 }
289
290 // libstdc++/1019
291 void test08()
292 {
293   using namespace std;
294
295   bool          test = true;
296   istringstream istrm("enero:2001");
297   int           year;
298   char          sep;
299   string        month;
300   
301   istrm >> setw(5) >> month >> sep >> year;
302   VERIFY( month.size() == 5 );
303   VERIFY( sep == ':' );
304   VERIFY( year == 2001 );
305 }
306
307 int main()
308
309   test01();
310
311   test04(1); // expected_size == 4
312   test04(1000); // expected_size == 2002
313   test04(10000); // expected_size == 20002
314
315   test05(1); 
316   test05(1000); 
317   test05(10000);
318
319   test06();
320   test07();
321
322   test08();
323   return 0;
324 }