a8f138eb7b41a8c5520014ec269958d07d5256be
[gcc.git] / libstdc++-v3 / testsuite / 27_io / istream_seeks.cc
1 // 2000-06-29 bkoz
2
3 // Copyright (C) 2000, 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.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 <debug_assert.h>
30
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
59 #ifdef DEBUG_ASSERT
60 assert(test);
61 #endif
62
63 return test;
64 }
65
66 const char* s = " lootpack, peanut butter wolf, rob swift, madlib, quasimoto";
67 const int times = 10;
68
69 void write_rewind(std::iostream& stream)
70 {
71 for (int j = 0; j < times; j++)
72 {
73 bool test = true;
74 std::streampos begin = stream.tellg();
75
76 for (int i = 0; i < times; ++i)
77 stream << j << '-' << i << s << '\n';
78
79 stream.seekg(begin);
80 std::streampos end = stream.tellg();
81 std::streampos badpos = std::streampos(std::streambuf::off_type(-1));
82 }
83 }
84
85 void check_contents(std::iostream& stream)
86 {
87 bool test = true;
88
89 stream.clear();
90 stream.seekg(0, std::ios::beg);
91 int i = 0;
92 int loop = times * times + 2;
93 while (i < loop)
94 {
95 stream.ignore(80, '\n');
96 if (stream.good())
97 ++i;
98 else
99 break;
100 }
101 VERIFY( i == times );
102 }
103
104 // fstream
105 // libstdc++/2346
106 void test02()
107 {
108 std::fstream ofstrm;
109 ofstrm.open("istream_seeks-3.txt", std::ios::out);
110 if (!ofstrm)
111 abort();
112 write_rewind(ofstrm);
113 ofstrm.close();
114
115 std::fstream ifstrm;
116 ifstrm.open("istream_seeks-3.txt", std::ios::in);
117 check_contents(ifstrm);
118 ifstrm.close();
119 }
120
121 // stringstream
122 // libstdc++/2346
123 void test03()
124 {
125 std::stringstream sstrm;
126
127 write_rewind(sstrm);
128 check_contents(sstrm);
129 }
130
131 // fstreams
132 void test04(void)
133 {
134 typedef std::istream::off_type off_type;
135
136 bool test = true;
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() );
147
148 std::istream is01(if01.rdbuf());
149 std::istream is02(if02.rdbuf());
150 std::istream is03(if03.rdbuf());
151
152 // pos_type tellg()
153 // in | out
154 pos01 = is01.tellg();
155 pos02 = is01.tellg();
156 VERIFY( pos01 == pos02 );
157 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
158
159 // in
160 pos03 = is02.tellg();
161 pos04 = is02.tellg();
162 VERIFY( pos03 == pos04 );
163 // VERIFY( istream::pos_type(0) != pos03 ); //deprecated
164
165 // out
166 pos05 = is03.tellg();
167 pos06 = is03.tellg();
168 VERIFY( pos05 == pos06 );
169 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
170
171 // istream& seekg(pos_type)
172 // istream& seekg(off_type, ios_base::seekdir)
173
174 // cur
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 );
187
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 );
196
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 );
205
206 // beg
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 );
215
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 );
224
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 );
233
234 #ifdef DEBUG_ASSERT
235 assert(test);
236 #endif
237 }
238
239 // stringstreams
240 void test05(void)
241 {
242 typedef std::istream::off_type off_type;
243
244 bool test = true;
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() );
254
255 std::stringbuf strbuf01(std::ios_base::in | std::ios_base::out);
256 if01 >> &strbuf01;
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);
261
262 std::istream is01(&strbuf01);
263 std::istream is02(&strbuf02);
264 std::istream is03(&strbuf03);
265
266 // pos_type tellg()
267 // in | out
268 pos01 = is01.tellg();
269 pos02 = is01.tellg();
270 VERIFY( pos01 == pos02 );
271 // VERIFY( istream::pos_type(0) != pos01 ); // deprecated
272
273 // in
274 pos03 = is02.tellg();
275 pos04 = is02.tellg();
276 VERIFY( pos03 == pos04 );
277 // VERIFY( istream::pos_type(0) != pos03 ); // deprecated
278
279 // out
280 pos05 = is03.tellg();
281 pos06 = is03.tellg();
282 VERIFY( pos05 == pos06 );
283 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
284
285 // istream& seekg(pos_type)
286 // istream& seekg(off_type, ios_base::seekdir)
287
288 // cur
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 );
301
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 );
310
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 );
319
320 // beg
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 );
329
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 );
338
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 );
347
348 #ifdef DEBUG_ASSERT
349 assert(test);
350 #endif
351 }
352
353 int main()
354 {
355 test01();
356
357 test02();
358 test03();
359
360 test04();
361 test05();
362 return 0;
363 }
364
365