2002-11-21 Phil Edwards <pme@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / testsuite / 27_io / filebuf_virtuals.cc
blob0a92788296a5b0e50e0fe7dcef0015a3f683959b
1 // 2001-05-21 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2001, 2002 Free Software Foundation, Inc.
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.
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.
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.
21 // 27.8.1.4 Overridden virtual functions
23 #include <fstream>
24 #include <testsuite_hooks.h>
26 // @require@ %-*.tst %-*.txt
27 // @diff@ %-*.tst %*.txt
29 void test01()
31 using namespace std;
33 bool test = true;
34 char buf[512];
35 const char* strlit = "how to tell a story and other essays: mark twain";
36 const size_t strlitsize = std::strlen(strlit);
37 filebuf fbuf01;
39 fbuf01.pubsetbuf(buf, 512);
40 fbuf01.sputn(strlit, strlitsize);
41 VERIFY( std::strncmp(strlit, buf, strlitsize) != 0 );
44 void test02()
46 using namespace std;
48 bool test = true;
49 char buf[512];
50 const char* strlit = "how to tell a story and other essays: mark twain";
51 const size_t strlitsize = std::strlen(strlit);
52 filebuf fbuf01;
53 fbuf01.open("tmp", ios_base::out);
55 fbuf01.pubsetbuf(buf, strlitsize);
56 fbuf01.sputn(strlit, strlitsize);
57 VERIFY( std::strncmp(strlit, buf, strlitsize) == 0 );
61 // NB: This test assumes that _M_buf_size == 40, and not the usual
62 // buffer_size length of BUFSIZ (8192), so that overflow/underflow can be
63 // simulated a bit more readily.
64 // NRB (Nota Really Bene): setting it to 40 breaks the test, as intended.
65 const int buffer_size = 8192;
66 //const int buffer_size = 40;
68 const char carray_01[] = "santa cruz or sandiego?";
69 const char carray_02[] = "memphis, new orleans, and savanah";
70 const char name_01[] = "filebuf_virtuals-1.txt"; // file with data in it
71 const char name_02[] = "filebuf_virtuals-2.txt"; // empty file, need to create
72 const char name_03[] = "filebuf_virtuals-3.txt"; // empty file, need to create
75 class derived_filebuf: public std::filebuf
77 public:
78 void
79 set_size(int_type __size) { _M_buf_size_opt = __size; }
82 derived_filebuf fb_01; // in
83 derived_filebuf fb_02; // out
84 derived_filebuf fb_03; // in | out
87 // Initialize filebufs to be the same size regardless of platform.
88 void test03()
90 fb_01.set_size(buffer_size);
91 fb_02.set_size(buffer_size);
92 fb_03.set_size(buffer_size);
96 // Test the filebuf/stringbuf locale settings.
97 void test04()
99 std::locale loc_tmp;
100 loc_tmp = fb_01.getloc();
101 fb_01.pubimbue(loc_tmp); //This should initialize _M_init to true
102 fb_01.getloc(); //This should just return _M_locale
105 // Test overloaded virtual functions.
106 void test05()
108 typedef std::filebuf::int_type int_type;
109 typedef std::filebuf::traits_type traits_type;
110 typedef std::filebuf::pos_type pos_type;
111 typedef std::filebuf::off_type off_type;
112 typedef size_t size_type;
114 bool test = true;
115 std::filebuf f_tmp;
116 std::streamsize strmsz_1, strmsz_2;
117 std::streamoff strmof_1, strmof_2;
118 int i = 0, j = 0, k = 0;
120 // GET
121 // int showmanyc()
122 // returns an estimate of the numbers of chars in the seq, or -1.
123 // if __retval > 0, then calls to underflow won't return
124 // traits_type::eof() till at least __retval chars.
125 // if __retval == -1, then calls to underflow or uflow will fail.
126 // NB overriding def if it can determine more chars can be read from
127 // the input sequence.
129 // int in_avail()
130 // if a read position is available, return _M_in_end - _M_in_cur.
131 // else return showmanyc.
132 strmof_1 = fb_01.in_avail();
133 strmof_2 = fb_02.in_avail();
134 VERIFY( strmof_1 == -1 );
135 VERIFY( strmof_1 == strmof_2 ); //fail because not open
136 strmof_1 = fb_03.in_avail();
137 VERIFY( strmof_1 == strmof_2 );
138 fb_01.open(name_01, std::ios_base::in);
139 fb_02.open(name_02, std::ios_base::out | std::ios_base::trunc);
140 fb_03.open(name_03, std::ios_base::out | std::ios_base::in | std::ios_base::trunc);
141 strmof_1 = fb_01.in_avail();
142 strmof_2 = fb_02.in_avail();
143 VERIFY( strmof_1 != strmof_2 );
144 VERIFY( strmof_1 >= 0 );
145 VERIFY( strmof_2 == -1 ); // empty file
146 strmof_1 = fb_03.in_avail();
147 VERIFY( strmof_1 == 0 ); // empty file
149 // int_type sbumpc()
150 // if read_cur not avail returns uflow(), else return *read_cur & increment
151 int_type c1 = fb_01.sbumpc();
152 int_type c2 = fb_02.sbumpc();
153 VERIFY( c1 != c2 );
154 VERIFY( c1 == '/' );
155 VERIFY( c2 == -1 );
156 int_type c3 = fb_01.sbumpc();
157 int_type c4 = fb_02.sbumpc();
158 VERIFY( c3 != c4 );
159 VERIFY( c1 == c3 ); // fluke, both happen to be '/'
160 VERIFY( c2 == c4 );
161 int_type c5 = fb_03.sbumpc();
162 VERIFY( c5 == traits_type::eof() );
163 // XXX should do some kind of test to make sure that internal
164 // buffers point ot the same thing, to check consistancy.
166 // int_type sgetc()
167 // if read_cur not avail, return uflow(), else return *read_cur
168 int_type c6 = fb_01.sgetc();
169 int_type c7 = fb_02.sgetc();
170 VERIFY( c6 != c3 );
171 VERIFY( c7 == c4 ); // both -1
172 int_type c8 = fb_01.sgetc();
173 int_type c9 = fb_02.sgetc();
174 VERIFY( c6 == c8 );
175 VERIFY( c7 == c9 );
176 c5 = fb_03.sgetc();
177 VERIFY( c5 == traits_type::eof() );
179 // int_type snextc()
180 // calls sbumpc and if sbumpc != eof, return sgetc
181 c6 = fb_01.snextc();
182 c7 = fb_02.snextc();
183 VERIFY( c6 != c8 );
184 VERIFY( c7 == c9 ); // -1
185 VERIFY( c6 == '9' );
186 c6 = fb_01.snextc();
187 c7 = fb_02.snextc();
188 VERIFY( c6 != c8 );
189 VERIFY( c7 == c9 ); // -1
190 VERIFY( c6 == '9' );
191 c5 = fb_03.snextc();
192 VERIFY( c5 == traits_type::eof() );
194 // streamsize sgetn(char_type *s, streamsize n)
195 // streamsize xsgetn(char_type *s, streamsize n)
196 // assign up to n chars to s from input sequence, indexing in_cur as
197 // approp and returning the number of chars assigned
198 strmsz_1 = fb_01.in_avail();
199 strmsz_2 = fb_02.in_avail();
200 test = strmsz_1 != strmsz_2;
201 char carray1[13] = "";
202 strmsz_1 = fb_01.sgetn(carray1, 10);
203 char carray2[buffer_size] = "";
204 strmsz_2 = fb_02.sgetn(carray2, 10);
205 VERIFY( strmsz_1 != strmsz_2 );
206 VERIFY( strmsz_1 == 10 );
207 VERIFY( strmsz_2 == 0 );
208 c1 = fb_01.sgetc();
209 c2 = fb_02.sgetc();
210 VERIFY( c1 == '\n' );
211 VERIFY( c7 == c2 ); // n != i
212 strmsz_1 = fb_03.sgetn(carray1, 10);
213 VERIFY( !strmsz_1 ); //zero
214 strmsz_1 = fb_01.in_avail();
215 strmsz_2 = fb_01.sgetn(carray2, strmsz_1 + 5);
216 VERIFY( strmsz_1 == strmsz_2 - 5 );
217 c4 = fb_01.sgetc(); // buffer should have underflowed from above.
218 VERIFY( c4 == 'i' );
219 strmsz_1 = fb_01.in_avail();
220 VERIFY( strmsz_1 > 0 );
221 strmsz_2 = fb_01.sgetn(carray2, strmsz_1 + 5);
222 VERIFY( strmsz_1 == strmsz_2 ); //at the end of the actual file
223 strmsz_1 = fb_02.in_avail();
224 strmsz_2 = fb_02.sgetn(carray2, strmsz_1 + 5);
225 VERIFY( strmsz_1 == -1 );
226 VERIFY( strmsz_2 == 0 );
227 c4 = fb_02.sgetc(); // should be EOF
228 VERIFY( c4 == traits_type::eof() );
230 // PUT
231 // int_type sputc(char_type c)
232 // if out_cur not avail, return overflow(traits_type::to_int_type(c))
233 // else, stores c at out_cur,
234 // increments out_cur, and returns c as int_type
235 // strmsz_1 = fb_03.in_avail(); // XXX valid for in|out??
236 c1 = fb_02.sputc('a');
237 c2 = fb_03.sputc('b');
238 VERIFY( c1 != c2 );
239 c1 = fb_02.sputc('c');
240 c2 = fb_03.sputc('d');
241 VERIFY( c1 != c2 );
242 // strmsz_2 = fb_03.in_avail();
243 // VERIFY( strmsz_1 != strmsz_2 );
244 for (int i = 50; i <= 90; ++i)
245 c2 = fb_02.sputc(char(i));
246 // 27filebuf-2.txt == ac23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
247 // fb_02._M_out_cur = '2'
248 strmsz_1 = fb_03.in_avail();
249 for (int i = 50; i <= 90; ++i)
250 c2 = fb_03.sputc(char(i));
251 strmsz_2 = fb_03.in_avail();
252 // VERIFY( strmsz_1 != strmsz_2 );
253 // VERIFY( strmsz_1 > 0 );
254 // VERIFY( strmsz_2 > 0 );
255 // 27filebuf-2.txt == bd23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
256 // fb_02._M_out_cur = '2'
257 c3 = fb_01.sputc('a'); // should be EOF because this is read-only
258 VERIFY( c3 == traits_type::eof() );
260 // streamsize sputn(const char_typs* s, streamsize n)
261 // write up to n chars to out_cur from s, returning number assigned
262 // NB *sputn will happily put '\0' into your stream if you give it a chance*
263 strmsz_1 = fb_03.sputn("racadabras", 10);//"abracadabras or what?"
264 VERIFY( strmsz_1 == 10 );
265 strmsz_2 = fb_03.sputn(", i wanna reach out and", 10);
266 VERIFY( strmsz_2 == 10 );
267 VERIFY( strmsz_1 == strmsz_2 );
268 // fb_03._M_out_beg = "YZracadabras, i wanna FGHIJKLMNOPQRSTUVW"
269 // fb_03._M_out_cur = "FGHIJKLMNOPQRSTUVW"
270 strmsz_1 = fb_02.sputn("racadabras", 10);
271 VERIFY( strmsz_1 == 10 );
272 // fb_02._M_out_beg = "YZracadabras<=>?@ABCDEFGHIJKLMNOPQRSTUVW"
273 // fb_02._M_out_cur = "<=>?@ABCDEFGHIJKLMNOPQRSTUVW"
274 strmsz_1 = fb_01.sputn("racadabra", 10);
275 VERIFY( strmsz_1 == 0 );
277 // PUTBACK
278 // int_type pbfail(int_type c)
279 // called when gptr() null, gptr() == eback(), or traits::eq(*gptr, c) false
280 // "pending sequence" is:
281 // 1) everything as defined in underflow
282 // 2) + if (traits::eq_int_type(c, traits::eof()), then input
283 // sequence is backed up one char before the pending sequence is
284 // determined.
285 // 3) + if (not 2) then c is prepended. Left unspecified is
286 // whether the input sequence is backedup or modified in any way
287 // returns traits::eof() for failure, unspecified other value for success
289 // int_type sputbackc(char_type c)
290 // if in_cur not avail || ! traits::eq(c, gptr() [-1]), return pbfail
291 // otherwise decrements in_cur and returns *gptr()
292 c1 = fb_03.sgetc(); // -1
293 c2 = fb_03.sputbackc('z');
294 strmsz_2 = fb_03.in_avail();
295 c3 = fb_03.sgetc();
296 VERIFY( c3 == c2 );
297 VERIFY( c1 != c3 );
298 VERIFY( 1 == strmsz_2 );
299 //test for _in_cur == _in_beg
300 // fb_03._M_out_beg = "bd23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZracada" etc
301 fb_03.pubseekoff(10, std::ios_base::beg,
302 std::ios_base::in | std::ios_base::out);
303 fb_03.sputc('m');
304 strmsz_1 = fb_03.in_avail();
305 c1 = fb_03.sgetc();
306 fb_03.snextc();
307 c2 = fb_03.sputbackc('z');
308 strmsz_2 = fb_03.in_avail();
309 c3 = fb_03.sgetc();
310 VERIFY( c1 != c2 );
311 VERIFY( c3 == c2 );
312 VERIFY( c1 != c3 );
313 VERIFY( c2 == 'z' );
314 // VERIFY( strmsz_1 == strmsz_2 );
315 // test for replacing char with identical one
316 fb_03.snextc();
317 fb_03.sputc('u');
318 fb_03.sputc('v');
319 fb_03.sputc('a');
320 strmsz_1 = fb_03.in_avail();
321 c2 = fb_03.sputbackc('a');
322 strmsz_2 = fb_03.in_avail();
323 c3 = fb_03.sgetc();
324 VERIFY( c3 == c2 );
325 VERIFY( strmsz_1 + 1 == strmsz_2 );
326 //test for ios_base::out
327 c1 = fb_02.sgetc(); // undefined
328 c2 = fb_02.sputbackc('a');
329 VERIFY( c1 == c2 );
330 VERIFY( c1 == -1 );
332 // int_type sungetc()
333 // if in_cur not avail, return pbackfail(), else decrement and
334 // return to_int_type(*gptr())
335 // fb_03._M_out_beg = "uvaacadabras, i wannaZ[\\]^_`abcdefghijkl"
336 // fb_03._M_out_cur = "aacadabras, i wannaZ[\\]^_`abcdefghijkl"
337 strmsz_1 = fb_03.in_avail();
338 c2 = fb_03.sungetc(); // delete the 'a'
339 strmsz_2 = fb_03.in_avail();
340 VERIFY( c2 == 'v' ); // VERIFY( c2 != traits_type::eof() );
341 VERIFY( strmsz_1 + 1 == strmsz_2 );
342 //test for _in_cur == _in_beg
343 for (int i = 50; i < 32 + 29; ++i)
344 fb_02.sputc(char(i));
345 fb_02.pubseekoff(0, std::ios_base::beg, std::ios_base::out);
346 c1 = fb_02.sgetc();
347 strmsz_1 = fb_02.in_avail();
348 c2 = fb_02.sungetc();
349 c3 = fb_02.sgetc();
350 strmsz_2 = fb_02.in_avail();
351 VERIFY( c1 == c2 );
352 VERIFY( c2 == c3 );
353 VERIFY( c1 == traits_type::eof() );
354 VERIFY( strmsz_1 == strmsz_2 );
355 //test for _in_cur == _in_end
356 fb_03.pubseekoff(0, std::ios_base::end);
357 strmsz_1 = fb_03.in_avail(); // -1 cuz at the end
358 c1 = fb_03.sgetc();
359 c2 = fb_03.sungetc();
360 strmsz_2 = fb_03.in_avail(); // 1
361 c3 = fb_03.sgetc();
362 VERIFY( c1 != c2 );
363 // VERIFY( c2 == c3 || c2 == traits_type::not_eof(int(c3)) );
364 VERIFY( strmsz_2 != strmsz_1 );
365 VERIFY( strmsz_2 == 1 );
366 //test for ios_base::out
368 // BUFFER MANAGEMENT & POSITIONING
369 // int sync()
370 // if a put area exists, overflow.
371 // if a get area exists, do something undefined. (like, nothing)
372 strmsz_1 = fb_01.in_avail();
373 fb_01.pubsync();
374 strmsz_2 = fb_01.in_avail();
375 VERIFY( strmsz_2 == strmsz_1 );
376 strmsz_1 = fb_02.in_avail();
377 fb_02.pubsync();
378 // 27filebuf-2.txt == 53 bytes after this.
379 strmsz_2 = fb_02.in_avail();
380 VERIFY( strmsz_2 == -1 );
381 VERIFY( strmsz_2 == strmsz_1 );
382 strmsz_1 = fb_03.in_avail();
383 fb_03.pubsync();
384 // 27filebuf-3.txt
385 // bd23456789mzuva?@ABCDEFGHIJKLMNOPQRSTUVWXYZracadabras, i wannaz
386 // 63 bytes.
387 strmsz_2 = fb_03.in_avail();
388 VERIFY( strmsz_1 == 1 );
389 // VERIFY( strmsz_2 == 1 );
391 // setbuf
392 // pubsetbuf(char_type* s, streamsize n)
393 fb_01.pubsetbuf(0,0);
394 fb_02.pubsetbuf(0,0);
395 fb_03.pubsetbuf(0,0);
396 // Need to test unbuffered output, which means calling this on some
397 // things that have just been opened.
400 // seekoff
401 // pubseekoff(off_type off, ios_base::seekdir way, ios_base::openmode which)
402 // alters the stream position to off
403 pos_type pt_1(off_type(-1));
404 pos_type pt_2(off_type(0));
405 off_type off_1 = 0;
406 off_type off_2 = 0;
407 //IN|OUT
408 // 27filebuf-3.txt = bd23456789:;<=>?...
409 //beg
410 strmsz_1 = fb_03.in_avail();
411 pt_1 = fb_03.pubseekoff(2, std::ios_base::beg);
412 strmsz_2 = fb_03.in_avail();
413 off_1 = pt_1;
414 VERIFY( off_1 > 0 );
415 c1 = fb_03.snextc(); //current in pointer +1
416 VERIFY( c1 == '3' );
417 c2 = fb_03.sputc('\n'); //current in pointer +1
418 c3 = fb_03.sgetc();
419 VERIFY( c2 != c3 );
420 VERIFY( c3 == '4' );
421 fb_03.pubsync();
422 c1 = fb_03.sgetc();
423 VERIFY( c1 == c3 );
424 //cur
425 // 27filebuf-3.txt = bd2\n456789:;<=>?...
426 pt_2 = fb_03.pubseekoff(2, std::ios_base::cur);
427 off_2 = pt_2;
428 VERIFY( (off_2 == (off_1 + 2 + 1 + 1)) );
429 c1 = fb_03.snextc(); //current in pointer +1
430 VERIFY( c1 == '7' );
431 c2 = fb_03.sputc('x'); //test current out pointer
432 c3 = fb_03.sputc('\n');
433 c1 = fb_03.sgetc();
434 fb_03.pubsync();
435 c3 = fb_03.sgetc();
436 VERIFY( c1 == c3 );
437 //end
438 // 27filebuf-3.txt = "bd2\n456x\n9"
439 pt_2 = fb_03.pubseekoff(0, std::ios_base::end,
440 std::ios_base::in|std::ios_base::out);
441 off_1 = pt_2;
442 VERIFY( off_1 > off_2 ); //weak, but don't know exactly where it ends
443 c3 = fb_03.sputc('\n');
444 strmsz_1 = fb_03.sputn("because because because. . .", 28);
445 VERIFY( strmsz_1 == 28 );
446 c1 = fb_03.sungetc();
447 // Defect? retval of sungetc is not necessarily the character ungotten.
448 // So re-get it.
449 c1 = fb_03.sgetc();
450 fb_03.pubsync();
451 c3 = fb_03.sgetc();
452 VERIFY( c1 == c3 );
453 // IN
454 // OUT
457 // seekpos
458 // pubseekpos(pos_type sp, ios_base::openmode)
459 // alters the stream position to sp
460 //IN|OUT
461 //beg
462 pt_1 = fb_03.pubseekoff(78, std::ios_base::beg);
463 off_1 = pt_1;
464 VERIFY( off_1 > 0 );
465 c1 = fb_03.snextc(); //current in pointer +1
466 VERIFY( c1 == ' ' );
467 c2 = fb_03.sputc('\n'); //test current out pointer
468 c3 = fb_03.sgetc();
469 fb_03.pubsync(); //resets pointers
470 pt_2 = fb_03.pubseekpos(pt_1);
471 off_2 = pt_2;
472 VERIFY( off_1 == off_2 );
473 c3 = fb_03.snextc(); //current in pointer +1
474 VERIFY( c2 == c3 );
475 pt_1 = fb_03.pubseekoff(0, std::ios_base::end);
476 off_1 = pt_1;
477 VERIFY( off_1 > off_2 );
478 fb_03.sputn("\nof the wonderful things he does!!\nok", 37);
479 fb_03.pubsync();
481 // IN
482 // OUT
484 // VIRTUALS (indirectly tested)
485 // underflow
486 // if read position avail, returns *gptr()
488 // pbackfail(int_type c)
489 // put c back into input sequence
491 // overflow
492 // appends c to output seq
494 // NB Have to close these suckers. . .
495 // filebuf_type* close()
496 fb_01.close();
497 fb_02.close();
498 fb_03.close();
499 VERIFY( !fb_01.is_open() );
500 VERIFY( !fb_02.is_open() );
501 VERIFY( !fb_03.is_open() );
504 void test06()
506 using namespace std;
507 typedef istream::int_type int_type;
509 bool test = true;
510 ifstream ifs(name_02);
511 char buffer[] = "xxxxxxxxxx";
512 int_type len1 = ifs.rdbuf()->sgetn(buffer, sizeof(buffer));
513 VERIFY( len1 == sizeof(buffer) );
514 VERIFY( buffer[0] == 'a' );
517 main()
519 test01();
520 test02();
522 test03();
523 test04();
524 test05();
525 test06();
527 return 0;