3 // Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
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)
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.
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,
21 // 21.3.7.9 inserters and extractors
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.
33 #include <debug_assert.h>
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;
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;
52 // istream& operator>>(istream&, string&)
53 std::istringstream istrs01(str01);
55 VERIFY( str10 == str02 );
57 std::istringstream::int_type i01 = istrs01.peek(); //a-boo
58 VERIFY( std::istringstream::traits_type::to_char_type(i01) == ' ' );
60 catch(std::exception& fail) {
61 VERIFY( false ); // shouldn't throw
65 VERIFY( str10 == str03 );
67 VERIFY( str10 == str04 ); // sentry picks out the white spaces. .
69 std::istringstream istrs02(str05); // empty
71 VERIFY( str10 == str04 );
73 // istream& getline(istream&, string&, char)
74 // istream& getline(istream&, string&)
76 getline(istrs01, str10);
77 VERIFY( !istrs01.fail() );
78 VERIFY( !istrs01.eof() );
79 VERIFY( istrs01.good() );
80 VERIFY( str10 == " bay" );
82 catch(std::exception& fail) {
83 VERIFY( false ); // shouldn't throw
88 getline(istrs01, str10,'\t');
89 VERIFY( !istrs01.fail() );
90 VERIFY( !istrs01.eof() );
91 VERIFY( istrs01.good() );
92 VERIFY( str10 == str05 );
94 catch(std::exception& fail) {
95 VERIFY( false ); // shouldn't throw
100 getline(istrs01, str10,'\t');
101 VERIFY( !istrs01.fail() );
102 VERIFY( !istrs01.eof() );
103 VERIFY( istrs01.good() );
104 VERIFY( str10 == str05 );
106 catch(std::exception& fail) {
107 VERIFY( false ); // shouldn't throw
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" );
118 catch(std::exception& fail) {
119 VERIFY( false ); // shouldn't throw
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" );
128 catch(std::exception& fail) {
129 VERIFY( false ); // shouldn't throw
132 // ostream& operator<<(ostream&, const basic_string&)
133 std::ostringstream ostrs01;
136 VERIFY( ostrs01.str() == str01 );
138 catch(std::exception& fail) {
142 std::string hello_world;
143 std::cout << hello_world;
152 // testing basic_stringbuf::xsputn via stress testing with large strings
153 // based on a bug report libstdc++ 9
154 void test04(int size)
157 std::string str(size, 's');
158 int expected_size = (2 * (size + sizeof(char)));
159 std::ostringstream oss(str);
162 VERIFY( str.size() == size );
163 VERIFY( oss.good() );
166 oss << str << std::endl;
170 oss << str << std::endl;
174 VERIFY( str.size() == size );
175 VERIFY( oss.good() );
176 std::string str_tmp = oss.str();
177 VERIFY( str_tmp.size() == expected_size );
185 // testing basic_filebuf::xsputn via stress testing with large strings
186 // based on a bug report libstdc++ 9
188 void test05(int size)
191 const char filename[] = "inserters_extractors-1.txt";
192 const char fillc = 'f';
193 std::ofstream ofs(filename);
194 std::string str(size, fillc);
197 VERIFY( str.size() == size );
198 VERIFY( ofs.good() );
201 ofs << str << std::endl;
205 ofs << str << std::endl;
209 VERIFY( str.size() == size );
210 VERIFY( ofs.good() );
214 // sanity check on the written file
215 std::ifstream ifs(filename);
218 while (count <= (2 * size) + 4)
221 if (ifs.good() && c == fillc)
230 VERIFY( count == 2 * size );
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
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();
250 size_type i03 = str01.size();
251 size_type i04 = str01.capacity();
252 VERIFY( i01 - 1 == i03 );
253 VERIFY( i02 >= i04 );
255 std::istringstream is(str01);
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 );
268 // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00085.html
269 // istream::operator>>(string)
271 // NB: this is a defect in the standard.
275 const std::string name("z6.cc");
276 std::istringstream iss (name);
283 VERIFY( static_cast<bool>(iss.rdstate() & std::ios_base::failbit) );
296 istringstream istrm("enero:2001");
301 istrm >> setw(5) >> month >> sep >> year;
302 VERIFY( month.size() == 5 );
303 VERIFY( sep == ':' );
304 VERIFY( year == 2001 );
311 test04(1); // expected_size == 4
312 test04(1000); // expected_size == 2002
313 test04(10000); // expected_size == 20002