3 // Copyright (C) 2000, 2001, 2002 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 // NB: ostream has a particular "seeks" category. Adopt this for istreams too.
23 // @require@ %-*.tst %-*.txt
24 // @diff@ %-*.tst %-*.txt
29 #include <testsuite_hooks.h>
34 typedef ios::pos_type pos_type
;
37 const char str_lit01
[] = "istream_seeks-1.tst";
40 // test default ctors leave things in the same positions...
42 pos_type p3
= ist1
.tellg();
45 pos_type p4
= ifs1
.tellg();
50 // test ctors leave things in the same positions...
51 istringstream
ist2("bob_marley:kaya");
54 ifstream
ifs2(str_lit01
);
61 const char* s
= " lootpack, peanut butter wolf, rob swift, madlib, quasimoto";
64 void write_rewind(std::iostream
& stream
)
66 for (int j
= 0; j
< times
; j
++)
69 std::streampos begin
= stream
.tellg();
71 for (int i
= 0; i
< times
; ++i
)
72 stream
<< j
<< '-' << i
<< s
<< '\n';
75 std::streampos end
= stream
.tellg();
76 std::streampos badpos
= std::streampos(std::streambuf::off_type(-1));
80 void check_contents(std::iostream
& stream
)
85 stream
.seekg(0, std::ios::beg
);
87 int loop
= times
* times
+ 2;
90 stream
.ignore(80, '\n');
104 ofstrm
.open("istream_seeks-3.txt", std::ios::out
);
107 write_rewind(ofstrm
);
111 ifstrm
.open("istream_seeks-3.txt", std::ios::in
);
112 check_contents(ifstrm
);
120 std::stringstream sstrm
;
123 check_contents(sstrm
);
129 typedef std::istream::off_type off_type
;
132 std::istream::pos_type pos01
, pos02
, pos03
, pos04
, pos05
, pos06
;
133 std::ios_base::iostate state01
, state02
;
134 const char str_lit01
[] = "istream_seeks-1.txt";
135 const char str_lit02
[] = "istream_seeks-2.txt";
136 std::ifstream
if01(str_lit01
, std::ios_base::in
| std::ios_base::out
);
137 std::ifstream
if02(str_lit01
, std::ios_base::in
);
138 std::ifstream
if03(str_lit02
, std::ios_base::out
| std::ios_base::trunc
);
139 VERIFY( if01
.good() );
140 VERIFY( if02
.good() );
141 VERIFY( if03
.good() );
143 std::istream
is01(if01
.rdbuf());
144 std::istream
is02(if02
.rdbuf());
145 std::istream
is03(if03
.rdbuf());
149 pos01
= is01
.tellg();
150 pos02
= is01
.tellg();
151 VERIFY( pos01
== pos02
);
152 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
155 pos03
= is02
.tellg();
156 pos04
= is02
.tellg();
157 VERIFY( pos03
== pos04
);
158 // VERIFY( istream::pos_type(0) != pos03 ); //deprecated
161 pos05
= is03
.tellg();
162 pos06
= is03
.tellg();
163 VERIFY( pos05
== pos06
);
164 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
166 // istream& seekg(pos_type)
167 // istream& seekg(off_type, ios_base::seekdir)
170 // NB: see library issues list 136. It's the v-3 interp that seekg
171 // only sets the input buffer, or else istreams with buffers that
172 // have _M_mode == ios_base::out will fail to have consistency
173 // between seekg and tellg.
174 state01
= is01
.rdstate();
175 is01
.seekg(10, std::ios_base::cur
);
176 state02
= is01
.rdstate();
177 pos01
= is01
.tellg();
178 VERIFY( pos01
== pos02
+ off_type(10) );
179 VERIFY( state01
== state02
);
180 pos02
= is01
.tellg();
181 VERIFY( pos02
== pos01
);
183 state01
= is02
.rdstate();
184 is02
.seekg(10, std::ios_base::cur
);
185 state02
= is02
.rdstate();
186 pos03
= is02
.tellg();
187 VERIFY( pos03
== pos04
+ off_type(10) );
188 VERIFY( state01
== state02
);
189 pos04
= is02
.tellg();
190 VERIFY( pos03
== pos04
);
192 state01
= is03
.rdstate();
193 is03
.seekg(10, std::ios_base::cur
);
194 state02
= is03
.rdstate();
195 pos05
= is03
.tellg();
196 VERIFY( pos05
== pos06
+ off_type(10) );
197 VERIFY( state01
== state02
);
198 pos06
= is03
.tellg();
199 VERIFY( pos05
== pos06
);
202 state01
= is01
.rdstate();
203 is01
.seekg(20, std::ios_base::beg
);
204 state02
= is01
.rdstate();
205 pos01
= is01
.tellg();
206 VERIFY( pos01
== pos02
+ off_type(10) );
207 VERIFY( state01
== state02
);
208 pos02
= is01
.tellg();
209 VERIFY( pos02
== pos01
);
211 state01
= is02
.rdstate();
212 is02
.seekg(20, std::ios_base::beg
);
213 state02
= is02
.rdstate();
214 pos03
= is02
.tellg();
215 VERIFY( pos03
== pos04
+ off_type(10) );
216 VERIFY( state01
== state02
);
217 pos04
= is02
.tellg();
218 VERIFY( pos03
== pos04
);
220 state01
= is03
.rdstate();
221 is03
.seekg(20, std::ios_base::beg
);
222 state02
= is03
.rdstate();
223 pos05
= is03
.tellg();
224 VERIFY( pos05
== pos06
+ off_type(10) );
225 VERIFY( state01
== state02
);
226 pos06
= is03
.tellg();
227 VERIFY( pos05
== pos06
);
230 if01
.seekg(0, std::ios_base::beg
);
231 pos01
= if01
.tellg();
233 pos02
= if01
.tellg();
234 VERIFY( pos02
== pos01
);
240 typedef std::istream::off_type off_type
;
243 std::istream::pos_type pos01
, pos02
, pos03
, pos04
, pos05
, pos06
;
244 std::ios_base::iostate state01
, state02
;
245 const char str_lit01
[] = "istream_seeks-1.tst";
246 std::ifstream
if01(str_lit01
);
247 std::ifstream
if02(str_lit01
);
248 std::ifstream
if03(str_lit01
);
249 VERIFY( if01
.good() );
250 VERIFY( if02
.good() );
251 VERIFY( if03
.good() );
253 std::stringbuf
strbuf01(std::ios_base::in
| std::ios_base::out
);
255 // initialize stringbufs that are ios_base::out
256 std::stringbuf
strbuf03(strbuf01
.str(), std::ios_base::out
);
257 // initialize stringbufs that are ios_base::in
258 std::stringbuf
strbuf02(strbuf01
.str(), std::ios_base::in
);
260 std::istream
is01(&strbuf01
);
261 std::istream
is02(&strbuf02
);
262 std::istream
is03(&strbuf03
);
266 pos01
= is01
.tellg();
267 pos02
= is01
.tellg();
268 VERIFY( pos01
== pos02
);
269 // VERIFY( istream::pos_type(0) != pos01 ); // deprecated
272 pos03
= is02
.tellg();
273 pos04
= is02
.tellg();
274 VERIFY( pos03
== pos04
);
275 // VERIFY( istream::pos_type(0) != pos03 ); // deprecated
278 pos05
= is03
.tellg();
279 pos06
= is03
.tellg();
280 VERIFY( pos05
== pos06
);
281 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
283 // istream& seekg(pos_type)
284 // istream& seekg(off_type, ios_base::seekdir)
287 // NB: see library issues list 136. It's the v-3 interp that seekg
288 // only sets the input buffer, or else istreams with buffers that
289 // have _M_mode == ios_base::out will fail to have consistency
290 // between seekg and tellg.
291 state01
= is01
.rdstate();
292 is01
.seekg(10, std::ios_base::cur
);
293 state02
= is01
.rdstate();
294 pos01
= is01
.tellg();
295 VERIFY( pos01
== pos02
+ off_type(10) );
296 VERIFY( state01
== state02
);
297 pos02
= is01
.tellg();
298 VERIFY( pos02
== pos01
);
300 state01
= is02
.rdstate();
301 is02
.seekg(10, std::ios_base::cur
);
302 state02
= is02
.rdstate();
303 pos03
= is02
.tellg();
304 VERIFY( pos03
== pos04
+ off_type(10) );
305 VERIFY( state01
== state02
);
306 pos04
= is02
.tellg();
307 VERIFY( pos03
== pos04
);
309 state01
= is03
.rdstate();
310 is03
.seekg(10, std::ios_base::cur
);
311 state02
= is03
.rdstate();
312 pos05
= is03
.tellg();
313 VERIFY( pos05
== pos06
); // as only out buffer
314 VERIFY( state01
!= state02
);
315 pos06
= is03
.tellg();
316 VERIFY( pos05
== pos06
);
319 state01
= is01
.rdstate();
320 is01
.seekg(20, std::ios_base::beg
);
321 state02
= is01
.rdstate();
322 pos01
= is01
.tellg();
323 VERIFY( pos01
== pos02
+ off_type(10) );
324 VERIFY( state01
== state02
);
325 pos02
= is01
.tellg();
326 VERIFY( pos02
== pos01
);
328 state01
= is02
.rdstate();
329 is02
.seekg(20, std::ios_base::beg
);
330 state02
= is02
.rdstate();
331 pos03
= is02
.tellg();
332 VERIFY( pos03
== pos04
+ off_type(10) );
333 VERIFY( state01
== state02
);
334 pos04
= is02
.tellg();
335 VERIFY( pos03
== pos04
);
337 state01
= is03
.rdstate();
338 is03
.seekg(20, std::ios_base::beg
);
339 state02
= is03
.rdstate();
340 pos05
= is03
.tellg();
341 VERIFY( pos05
== pos06
); // as only out buffer
342 VERIFY( state01
== state02
);
343 pos06
= is03
.tellg();
344 VERIFY( pos05
== pos06
);
356 istringstream
iss(num1
);
357 istream::pos_type pos1
= iss
.tellg();
360 VERIFY( test
= iss
.eof() );
361 VERIFY( test
= !iss
.fail() );
363 VERIFY( test
= !iss
.fail() );
368 istringstream
iss(num1
);
369 istream::pos_type pos1
= iss
.tellg();
372 VERIFY( test
= iss
.eof() );
373 VERIFY( test
= !iss
.fail() );
374 iss
.seekg(0, ios_base::beg
);
375 VERIFY( test
= !iss
.fail() );
380 istringstream
iss(num1
);
381 istream::pos_type pos1
= iss
.tellg();
384 VERIFY( test
= iss
.eof() );
385 VERIFY( test
= !iss
.fail() );
387 VERIFY( test
= !iss
.fail() );