]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/testsuite/27_io/istream_seeks.cc
97ef01b121cb9a36d48c9d58b9b0c8b42ed8e77b
[gcc.git] / libstdc++-v3 / testsuite / 27_io / istream_seeks.cc
1 // 2000-06-29 bkoz
2
3 // Copyright (C) 2000 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
32 bool test01()
33 {
34 using namespace std;
35 typedef ios::pos_type pos_type;
36
37 bool test = true;
38 const char str_lit01[] = "istream_seeks-1.tst";
39
40 // in
41 // test default ctors leave things in the same positions...
42 istringstream ist1;
43 pos_type p3 = ist1.tellg();
44
45 ifstream ifs1;
46 pos_type p4 = ifs1.tellg();
47
48 VERIFY( p3 == p4 );
49
50 // in
51 // test ctors leave things in the same positions...
52 istringstream ist2("bob_marley:kaya");
53 p3 = ist2.tellg();
54
55 ifstream ifs2(str_lit01);
56 p4 = ifs2.tellg();
57
58 VERIFY( p3 == p4 );
59
60 #ifdef DEBUG_ASSERT
61 assert(test);
62 #endif
63
64 return test;
65 }
66
67 // fstreams
68 void test04(void)
69 {
70 typedef std::istream::off_type off_type;
71
72 bool test = true;
73 std::istream::pos_type pos01, pos02, pos03, pos04, pos05, pos06;
74 std::ios_base::iostate state01, state02;
75 const char str_lit01[] = "istream_seeks-1.txt";
76 const char str_lit02[] = "istream_seeks-2.txt";
77 std::ifstream if01(str_lit01, std::ios_base::in | std::ios_base::out);
78 std::ifstream if02(str_lit01, std::ios_base::in);
79 std::ifstream if03(str_lit02, std::ios_base::out | std::ios_base::trunc);
80 VERIFY( if01.good() );
81 VERIFY( if02.good() );
82 VERIFY( if03.good() );
83
84 std::istream is01(if01.rdbuf());
85 std::istream is02(if02.rdbuf());
86 std::istream is03(if03.rdbuf());
87
88 // pos_type tellg()
89 // in | out
90 pos01 = is01.tellg();
91 pos02 = is01.tellg();
92 VERIFY( pos01 == pos02 );
93 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
94
95 // in
96 pos03 = is02.tellg();
97 pos04 = is02.tellg();
98 VERIFY( pos03 == pos04 );
99 // VERIFY( istream::pos_type(0) != pos03 ); //deprecated
100
101 // out
102 pos05 = is03.tellg();
103 pos06 = is03.tellg();
104 VERIFY( pos05 == pos06 );
105 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
106
107 // istream& seekg(pos_type)
108 // istream& seekg(off_type, ios_base::seekdir)
109
110 // cur
111 // NB: see library issues list 136. It's the v-3 interp that seekg
112 // only sets the input buffer, or else istreams with buffers that
113 // have _M_mode == ios_base::out will fail to have consistency
114 // between seekg and tellg.
115 state01 = is01.rdstate();
116 is01.seekg(10, std::ios_base::cur);
117 state02 = is01.rdstate();
118 pos01 = is01.tellg();
119 VERIFY( pos01 == pos02 + off_type(10) );
120 VERIFY( state01 == state02 );
121 pos02 = is01.tellg();
122 VERIFY( pos02 == pos01 );
123
124 state01 = is02.rdstate();
125 is02.seekg(10, std::ios_base::cur);
126 state02 = is02.rdstate();
127 pos03 = is02.tellg();
128 VERIFY( pos03 == pos04 + off_type(10) );
129 VERIFY( state01 == state02 );
130 pos04 = is02.tellg();
131 VERIFY( pos03 == pos04 );
132
133 state01 = is03.rdstate();
134 is03.seekg(10, std::ios_base::cur);
135 state02 = is03.rdstate();
136 pos05 = is03.tellg();
137 VERIFY( pos05 == pos06 + off_type(10) );
138 VERIFY( state01 == state02 );
139 pos06 = is03.tellg();
140 VERIFY( pos05 == pos06 );
141
142 // beg
143 state01 = is01.rdstate();
144 is01.seekg(20, std::ios_base::beg);
145 state02 = is01.rdstate();
146 pos01 = is01.tellg();
147 VERIFY( pos01 == pos02 + off_type(10) );
148 VERIFY( state01 == state02 );
149 pos02 = is01.tellg();
150 VERIFY( pos02 == pos01 );
151
152 state01 = is02.rdstate();
153 is02.seekg(20, std::ios_base::beg);
154 state02 = is02.rdstate();
155 pos03 = is02.tellg();
156 VERIFY( pos03 == pos04 + off_type(10) );
157 VERIFY( state01 == state02 );
158 pos04 = is02.tellg();
159 VERIFY( pos03 == pos04 );
160
161 state01 = is03.rdstate();
162 is03.seekg(20, std::ios_base::beg);
163 state02 = is03.rdstate();
164 pos05 = is03.tellg();
165 VERIFY( pos05 == pos06 + off_type(10) );
166 VERIFY( state01 == state02 );
167 pos06 = is03.tellg();
168 VERIFY( pos05 == pos06 );
169
170 #ifdef DEBUG_ASSERT
171 assert(test);
172 #endif
173 }
174
175 // stringstreams
176 void test05(void)
177 {
178 typedef std::istream::off_type off_type;
179
180 bool test = true;
181 std::istream::pos_type pos01, pos02, pos03, pos04, pos05, pos06;
182 std::ios_base::iostate state01, state02;
183 const char str_lit01[] = "istream_seeks-1.tst";
184 std::ifstream if01(str_lit01);
185 std::ifstream if02(str_lit01);
186 std::ifstream if03(str_lit01);
187 VERIFY( if01.good() );
188 VERIFY( if02.good() );
189 VERIFY( if03.good() );
190
191 std::stringbuf strbuf01(std::ios_base::in | std::ios_base::out);
192 if01 >> &strbuf01;
193 // initialize stringbufs that are ios_base::out
194 std::stringbuf strbuf03(strbuf01.str(), std::ios_base::out);
195 // initialize stringbufs that are ios_base::in
196 std::stringbuf strbuf02(strbuf01.str(), std::ios_base::in);
197
198 std::istream is01(&strbuf01);
199 std::istream is02(&strbuf02);
200 std::istream is03(&strbuf03);
201
202 // pos_type tellg()
203 // in | out
204 pos01 = is01.tellg();
205 pos02 = is01.tellg();
206 VERIFY( pos01 == pos02 );
207 // VERIFY( istream::pos_type(0) != pos01 ); // deprecated
208
209 // in
210 pos03 = is02.tellg();
211 pos04 = is02.tellg();
212 VERIFY( pos03 == pos04 );
213 // VERIFY( istream::pos_type(0) != pos03 ); // deprecated
214
215 // out
216 pos05 = is03.tellg();
217 pos06 = is03.tellg();
218 VERIFY( pos05 == pos06 );
219 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
220
221 // istream& seekg(pos_type)
222 // istream& seekg(off_type, ios_base::seekdir)
223
224 // cur
225 // NB: see library issues list 136. It's the v-3 interp that seekg
226 // only sets the input buffer, or else istreams with buffers that
227 // have _M_mode == ios_base::out will fail to have consistency
228 // between seekg and tellg.
229 state01 = is01.rdstate();
230 is01.seekg(10, std::ios_base::cur);
231 state02 = is01.rdstate();
232 pos01 = is01.tellg();
233 VERIFY( pos01 == pos02 + off_type(10) );
234 VERIFY( state01 == state02 );
235 pos02 = is01.tellg();
236 VERIFY( pos02 == pos01 );
237
238 state01 = is02.rdstate();
239 is02.seekg(10, std::ios_base::cur);
240 state02 = is02.rdstate();
241 pos03 = is02.tellg();
242 VERIFY( pos03 == pos04 + off_type(10) );
243 VERIFY( state01 == state02 );
244 pos04 = is02.tellg();
245 VERIFY( pos03 == pos04 );
246
247 state01 = is03.rdstate();
248 is03.seekg(10, std::ios_base::cur);
249 state02 = is03.rdstate();
250 pos05 = is03.tellg();
251 VERIFY( pos05 == pos06 ); // as only out buffer
252 VERIFY( state01 != state02 );
253 pos06 = is03.tellg();
254 VERIFY( pos05 == pos06 );
255
256 // beg
257 state01 = is01.rdstate();
258 is01.seekg(20, std::ios_base::beg);
259 state02 = is01.rdstate();
260 pos01 = is01.tellg();
261 VERIFY( pos01 == pos02 + off_type(10) );
262 VERIFY( state01 == state02 );
263 pos02 = is01.tellg();
264 VERIFY( pos02 == pos01 );
265
266 state01 = is02.rdstate();
267 is02.seekg(20, std::ios_base::beg);
268 state02 = is02.rdstate();
269 pos03 = is02.tellg();
270 VERIFY( pos03 == pos04 + off_type(10) );
271 VERIFY( state01 == state02 );
272 pos04 = is02.tellg();
273 VERIFY( pos03 == pos04 );
274
275 state01 = is03.rdstate();
276 is03.seekg(20, std::ios_base::beg);
277 state02 = is03.rdstate();
278 pos05 = is03.tellg();
279 VERIFY( pos05 == pos06 ); // as only out buffer
280 VERIFY( state01 == state02 );
281 pos06 = is03.tellg();
282 VERIFY( pos05 == pos06 );
283
284 #ifdef DEBUG_ASSERT
285 assert(test);
286 #endif
287 }
288
289 int main()
290 {
291 test01();
292 test04();
293 test05();
294 return 0;
295 }
This page took 0.049088 seconds and 4 git commands to generate.