1 // 1999-07-01 bkoz
2
3 // Copyright (C) 1999, 2000, 2001, 2002 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 <testsuite_hooks.h>
34
test01(void)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 {
58 std::istringstream::int_type i01 = istrs01.peek(); //a-boo
59 VERIFY( std::istringstream::traits_type::to_char_type(i01) == ' ' );
60 }
61 catch(std::exception& fail)
62 {
63 VERIFY( false ); // shouldn't throw
64 }
65
66 istrs01.clear();
67 istrs01 >> str10;
68 VERIFY( str10 == str03 );
69 istrs01.clear();
70 istrs01 >> str10;
71 VERIFY( str10 == str04 ); // sentry picks out the white spaces. .
72
73 std::istringstream istrs02(str05); // empty
74 istrs02 >> str10;
75 VERIFY( str10 == str04 );
76
77 // istream& getline(istream&, string&, char)
78 // istream& getline(istream&, string&)
79 try
80 {
81 istrs01.clear();
82 getline(istrs01, str10);
83 VERIFY( !istrs01.fail() );
84 VERIFY( !istrs01.eof() );
85 VERIFY( istrs01.good() );
86 VERIFY( str10 == " bay" );
87 }
88 catch(std::exception& fail)
89 {
90 VERIFY( false ); // shouldn't throw
91 }
92
93 try
94 {
95 istrs01.clear();
96 getline(istrs01, str10,'\t');
97 VERIFY( !istrs01.fail() );
98 VERIFY( !istrs01.eof() );
99 VERIFY( istrs01.good() );
100 VERIFY( str10 == str05 );
101 }
102 catch(std::exception& fail)
103 {
104 VERIFY( false ); // shouldn't throw
105 }
106
107 try
108 {
109 istrs01.clear();
110 getline(istrs01, str10,'\t');
111 VERIFY( !istrs01.fail() );
112 VERIFY( !istrs01.eof() );
113 VERIFY( istrs01.good() );
114 VERIFY( str10 == str05 );
115 }
116 catch(std::exception& fail)
117 {
118 VERIFY( false ); // shouldn't throw
119 }
120
121 try
122 {
123 istrs01.clear();
124 getline(istrs01, str10, '.');
125 VERIFY( !istrs01.fail() );
126 VERIFY( istrs01.eof() );
127 VERIFY( !istrs01.good() );
128 VERIFY( str10 == "\t from Elk Rapids to the point reminds me of miles" );
129 }
130 catch(std::exception& fail)
131 {
132 VERIFY( false ); // shouldn't throw
133 }
134
135 try
136 {
137 getline(istrs02, str10);
138 VERIFY( istrs02.fail() );
139 VERIFY( istrs02.eof() );
140 VERIFY( str10 =="\t from Elk Rapids to the point reminds me of miles" );
141 }
142 catch(std::exception& fail)
143 {
144 VERIFY( false ); // shouldn't throw
145 }
146
147 // ostream& operator<<(ostream&, const basic_string&)
148 std::ostringstream ostrs01;
149 try
150 {
151 ostrs01 << str01;
152 VERIFY( ostrs01.str() == str01 );
153 }
154 catch(std::exception& fail)
155 {
156 VERIFY( false );
157 }
158
159 std::string hello_world;
160 std::cout << hello_world;
161
162 #ifdef DEBUG_ASSERT
163 assert(test);
164 #endif
165 return test;
166 }
167
168
169 // testing basic_stringbuf::xsputn via stress testing with large strings
170 // based on a bug report libstdc++ 9
test04(int size)171 void test04(int size)
172 {
173 bool test = true;
174 std::string str(size, 's');
175 int expected_size = 2 * (size + 1);
176 std::ostringstream oss(str);
177
178 // sanity checks
179 VERIFY( str.size() == size );
180 VERIFY( oss.good() );
181
182 // stress test
183 oss << str << std::endl;
184 if (!oss.good())
185 test = false;
186
187 oss << str << std::endl;
188 if (!oss.good())
189 test = false;
190
191 VERIFY( str.size() == size );
192 VERIFY( oss.good() );
193 std::string str_tmp = oss.str();
194 VERIFY( str_tmp.size() == expected_size );
195
196 #ifdef DEBUG_ASSERT
197 assert(test);
198 #endif
199 }
200
201
202 // testing basic_filebuf::xsputn via stress testing with large strings
203 // based on a bug report libstdc++ 9
204 // mode == out
test05(int size)205 void test05(int size)
206 {
207 bool test = true;
208 const char filename[] = "inserters_extractors-1.txt";
209 const char fillc = 'f';
210 std::ofstream ofs(filename);
211 std::string str(size, fillc);
212
213 // sanity checks
214 VERIFY( str.size() == size );
215 VERIFY( ofs.good() );
216
217 // stress test
218 ofs << str << std::endl;
219 if (!ofs.good())
220 test = false;
221
222 ofs << str << std::endl;
223 if (!ofs.good())
224 test = false;
225
226 VERIFY( str.size() == size );
227 VERIFY( ofs.good() );
228
229 ofs.close();
230
231 // sanity check on the written file
232 std::ifstream ifs(filename);
233 int count = 0;
234 char c;
235 while (count <= (2 * size) + 4)
236 {
237 ifs >> c;
238 if (ifs.good() && c == fillc)
239 {
240 ++count;
241 c = '0';
242 }
243 else
244 break;
245 }
246
247 VERIFY( count == 2 * size );
248
249 #ifdef DEBUG_ASSERT
250 assert(test);
251 #endif
252 }
253
254
255 // istringstream/stringbuf extractor properly size buffer based on
256 // actual, not allocated contents (string.size() vs. string.capacity()).
257 // http://gcc.gnu.org/ml/libstdc++/1999-q4/msg00049.html
test06(void)258 void test06(void)
259 {
260 bool test = true;
261
262 typedef std::string::size_type size_type;
263 std::string str01("@silent");
264 size_type i01 = str01.size();
265 size_type i02 = str01.capacity();
266 str01.erase(0, 1);
267 size_type i03 = str01.size();
268 size_type i04 = str01.capacity();
269 VERIFY( i01 - 1 == i03 );
270 VERIFY( i02 >= i04 );
271
272 std::istringstream is(str01);
273 std::string str02;
274 is >> str02 >> std::ws;
275 size_type i05 = str02.size();
276 size_type i06 = str02.capacity();
277 VERIFY( i05 == i03 );
278 VERIFY( i06 <= i04 );
279
280 #ifdef DEBUG_ASSERT
281 assert(test);
282 #endif
283 }
284
285 // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00085.html
286 // istream::operator>>(string)
287 // sets failbit
288 // NB: this is a defect in the standard.
test07(void)289 void test07(void)
290 {
291 bool test = true;
292 const std::string name("z6.cc");
293 std::istringstream iss (name);
294 int i = 0;
295 std::string s;
296 while (iss >> s)
297 ++i;
298
299 VERIFY( i < 3 );
300 VERIFY( static_cast<bool>(iss.rdstate() & std::ios_base::failbit) );
301
302 #ifdef DEBUG_ASSERT
303 assert(test);
304 #endif
305 }
306
307 // libstdc++/1019
test08()308 void test08()
309 {
310 using namespace std;
311
312 bool test = true;
313 istringstream istrm("enero:2001");
314 int year;
315 char sep;
316 string month;
317
318 istrm >> setw(5) >> month >> sep >> year;
319 VERIFY( month.size() == 5 );
320 VERIFY( sep == ':' );
321 VERIFY( year == 2001 );
322 }
323
324 // libstdc++/2830
test09()325 void test09()
326 {
327 bool test = true;
328 std::string blanks( 3, '\0');
329 std::string foo = "peace";
330 foo += blanks;
331 foo += "& love";
332
333 std::ostringstream oss1;
334 oss1 << foo;
335 VERIFY( oss1.str() == foo );
336
337 std::ostringstream oss2;
338 oss2.width(20);
339 oss2 << foo;
340 VERIFY( oss2.str() != foo );
341 VERIFY( oss2.str().size() == 20 );
342 }
343
main()344 int main()
345 {
346 test01();
347
348 test04(1); // expected_size == 4
349 test04(1000); // expected_size == 2002
350 test04(10000); // expected_size == 20002
351
352 test05(1);
353 test05(1000);
354 test05(10000);
355
356 test06();
357 test07();
358
359 test08();
360
361 test09();
362 return 0;
363 }
364