]>
git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/27_io/istream_seeks.cc
3 // Copyright (C) 2000, 2001 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
);
66 const char* s
= " lootpack, peanut butter wolf, rob swift, madlib, quasimoto";
69 void write_rewind(std::iostream
& stream
)
71 for (int j
= 0; j
< times
; j
++)
74 std::streampos begin
= stream
.tellg();
76 for (int i
= 0; i
< times
; ++i
)
77 stream
<< j
<< '-' << i
<< s
<< '\n';
80 std::streampos end
= stream
.tellg();
81 std::streampos badpos
= std::streampos(std::streambuf::off_type(-1));
85 void check_contents(std::iostream
& stream
)
90 stream
.seekg(0, std::ios::beg
);
92 int loop
= times
* times
+ 2;
95 stream
.ignore(80, '\n');
101 VERIFY( i
== times
);
109 ofstrm
.open("istream_seeks-3.txt", std::ios::out
);
112 write_rewind(ofstrm
);
116 ifstrm
.open("istream_seeks-3.txt", std::ios::in
);
117 check_contents(ifstrm
);
125 std::stringstream sstrm
;
128 check_contents(sstrm
);
134 typedef std::istream::off_type off_type
;
137 std::istream::pos_type pos01
, pos02
, pos03
, pos04
, pos05
, pos06
;
138 std::ios_base::iostate state01
, state02
;
139 const char str_lit01
[] = "istream_seeks-1.txt";
140 const char str_lit02
[] = "istream_seeks-2.txt";
141 std::ifstream
if01(str_lit01
, std::ios_base::in
| std::ios_base::out
);
142 std::ifstream
if02(str_lit01
, std::ios_base::in
);
143 std::ifstream
if03(str_lit02
, std::ios_base::out
| std::ios_base::trunc
);
144 VERIFY( if01
.good() );
145 VERIFY( if02
.good() );
146 VERIFY( if03
.good() );
148 std::istream
is01(if01
.rdbuf());
149 std::istream
is02(if02
.rdbuf());
150 std::istream
is03(if03
.rdbuf());
154 pos01
= is01
.tellg();
155 pos02
= is01
.tellg();
156 VERIFY( pos01
== pos02
);
157 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
160 pos03
= is02
.tellg();
161 pos04
= is02
.tellg();
162 VERIFY( pos03
== pos04
);
163 // VERIFY( istream::pos_type(0) != pos03 ); //deprecated
166 pos05
= is03
.tellg();
167 pos06
= is03
.tellg();
168 VERIFY( pos05
== pos06
);
169 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
171 // istream& seekg(pos_type)
172 // istream& seekg(off_type, ios_base::seekdir)
175 // NB: see library issues list 136. It's the v-3 interp that seekg
176 // only sets the input buffer, or else istreams with buffers that
177 // have _M_mode == ios_base::out will fail to have consistency
178 // between seekg and tellg.
179 state01
= is01
.rdstate();
180 is01
.seekg(10, std::ios_base::cur
);
181 state02
= is01
.rdstate();
182 pos01
= is01
.tellg();
183 VERIFY( pos01
== pos02
+ off_type(10) );
184 VERIFY( state01
== state02
);
185 pos02
= is01
.tellg();
186 VERIFY( pos02
== pos01
);
188 state01
= is02
.rdstate();
189 is02
.seekg(10, std::ios_base::cur
);
190 state02
= is02
.rdstate();
191 pos03
= is02
.tellg();
192 VERIFY( pos03
== pos04
+ off_type(10) );
193 VERIFY( state01
== state02
);
194 pos04
= is02
.tellg();
195 VERIFY( pos03
== pos04
);
197 state01
= is03
.rdstate();
198 is03
.seekg(10, std::ios_base::cur
);
199 state02
= is03
.rdstate();
200 pos05
= is03
.tellg();
201 VERIFY( pos05
== pos06
+ off_type(10) );
202 VERIFY( state01
== state02
);
203 pos06
= is03
.tellg();
204 VERIFY( pos05
== pos06
);
207 state01
= is01
.rdstate();
208 is01
.seekg(20, std::ios_base::beg
);
209 state02
= is01
.rdstate();
210 pos01
= is01
.tellg();
211 VERIFY( pos01
== pos02
+ off_type(10) );
212 VERIFY( state01
== state02
);
213 pos02
= is01
.tellg();
214 VERIFY( pos02
== pos01
);
216 state01
= is02
.rdstate();
217 is02
.seekg(20, std::ios_base::beg
);
218 state02
= is02
.rdstate();
219 pos03
= is02
.tellg();
220 VERIFY( pos03
== pos04
+ off_type(10) );
221 VERIFY( state01
== state02
);
222 pos04
= is02
.tellg();
223 VERIFY( pos03
== pos04
);
225 state01
= is03
.rdstate();
226 is03
.seekg(20, std::ios_base::beg
);
227 state02
= is03
.rdstate();
228 pos05
= is03
.tellg();
229 VERIFY( pos05
== pos06
+ off_type(10) );
230 VERIFY( state01
== state02
);
231 pos06
= is03
.tellg();
232 VERIFY( pos05
== pos06
);
242 typedef std::istream::off_type off_type
;
245 std::istream::pos_type pos01
, pos02
, pos03
, pos04
, pos05
, pos06
;
246 std::ios_base::iostate state01
, state02
;
247 const char str_lit01
[] = "istream_seeks-1.tst";
248 std::ifstream
if01(str_lit01
);
249 std::ifstream
if02(str_lit01
);
250 std::ifstream
if03(str_lit01
);
251 VERIFY( if01
.good() );
252 VERIFY( if02
.good() );
253 VERIFY( if03
.good() );
255 std::stringbuf
strbuf01(std::ios_base::in
| std::ios_base::out
);
257 // initialize stringbufs that are ios_base::out
258 std::stringbuf
strbuf03(strbuf01
.str(), std::ios_base::out
);
259 // initialize stringbufs that are ios_base::in
260 std::stringbuf
strbuf02(strbuf01
.str(), std::ios_base::in
);
262 std::istream
is01(&strbuf01
);
263 std::istream
is02(&strbuf02
);
264 std::istream
is03(&strbuf03
);
268 pos01
= is01
.tellg();
269 pos02
= is01
.tellg();
270 VERIFY( pos01
== pos02
);
271 // VERIFY( istream::pos_type(0) != pos01 ); // deprecated
274 pos03
= is02
.tellg();
275 pos04
= is02
.tellg();
276 VERIFY( pos03
== pos04
);
277 // VERIFY( istream::pos_type(0) != pos03 ); // deprecated
280 pos05
= is03
.tellg();
281 pos06
= is03
.tellg();
282 VERIFY( pos05
== pos06
);
283 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
285 // istream& seekg(pos_type)
286 // istream& seekg(off_type, ios_base::seekdir)
289 // NB: see library issues list 136. It's the v-3 interp that seekg
290 // only sets the input buffer, or else istreams with buffers that
291 // have _M_mode == ios_base::out will fail to have consistency
292 // between seekg and tellg.
293 state01
= is01
.rdstate();
294 is01
.seekg(10, std::ios_base::cur
);
295 state02
= is01
.rdstate();
296 pos01
= is01
.tellg();
297 VERIFY( pos01
== pos02
+ off_type(10) );
298 VERIFY( state01
== state02
);
299 pos02
= is01
.tellg();
300 VERIFY( pos02
== pos01
);
302 state01
= is02
.rdstate();
303 is02
.seekg(10, std::ios_base::cur
);
304 state02
= is02
.rdstate();
305 pos03
= is02
.tellg();
306 VERIFY( pos03
== pos04
+ off_type(10) );
307 VERIFY( state01
== state02
);
308 pos04
= is02
.tellg();
309 VERIFY( pos03
== pos04
);
311 state01
= is03
.rdstate();
312 is03
.seekg(10, std::ios_base::cur
);
313 state02
= is03
.rdstate();
314 pos05
= is03
.tellg();
315 VERIFY( pos05
== pos06
); // as only out buffer
316 VERIFY( state01
!= state02
);
317 pos06
= is03
.tellg();
318 VERIFY( pos05
== pos06
);
321 state01
= is01
.rdstate();
322 is01
.seekg(20, std::ios_base::beg
);
323 state02
= is01
.rdstate();
324 pos01
= is01
.tellg();
325 VERIFY( pos01
== pos02
+ off_type(10) );
326 VERIFY( state01
== state02
);
327 pos02
= is01
.tellg();
328 VERIFY( pos02
== pos01
);
330 state01
= is02
.rdstate();
331 is02
.seekg(20, std::ios_base::beg
);
332 state02
= is02
.rdstate();
333 pos03
= is02
.tellg();
334 VERIFY( pos03
== pos04
+ off_type(10) );
335 VERIFY( state01
== state02
);
336 pos04
= is02
.tellg();
337 VERIFY( pos03
== pos04
);
339 state01
= is03
.rdstate();
340 is03
.seekg(20, std::ios_base::beg
);
341 state02
= is03
.rdstate();
342 pos05
= is03
.tellg();
343 VERIFY( pos05
== pos06
); // as only out buffer
344 VERIFY( state01
== state02
);
345 pos06
= is03
.tellg();
346 VERIFY( pos05
== pos06
);