OSDN Git Service

2003-09-23 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / 27_io / basic_istream / getline / char / 1.cc
1 // 1999-08-11 bkoz
2
3 // Copyright (C) 1999, 2000, 2001, 2002, 2003 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 <testsuite_hooks.h>
30
31 void
32 test02()
33 {
34   typedef std::char_traits<char>        traits_type;
35
36   bool test __attribute__((unused)) = true;
37   const char str_lit01[] = "\t\t\t    sun*ra \n"
38   "                            "
39   "and his myth science arkestra present\n"
40   "                            "
41   "angles and demons @ play\n"
42   "                            "
43   "the nubians of plutonia";
44   std::string str01(str_lit01);
45   std::string strtmp;
46
47   std::stringbuf sbuf_04(str01, std::ios_base::in);
48
49   std::istream is_00(NULL);
50   std::istream is_04(&sbuf_04);
51   std::ios_base::iostate state1, state2, statefail, stateeof;
52   statefail = std::ios_base::failbit;
53   stateeof = std::ios_base::eofbit;
54   char carray1[400] = "";
55
56   // istream& getline(char* s, streamsize n, char delim)
57   // istream& getline(char* s, streamsize n)
58   state1 = is_00.rdstate();
59   is_00.getline(carray1, 20, '*');
60   state2 = is_00.rdstate();
61   // make sure failbit was set, since we couldn't extract
62   // from the NULL streambuf...
63   VERIFY( state1 != state2 );
64   VERIFY( static_cast<bool>(state2 & statefail) );
65   
66   VERIFY( is_04.gcount() == 0 );
67   state1 = is_04.rdstate();
68   is_04.getline(carray1, 1, '\t'); // extracts, throws away
69   state2 = is_04.rdstate();  
70   VERIFY( is_04.gcount() == 1 );
71   VERIFY( state1 == state2 );
72   VERIFY( state1 == 0 );
73   VERIFY( !traits_type::compare("", carray1, 1) );
74
75   state1 = is_04.rdstate();
76   is_04.getline(carray1, 20, '*');
77   state2 = is_04.rdstate();  
78   VERIFY( is_04.gcount() == 10 );
79   VERIFY( state1 == state2 );
80   VERIFY( state1 == 0 );
81   VERIFY( !traits_type::compare("\t\t    sun", carray1, 10) );
82
83   state1 = is_04.rdstate();
84   is_04.getline(carray1, 20);
85   state2 = is_04.rdstate();  
86   VERIFY( is_04.gcount() == 4 );
87   VERIFY( state1 == state2 );
88   VERIFY( state1 == 0 );
89   VERIFY( !traits_type::compare("ra ", carray1, 4) );
90
91   state1 = is_04.rdstate();
92   is_04.getline(carray1, 65);
93   state2 = is_04.rdstate();  
94   VERIFY( is_04.gcount() == 64 );
95   VERIFY( state1 != state2 );
96   VERIFY( state2 == statefail );
97   VERIFY( !traits_type::compare(
98   "                            and his myth science arkestra presen",
99                                carray1, 65) );
100
101   is_04.clear();
102   state1 = is_04.rdstate();
103   is_04.getline(carray1, 120, '|');
104   state2 = is_04.rdstate();  
105   VERIFY( is_04.gcount() == 106 );
106   VERIFY( state1 != state2 );
107   VERIFY( state2 == stateeof );
108
109   is_04.clear();
110   state1 = is_04.rdstate();
111   is_04.getline(carray1, 100, '|');
112   state2 = is_04.rdstate();  
113   VERIFY( is_04.gcount() == 0 ); 
114   VERIFY( state1 != state2 );
115   VERIFY( static_cast<bool>(state2 & stateeof) );
116   VERIFY( static_cast<bool>(state2 & statefail) );
117 }
118  
119 int 
120 main()
121 {
122   test02();
123   return 0;
124 }