]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/istream_extractor_other.cc
*.cc: Remove spaces, make sure testcases return zero.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 27_io / istream_extractor_other.cc
1 // 1999-07-28 bkoz
2
3 // Copyright (C) 1999, 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.2.3 basic_istream::operator>>
22 // @require@ %-*.tst %-*.txt
23 // @diff@ %-*.tst %-*.txt
24
25 #include <istream>
26 #include <sstream>
27 #include <fstream>
28 #include <debug_assert.h>
29
30
31 // stringbufs.
32 bool test01() {
33
34 typedef std::ios::traits_type ctraits_type;
35
36 bool test = true;
37 const std::string str_01;
38 const std::string str_02("art taylor kickin it on DAKAR");
39 std::string strtmp;
40
41 std::stringbuf isbuf_00(std::ios_base::in);
42 std::stringbuf isbuf_01(std::ios_base::in | std::ios_base::out);
43 std::stringbuf isbuf_02(str_01, std::ios_base::in);
44 std::stringbuf isbuf_03(str_01, std::ios_base::in | std::ios_base::out);
45 std::stringbuf isbuf_04(str_02, std::ios_base::in);
46 std::stringbuf isbuf_05(str_02, std::ios_base::in | std::ios_base::out);
47
48 std::istream is_00(NULL);
49 std::istream is_01(&isbuf_01);
50 std::istream is_02(&isbuf_02);
51 std::istream is_03(&isbuf_03);
52 std::istream is_04(&isbuf_04);
53 std::istream is_05(&isbuf_05);
54 std::ios_base::iostate state1, state2, statefail, stateeof;
55 statefail = std::ios_base::failbit;
56 stateeof = std::ios_base::eofbit;
57
58
59 // template<_CharT, _Traits>
60 // basic_istream& operator>>(basic_streambuf*)
61
62 // null istream to empty in_buf
63 state1 = is_00.rdstate();
64 is_00 >> &isbuf_00;
65 state2 = is_00.rdstate();
66 VERIFY( state1 != state2 );
67 VERIFY( static_cast<bool>(state2 & statefail) );
68 VERIFY( isbuf_00.str() == str_01 );
69
70 // null istream to empty in_out_buf
71 is_00.clear(std::ios_base::goodbit);
72 state1 = is_00.rdstate();
73 is_00 >> &isbuf_01;
74 state2 = is_00.rdstate();
75 VERIFY( state1 != state2 );
76 VERIFY( static_cast<bool>(state2 & statefail) );
77 VERIFY( isbuf_01.str() == str_01 );
78
79 // null istream to full in_buf
80 is_00.clear(std::ios_base::goodbit);
81 state1 = is_00.rdstate();
82 is_00 >> &isbuf_04;
83 state2 = is_00.rdstate();
84 VERIFY( state1 != state2 );
85 VERIFY( static_cast<bool>(state2 & statefail) );
86 VERIFY( isbuf_04.str() == str_02 );
87
88 // null istream to full in_out_buf
89 is_00.clear(std::ios_base::goodbit);
90 state1 = is_00.rdstate();
91 is_00 >> &isbuf_05;
92 state2 = is_00.rdstate();
93 VERIFY( state1 != state2 );
94 VERIFY( static_cast<bool>(state2 & statefail) );
95 VERIFY( isbuf_05.str() == str_02 );
96
97 // empty but non-null istream to full in_buf
98 state1 = is_02.rdstate();
99 is_02 >> &isbuf_04;
100 state2 = is_02.rdstate();
101 VERIFY( state1 != state2 );
102 VERIFY( static_cast<bool>(state2 & statefail) );
103 VERIFY( isbuf_04.str() == str_02 ); // as only an "in" buffer
104 VERIFY( isbuf_04.sgetc() == 'a' );
105
106 // empty but non-null istream to full in_out_buf
107 is_02.clear(std::ios_base::goodbit);
108 state1 = is_02.rdstate();
109 is_02 >> &isbuf_05;
110 state2 = is_02.rdstate();
111 VERIFY( state1 != state2 );
112 VERIFY( static_cast<bool>(state2 & statefail) );
113 VERIFY( isbuf_05.str() == str_02 ); // as only an "in" buffer
114 VERIFY( isbuf_05.sgetc() == 'a' );
115
116 // full istream to empty in_buf (need out_buf, you know?)
117 state1 = is_04.rdstate();
118 is_04 >> &isbuf_02;
119 state2 = is_04.rdstate();
120 VERIFY( state1 != state2 );
121 VERIFY( static_cast<bool>(state2 & statefail) );
122 VERIFY( isbuf_02.str() == str_01 ); // as only an "in" buffer
123 VERIFY( isbuf_02.sgetc() == ctraits_type::eof() );
124 VERIFY( is_04.peek() == ctraits_type::eof() ); // as failed
125
126 // full istream to empty in_out_buf
127 is_04.clear(std::ios_base::goodbit);
128 state1 = is_04.rdstate();
129 is_04 >> &isbuf_03;
130 state2 = is_04.rdstate();
131 VERIFY( state1 != state2 );
132 VERIFY( !static_cast<bool>(state2 & statefail) );
133 VERIFY( state2 == stateeof );
134 strtmp = isbuf_03.str();
135 VERIFY( strtmp == str_02 ); // as only an "in" buffer
136 VERIFY( isbuf_03.sgetc() == 'a' );
137 VERIFY( is_04.peek() == ctraits_type::eof() );
138
139 #ifdef DEBUG_ASSERT
140 assert(test);
141 #endif
142
143 return test;
144 }
145
146 // filebufs.
147 bool test02() {
148
149 bool test = true;
150 typedef std::ios::traits_type ctraits_type;
151 const char name_01[] = "istream_extractor_other-1.txt"; //read
152 const char name_02[] = "istream_extractor_other-2.txt"; //write
153
154 std::filebuf fbin, fbout;
155 fbin.open(name_01, std::ios_base::in);
156 fbout.open(name_02, std::ios_base::out | std::ios_base::trunc);
157 VERIFY( fbin.is_open() );
158 VERIFY( fbout.is_open() );
159
160 if (test)
161 {
162 std::istream is(&fbin);
163 is.unsetf(std::ios_base::skipws);
164 is >> &fbout;
165 }
166
167 fbout.close();
168 fbin.close();
169 VERIFY( !fbin.is_open() );
170 VERIFY( !fbout.is_open() );
171
172 #ifdef DEBUG_ASSERT
173 assert(test);
174 #endif
175
176 return test;
177 }
178
179 void test03()
180 {
181 using namespace std;
182 bool test = true;
183
184 // template<_CharT, _Traits>
185 // basic_istream& operator>>(ios_base& (*pf) (ios_base&))
186 {
187 int i = 0;
188 std::istringstream iss(" 43");
189 iss >> std::noskipws >> i;
190 std::ios::iostate i3 = iss.rdstate();
191 VERIFY ( !iss ); //should set failbit
192 }
193
194 // template<_CharT, _Traits>
195 // basic_istream& operator>>(basic_ios& (*pf) (basic_ios&))
196
197 // template<_CharT, _Traits>
198 // basic_istream& operator>>(basic_istream& (*pf) (basic_istream&))
199 }
200
201
202 int main()
203 {
204 test01();
205 test02();
206 test03();
207 return 0;
208 }