]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/istream_unformatted.cc
3 // Copyright (C) 1999, 2000, 2001 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 <debug_assert.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 // { dg-warning "string literals" "" { xfail *-*-* } 146 }
146 const char str_lit01
[] = " sun*ra
147 and his myth science arkestra present
148 angles and demons @ play
149 the nubians of plutonia";
150 std::string
str01(str_lit01
);
153 std::stringbuf
sbuf_04(str01
, std::ios_base::in
);
155 std::istream
is_00(NULL
);
156 std::istream
is_04(&sbuf_04
);
157 std::ios_base::iostate state1
, state2
, statefail
, stateeof
;
158 statefail
= std::ios_base::failbit
;
159 stateeof
= std::ios_base::eofbit
;
160 std::streamsize count1
, count2
;
161 char carray1
[400] = "";
163 // istream& getline(char* s, streamsize n, char delim)
164 // istream& getline(char* s, streamsize n)
165 state1
= is_00
.rdstate();
166 is_00
.getline(carray1
, 20, '*');
167 state2
= is_00
.rdstate();
168 // make sure failbit was set, since we couldn't extract
169 // from the NULL streambuf...
170 VERIFY( state1
!= state2
);
171 VERIFY( static_cast<bool>(state2
& statefail
) );
173 VERIFY( is_04
.gcount() == 0 );
174 state1
= is_04
.rdstate();
175 is_04
.getline(carray1
, 1, '\t'); // extracts, throws away
176 state2
= is_04
.rdstate();
177 VERIFY( is_04
.gcount() == 1 );
178 VERIFY( state1
== state2
);
179 VERIFY( state1
== 0 );
180 VERIFY( !traits_type::compare("", carray1
, 1) );
182 state1
= is_04
.rdstate();
183 is_04
.getline(carray1
, 20, '*');
184 state2
= is_04
.rdstate();
185 VERIFY( is_04
.gcount() == 10 );
186 VERIFY( state1
== state2
);
187 VERIFY( state1
== 0 );
188 VERIFY( !traits_type::compare("\t\t sun", carray1
, 10) );
190 state1
= is_04
.rdstate();
191 is_04
.getline(carray1
, 20);
192 state2
= is_04
.rdstate();
193 VERIFY( is_04
.gcount() == 4 );
194 VERIFY( state1
== state2
);
195 VERIFY( state1
== 0 );
196 VERIFY( !traits_type::compare("ra ", carray1
, 4) );
198 state1
= is_04
.rdstate();
199 is_04
.getline(carray1
, 65);
200 state2
= is_04
.rdstate();
201 VERIFY( is_04
.gcount() == 64 );
202 VERIFY( state1
!= state2
);
203 VERIFY( state2
== statefail
);
204 // { dg-warning "string literals" "" { xfail *-*-* } 205 }
205 VERIFY( !traits_type::compare(" and his myth science arkestra presen", carray1
, 65) );
208 state1
= is_04
.rdstate();
209 is_04
.getline(carray1
, 120, '|');
210 state2
= is_04
.rdstate();
211 VERIFY( is_04
.gcount() == 106 );
212 VERIFY( state1
!= state2
);
213 VERIFY( state2
== stateeof
);
216 state1
= is_04
.rdstate();
217 is_04
.getline(carray1
, 100, '|');
218 state2
= is_04
.rdstate();
219 VERIFY( is_04
.gcount() == 0 );
220 VERIFY( state1
!= state2
);
221 VERIFY( static_cast<bool>(state2
& stateeof
) );
222 VERIFY( static_cast<bool>(state2
& statefail
) );
234 typedef std::char_traits
<char> traits_type
;
237 // { dg-warning "string literals" "" { xfail *-*-* } 238 }
238 const char str_lit01
[] = " sun*ra
239 & his arkestra, featuring john gilmore:
240 jazz in silhouette: images and forecasts of tomorrow";
241 std::string
str01(str_lit01
);
244 std::stringbuf sbuf_03
;
245 std::stringbuf
sbuf_04(str01
, std::ios_base::in
);
246 std::stringbuf
sbuf_05(str01
, std::ios_base::in
);
248 std::istream
is_00(NULL
);
249 std::istream
is_04(&sbuf_04
);
250 std::istream
is_05(&sbuf_05
);
251 std::ios_base::iostate state1
, state2
, statefail
, stateeof
;
252 statefail
= std::ios_base::failbit
;
253 stateeof
= std::ios_base::eofbit
;
254 std::streamsize count1
, count2
;
255 char carray1
[400] = "";
258 // istream& get(char*, streamsize, char delim)
259 // istream& get(char*, streamsize)
260 // istream& get(streambuf&, char delim)
261 // istream& get(streambuf&)
262 is_00
.get(carray1
, 2);
263 VERIFY( static_cast<bool>(is_00
.rdstate() & statefail
) );
264 VERIFY( is_00
.gcount() == 0 );
266 is_04
.get(carray1
, 4);
267 VERIFY( !(is_04
.rdstate() & statefail
) );
268 VERIFY( !traits_type::compare(carray1
, " ", 4) );
269 VERIFY( is_04
.gcount() == 3 );
272 is_04
.get(carray1
+ 3, 200);
273 VERIFY( !(is_04
.rdstate() & statefail
) );
274 VERIFY( !(is_04
.rdstate() & stateeof
) );
275 VERIFY( !traits_type::compare(carray1
, str_lit01
, 10) );
276 VERIFY( is_04
.gcount() == 7 );
279 is_04
.get(carray1
, 200);
280 VERIFY( !(is_04
.rdstate() & stateeof
) );
281 VERIFY( static_cast<bool>(is_04
.rdstate() & statefail
) ); // delimiter
282 VERIFY( is_04
.gcount() == 0 );
284 is_04
.get(carray1
, 200, '[');
285 VERIFY( static_cast<bool>(is_04
.rdstate() & stateeof
) );
286 VERIFY( !(is_04
.rdstate() & statefail
) );
287 VERIFY( is_04
.gcount() == 125 );
289 is_04
.get(carray1
, 200);
290 VERIFY( static_cast<bool>(is_04
.rdstate() & stateeof
) );
291 VERIFY( static_cast<bool>(is_04
.rdstate() & statefail
) );
292 VERIFY( is_04
.gcount() == 0 );
294 std::stringbuf
sbuf_02(std::ios_base::in
);
297 VERIFY( is_05
.gcount() == 0 );
298 VERIFY( static_cast<bool>(is_05
.rdstate() & statefail
) );
299 VERIFY( !(is_05
.rdstate() & stateeof
) );
303 VERIFY( is_05
.gcount() == 10 );
304 VERIFY( sbuf_03
.str() == " sun*ra " );
305 VERIFY( !(is_05
.rdstate() & statefail
) );
306 VERIFY( !(is_05
.rdstate() & stateeof
) );
309 is_05
.get(sbuf_03
, '|');
310 VERIFY( is_05
.gcount() == 125 );
311 VERIFY( sbuf_03
.str() == str_lit01
);
312 VERIFY( !(is_05
.rdstate() & statefail
) );
313 VERIFY( static_cast<bool>(is_05
.rdstate() & stateeof
) );
316 is_05
.get(sbuf_03
, '|');
317 VERIFY( is_05
.gcount() == 0 );
318 VERIFY( static_cast<bool>(is_05
.rdstate() & stateeof
) );
319 VERIFY( static_cast<bool>(is_05
.rdstate() & statefail
) );
328 // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00177.html
334 const std::string
str_00("Red_Garland_Qunitet-Soul_Junction");
336 char c_array
[str_00
.size() + 4];
338 std::stringbuf
isbuf_00(str_00
, std::ios_base::in
);
339 std::istream
is_00(&isbuf_00
);
340 std::ios_base::iostate state1
, state2
, statefail
, stateeof
;
341 statefail
= std::ios_base::failbit
;
342 stateeof
= std::ios_base::eofbit
;
344 state1
= stateeof
| statefail
;
345 VERIFY( is_00
.gcount() == 0 );
346 is_00
.read(c_array
, str_00
.size() + 1);
347 VERIFY( is_00
.gcount() == str_00
.size() );
348 VERIFY( is_00
.rdstate() == state1
);
350 is_00
.read(c_array
, str_00
.size());
351 VERIFY( is_00
.rdstate() == state1
);
359 // http://gcc.gnu.org/ml/libstdc++/2000-07/msg00003.html
363 // { dg-warning "string literals" "" { xfail *-*-* } 364 }
364 const char* charray
= "
382 const std::streamsize it
= 5;
383 std::streamsize br
= 0;
385 std::stringbuf
sb(charray
, std::ios_base::in
);
386 std::istream
ifs(&sb
);
387 std::streamsize blen
= std::strlen(charray
);
389 while(ifs
.getline(tmp
, it
) || ifs
.gcount())
394 // Just sanity checks to make sure we've extracted the same
395 // number of chars that were in the streambuf
397 // Also, we should only set the failbit if we could
398 // _extract_ no chars from the stream, i.e. the first read
400 VERIFY(ifs
.fail() && ifs
.gcount() == 0);
404 // delimiter not read
407 // -> extracted no characters
409 // -> n - 1 characters are stored
410 ifs
.clear(ifs
.rdstate() & ~std::ios::failbit
);
411 VERIFY((ifs
.gcount() == 0) || (std::strlen(tmp
) == it
- 1));
417 // delimiter was read.
419 // -> strlen(__s) < n - 1
420 // -> delimiter was seen -> gcount() > strlen(__s)
421 VERIFY(ifs
.gcount() == std::strlen(tmp
) + 1);
430 // http://gcc.gnu.org/ml/libstdc++/2000-07/msg00126.html
437 const streamsize it
= 5;
439 const char* str_lit
= "abcd\n";
441 stringbuf
strbuf(str_lit
, std::ios_base::in
);
442 istream
istr(&strbuf
);
444 istr
.getline(tmp
,it
);
445 VERIFY( istr
.gcount() == it
); // extracted whole string
446 VERIFY( strlen(tmp
) == 4 ); // stored all but '\n'
447 VERIFY( !istr
.eof() ); // extracted up to but not eof
448 VERIFY( !istr
.fail() ); // failbit not set
453 VERIFY( istr
.eof() );
462 // bug reported by bgarcia@laurelnetworks.com
463 // http://gcc.gnu.org/ml/libstdc++-prs/2000-q3/msg00041.html
468 const char* tfn
= "istream_unformatted-1.txt";
469 std::ifstream infile
;
471 VERIFY( !(!infile
) );
475 std::ostringstream line_ss
;
476 while (infile
.peek() == '\n')
478 infile
.get(*(line_ss
.rdbuf()));
479 line
= line_ss
.str();
480 VERIFY( line
== "1234567890" || line
== "" );