2002-11-21 Phil Edwards <pme@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / istream_unformatted.cc
blobe7676ecb370e35df60c7ef304917ae9a238147e7
1 // 1999-08-11 bkoz
3 // Copyright (C) 1999, 2000, 2001, 2002 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.
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,
19 // USA.
21 // 27.6.1.3 unformatted input functions
22 // @require@ %-*.tst %-*.txt
23 // @diff@ %-*.tst %-*.txt
25 #include <cstring> // for strncmp,...
26 #include <istream>
27 #include <sstream>
28 #include <fstream>
29 #include <testsuite_hooks.h>
31 int
32 test01()
34 typedef std::ios::traits_type traits_type;
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;
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)
52 char carray[60] = "";
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();
76 is_04.ignore();
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();
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() == ' ' );
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' );
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 );
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 );
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() == '|' );
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' );
129 // int sync()
130 int i = is_00.sync();
132 #ifdef DEBUG_ASSERT
133 assert(test);
134 #endif
136 return 0;
140 test02()
142 typedef std::char_traits<char> traits_type;
144 bool test = true;
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);
153 std::string strtmp;
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",
208 carray1, 65) );
210 is_04.clear();
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 );
218 is_04.clear();
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) );
227 #ifdef DEBUG_ASSERT
228 assert(test);
229 #endif
231 return 0;
235 test03()
237 typedef std::char_traits<char> traits_type;
239 bool test = true;
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);
246 std::string strtmp;
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] = "";
261 // int_type get()
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 );
275 is_04.clear();
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 );
282 is_04.clear();
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 );
287 is_04.clear();
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 );
292 is_04.clear();
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);
299 is_05.clear();
300 is_05.get(sbuf_02);
301 VERIFY( is_05.gcount() == 0 );
302 VERIFY( static_cast<bool>(is_05.rdstate() & statefail) );
303 VERIFY( !(is_05.rdstate() & stateeof) );
305 is_05.clear();
306 is_05.get(sbuf_03);
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) );
312 is_05.clear();
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) );
319 is_05.clear();
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) );
325 #ifdef DEBUG_ASSERT
326 assert(test);
327 #endif
329 return 0;
332 // http://gcc.gnu.org/ml/libstdc++/2000-q1/msg00177.html
334 test04()
336 bool test = true;
338 const std::string str_00("Red_Garland_Qunitet-Soul_Junction");
339 std::string strtmp;
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 );
357 #ifdef DEBUG_ASSERT
358 assert(test);
359 #endif
360 return 0;
363 // http://gcc.gnu.org/ml/libstdc++/2000-07/msg00003.html
365 test05()
367 const char* charray = "\n"
368 "a\n"
369 "aa\n"
370 "aaa\n"
371 "aaaa\n"
372 "aaaaa\n"
373 "aaaaaa\n"
374 "aaaaaaa\n"
375 "aaaaaaaa\n"
376 "aaaaaaaaa\n"
377 "aaaaaaaaaa\n"
378 "aaaaaaaaaaa\n"
379 "aaaaaaaaaaaa\n"
380 "aaaaaaaaaaaaa\n"
381 "aaaaaaaaaaaaaa\n";
383 bool test = true;
384 const std::streamsize it = 5;
385 std::streamsize br = 0;
386 char tmp[it];
387 std::stringbuf sb(charray, std::ios_base::in);
388 std::istream ifs(&sb);
389 std::streamsize blen = std::strlen(charray);
390 VERIFY(!(!ifs));
391 while(ifs.getline(tmp, it) || ifs.gcount())
393 br += ifs.gcount();
394 if(ifs.eof())
396 // Just sanity checks to make sure we've extracted the same
397 // number of chars that were in the streambuf
398 VERIFY(br == blen);
399 // Also, we should only set the failbit if we could
400 // _extract_ no chars from the stream, i.e. the first read
401 // returned EOF.
402 VERIFY(ifs.fail() && ifs.gcount() == 0);
404 else if(ifs.fail())
406 // delimiter not read
408 // either
409 // -> extracted no characters
410 // or
411 // -> n - 1 characters are stored
412 ifs.clear(ifs.rdstate() & ~std::ios::failbit);
413 VERIFY((ifs.gcount() == 0) || (std::strlen(tmp) == it - 1));
414 VERIFY(!(!ifs));
415 continue;
417 else
419 // delimiter was read.
421 // -> strlen(__s) < n - 1
422 // -> delimiter was seen -> gcount() > strlen(__s)
423 VERIFY(ifs.gcount() == std::strlen(tmp) + 1);
424 continue;
428 return 0;
432 // http://gcc.gnu.org/ml/libstdc++/2000-07/msg00126.html
434 test06()
436 using namespace std;
438 bool test = true;
439 const streamsize it = 5;
440 char tmp[it];
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
452 char c = 'z';
453 istr.get(c);
454 VERIFY( c == 'z' );
455 VERIFY( istr.eof() );
457 #ifdef DEBUG_ASSERT
458 assert(test);
459 #endif
461 return 0;
464 // bug reported by bgarcia@laurelnetworks.com
465 // http://gcc.gnu.org/ml/libstdc++-prs/2000-q3/msg00041.html
466 void
467 test07()
469 bool test = true;
470 const char* tfn = "istream_unformatted-1.txt";
471 std::ifstream infile;
472 infile.open(tfn);
473 VERIFY( !(!infile) );
474 while (infile)
476 std::string line;
477 std::ostringstream line_ss;
478 while (infile.peek() == '\n')
479 infile.get();
480 infile.get(*(line_ss.rdbuf()));
481 line = line_ss.str();
482 VERIFY( line == "1234567890" || line == "" );
486 // 2002-04-19 PR libstdc++ 6360
487 void
488 test08()
490 using namespace std;
491 bool test = true;
493 stringstream ss("abcd" "\xFF" "1234ina donna coolbrith");
494 char c;
495 ss >> c;
496 VERIFY( c == 'a' );
497 ss.ignore(8);
498 ss >> c;
499 VERIFY( c == 'i' );
502 // Theodore Papadopoulo
503 void
504 test09()
506 using namespace std;
507 bool test = true;
509 istringstream iss("Juana Briones");
510 char tab[13];
511 iss.read(tab, 13);
512 if (!iss)
513 test = false;
514 VERIFY( test );
517 // libstdc++/70220
518 void
519 test10()
521 using namespace std;
522 bool test = true;
523 typedef string string_type;
524 typedef stringbuf stringbuf_type;
525 typedef istream istream_type;
527 int res = 0;
528 streamsize n;
529 string_type input("abcdefg\n");
530 stringbuf_type sbuf(input);
531 istream_type istr(&sbuf);
533 istr.ignore(0);
534 if (istr.gcount() != 0)
535 test = false;
536 VERIFY( test );
538 istr.ignore(0, 'b');
539 if (istr.gcount() != 0)
540 test = false;
541 VERIFY( test );
543 istr.ignore(); // Advance to next position.
544 istr.ignore(0, 'b');
545 if ((n=istr.gcount()) != 0)
546 test = false;
547 VERIFY( test );
549 if (istr.peek() != 'b')
550 test = false;
551 VERIFY( test );
555 // libstdc++/8258
556 class mybuf : public std::basic_streambuf<char>
557 { };
559 void test11()
561 bool test = true;
562 using namespace std;
563 char arr[10];
564 mybuf sbuf;
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);
572 // libstdc++/6746
573 void test12()
575 using namespace std;
576 bool test = true;
577 streamsize sum = 0;
578 istringstream iss("shamma shamma");
580 // test01
581 size_t i = iss.rdbuf()->in_avail();
582 VERIFY( i != 0 );
584 // test02
585 streamsize extracted;
588 char buf[1024];
589 extracted = iss.readsome(buf, sizeof buf);
590 sum += extracted;
592 while (iss.good() && extracted);
593 VERIFY( sum != 0 );
596 // libstdc++/6746
597 void test13()
599 using namespace std;
600 bool test = true;
601 streamsize sum = 0;
602 ifstream ifs("istream_unformatted-1.tst");
604 // test01
605 size_t i = ifs.rdbuf()->in_avail();
606 VERIFY( i != 0 );
608 // test02
609 streamsize extracted;
612 char buf[1024];
613 extracted = ifs.readsome(buf, sizeof buf);
614 sum += extracted;
616 while (ifs.good() && extracted);
617 VERIFY( sum != 0 );
620 int
621 main()
623 test01();
624 test02();
625 test03();
626 test04();
627 test05();
628 test06();
629 test07();
630 test08();
631 test09();
632 test10();
633 test11();
635 test12();
636 test13();
638 return 0;