acinclude.m4 (GLIBCPP_CONFIGURE_TESTSUITE): New macro, calls...
[gcc.git] / libstdc++-v3 / testsuite / 27_io / ostream_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.2.4 basic_ostream seek members
22
23 #include <ostream>
24 #include <sstream>
25 #include <fstream>
26 #include <testsuite_hooks.h>
27
28
29 bool test01()
30 {
31 using namespace std;
32 typedef ios::pos_type pos_type;
33
34 bool test = true;
35 const char str_lit01[] = "ostream_seeks-1.txt";
36
37 // out
38 // test default ctors leave things in the same positions...
39 ostringstream ost1;
40 pos_type p1 = ost1.tellp();
41
42 ofstream ofs1;
43 pos_type p2 = ofs1.tellp();
44
45 VERIFY( p1 == p2 );
46
47 // out
48 // test ctors leave things in the same positions...
49 ostringstream ost2("bob_marley:kaya");
50 p1 = ost2.tellp();
51
52 ofstream ofs2(str_lit01);
53 p2 = ofs2.tellp();
54
55 VERIFY( p1 == p2 );
56
57 #ifdef DEBUG_ASSERT
58 assert(test);
59 #endif
60
61 return test;
62 }
63
64 #if 0
65 // XXX FIX ME
66 // basically this is the istreams_seeks.cc code. We need to fix it up
67 // for ostreams......
68
69 // fstreams
70 void test04(void)
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_unformatted-1.txt";
76 const char str_lit02[] = "istream_unformatted-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 tellp()
89 // in | out
90 pos01 = is01.tellp();
91 pos02 = is01.tellp();
92 VERIFY( pos01 == pos02 );
93 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
94
95 // in
96 pos03 = is02.tellp();
97 pos04 = is02.tellp();
98 VERIFY( pos03 == pos04 );
99 // VERIFY( istream::pos_type(0) != pos03 ); //deprecated
100
101 // out
102 pos05 = is03.tellp();
103 pos06 = is03.tellp();
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 tellp.
115 state01 = is01.rdstate();
116 is01.seekg(10, std::ios_base::cur);
117 state02 = is01.rdstate();
118 pos01 = is01.tellp();
119 VERIFY( pos01 == pos02 + 10 );
120 VERIFY( state01 == state02 );
121 pos02 = is01.tellp();
122 VERIFY( pos02 == pos01 );
123
124 state01 = is02.rdstate();
125 is02.seekg(10, std::ios_base::cur);
126 state02 = is02.rdstate();
127 pos03 = is02.tellp();
128 VERIFY( pos03 == pos04 + 10 );
129 VERIFY( state01 == state02 );
130 pos04 = is02.tellp();
131 VERIFY( pos03 == pos04 );
132
133 state01 = is03.rdstate();
134 is03.seekg(10, std::ios_base::cur);
135 state02 = is03.rdstate();
136 pos05 = is03.tellp();
137 VERIFY( pos05 == pos06 + 10 );
138 VERIFY( state01 == state02 );
139 pos06 = is03.tellp();
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.tellp();
147 VERIFY( pos01 == pos02 + 10 );
148 VERIFY( state01 == state02 );
149 pos02 = is01.tellp();
150 VERIFY( pos02 == pos01 );
151
152 state01 = is02.rdstate();
153 is02.seekg(20, std::ios_base::beg);
154 state02 = is02.rdstate();
155 pos03 = is02.tellp();
156 VERIFY( pos03 == pos04 + 10 );
157 VERIFY( state01 == state02 );
158 pos04 = is02.tellp();
159 VERIFY( pos03 == pos04 );
160
161 state01 = is03.rdstate();
162 is03.seekg(20, std::ios_base::beg);
163 state02 = is03.rdstate();
164 pos05 = is03.tellp();
165 VERIFY( pos05 == pos06 + 10 );
166 VERIFY( state01 == state02 );
167 pos06 = is03.tellp();
168 VERIFY( pos05 == pos06 );
169
170 #ifdef DEBUG_ASSERT
171 assert(test);
172 #endif
173 }
174
175 // stringstreams
176 void test05(void)
177 {
178 bool test = true;
179 std::istream::pos_type pos01, pos02, pos03, pos04, pos05, pos06;
180 std::ios_base::iostate state01, state02;
181 const char str_lit01[] = "istream_unformatted-1.tst";
182 std::ifstream if01(str_lit01);
183 std::ifstream if02(str_lit01);
184 std::ifstream if03(str_lit01);
185 VERIFY( if01.good() );
186 VERIFY( if02.good() );
187 VERIFY( if03.good() );
188
189 std::stringbuf strbuf01(std::ios_base::in | std::ios_base::out);
190 if01 >> &strbuf01;
191 // initialize stringbufs that are ios_base::out
192 std::stringbuf strbuf03(strbuf01.str(), std::ios_base::out);
193 // initialize stringbufs that are ios_base::in
194 std::stringbuf strbuf02(strbuf01.str(), std::ios_base::in);
195
196 std::istream is01(&strbuf01);
197 std::istream is02(&strbuf02);
198 std::istream is03(&strbuf03);
199
200 // pos_type tellp()
201 // in | out
202 pos01 = is01.tellp();
203 pos02 = is01.tellp();
204 VERIFY( pos01 == pos02 );
205 // VERIFY( istream::pos_type(0) != pos01 ); // deprecated
206
207 // in
208 pos03 = is02.tellp();
209 pos04 = is02.tellp();
210 VERIFY( pos03 == pos04 );
211 // VERIFY( istream::pos_type(0) != pos03 ); // deprecated
212
213 // out
214 pos05 = is03.tellp();
215 pos06 = is03.tellp();
216 VERIFY( pos05 == pos06 );
217 // VERIFY( istream::pos_type(0) != pos01 ); //deprecated
218
219 // istream& seekg(pos_type)
220 // istream& seekg(off_type, ios_base::seekdir)
221
222 // cur
223 // NB: see library issues list 136. It's the v-3 interp that seekg
224 // only sets the input buffer, or else istreams with buffers that
225 // have _M_mode == ios_base::out will fail to have consistency
226 // between seekg and tellp.
227 state01 = is01.rdstate();
228 is01.seekg(10, std::ios_base::cur);
229 state02 = is01.rdstate();
230 pos01 = is01.tellp();
231 VERIFY( pos01 == pos02 + 10 );
232 VERIFY( state01 == state02 );
233 pos02 = is01.tellp();
234 VERIFY( pos02 == pos01 );
235
236 state01 = is02.rdstate();
237 is02.seekg(10, std::ios_base::cur);
238 state02 = is02.rdstate();
239 pos03 = is02.tellp();
240 VERIFY( pos03 == pos04 + 10 );
241 VERIFY( state01 == state02 );
242 pos04 = is02.tellp();
243 VERIFY( pos03 == pos04 );
244
245 state01 = is03.rdstate();
246 is03.seekg(10, std::ios_base::cur);
247 state02 = is03.rdstate();
248 pos05 = is03.tellp();
249 VERIFY( pos05 == pos06 ); // as only out buffer
250 VERIFY( state01 == state02 );
251 pos06 = is03.tellp();
252 VERIFY( pos05 == pos06 );
253
254 // beg
255 state01 = is01.rdstate();
256 is01.seekg(20, std::ios_base::beg);
257 state02 = is01.rdstate();
258 pos01 = is01.tellp();
259 VERIFY( pos01 == pos02 + 10 );
260 VERIFY( state01 == state02 );
261 pos02 = is01.tellp();
262 VERIFY( pos02 == pos01 );
263
264 state01 = is02.rdstate();
265 is02.seekg(20, std::ios_base::beg);
266 state02 = is02.rdstate();
267 pos03 = is02.tellp();
268 VERIFY( pos03 == pos04 + 10 );
269 VERIFY( state01 == state02 );
270 pos04 = is02.tellp();
271 VERIFY( pos03 == pos04 );
272
273 state01 = is03.rdstate();
274 is03.seekg(20, std::ios_base::beg);
275 state02 = is03.rdstate();
276 pos05 = is03.tellp();
277 VERIFY( pos05 == pos06 ); // as only out buffer
278 VERIFY( state01 == state02 );
279 pos06 = is03.tellp();
280 VERIFY( pos05 == pos06 );
281
282 #ifdef DEBUG_ASSERT
283 assert(test);
284 #endif
285 }
286 #endif // XXX
287
288 int main()
289 {
290 test01();
291 // test04();
292 // test05();
293 return 0;
294 }