3 // Copyright (C) 1999, 2000, 2001, 2002 Free Software Foundation
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 // 27.6.1.3 unformatted input functions
22 // @require@ %-*.tst %-*.txt
23 // @diff@ %-*.tst %-*.txt
25 #include <cstring> // for strncmp,...
29 #include <testsuite_hooks.h>
34 typedef std::ios::traits_type traits_type
;
37 const std::string str_01
;
38 const std::string
str_02("soul eyes: john coltrane quartet");
41 std::stringbuf
isbuf_03(str_02
, std::ios_base::in
);
42 std::stringbuf
isbuf_04(str_02
, std::ios_base::in
);
44 std::istream
is_00(NULL
);
45 std::istream
is_03(&isbuf_03
);
46 std::istream
is_04(&isbuf_04
);
47 std::ios_base::iostate state1
, state2
, statefail
, stateeof
;
48 statefail
= std::ios_base::failbit
;
49 stateeof
= std::ios_base::eofbit
;
51 // istream& read(char_type* s, streamsize n)
53 state1
= is_04
.rdstate();
54 is_04
.read(carray
, 0);
55 state2
= is_04
.rdstate();
56 VERIFY( state1
== state2
);
58 state1
= is_04
.rdstate();
59 is_04
.read(carray
, 9);
60 state2
= is_04
.rdstate();
61 VERIFY( state1
== state2
);
62 VERIFY( !std::strncmp(carray
, "soul eyes", 9) );
63 VERIFY( is_04
.peek() == ':' );
65 state1
= is_03
.rdstate();
66 is_03
.read(carray
, 60);
67 state2
= is_03
.rdstate();
68 VERIFY( state1
!= state2
);
69 VERIFY( static_cast<bool>(state2
& stateeof
) );
70 VERIFY( static_cast<bool>(state2
& statefail
) );
71 VERIFY( !std::strncmp(carray
, "soul eyes: john coltrane quartet", 35) );
74 // istream& ignore(streamsize n = 1, int_type delim = traits::eof())
75 state1
= is_04
.rdstate();
77 VERIFY( is_04
.gcount() == 1 );
78 state2
= is_04
.rdstate();
79 VERIFY( state1
== state2
);
80 VERIFY( is_04
.peek() == ' ' );
82 state1
= is_04
.rdstate();
84 VERIFY( is_04
.gcount() == 0 );
85 state2
= is_04
.rdstate();
86 VERIFY( state1
== state2
);
87 VERIFY( is_04
.peek() == ' ' );
89 state1
= is_04
.rdstate();
90 is_04
.ignore(5, traits_type::to_int_type(' '));
91 VERIFY( is_04
.gcount() == 1 );
92 state2
= is_04
.rdstate();
93 VERIFY( state1
== state2
);
94 VERIFY( is_04
.peek() == 'j' );
97 state1
= is_04
.rdstate();
98 VERIFY( is_04
.peek() == 'j' );
99 VERIFY( is_04
.gcount() == 0 );
100 state2
= is_04
.rdstate();
101 VERIFY( state1
== state2
);
104 state1
= is_04
.rdstate();
105 VERIFY( is_04
.peek() == traits_type::eof() );
106 VERIFY( is_04
.gcount() == 0 );
107 state2
= is_04
.rdstate();
108 VERIFY( state1
!= state2
);
111 // istream& putback(char c)
113 state1
= is_04
.rdstate();
115 VERIFY( is_04
.gcount() == 0 );
116 state2
= is_04
.rdstate();
117 VERIFY( state1
== state2
);
118 VERIFY( is_04
.peek() == '|' );
122 state1
= is_04
.rdstate();
124 VERIFY( is_04
.gcount() == 0 );
125 state2
= is_04
.rdstate();
126 VERIFY( state1
== state2
);
127 VERIFY( is_04
.peek() == 'e' );
130 int i
= is_00
.sync();
142 typedef std::char_traits
<char> traits_type
;
145 const char str_lit01
[] = "\t\t\t sun*ra \n"
147 "and his myth science arkestra present\n"
149 "angles and demons @ play\n"
151 "the nubians of plutonia";
152 std::string
str01(str_lit01
);
155 std::stringbuf
sbuf_04(str01
, std::ios_base::in
);
157 std::istream
is_00(NULL
);
158 std::istream
is_04(&sbuf_04
);
159 std::ios_base::iostate state1
, state2
, statefail
, stateeof
;
160 statefail
= std::ios_base::failbit
;
161 stateeof
= std::ios_base::eofbit
;
162 std::streamsize count1
, count2
;
163 char carray1
[400] = "";
165 // istream& getline(char* s, streamsize n, char delim)
166 // istream& getline(char* s, streamsize n)
167 state1
= is_00
.rdstate();
168 is_00
.getline(carray1
, 20, '*');
169 state2
= is_00
.rdstate();
170 // make sure failbit was set, since we couldn't extract
171 // from the NULL streambuf...
172 VERIFY( state1
!= state2
);
173 VERIFY( static_cast<bool>(state2
& statefail
) );
175 VERIFY( is_04
.gcount() == 0 );
176 state1
= is_04
.rdstate();
177 is_04
.getline(carray1
, 1, '\t'); // extracts, throws away
178 state2
= is_04
.rdstate();
179 VERIFY( is_04
.gcount() == 1 );
180 VERIFY( state1
== state2
);
181 VERIFY( state1
== 0 );
182 VERIFY( !traits_type::compare("", carray1
, 1) );
184 state1
= is_04
.rdstate();
185 is_04
.getline(carray1
, 20, '*');
186 state2
= is_04
.rdstate();
187 VERIFY( is_04
.gcount() == 10 );
188 VERIFY( state1
== state2
);
189 VERIFY( state1
== 0 );
190 VERIFY( !traits_type::compare("\t\t sun", carray1
, 10) );
192 state1
= is_04
.rdstate();
193 is_04
.getline(carray1
, 20);
194 state2
= is_04
.rdstate();
195 VERIFY( is_04
.gcount() == 4 );
196 VERIFY( state1
== state2
);
197 VERIFY( state1
== 0 );
198 VERIFY( !traits_type::compare("ra ", carray1
, 4) );
200 state1
= is_04
.rdstate();
201 is_04
.getline(carray1
, 65);
202 state2
= is_04
.rdstate();
203 VERIFY( is_04
.gcount() == 64 );
204 VERIFY( state1
!= state2
);
205 VERIFY( state2
== statefail
);
206 VERIFY( !traits_type::compare(
207 " and his myth science arkestra presen",
211 state1
= is_04
.rdstate();
212 is_04
.getline(carray1
, 120, '|');
213 state2
= is_04
.rdstate();
214 VERIFY( is_04
.gcount() == 106 );
215 VERIFY( state1
!= state2
);
216 VERIFY( state2
== stateeof
);
219 state1
= is_04
.rdstate();
220 is_04
.getline(carray1
, 100, '|');
221 state2
= is_04
.rdstate();
222 VERIFY( is_04
.gcount() == 0 );
223 VERIFY( state1
!= state2
);
224 VERIFY( static_cast<bool>(state2
& stateeof
) );
225 VERIFY( static_cast<bool>(state2
& statefail
) );
237 typedef std::char_traits
<char> traits_type
;
240 const char str_lit01
[] =
241 " sun*ra \n\t\t\t & his arkestra, featuring john gilmore: \n"
243 "jazz in silhouette: images and forecasts of tomorrow";
245 std::string
str01(str_lit01
);
248 std::stringbuf sbuf_03
;
249 std::stringbuf
sbuf_04(str01
, std::ios_base::in
);
250 std::stringbuf
sbuf_05(str01
, std::ios_base::in
);
252 std::istream
is_00(NULL
);
253 std::istream
is_04(&sbuf_04
);
254 std::istream
is_05(&sbuf_05
);
255 std::ios_base::iostate state1
, state2
, statefail
, stateeof
;
256 statefail
= std::ios_base::failbit
;
257 stateeof
= std::ios_base::eofbit
;
258 std::streamsize count1
, count2
;
259 char carray1
[400] = "";
262 // istream& get(char*, streamsize, char delim)
263 // istream& get(char*, streamsize)
264 // istream& get(streambuf&, char delim)
265 // istream& get(streambuf&)
266 is_00
.get(carray1
, 2);
267 VERIFY( static_cast<bool>(is_00
.rdstate() & statefail
) );
268 VERIFY( is_00
.gcount() == 0 );
270 is_04
.get(carray1
, 4);
271 VERIFY( !(is_04
.rdstate() & statefail
) );
272 VERIFY( !traits_type::compare(carray1
, " ", 4) );
273 VERIFY( is_04
.gcount() == 3 );
276 is_04
.get(carray1
+ 3, 200);
277 VERIFY( !(is_04
.rdstate() & statefail
) );
278 VERIFY( !(is_04
.rdstate() & stateeof
) );
279 VERIFY( !traits_type::compare(carray1
, str_lit01
, 10) );
280 VERIFY( is_04
.gcount() == 7 );
283 is_04
.get(carray1
, 200);
284 VERIFY( !(is_04
.rdstate() & stateeof
) );
285 VERIFY( static_cast<bool>(is_04
.rdstate() & statefail
) ); // delimiter
286 VERIFY( is_04
.gcount() == 0 );
288 is_04
.get(carray1
, 200, '[');
289 VERIFY( static_cast<bool>(is_04
.rdstate() & stateeof
) );
290 VERIFY( !(is_04
.rdstate() & statefail
) );
291 VERIFY( is_04
.gcount() == 125 );
293 is_04
.get(carray1
, 200);
294 VERIFY( static_cast<bool>(is_04
.rdstate() & stateeof
) );
295 VERIFY( static_cast<bool>(is_04
.rdstate() & statefail
) );
296 VERIFY( is_04
.gcount() == 0 );
298 std::stringbuf
sbuf_02(std::ios_base::in
);
301 VERIFY( is_05
.gcount() == 0 );
302 VERIFY( static_cast<bool>(is_05
.rdstate() & statefail
) );
303 VERIFY( !(is_05
.rdstate() & stateeof
) );
307 VERIFY( is_05
.gcount() == 10 );
308 VERIFY( sbuf_03
.str() == " sun*ra " );
309 VERIFY( !(is_05
.rdstate() & statefail
) );
310 VERIFY( !(is_05
.rdstate() & stateeof
) );
313 is_05
.get(sbuf_03
, '|');
314 VERIFY( is_05
.gcount() == 125 );
315 VERIFY( sbuf_03
.str() == str_lit01
);
316 VERIFY( !(is_05
.rdstate() & statefail
) );
317 VERIFY( static_cast<bool>(is_05
.rdstate() & stateeof
) );
320 is_05
.get(sbuf_03
, '|');
321 VERIFY( is_05
.gcount() == 0 );
322 VERIFY( static_cast<bool>(is_05
.rdstate() & stateeof
) );
323 VERIFY( static_cast<bool>(is_05
.rdstate() & statefail
) );
332 // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00177.html
338 const std::string
str_00("Red_Garland_Qunitet-Soul_Junction");
340 char c_array
[str_00
.size() + 4];
342 std::stringbuf
isbuf_00(str_00
, std::ios_base::in
);
343 std::istream
is_00(&isbuf_00
);
344 std::ios_base::iostate state1
, state2
, statefail
, stateeof
;
345 statefail
= std::ios_base::failbit
;
346 stateeof
= std::ios_base::eofbit
;
348 state1
= stateeof
| statefail
;
349 VERIFY( is_00
.gcount() == 0 );
350 is_00
.read(c_array
, str_00
.size() + 1);
351 VERIFY( is_00
.gcount() == str_00
.size() );
352 VERIFY( is_00
.rdstate() == state1
);
354 is_00
.read(c_array
, str_00
.size());
355 VERIFY( is_00
.rdstate() == state1
);
363 // http://gcc.gnu.org/ml/libstdc++/2000-07/msg00003.html
367 const char* charray
= "\n"
384 const std::streamsize it
= 5;
385 std::streamsize br
= 0;
387 std::stringbuf
sb(charray
, std::ios_base::in
);
388 std::istream
ifs(&sb
);
389 std::streamsize blen
= std::strlen(charray
);
391 while(ifs
.getline(tmp
, it
) || ifs
.gcount())
396 // Just sanity checks to make sure we've extracted the same
397 // number of chars that were in the streambuf
399 // Also, we should only set the failbit if we could
400 // _extract_ no chars from the stream, i.e. the first read
402 VERIFY(ifs
.fail() && ifs
.gcount() == 0);
406 // delimiter not read
409 // -> extracted no characters
411 // -> n - 1 characters are stored
412 ifs
.clear(ifs
.rdstate() & ~std::ios::failbit
);
413 VERIFY((ifs
.gcount() == 0) || (std::strlen(tmp
) == it
- 1));
419 // delimiter was read.
421 // -> strlen(__s) < n - 1
422 // -> delimiter was seen -> gcount() > strlen(__s)
423 VERIFY(ifs
.gcount() == std::strlen(tmp
) + 1);
432 // http://gcc.gnu.org/ml/libstdc++/2000-07/msg00126.html
439 const streamsize it
= 5;
441 const char* str_lit
= "abcd\n";
443 stringbuf
strbuf(str_lit
, std::ios_base::in
);
444 istream
istr(&strbuf
);
446 istr
.getline(tmp
,it
);
447 VERIFY( istr
.gcount() == it
); // extracted whole string
448 VERIFY( strlen(tmp
) == 4 ); // stored all but '\n'
449 VERIFY( !istr
.eof() ); // extracted up to but not eof
450 VERIFY( !istr
.fail() ); // failbit not set
455 VERIFY( istr
.eof() );
464 // bug reported by bgarcia@laurelnetworks.com
465 // http://gcc.gnu.org/ml/libstdc++-prs/2000-q3/msg00041.html
470 const char* tfn
= "istream_unformatted-1.txt";
471 std::ifstream infile
;
473 VERIFY( !(!infile
) );
477 std::ostringstream line_ss
;
478 while (infile
.peek() == '\n')
480 infile
.get(*(line_ss
.rdbuf()));
481 line
= line_ss
.str();
482 VERIFY( line
== "1234567890" || line
== "" );
486 // 2002-04-19 PR libstdc++ 6360
493 stringstream
ss("abcd" "\xFF" "1234ina donna coolbrith");
502 // Theodore Papadopoulo
509 istringstream
iss("Juana Briones");
523 typedef string string_type
;
524 typedef stringbuf stringbuf_type
;
525 typedef istream istream_type
;
529 string_type
input("abcdefg\n");
530 stringbuf_type
sbuf(input
);
531 istream_type
istr(&sbuf
);
534 if (istr
.gcount() != 0)
539 if (istr
.gcount() != 0)
543 istr
.ignore(); // Advance to next position.
545 if ((n
=istr
.gcount()) != 0)
549 if (istr
.peek() != 'b')
556 class mybuf
: public std::basic_streambuf
<char>
565 basic_istream
<char, char_traits
<char> > istr(&sbuf
);
567 VERIFY(istr
.rdstate() == ios_base::goodbit
);
568 VERIFY(istr
.readsome(arr
, 10) == 0);
569 VERIFY(istr
.rdstate() == ios_base::goodbit
);
578 istringstream
iss("shamma shamma");
581 size_t i
= iss
.rdbuf()->in_avail();
585 streamsize extracted
;
589 extracted
= iss
.readsome(buf
, sizeof buf
);
592 while (iss
.good() && extracted
);
602 ifstream
ifs("istream_unformatted-1.tst");
605 size_t i
= ifs
.rdbuf()->in_avail();
609 streamsize extracted
;
613 extracted
= ifs
.readsome(buf
, sizeof buf
);
616 while (ifs
.good() && extracted
);