]>
gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/testsuite/27_io/filebuf_virtuals.cc
3090f315912d88a2067bfd2b7c666331ca8aab26
1 // 2001-05-21 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
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)
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,
21 // 27.8.1.4 Overridden virtual functions
25 #include <testsuite_hooks.h>
27 // @require@ %-*.tst %-*.txt
28 // @diff@ %-*.tst %*.txt
36 const char* strlit
= "how to tell a story and other essays: mark twain";
37 const size_t strlitsize
= std::strlen(strlit
);
40 fbuf01
.pubsetbuf(buf
, 512);
41 fbuf01
.sputn(strlit
, strlitsize
);
42 VERIFY( std::strncmp(strlit
, buf
, strlitsize
) != 0 );
51 const char* strlit
= "how to tell a story and other essays: mark twain";
52 const size_t strlitsize
= std::strlen(strlit
);
54 fbuf01
.open("tmp", ios_base::out
);
56 fbuf01
.pubsetbuf(buf
, strlitsize
);
57 fbuf01
.sputn(strlit
, strlitsize
);
58 VERIFY( std::strncmp(strlit
, buf
, strlitsize
) == 0 );
62 // NB: This test assumes that _M_buf_size == 40, and not the usual
63 // buffer_size length of BUFSIZ (8192), so that overflow/underflow can be
64 // simulated a bit more readily.
65 // NRB (Nota Really Bene): setting it to 40 breaks the test, as intended.
66 const int buffer_size
= 8192;
67 //const int buffer_size = 40;
69 const char carray_01
[] = "santa cruz or sandiego?";
70 const char carray_02
[] = "memphis, new orleans, and savanah";
71 const char name_01
[] = "filebuf_virtuals-1.txt"; // file with data in it
72 const char name_02
[] = "filebuf_virtuals-2.txt"; // empty file, need to create
73 const char name_03
[] = "filebuf_virtuals-3.txt"; // empty file, need to create
76 class derived_filebuf
: public std::filebuf
80 set_size(int_type __size
) { _M_buf_size_opt
= __size
; }
83 derived_filebuf fb_01
; // in
84 derived_filebuf fb_02
; // out
85 derived_filebuf fb_03
; // in | out
88 // Initialize filebufs to be the same size regardless of platform.
91 fb_01
.set_size(buffer_size
);
92 fb_02
.set_size(buffer_size
);
93 fb_03
.set_size(buffer_size
);
97 // Test the filebuf/stringbuf locale settings.
101 loc_tmp
= fb_01
.getloc();
102 fb_01
.pubimbue(loc_tmp
); //This should initialize _M_init to true
103 fb_01
.getloc(); //This should just return _M_locale
106 // Test overloaded virtual functions.
109 typedef std::filebuf::int_type int_type
;
110 typedef std::filebuf::traits_type traits_type
;
111 typedef std::filebuf::pos_type pos_type
;
112 typedef std::filebuf::off_type off_type
;
113 typedef size_t size_type
;
117 std::streamsize strmsz_1
, strmsz_2
;
118 std::streamoff strmof_1
, strmof_2
;
119 int i
= 0, j
= 0, k
= 0;
123 // returns an estimate of the numbers of chars in the seq, or -1.
124 // if __retval > 0, then calls to underflow won't return
125 // traits_type::eof() till at least __retval chars.
126 // if __retval == -1, then calls to underflow or uflow will fail.
127 // NB overriding def if it can determine more chars can be read from
128 // the input sequence.
131 // if a read position is available, return _M_in_end - _M_in_cur.
132 // else return showmanyc.
133 strmof_1
= fb_01
.in_avail();
134 strmof_2
= fb_02
.in_avail();
135 VERIFY( strmof_1
== -1 );
136 VERIFY( strmof_1
== strmof_2
); //fail because not open
137 strmof_1
= fb_03
.in_avail();
138 VERIFY( strmof_1
== strmof_2
);
139 fb_01
.open(name_01
, std::ios_base::in
);
140 fb_02
.open(name_02
, std::ios_base::out
| std::ios_base::trunc
);
141 fb_03
.open(name_03
, std::ios_base::out
| std::ios_base::in
| std::ios_base::trunc
);
142 strmof_1
= fb_01
.in_avail();
143 strmof_2
= fb_02
.in_avail();
144 VERIFY( strmof_1
!= strmof_2
);
145 VERIFY( strmof_1
>= 0 );
146 VERIFY( strmof_2
== -1 ); // empty file
147 strmof_1
= fb_03
.in_avail();
148 VERIFY( strmof_1
== 0 ); // empty file
151 // if read_cur not avail returns uflow(), else return *read_cur & increment
152 int_type c1
= fb_01
.sbumpc();
153 int_type c2
= fb_02
.sbumpc();
157 int_type c3
= fb_01
.sbumpc();
158 int_type c4
= fb_02
.sbumpc();
160 VERIFY( c1
== c3
); // fluke, both happen to be '/'
162 int_type c5
= fb_03
.sbumpc();
163 VERIFY( c5
== traits_type::eof() );
164 // XXX should do some kind of test to make sure that internal
165 // buffers point ot the same thing, to check consistancy.
168 // if read_cur not avail, return uflow(), else return *read_cur
169 int_type c6
= fb_01
.sgetc();
170 int_type c7
= fb_02
.sgetc();
172 VERIFY( c7
== c4
); // both -1
173 int_type c8
= fb_01
.sgetc();
174 int_type c9
= fb_02
.sgetc();
178 VERIFY( c5
== traits_type::eof() );
181 // calls sbumpc and if sbumpc != eof, return sgetc
185 VERIFY( c7
== c9
); // -1
190 VERIFY( c7
== c9
); // -1
193 VERIFY( c5
== traits_type::eof() );
195 // streamsize sgetn(char_type *s, streamsize n)
196 // streamsize xsgetn(char_type *s, streamsize n)
197 // assign up to n chars to s from input sequence, indexing in_cur as
198 // approp and returning the number of chars assigned
199 strmsz_1
= fb_01
.in_avail();
200 strmsz_2
= fb_02
.in_avail();
201 test
= strmsz_1
!= strmsz_2
;
202 char carray1
[13] = "";
203 strmsz_1
= fb_01
.sgetn(carray1
, 10);
204 char carray2
[buffer_size
] = "";
205 strmsz_2
= fb_02
.sgetn(carray2
, 10);
206 VERIFY( strmsz_1
!= strmsz_2
);
207 VERIFY( strmsz_1
== 10 );
208 VERIFY( strmsz_2
== 0 );
211 VERIFY( c1
== '\n' );
212 VERIFY( c7
== c2
); // n != i
213 strmsz_1
= fb_03
.sgetn(carray1
, 10);
214 VERIFY( !strmsz_1
); //zero
215 strmsz_1
= fb_01
.in_avail();
216 strmsz_2
= fb_01
.sgetn(carray2
, strmsz_1
+ 5);
217 VERIFY( strmsz_1
== strmsz_2
- 5 );
218 c4
= fb_01
.sgetc(); // buffer should have underflowed from above.
220 strmsz_1
= fb_01
.in_avail();
221 VERIFY( strmsz_1
> 0 );
222 strmsz_2
= fb_01
.sgetn(carray2
, strmsz_1
+ 5);
223 VERIFY( strmsz_1
== strmsz_2
); //at the end of the actual file
224 strmsz_1
= fb_02
.in_avail();
225 strmsz_2
= fb_02
.sgetn(carray2
, strmsz_1
+ 5);
226 VERIFY( strmsz_1
== -1 );
227 VERIFY( strmsz_2
== 0 );
228 c4
= fb_02
.sgetc(); // should be EOF
229 VERIFY( c4
== traits_type::eof() );
232 // int_type sputc(char_type c)
233 // if out_cur not avail, return overflow(traits_type::to_int_type(c))
234 // else, stores c at out_cur,
235 // increments out_cur, and returns c as int_type
236 // strmsz_1 = fb_03.in_avail(); // XXX valid for in|out??
237 c1
= fb_02
.sputc('a');
238 c2
= fb_03
.sputc('b');
240 c1
= fb_02
.sputc('c');
241 c2
= fb_03
.sputc('d');
243 // strmsz_2 = fb_03.in_avail();
244 // VERIFY( strmsz_1 != strmsz_2 );
245 for (int i
= 50; i
<= 90; ++i
)
246 c2
= fb_02
.sputc(char(i
));
247 // 27filebuf-2.txt == ac23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
248 // fb_02._M_out_cur = '2'
249 strmsz_1
= fb_03
.in_avail();
250 for (int i
= 50; i
<= 90; ++i
)
251 c2
= fb_03
.sputc(char(i
));
252 strmsz_2
= fb_03
.in_avail();
253 // VERIFY( strmsz_1 != strmsz_2 );
254 // VERIFY( strmsz_1 > 0 );
255 // VERIFY( strmsz_2 > 0 );
256 // 27filebuf-2.txt == bd23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWX
257 // fb_02._M_out_cur = '2'
258 c3
= fb_01
.sputc('a'); // should be EOF because this is read-only
259 VERIFY( c3
== traits_type::eof() );
261 // streamsize sputn(const char_typs* s, streamsize n)
262 // write up to n chars to out_cur from s, returning number assigned
263 // NB *sputn will happily put '\0' into your stream if you give it a chance*
264 strmsz_1
= fb_03
.sputn("racadabras", 10);//"abracadabras or what?"
265 VERIFY( strmsz_1
== 10 );
266 strmsz_2
= fb_03
.sputn(", i wanna reach out and", 10);
267 VERIFY( strmsz_2
== 10 );
268 VERIFY( strmsz_1
== strmsz_2
);
269 // fb_03._M_out_beg = "YZracadabras, i wanna FGHIJKLMNOPQRSTUVW"
270 // fb_03._M_out_cur = "FGHIJKLMNOPQRSTUVW"
271 strmsz_1
= fb_02
.sputn("racadabras", 10);
272 VERIFY( strmsz_1
== 10 );
273 // fb_02._M_out_beg = "YZracadabras<=>?@ABCDEFGHIJKLMNOPQRSTUVW"
274 // fb_02._M_out_cur = "<=>?@ABCDEFGHIJKLMNOPQRSTUVW"
275 strmsz_1
= fb_01
.sputn("racadabra", 10);
276 VERIFY( strmsz_1
== 0 );
279 // int_type pbfail(int_type c)
280 // called when gptr() null, gptr() == eback(), or traits::eq(*gptr, c) false
281 // "pending sequence" is:
282 // 1) everything as defined in underflow
283 // 2) + if (traits::eq_int_type(c, traits::eof()), then input
284 // sequence is backed up one char before the pending sequence is
286 // 3) + if (not 2) then c is prepended. Left unspecified is
287 // whether the input sequence is backedup or modified in any way
288 // returns traits::eof() for failure, unspecified other value for success
290 // int_type sputbackc(char_type c)
291 // if in_cur not avail || ! traits::eq(c, gptr() [-1]), return pbfail
292 // otherwise decrements in_cur and returns *gptr()
293 c1
= fb_03
.sgetc(); // -1
294 c2
= fb_03
.sputbackc('z');
295 strmsz_2
= fb_03
.in_avail();
299 VERIFY( 1 == strmsz_2
);
300 //test for _in_cur == _in_beg
301 // fb_03._M_out_beg = "bd23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZracada" etc
302 fb_03
.pubseekoff(10, std::ios_base::beg
,
303 std::ios_base::in
| std::ios_base::out
);
305 strmsz_1
= fb_03
.in_avail();
308 c2
= fb_03
.sputbackc('z');
309 strmsz_2
= fb_03
.in_avail();
315 // VERIFY( strmsz_1 == strmsz_2 );
316 // test for replacing char with identical one
321 strmsz_1
= fb_03
.in_avail();
322 c2
= fb_03
.sputbackc('a');
323 strmsz_2
= fb_03
.in_avail();
326 VERIFY( strmsz_1
+ 1 == strmsz_2
);
327 //test for ios_base::out
328 c1
= fb_02
.sgetc(); // undefined
329 c2
= fb_02
.sputbackc('a');
333 // int_type sungetc()
334 // if in_cur not avail, return pbackfail(), else decrement and
335 // return to_int_type(*gptr())
336 // fb_03._M_out_beg = "uvaacadabras, i wannaZ[\\]^_`abcdefghijkl"
337 // fb_03._M_out_cur = "aacadabras, i wannaZ[\\]^_`abcdefghijkl"
338 strmsz_1
= fb_03
.in_avail();
339 c2
= fb_03
.sungetc(); // delete the 'a'
340 strmsz_2
= fb_03
.in_avail();
341 VERIFY( c2
== 'v' ); // VERIFY( c2 != traits_type::eof() );
342 VERIFY( strmsz_1
+ 1 == strmsz_2
);
343 //test for _in_cur == _in_beg
344 for (int i
= 50; i
< 32 + 29; ++i
)
345 fb_02
.sputc(char(i
));
346 fb_02
.pubseekoff(0, std::ios_base::beg
, std::ios_base::out
);
348 strmsz_1
= fb_02
.in_avail();
349 c2
= fb_02
.sungetc();
351 strmsz_2
= fb_02
.in_avail();
354 VERIFY( c1
== traits_type::eof() );
355 VERIFY( strmsz_1
== strmsz_2
);
356 //test for _in_cur == _in_end
357 fb_03
.pubseekoff(0, std::ios_base::end
);
358 strmsz_1
= fb_03
.in_avail(); // -1 cuz at the end
360 c2
= fb_03
.sungetc();
361 strmsz_2
= fb_03
.in_avail(); // 1
364 // VERIFY( c2 == c3 || c2 == traits_type::not_eof(int(c3)) );
365 VERIFY( strmsz_2
!= strmsz_1
);
366 VERIFY( strmsz_2
== 1 );
367 //test for ios_base::out
369 // BUFFER MANAGEMENT & POSITIONING
371 // if a put area exists, overflow.
372 // if a get area exists, do something undefined. (like, nothing)
373 strmsz_1
= fb_01
.in_avail();
375 strmsz_2
= fb_01
.in_avail();
376 VERIFY( strmsz_2
== strmsz_1
);
377 strmsz_1
= fb_02
.in_avail();
379 // 27filebuf-2.txt == 53 bytes after this.
380 strmsz_2
= fb_02
.in_avail();
381 VERIFY( strmsz_2
== -1 );
382 VERIFY( strmsz_2
== strmsz_1
);
383 strmsz_1
= fb_03
.in_avail();
386 // bd23456789mzuva?@ABCDEFGHIJKLMNOPQRSTUVWXYZracadabras, i wannaz
388 strmsz_2
= fb_03
.in_avail();
389 VERIFY( strmsz_1
== 1 );
390 // VERIFY( strmsz_2 == 1 );
393 // pubsetbuf(char_type* s, streamsize n)
394 fb_01
.pubsetbuf(0,0);
395 fb_02
.pubsetbuf(0,0);
396 fb_03
.pubsetbuf(0,0);
397 // Need to test unbuffered output, which means calling this on some
398 // things that have just been opened.
402 // pubseekoff(off_type off, ios_base::seekdir way, ios_base::openmode which)
403 // alters the stream position to off
404 pos_type
pt_1(off_type(-1));
405 pos_type
pt_2(off_type(0));
409 // 27filebuf-3.txt = bd23456789:;<=>?...
411 strmsz_1
= fb_03
.in_avail();
412 pt_1
= fb_03
.pubseekoff(2, std::ios_base::beg
);
413 strmsz_2
= fb_03
.in_avail();
416 c1
= fb_03
.snextc(); //current in pointer +1
418 c2
= fb_03
.sputc('\n'); //current in pointer +1
426 // 27filebuf-3.txt = bd2\n456789:;<=>?...
427 pt_2
= fb_03
.pubseekoff(2, std::ios_base::cur
);
429 VERIFY( (off_2
== (off_1
+ 2 + 1 + 1)) );
430 c1
= fb_03
.snextc(); //current in pointer +1
432 c2
= fb_03
.sputc('x'); //test current out pointer
433 c3
= fb_03
.sputc('\n');
439 // 27filebuf-3.txt = "bd2\n456x\n9"
440 pt_2
= fb_03
.pubseekoff(0, std::ios_base::end
,
441 std::ios_base::in
|std::ios_base::out
);
443 VERIFY( off_1
> off_2
); //weak, but don't know exactly where it ends
444 c3
= fb_03
.sputc('\n');
445 strmsz_1
= fb_03
.sputn("because because because. . .", 28);
446 VERIFY( strmsz_1
== 28 );
447 c1
= fb_03
.sungetc();
448 // Defect? retval of sungetc is not necessarily the character ungotten.
459 // pubseekpos(pos_type sp, ios_base::openmode)
460 // alters the stream position to sp
463 pt_1
= fb_03
.pubseekoff(78, std::ios_base::beg
);
466 c1
= fb_03
.snextc(); //current in pointer +1
468 c2
= fb_03
.sputc('\n'); //test current out pointer
470 fb_03
.pubsync(); //resets pointers
471 pt_2
= fb_03
.pubseekpos(pt_1
);
473 VERIFY( off_1
== off_2
);
474 c3
= fb_03
.snextc(); //current in pointer +1
476 pt_1
= fb_03
.pubseekoff(0, std::ios_base::end
);
478 VERIFY( off_1
> off_2
);
479 fb_03
.sputn("\nof the wonderful things he does!!\nok", 37);
485 // VIRTUALS (indirectly tested)
487 // if read position avail, returns *gptr()
489 // pbackfail(int_type c)
490 // put c back into input sequence
493 // appends c to output seq
495 // NB Have to close these suckers. . .
496 // filebuf_type* close()
500 VERIFY( !fb_01
.is_open() );
501 VERIFY( !fb_02
.is_open() );
502 VERIFY( !fb_03
.is_open() );
508 typedef istream::int_type int_type
;
511 ifstream
ifs(name_02
);
512 char buffer
[] = "xxxxxxxxxx";
513 int_type len1
= ifs
.rdbuf()->sgetn(buffer
, sizeof(buffer
));
514 VERIFY( len1
== sizeof(buffer
) );
515 VERIFY( buffer
[0] == 'a' );
526 VERIFY( ob
.getloc() == loc
);
528 locale::global(locale("en_US"));
529 VERIFY( ob
.getloc() == loc
);
531 locale
loc_de ("de_DE");
532 locale ret
= ob
.pubimbue(loc_de
);
533 VERIFY( ob
.getloc() == loc_de
);
534 VERIFY( ret
== loc
);
537 VERIFY( ob
.getloc() == loc_de
);
540 class MyTraits
: public std::char_traits
<char>
543 static bool eq(char c1
, char c2
)
547 return std::char_traits
<char>::eq(c1
, c2
);
551 class MyBuf
: public std::basic_streambuf
<char, MyTraits
>
558 std::memset(buffer
, 'X', sizeof(buffer
));
559 std::memset(buffer
+ 2, 'f', 4);
560 setg(buffer
+ 2, buffer
+ 2, buffer
+ 6);
573 // libstdc++/9439, libstdc++/9425
580 fbuf
.open(name_01
, ios_base::in
);
581 filebuf::int_type r
= fbuf
.sputbackc('a');
584 VERIFY( r
== filebuf::traits_type::eof() );
This page took 0.066663 seconds and 5 git commands to generate.