]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/istream_unformatted.cc
*.cc: Remove spaces, make sure testcases return zero.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / istream_unformatted.cc
1 // 1999-08-11 bkoz
2
3 // Copyright (C) 1999, 2000, 2001 Free Software Foundation
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 // 27.6.1.3 unformatted input functions
22 // @require@ %-*.tst %-*.txt
23 // @diff@ %-*.tst %-*.txt
24
25 #include <cstring> // for strncmp,...
26 #include <istream>
27 #include <sstream>
28 #include <fstream>
29 #include <debug_assert.h>
30
31 int
32 test01()
33 {
34 typedef std::ios::traits_type traits_type;
35
36 bool test = true;
37 const std::string str_01;
38 const std::string str_02("soul eyes: john coltrane quartet");
39 std::string strtmp;
40
41 std::stringbuf isbuf_03(str_02, std::ios_base::in);
42 std::stringbuf isbuf_04(str_02, std::ios_base::in);
43
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;
50
51 // istream& read(char_type* s, streamsize n)
52 char carray[60] = "";
53 state1 = is_04.rdstate();
54 is_04.read(carray, 0);
55 state2 = is_04.rdstate();
56 VERIFY( state1 == state2 );
57
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() == ':' );
64
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) );
72
73
74 // istream& ignore(streamsize n = 1, int_type delim = traits::eof())
75 state1 = is_04.rdstate();
76 is_04.ignore();
77 VERIFY( is_04.gcount() == 1 );
78 state2 = is_04.rdstate();
79 VERIFY( state1 == state2 );
80 VERIFY( is_04.peek() == ' ' );
81
82 state1 = is_04.rdstate();
83 is_04.ignore(0);
84 VERIFY( is_04.gcount() == 0 );
85 state2 = is_04.rdstate();
86 VERIFY( state1 == state2 );
87 VERIFY( is_04.peek() == ' ' );
88
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' );
95
96 // int_type peek()
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 );
102
103 is_04.ignore(30);
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 );
109
110
111 // istream& putback(char c)
112 is_04.clear();
113 state1 = is_04.rdstate();
114 is_04.putback('|');
115 VERIFY( is_04.gcount() == 0 );
116 state2 = is_04.rdstate();
117 VERIFY( state1 == state2 );
118 VERIFY( is_04.peek() == '|' );
119
120 // istream& unget()
121 is_04.clear();
122 state1 = is_04.rdstate();
123 is_04.unget();
124 VERIFY( is_04.gcount() == 0 );
125 state2 = is_04.rdstate();
126 VERIFY( state1 == state2 );
127 VERIFY( is_04.peek() == 'e' );
128
129 // int sync()
130 int i = is_00.sync();
131
132 #ifdef DEBUG_ASSERT
133 assert(test);
134 #endif
135
136 return 0;
137 }
138
139 int
140 test02()
141 {
142 typedef std::char_traits<char> traits_type;
143
144 bool test = true;
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);
151 std::string strtmp;
152
153 std::stringbuf sbuf_04(str01, std::ios_base::in);
154
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] = "";
162
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) );
172
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) );
181
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) );
189
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) );
197
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) );
206
207 is_04.clear();
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 );
214
215 is_04.clear();
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) );
223
224 #ifdef DEBUG_ASSERT
225 assert(test);
226 #endif
227
228 return 0;
229 }
230
231 int
232 test03()
233 {
234 typedef std::char_traits<char> traits_type;
235
236 bool test = true;
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);
242 std::string strtmp;
243
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);
247
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] = "";
256
257 // int_type get()
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 );
265
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 );
270
271 is_04.clear();
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 );
277
278 is_04.clear();
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 );
283 is_04.clear();
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 );
288 is_04.clear();
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 );
293
294 std::stringbuf sbuf_02(std::ios_base::in);
295 is_05.clear();
296 is_05.get(sbuf_02);
297 VERIFY( is_05.gcount() == 0 );
298 VERIFY( static_cast<bool>(is_05.rdstate() & statefail) );
299 VERIFY( !(is_05.rdstate() & stateeof) );
300
301 is_05.clear();
302 is_05.get(sbuf_03);
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) );
307
308 is_05.clear();
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) );
314
315 is_05.clear();
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) );
320
321 #ifdef DEBUG_ASSERT
322 assert(test);
323 #endif
324
325 return 0;
326 }
327
328 // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00177.html
329 int
330 test04()
331 {
332 bool test = true;
333
334 const std::string str_00("Red_Garland_Qunitet-Soul_Junction");
335 std::string strtmp;
336 char c_array[str_00.size() + 4];
337
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;
343
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 );
349
350 is_00.read(c_array, str_00.size());
351 VERIFY( is_00.rdstate() == state1 );
352
353 #ifdef DEBUG_ASSERT
354 assert(test);
355 #endif
356 return 0;
357 }
358
359 // http://gcc.gnu.org/ml/libstdc++/2000-07/msg00003.html
360 int
361 test05()
362 {
363 // { dg-warning "string literals" "" { xfail *-*-* } 364 }
364 const char* charray = "
365 a
366 aa
367 aaa
368 aaaa
369 aaaaa
370 aaaaaa
371 aaaaaaa
372 aaaaaaaa
373 aaaaaaaaa
374 aaaaaaaaaa
375 aaaaaaaaaaa
376 aaaaaaaaaaaa
377 aaaaaaaaaaaaa
378 aaaaaaaaaaaaaa
379 ";
380
381 bool test = true;
382 const std::streamsize it = 5;
383 std::streamsize br = 0;
384 char tmp[it];
385 std::stringbuf sb(charray, std::ios_base::in);
386 std::istream ifs(&sb);
387 std::streamsize blen = std::strlen(charray);
388 VERIFY(!(!ifs));
389 while(ifs.getline(tmp, it) || ifs.gcount())
390 {
391 br += ifs.gcount();
392 if(ifs.eof())
393 {
394 // Just sanity checks to make sure we've extracted the same
395 // number of chars that were in the streambuf
396 VERIFY(br == blen);
397 // Also, we should only set the failbit if we could
398 // _extract_ no chars from the stream, i.e. the first read
399 // returned EOF.
400 VERIFY(ifs.fail() && ifs.gcount() == 0);
401 }
402 else if(ifs.fail())
403 {
404 // delimiter not read
405 //
406 // either
407 // -> extracted no characters
408 // or
409 // -> n - 1 characters are stored
410 ifs.clear(ifs.rdstate() & ~std::ios::failbit);
411 VERIFY((ifs.gcount() == 0) || (std::strlen(tmp) == it - 1));
412 VERIFY(!(!ifs));
413 continue;
414 }
415 else
416 {
417 // delimiter was read.
418 //
419 // -> strlen(__s) < n - 1
420 // -> delimiter was seen -> gcount() > strlen(__s)
421 VERIFY(ifs.gcount() == std::strlen(tmp) + 1);
422 continue;
423 }
424 }
425
426 return 0;
427 }
428
429
430 // http://gcc.gnu.org/ml/libstdc++/2000-07/msg00126.html
431 int
432 test06()
433 {
434 using namespace std;
435
436 bool test = true;
437 const streamsize it = 5;
438 char tmp[it];
439 const char* str_lit = "abcd\n";
440
441 stringbuf strbuf(str_lit, std::ios_base::in);
442 istream istr(&strbuf);
443
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
449
450 char c = 'z';
451 istr.get(c);
452 VERIFY( c == 'z' );
453 VERIFY( istr.eof() );
454
455 #ifdef DEBUG_ASSERT
456 assert(test);
457 #endif
458
459 return 0;
460 }
461
462 // bug reported by bgarcia@laurelnetworks.com
463 // http://gcc.gnu.org/ml/libstdc++-prs/2000-q3/msg00041.html
464 int
465 test07()
466 {
467 bool test = true;
468 const char* tfn = "istream_unformatted-1.txt";
469 std::ifstream infile;
470 infile.open(tfn);
471 VERIFY( !(!infile) );
472 while (infile)
473 {
474 std::string line;
475 std::ostringstream line_ss;
476 while (infile.peek() == '\n')
477 infile.get();
478 infile.get(*(line_ss.rdbuf()));
479 line = line_ss.str();
480 VERIFY( line == "1234567890" || line == "" );
481 }
482 return 0;
483 }
484
485 int
486 main()
487 {
488 test01();
489 test02();
490 test03();
491 test04();
492 test05();
493 test06();
494 test07();
495
496 return 0;
497 }