1 // 2000-06-29 bkoz
2
3 // Copyright (C) 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.
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 // NB: ostream has a particular "seeks" category. Adopt this for istreams too.
23 // @require@ %-*.tst %-*.txt
24 // @diff@ %-*.tst %-*.txt
25
26 #include <istream>
27 #include <sstream>
28 #include <fstream>
29 #include <testsuite_hooks.h>
30
test01()31 bool test01()
32 {
33 using namespace std;
34 typedef ios::pos_type pos_type;
35
36 bool test = true;
37 const char str_lit01[] = "istream_seeks-1.tst";
38
39 // in
40 // test default ctors leave things in the same positions...
41 istringstream ist1;
42 pos_type p3 = ist1.tellg();
43
44 ifstream ifs1;
45 pos_type p4 = ifs1.tellg();
46
47 VERIFY( p3 == p4 );
48
49 // in
50 // test ctors leave things in the same positions...
51 istringstream ist2("bob_marley:kaya");
52 p3 = ist2.tellg();
53
54 ifstream ifs2(str_lit01);
55 p4 = ifs2.tellg();
56
57 VERIFY( p3 == p4 );
58 return test;
59 }
60
61 const char* s = " lootpack, peanut butter wolf, rob swift, madlib, quasimoto";
62 const int times = 10;
63
write_rewind(std::iostream & stream)64 void write_rewind(std::iostream& stream)
65 {
66 for (int j = 0; j < times; j++)
67 {
68 bool test = true;
69 std::streampos begin = stream.tellg();
70
71 for (int i = 0; i < times; ++i)
72 stream << j << '-' << i << s << '\n';
73
74 stream.seekg(begin);
75 std::streampos end = stream.tellg();
76 std::streampos badpos = std::streampos(std::streambuf::off_type(-1));
77 }
78 }
79
check_contents(std::iostream & stream)80 void check_contents(std::iostream& stream)
81 {
82 bool test = true;
83
84 stream.clear();
85 stream.seekg(0, std::ios::beg);
86 int i = 0;
87 int loop = times * times + 2;
88 while (i < loop)
89 {
90 stream.ignore(80, '\n');
91 if (stream.good())
92 ++i;
93 else
94 break;
95 }
96 VERIFY( i == times );
97 }
98
99 // fstream
100 // libstdc++/2346
test02()101 void test02()
102 {
103 std::fstream ofstrm;
104 ofstrm.open("istream_seeks-3.txt", std::ios::out);
105 if (!ofstrm)
106 std::abort();
107 write_rewind(ofstrm);
108 ofstrm.close();
109
110 std::fstream ifstrm;
111 ifstrm.open("istream_seeks-3.txt", std::ios::in);
112 check_contents(ifstrm);
113 ifstrm.close();
114 }
115
116 // stringstream
117 // libstdc++/2346
test03()118 void test03()
119 {
120 std::stringstream sstrm;
121
122 write_rewind(sstrm);
123 check_contents(sstrm);
124 }
125
126 // fstreams
test04(void)127 void test04(void)
128 {
129 typedef std::istream::off_type off_type;
130
131 bool test = true;
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() );
142
143 std::istream is01(if01.rdbuf());
144 std::istream is02(if02.rdbuf());
145 std::istream is03(if03.rdbuf());
146
147 // pos_type tellg()
148 // in | out
149 pos01 = is01.tellg();
150 pos02 = is01.tellg();
151 VERIFY( pos01 == pos02 );
152 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
153
154 // in
155 pos03 = is02.tellg();
156 pos04 = is02.tellg();
157 VERIFY( pos03 == pos04 );
158 // VERIFY( istream::pos_type(0) != pos03 ); //deprecated
159
160 // out
161 pos05 = is03.tellg();
162 pos06 = is03.tellg();
163 VERIFY( pos05 == pos06 );
164 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
165
166 // istream& seekg(pos_type)
167 // istream& seekg(off_type, ios_base::seekdir)
168
169 // cur
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 );
182
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 );
191
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 );
200
201 // beg
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 );
210
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 );
219
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 );
228
229 // libstdc++/6414
230 if01.seekg(0, std::ios_base::beg);
231 pos01 = if01.tellg();
232 if01.peek();
233 pos02 = if01.tellg();
234 VERIFY( pos02 == pos01 );
235 }
236
237 // stringstreams
test05(void)238 void test05(void)
239 {
240 typedef std::istream::off_type off_type;
241
242 bool test = true;
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() );
252
253 std::stringbuf strbuf01(std::ios_base::in | std::ios_base::out);
254 if01 >> &strbuf01;
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);
259
260 std::istream is01(&strbuf01);
261 std::istream is02(&strbuf02);
262 std::istream is03(&strbuf03);
263
264 // pos_type tellg()
265 // in | out
266 pos01 = is01.tellg();
267 pos02 = is01.tellg();
268 VERIFY( pos01 == pos02 );
269 // VERIFY( istream::pos_type(0) != pos01 ); // deprecated
270
271 // in
272 pos03 = is02.tellg();
273 pos04 = is02.tellg();
274 VERIFY( pos03 == pos04 );
275 // VERIFY( istream::pos_type(0) != pos03 ); // deprecated
276
277 // out
278 pos05 = is03.tellg();
279 pos06 = is03.tellg();
280 VERIFY( pos05 == pos06 );
281 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
282
283 // istream& seekg(pos_type)
284 // istream& seekg(off_type, ios_base::seekdir)
285
286 // cur
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 );
299
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 );
308
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 );
317
318 // beg
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 );
327
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 );
336
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 );
345 }
346
347 // libstdc++/8348
test06(void)348 void test06(void)
349 {
350 using namespace std;
351 bool test = true;
352 string num1("555");
353
354 // tellg
355 {
356 istringstream iss(num1);
357 istream::pos_type pos1 = iss.tellg();
358 int asNum = 0;
359 iss >> asNum;
360 VERIFY( test = iss.eof() );
361 VERIFY( test = !iss.fail() );
362 iss.tellg();
363 VERIFY( test = !iss.fail() );
364 }
365
366 // seekg
367 {
368 istringstream iss(num1);
369 istream::pos_type pos1 = iss.tellg();
370 int asNum = 0;
371 iss >> asNum;
372 VERIFY( test = iss.eof() );
373 VERIFY( test = !iss.fail() );
374 iss.seekg(0, ios_base::beg);
375 VERIFY( test = !iss.fail() );
376 }
377
378 // seekg
379 {
380 istringstream iss(num1);
381 istream::pos_type pos1 = iss.tellg();
382 int asNum = 0;
383 iss >> asNum;
384 VERIFY( test = iss.eof() );
385 VERIFY( test = !iss.fail() );
386 iss.seekg(pos1);
387 VERIFY( test = !iss.fail() );
388 }
389 }
390
main()391 int main()
392 {
393 test01();
394
395 test02();
396 test03();
397
398 test04();
399 test05();
400
401 test06();
402 return 0;
403 }
404
405
406