]>
Commit | Line | Data |
---|---|---|
fbd5f73b BK |
1 | // 2000-06-29 bkoz |
2 | ||
8081da07 | 3 | // Copyright (C) 2000, 2001, 2002 Free Software Foundation |
fbd5f73b BK |
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. | |
599d54fd LR |
23 | // @require@ %-*.tst %-*.txt |
24 | // @diff@ %-*.tst %-*.txt | |
fbd5f73b BK |
25 | |
26 | #include <istream> | |
27 | #include <sstream> | |
28 | #include <fstream> | |
fe413112 | 29 | #include <testsuite_hooks.h> |
fbd5f73b | 30 | |
fbd5f73b BK |
31 | bool test01() |
32 | { | |
33 | using namespace std; | |
34 | typedef ios::pos_type pos_type; | |
35 | ||
36 | bool test = true; | |
d32c94be | 37 | const char str_lit01[] = "istream_seeks-1.tst"; |
fbd5f73b BK |
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 | ||
aa1b2f7d | 47 | VERIFY( p3 == p4 ); |
fbd5f73b BK |
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 | ||
aa1b2f7d | 57 | VERIFY( p3 == p4 ); |
fbd5f73b BK |
58 | |
59 | #ifdef DEBUG_ASSERT | |
60 | assert(test); | |
61 | #endif | |
62 | ||
63 | return test; | |
64 | } | |
65 | ||
3b0fd4bc BK |
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) | |
8081da07 | 111 | std::abort(); |
3b0fd4bc BK |
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 | ||
fbd5f73b BK |
131 | // fstreams |
132 | void test04(void) | |
133 | { | |
d32c94be BK |
134 | typedef std::istream::off_type off_type; |
135 | ||
fbd5f73b BK |
136 | bool test = true; |
137 | std::istream::pos_type pos01, pos02, pos03, pos04, pos05, pos06; | |
138 | std::ios_base::iostate state01, state02; | |
d32c94be BK |
139 | const char str_lit01[] = "istream_seeks-1.txt"; |
140 | const char str_lit02[] = "istream_seeks-2.txt"; | |
fbd5f73b BK |
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); | |
aa1b2f7d BV |
144 | VERIFY( if01.good() ); |
145 | VERIFY( if02.good() ); | |
146 | VERIFY( if03.good() ); | |
fbd5f73b BK |
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(); | |
aa1b2f7d | 156 | VERIFY( pos01 == pos02 ); |
189244a4 | 157 | // VERIFY( istream::pos_type(0) != pos01 ); //deprecated |
fbd5f73b BK |
158 | |
159 | // in | |
160 | pos03 = is02.tellg(); | |
161 | pos04 = is02.tellg(); | |
aa1b2f7d | 162 | VERIFY( pos03 == pos04 ); |
189244a4 | 163 | // VERIFY( istream::pos_type(0) != pos03 ); //deprecated |
fbd5f73b BK |
164 | |
165 | // out | |
166 | pos05 = is03.tellg(); | |
167 | pos06 = is03.tellg(); | |
aa1b2f7d | 168 | VERIFY( pos05 == pos06 ); |
189244a4 | 169 | // VERIFY( istream::pos_type(0) != pos01 ); //deprecated |
fbd5f73b BK |
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(); | |
d32c94be | 183 | VERIFY( pos01 == pos02 + off_type(10) ); |
aa1b2f7d | 184 | VERIFY( state01 == state02 ); |
fbd5f73b | 185 | pos02 = is01.tellg(); |
aa1b2f7d | 186 | VERIFY( pos02 == pos01 ); |
fbd5f73b BK |
187 | |
188 | state01 = is02.rdstate(); | |
189 | is02.seekg(10, std::ios_base::cur); | |
190 | state02 = is02.rdstate(); | |
191 | pos03 = is02.tellg(); | |
d32c94be | 192 | VERIFY( pos03 == pos04 + off_type(10) ); |
aa1b2f7d | 193 | VERIFY( state01 == state02 ); |
fbd5f73b | 194 | pos04 = is02.tellg(); |
aa1b2f7d | 195 | VERIFY( pos03 == pos04 ); |
fbd5f73b BK |
196 | |
197 | state01 = is03.rdstate(); | |
198 | is03.seekg(10, std::ios_base::cur); | |
199 | state02 = is03.rdstate(); | |
200 | pos05 = is03.tellg(); | |
d32c94be | 201 | VERIFY( pos05 == pos06 + off_type(10) ); |
aa1b2f7d | 202 | VERIFY( state01 == state02 ); |
fbd5f73b | 203 | pos06 = is03.tellg(); |
aa1b2f7d | 204 | VERIFY( pos05 == pos06 ); |
fbd5f73b BK |
205 | |
206 | // beg | |
207 | state01 = is01.rdstate(); | |
208 | is01.seekg(20, std::ios_base::beg); | |
209 | state02 = is01.rdstate(); | |
210 | pos01 = is01.tellg(); | |
d32c94be | 211 | VERIFY( pos01 == pos02 + off_type(10) ); |
aa1b2f7d | 212 | VERIFY( state01 == state02 ); |
fbd5f73b | 213 | pos02 = is01.tellg(); |
aa1b2f7d | 214 | VERIFY( pos02 == pos01 ); |
fbd5f73b BK |
215 | |
216 | state01 = is02.rdstate(); | |
217 | is02.seekg(20, std::ios_base::beg); | |
218 | state02 = is02.rdstate(); | |
219 | pos03 = is02.tellg(); | |
d32c94be | 220 | VERIFY( pos03 == pos04 + off_type(10) ); |
aa1b2f7d | 221 | VERIFY( state01 == state02 ); |
fbd5f73b | 222 | pos04 = is02.tellg(); |
aa1b2f7d | 223 | VERIFY( pos03 == pos04 ); |
fbd5f73b BK |
224 | |
225 | state01 = is03.rdstate(); | |
226 | is03.seekg(20, std::ios_base::beg); | |
227 | state02 = is03.rdstate(); | |
228 | pos05 = is03.tellg(); | |
d32c94be | 229 | VERIFY( pos05 == pos06 + off_type(10) ); |
aa1b2f7d | 230 | VERIFY( state01 == state02 ); |
fbd5f73b | 231 | pos06 = is03.tellg(); |
74c263af JM |
232 | VERIFY( pos05 == pos06 ); |
233 | ||
234 | // libstdc++/6414 | |
235 | if01.seekg(0, std::ios_base::beg); | |
236 | pos01 = if01.tellg(); | |
237 | if01.peek(); | |
238 | pos02 = if01.tellg(); | |
239 | VERIFY( pos02 == pos01 ); | |
fbd5f73b BK |
240 | |
241 | #ifdef DEBUG_ASSERT | |
242 | assert(test); | |
243 | #endif | |
244 | } | |
245 | ||
246 | // stringstreams | |
247 | void test05(void) | |
248 | { | |
d32c94be BK |
249 | typedef std::istream::off_type off_type; |
250 | ||
fbd5f73b BK |
251 | bool test = true; |
252 | std::istream::pos_type pos01, pos02, pos03, pos04, pos05, pos06; | |
253 | std::ios_base::iostate state01, state02; | |
d32c94be | 254 | const char str_lit01[] = "istream_seeks-1.tst"; |
fbd5f73b BK |
255 | std::ifstream if01(str_lit01); |
256 | std::ifstream if02(str_lit01); | |
257 | std::ifstream if03(str_lit01); | |
aa1b2f7d BV |
258 | VERIFY( if01.good() ); |
259 | VERIFY( if02.good() ); | |
260 | VERIFY( if03.good() ); | |
fbd5f73b BK |
261 | |
262 | std::stringbuf strbuf01(std::ios_base::in | std::ios_base::out); | |
263 | if01 >> &strbuf01; | |
264 | // initialize stringbufs that are ios_base::out | |
265 | std::stringbuf strbuf03(strbuf01.str(), std::ios_base::out); | |
266 | // initialize stringbufs that are ios_base::in | |
267 | std::stringbuf strbuf02(strbuf01.str(), std::ios_base::in); | |
268 | ||
269 | std::istream is01(&strbuf01); | |
270 | std::istream is02(&strbuf02); | |
271 | std::istream is03(&strbuf03); | |
272 | ||
273 | // pos_type tellg() | |
274 | // in | out | |
275 | pos01 = is01.tellg(); | |
276 | pos02 = is01.tellg(); | |
aa1b2f7d BV |
277 | VERIFY( pos01 == pos02 ); |
278 | // VERIFY( istream::pos_type(0) != pos01 ); // deprecated | |
fbd5f73b BK |
279 | |
280 | // in | |
281 | pos03 = is02.tellg(); | |
282 | pos04 = is02.tellg(); | |
aa1b2f7d BV |
283 | VERIFY( pos03 == pos04 ); |
284 | // VERIFY( istream::pos_type(0) != pos03 ); // deprecated | |
fbd5f73b BK |
285 | |
286 | // out | |
287 | pos05 = is03.tellg(); | |
288 | pos06 = is03.tellg(); | |
aa1b2f7d BV |
289 | VERIFY( pos05 == pos06 ); |
290 | // VERIFY( istream::pos_type(0) != pos01 ); //deprecated | |
fbd5f73b BK |
291 | |
292 | // istream& seekg(pos_type) | |
293 | // istream& seekg(off_type, ios_base::seekdir) | |
294 | ||
295 | // cur | |
296 | // NB: see library issues list 136. It's the v-3 interp that seekg | |
297 | // only sets the input buffer, or else istreams with buffers that | |
298 | // have _M_mode == ios_base::out will fail to have consistency | |
299 | // between seekg and tellg. | |
300 | state01 = is01.rdstate(); | |
301 | is01.seekg(10, std::ios_base::cur); | |
302 | state02 = is01.rdstate(); | |
303 | pos01 = is01.tellg(); | |
d32c94be | 304 | VERIFY( pos01 == pos02 + off_type(10) ); |
aa1b2f7d | 305 | VERIFY( state01 == state02 ); |
fbd5f73b | 306 | pos02 = is01.tellg(); |
aa1b2f7d | 307 | VERIFY( pos02 == pos01 ); |
fbd5f73b BK |
308 | |
309 | state01 = is02.rdstate(); | |
310 | is02.seekg(10, std::ios_base::cur); | |
311 | state02 = is02.rdstate(); | |
312 | pos03 = is02.tellg(); | |
d32c94be | 313 | VERIFY( pos03 == pos04 + off_type(10) ); |
aa1b2f7d | 314 | VERIFY( state01 == state02 ); |
fbd5f73b | 315 | pos04 = is02.tellg(); |
aa1b2f7d | 316 | VERIFY( pos03 == pos04 ); |
fbd5f73b BK |
317 | |
318 | state01 = is03.rdstate(); | |
319 | is03.seekg(10, std::ios_base::cur); | |
320 | state02 = is03.rdstate(); | |
321 | pos05 = is03.tellg(); | |
aa1b2f7d | 322 | VERIFY( pos05 == pos06 ); // as only out buffer |
d52a9847 | 323 | VERIFY( state01 != state02 ); |
fbd5f73b | 324 | pos06 = is03.tellg(); |
aa1b2f7d | 325 | VERIFY( pos05 == pos06 ); |
fbd5f73b BK |
326 | |
327 | // beg | |
328 | state01 = is01.rdstate(); | |
329 | is01.seekg(20, std::ios_base::beg); | |
330 | state02 = is01.rdstate(); | |
331 | pos01 = is01.tellg(); | |
d32c94be | 332 | VERIFY( pos01 == pos02 + off_type(10) ); |
aa1b2f7d | 333 | VERIFY( state01 == state02 ); |
fbd5f73b | 334 | pos02 = is01.tellg(); |
aa1b2f7d | 335 | VERIFY( pos02 == pos01 ); |
fbd5f73b BK |
336 | |
337 | state01 = is02.rdstate(); | |
338 | is02.seekg(20, std::ios_base::beg); | |
339 | state02 = is02.rdstate(); | |
340 | pos03 = is02.tellg(); | |
d32c94be | 341 | VERIFY( pos03 == pos04 + off_type(10) ); |
aa1b2f7d | 342 | VERIFY( state01 == state02 ); |
fbd5f73b | 343 | pos04 = is02.tellg(); |
aa1b2f7d | 344 | VERIFY( pos03 == pos04 ); |
fbd5f73b BK |
345 | |
346 | state01 = is03.rdstate(); | |
347 | is03.seekg(20, std::ios_base::beg); | |
348 | state02 = is03.rdstate(); | |
349 | pos05 = is03.tellg(); | |
aa1b2f7d BV |
350 | VERIFY( pos05 == pos06 ); // as only out buffer |
351 | VERIFY( state01 == state02 ); | |
fbd5f73b | 352 | pos06 = is03.tellg(); |
aa1b2f7d | 353 | VERIFY( pos05 == pos06 ); |
fbd5f73b BK |
354 | |
355 | #ifdef DEBUG_ASSERT | |
356 | assert(test); | |
357 | #endif | |
358 | } | |
359 | ||
360 | int main() | |
361 | { | |
362 | test01(); | |
3b0fd4bc BK |
363 | |
364 | test02(); | |
365 | test03(); | |
366 | ||
fbd5f73b BK |
367 | test04(); |
368 | test05(); | |
369 | return 0; | |
370 | } | |
3b0fd4bc BK |
371 | |
372 |